Bug 1374629 - Ensure that mozilla::NotNull has zero space overhead. r?njn draft
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Thu, 22 Jun 2017 00:52:28 +0900
changeset 599191 b44ced1809800e956a7a1985977de6d3905666c9
parent 599190 f70f7af269197d3533f1a5a2bd35b57354a72626
child 634699 8d4e4e6592bd587b04975479b5bc36170abff8fc
push id65443
push userVYV03354@nifty.ne.jp
push dateThu, 22 Jun 2017 19:52:49 +0000
reviewersnjn
bugs1374629
milestone56.0a1
Bug 1374629 - Ensure that mozilla::NotNull has zero space overhead. r?njn MozReview-Commit-ID: 9Bo2qxd3HRv
mfbt/NotNull.h
--- a/mfbt/NotNull.h
+++ b/mfbt/NotNull.h
@@ -58,16 +58,17 @@
 // - When the handle is rebound to another object. References don't allow this.
 //
 // - When the handle has type |void|. |void&| is not allowed.
 //
 // NotNull is an alternative that can be used in any of the above cases except
 // for the last one, where the handle type is |void|. See below.
 
 #include "mozilla/Assertions.h"
+#include <stddef.h>
 
 namespace mozilla {
 
 // NotNull can be used to wrap a "base" pointer (raw or smart) to indicate it
 // is not null. Some examples:
 //
 // - NotNull<char*>
 // - NotNull<RefPtr<Event>>
@@ -109,17 +110,22 @@ class NotNull
   explicit NotNull(U aBasePtr) : mBasePtr(aBasePtr) {}
 
 public:
   // Disallow default construction.
   NotNull() = delete;
 
   // Construct/assign from another NotNull with a compatible base pointer type.
   template <typename U>
-  MOZ_IMPLICIT NotNull(const NotNull<U>& aOther) : mBasePtr(aOther.get()) {}
+  MOZ_IMPLICIT NotNull(const NotNull<U>& aOther) : mBasePtr(aOther.get()) {
+    static_assert(sizeof(T) == sizeof(NotNull<T>),
+                  "NotNull must have zero space overhead.");
+    static_assert(offsetof(NotNull<T>, mBasePtr) == 0,
+                  "mBasePtr must have zero offset.");
+  }
 
   // Default copy/move construction and assignment.
   NotNull(const NotNull<T>&) = default;
   NotNull<T>& operator=(const NotNull<T>&) = default;
   NotNull(NotNull<T>&&) = default;
   NotNull<T>& operator=(NotNull<T>&&) = default;
 
   // Disallow null checks, which are unnecessary for this type.