Bug 1367904 - Part 15: stylo: Override ArenaRefPtr for ServoStyleContexts; r?bholley draft
authorManish Goregaokar <manishearth@gmail.com>
Thu, 15 Jun 2017 22:49:50 -0700
changeset 609593 24ca2921a144c3fdbf8d57628dfb83a4923678b3
parent 609458 cf9197a5d66cd69911f67299032bf18800ebc26d
child 609771 a8d9829967b85204371447ed5edf5d40af849aad
push id68590
push userbmo:manishearth@gmail.com
push dateMon, 17 Jul 2017 01:13:28 +0000
reviewersbholley
bugs1367904
milestone56.0a1
Bug 1367904 - Part 15: stylo: Override ArenaRefPtr for ServoStyleContexts; r?bholley MozReview-Commit-ID: BV4ek093JCR
layout/base/ArenaRefPtr.h
layout/base/ArenaRefPtrInlines.h
--- a/layout/base/ArenaRefPtr.h
+++ b/layout/base/ArenaRefPtr.h
@@ -13,16 +13,27 @@
 
 #ifndef mozilla_ArenaRefPtr_h
 #define mozilla_ArenaRefPtr_h
 
 class nsPresArena;
 
 namespace mozilla {
 
+// Most consumers of ArenaRefPtr will unconditionally use the arena
+// However, nsStyleContext only uses the arena if it is a GeckoStyleContext
+// so we need a mechanism to override this
+template<class U>
+struct ArenaRefPtrTraits
+{
+  static bool UsesArena(U* aPtr) {
+    return true;
+  }
+};
+
 /**
  * A class for holding strong references to nsPresArena-allocated
  * objects.
  *
  * Since the arena's lifetime is not related to the refcounts
  * of the objects allocated within it, it is possible to have a strong
  * reference to an arena-allocated object that lives until the
  * destruction of the arena.  An ArenaRefPtr acts like a weak reference
@@ -138,22 +149,22 @@ private:
 
   template<typename I>
   void assignFrom(I& aPtr)
   {
     if (aPtr == mPtr) {
       return;
     }
     bool sameArena = mPtr && aPtr && mPtr->Arena() == aPtr->Arena();
-    if (mPtr && !sameArena) {
+    if (mPtr && !sameArena && ArenaRefPtrTraits<T>::UsesArena(mPtr)) {
       MOZ_ASSERT(mPtr->Arena());
       mPtr->Arena()->DeregisterArenaRefPtr(this);
     }
     mPtr = Move(aPtr);
-    if (mPtr && !sameArena) {
+    if (mPtr && !sameArena && ArenaRefPtrTraits<T>::UsesArena(aPtr)) {
       MOZ_ASSERT(mPtr->Arena());
       mPtr->Arena()->RegisterArenaRefPtr(this);
     }
   }
 
   RefPtr<T> mPtr;
 };
 
--- a/layout/base/ArenaRefPtrInlines.h
+++ b/layout/base/ArenaRefPtrInlines.h
@@ -9,16 +9,17 @@
    the inclusion of headers for all types that ArenaRefPtr can handle */
 
 #ifndef mozilla_ArenaRefPtrInlines_h
 #define mozilla_ArenaRefPtrInlines_h
 
 #include "mozilla/ArenaObjectID.h"
 #include "mozilla/Assertions.h"
 #include "nsStyleStruct.h"
+#include "nsStyleContext.h"
 
 namespace mozilla {
 
 template<typename T>
 void
 ArenaRefPtr<T>::AssertValidType()
 {
 #ifdef DEBUG
@@ -29,19 +30,29 @@ ArenaRefPtr<T>::AssertValidType()
 #undef PRES_ARENA_OBJECT_WITH_ARENAREFPTR_SUPPORT
     false;
   MOZ_ASSERT(ok, "ArenaRefPtr<T> template parameter T must be declared in "
                  "nsPresArenaObjectList with "
                  "PRES_ARENA_OBJECT_WITH_ARENAREFPTR_SUPPORT");
 #endif
 }
 
+template<>
+struct ArenaRefPtrTraits<nsStyleContext>
+{
+  static bool UsesArena(nsStyleContext* aPtr) {
+    MOZ_ASSERT(aPtr);
+    return aPtr->IsGecko();
+  }
+};
+
 } // namespace mozilla
 
 template<typename T>
 void
 nsPresArena::RegisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr)
 {
   MOZ_ASSERT(!mArenaRefPtrs.Contains(aPtr));
   mArenaRefPtrs.Put(aPtr, T::ArenaObjectID());
 }
 
+
 #endif