Bug 1639958 part 1: Add MakeUniqueForOverwrite and MakeUniqueForOverwriteFallible helpers. r=froydnj
authorBob Owen <bobowencode@gmail.com>
Fri, 22 May 2020 07:22:28 +0000
changeset 531594 d136262c64d75a38f58b2e4dea0ab89c2ff3f261
parent 531593 ebeb3fbc43fbd016271ed508493c9b41cd60cc59
child 531595 c0f2da5467b282ee525dc3e3ffc69c0009a85a62
push id37441
push userapavel@mozilla.com
push dateFri, 22 May 2020 21:38:53 +0000
treeherdermozilla-central@d6abd35b54ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1639958
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1639958 part 1: Add MakeUniqueForOverwrite and MakeUniqueForOverwriteFallible helpers. r=froydnj These are the equivalent of std::make_unique_for_overwrite and std::make_unique_for_overwrite with fallible allocation. Differential Revision: https://phabricator.services.mozilla.com/D75509
mfbt/UniquePtrExtensions.h
--- a/mfbt/UniquePtrExtensions.h
+++ b/mfbt/UniquePtrExtensions.h
@@ -37,16 +37,55 @@ typename detail::UniqueSelector<T>::Unkn
   using ArrayType = std::remove_extent_t<T>;
   return UniquePtr<T>(new (fallible) ArrayType[aN]());
 }
 
 template <typename T, typename... Args>
 typename detail::UniqueSelector<T>::KnownBound MakeUniqueFallible(
     Args&&... aArgs) = delete;
 
+/**
+ * MakeUniqueForOverwrite and MakeUniqueFallibleForOverwrite are like MakeUnique
+ * and MakeUniqueFallible except they use default-initialization. This is
+ * useful, for example, when you have a POD type array that will be overwritten
+ * directly after construction and so zero-initialization is a waste.
+ */
+template <typename T, typename... Args>
+typename detail::UniqueSelector<T>::SingleObject MakeUniqueForOverwrite() {
+  return UniquePtr<T>(new T);
+}
+
+template <typename T>
+typename detail::UniqueSelector<T>::UnknownBound MakeUniqueForOverwrite(
+    decltype(sizeof(int)) aN) {
+  using ArrayType = std::remove_extent_t<T>;
+  return UniquePtr<T>(new ArrayType[aN]);
+}
+
+template <typename T, typename... Args>
+typename detail::UniqueSelector<T>::KnownBound MakeUniqueForOverwrite(
+    Args&&... aArgs) = delete;
+
+template <typename T, typename... Args>
+typename detail::UniqueSelector<T>::SingleObject
+MakeUniqueForOverwriteFallible() {
+  return UniquePtr<T>(new (fallible) T);
+}
+
+template <typename T>
+typename detail::UniqueSelector<T>::UnknownBound MakeUniqueForOverwriteFallible(
+    decltype(sizeof(int)) aN) {
+  using ArrayType = std::remove_extent_t<T>;
+  return UniquePtr<T>(new (fallible) ArrayType[aN]);
+}
+
+template <typename T, typename... Args>
+typename detail::UniqueSelector<T>::KnownBound MakeUniqueForOverwriteFallible(
+    Args&&... aArgs) = delete;
+
 namespace detail {
 
 template <typename T>
 struct FreePolicy {
   void operator()(const void* ptr) { free(const_cast<void*>(ptr)); }
 };
 
 #if defined(XP_WIN)