Bug 1448535 part 4 - Remove ArenaRefPtr. r?bz draft
authorXidorn Quan <me@upsuper.org>
Mon, 26 Mar 2018 12:32:26 +1100
changeset 772913 f817dff1c5d4f9ba460c9dc5d1a45ee1c53202ff
parent 772912 ae97a4674ae1633a068e8e5eb596cfd79c6df035
child 772914 c4890f607e6e57185e7284b2d1a18dd39eaa8560
push id104086
push userxquan@mozilla.com
push dateTue, 27 Mar 2018 05:15:17 +0000
reviewersbz
bugs1448535
milestone61.0a1
Bug 1448535 part 4 - Remove ArenaRefPtr. r?bz The only usage of this class was for nsStyleContext in the old style system, which has been removed. The only remaining mention of this type is removed by the previous commit, so we can remove this type and all related code here. MozReview-Commit-ID: 9qYNbFP5kR
layout/base/ArenaRefPtr.h
layout/base/ArenaRefPtrInlines.h
layout/base/PresShell.cpp
layout/base/moz.build
layout/base/nsIPresShell.h
layout/base/nsPresArena.cpp
layout/base/nsPresArena.h
deleted file mode 100644
--- a/layout/base/ArenaRefPtr.h
+++ /dev/null
@@ -1,162 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- */
-
-/* smart pointer for strong references to nsPresArena-allocated objects
-   that might be held onto until the arena's destruction */
-
-#include "mozilla/Assertions.h"
-#include "mozilla/RefPtr.h"
-
-#ifndef mozilla_ArenaRefPtr_h
-#define mozilla_ArenaRefPtr_h
-
-class nsPresArena;
-
-namespace mozilla {
-
-/**
- * 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
- * in that it will clear its referent if the arena is about to go away.
- *
- * T must be a class that has these two methods:
- *
- *   static mozilla::ArenaObjectID ArenaObjectID();
- *   U* Arena();
- *
- * where U is a class that has these two methods:
- *
- *   void RegisterArenaRefPtr(ArenaRefPtr<T>*);
- *   void DeregisterArenaRefPtr(ArenaRefPtr<T>*);
- *
- * Currently, both nsPresArena and nsIPresShell can be used as U.
- *
- * The ArenaObjectID method must return the mozilla::ArenaObjectID that
- * uniquely identifies T, and the Arena method must return the nsPresArena
- * (or a proxy for it) in which the object was allocated.
- */
-template<typename T>
-class ArenaRefPtr
-{
-  friend class ::nsPresArena;
-
-public:
-  ArenaRefPtr()
-  {
-    AssertValidType();
-  }
-
-  template<typename I>
-  MOZ_IMPLICIT ArenaRefPtr(already_AddRefed<I>& aRhs)
-  {
-    AssertValidType();
-    assign(aRhs);
-  }
-
-  template<typename I>
-  MOZ_IMPLICIT ArenaRefPtr(already_AddRefed<I>&& aRhs)
-  {
-    AssertValidType();
-    assign(aRhs);
-  }
-
-  MOZ_IMPLICIT ArenaRefPtr(T* aRhs)
-  {
-    AssertValidType();
-    assign(aRhs);
-  }
-
-  template<typename I>
-  ArenaRefPtr<T>& operator=(already_AddRefed<I>& aRhs)
-  {
-    assign(aRhs);
-    return *this;
-  }
-
-  template<typename I>
-  ArenaRefPtr<T>& operator=(already_AddRefed<I>&& aRhs)
-  {
-    assign(aRhs);
-    return *this;
-  }
-
-  ArenaRefPtr<T>& operator=(T* aRhs)
-  {
-    assign(aRhs);
-    return *this;
-  }
-
-  ~ArenaRefPtr() { assign(nullptr); }
-
-  operator T*() const & { return get(); }
-  operator T*() const && = delete;
-  explicit operator bool() const { return !!mPtr; }
-  bool operator!() const { return !mPtr; }
-  T* operator->() const { return mPtr.operator->(); }
-  T& operator*() const { return *get(); }
-
-  T* get() const { return mPtr; }
-
-private:
-  void AssertValidType();
-
-  /**
-   * Clears the pointer to the arena-allocated object but skips the usual
-   * step of deregistering the ArenaRefPtr from the nsPresArena.  This
-   * method is called by nsPresArena when clearing all registered ArenaRefPtrs
-   * so that it can deregister them all at once, avoiding hash table churn.
-   */
-  void ClearWithoutDeregistering()
-  {
-    mPtr = nullptr;
-  }
-
-  template<typename I>
-  void assign(already_AddRefed<I>& aSmartPtr)
-  {
-    RefPtr<T> newPtr(aSmartPtr);
-    assignFrom(newPtr);
-  }
-
-  template<typename I>
-  void assign(already_AddRefed<I>&& aSmartPtr)
-  {
-    RefPtr<T> newPtr(aSmartPtr);
-    assignFrom(newPtr);
-  }
-
-  void assign(T* aPtr) { assignFrom(aPtr); }
-
-  template<typename I>
-  void assignFrom(I& aPtr)
-  {
-    if (aPtr == mPtr) {
-      return;
-    }
-    bool sameArena = mPtr && aPtr && mPtr->Arena() == aPtr->Arena();
-    if (mPtr && !sameArena) {
-      MOZ_ASSERT(mPtr->Arena());
-      mPtr->Arena()->DeregisterArenaRefPtr(this);
-    }
-    mPtr = Move(aPtr);
-    if (mPtr && !sameArena) {
-      MOZ_ASSERT(mPtr->Arena());
-      mPtr->Arena()->RegisterArenaRefPtr(this);
-    }
-  }
-
-  RefPtr<T> mPtr;
-};
-
-} // namespace mozilla
-
-#endif // mozilla_ArenaRefPtr_h
deleted file mode 100644
--- a/layout/base/ArenaRefPtrInlines.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- */
-
-/* inline methods that belong in ArenaRefPtr.h, except that they require
-   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 "mozilla/ComputedStyle.h"
-#include "nsStyleStruct.h"
-
-namespace mozilla {
-
-template<typename T>
-void
-ArenaRefPtr<T>::AssertValidType()
-{
-  // If adding new types, please update nsPresArena::ClearArenaRefPtrWithoutDeregistering
-  // as well
-  static_assert(IsSame<T, ComputedStyle>::value,
-                "ArenaRefPtr<T> template parameter T must be declared in "
-                "nsPresArenaObjectList and explicitly handled in"
-                "nsPresArena.cpp");
-}
-
-} // namespace mozilla
-
-template<typename T>
-void
-nsPresArena::RegisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr)
-{
-  MOZ_ASSERT(!mArenaRefPtrs.Contains(aPtr));
-  mArenaRefPtrs.Put(aPtr, T::ArenaObjectID());
-}
-
-
-#endif
--- a/layout/base/PresShell.cpp
+++ b/layout/base/PresShell.cpp
@@ -1327,33 +1327,16 @@ PresShell::Destroy()
 
   if (mViewManager) {
     // Clear the view manager's weak pointer back to |this| in case it
     // was leaked.
     mViewManager->SetPresShell(nullptr);
     mViewManager = nullptr;
   }
 
-  // mFrameArena will be destroyed soon.  Clear out any ArenaRefPtrs
-  // pointing to objects in the arena now.  This is done:
-  //
-  //   (a) before mFrameArena's destructor runs so that our
-  //       mAllocatedPointers becomes empty and doesn't trip the assertion
-  //       in ~PresShell,
-  //   (b) before the mPresContext->DetachShell() below, so
-  //       that when we clear the ArenaRefPtrs they'll still be able to
-  //       get back to this PresShell to deregister themselves (e.g. note
-  //       how ComputedStyle::Arena returns the PresShell got from its
-  //       rule node's nsPresContext, which would return null if we'd already
-  //       called mPresContext->DetachShell()), and
-  //   (c) before the mStyleSet->BeginShutdown() call just below, so that
-  //       the ComputedStyles don't complain they're being destroyed later
-  //       than the rule tree is.
-  mFrameArena.ClearArenaRefPtrs();
-
   mStyleSet->BeginShutdown();
   nsRefreshDriver* rd = GetPresContext()->RefreshDriver();
 
   // This shell must be removed from the document before the frame
   // hierarchy is torn down to avoid finding deleted frames through
   // this presshell while the frames are being torn down
   if (mDocument) {
     NS_ASSERTION(mDocument->GetShell() == this, "Wrong shell?");
--- a/layout/base/moz.build
+++ b/layout/base/moz.build
@@ -69,18 +69,16 @@ EXPORTS += [
     'Units.h',
     'UnitTransforms.h',
     'WordMovementType.h',
     'ZoomConstraintsClient.h',
 ]
 
 EXPORTS.mozilla += [
     'ArenaObjectID.h',
-    'ArenaRefPtr.h',
-    'ArenaRefPtrInlines.h',
     'GeometryUtils.h',
     'OverflowChangedTracker.h',
     'PresShell.h',
     'RestyleLogging.h',
     'RestyleManager.h',
     'RestyleManagerInlines.h',
     'ServoRestyleManager.h',
     'ShapeUtils.h',
--- a/layout/base/nsIPresShell.h
+++ b/layout/base/nsIPresShell.h
@@ -230,33 +230,16 @@ public:
 
   void FreeByObjectID(mozilla::ArenaObjectID aID, void* aPtr)
   {
     RecordFree(aPtr);
     if (!mIsDestroying)
       mFrameArena.FreeByObjectID(aID, aPtr);
   }
 
-  template<typename T>
-  void RegisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr)
-  {
-    mFrameArena.RegisterArenaRefPtr(aPtr);
-  }
-
-  template<typename T>
-  void DeregisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr)
-  {
-    mFrameArena.DeregisterArenaRefPtr(aPtr);
-  }
-
-  void ClearArenaRefPtrs(mozilla::ArenaObjectID aObjectID)
-  {
-    mFrameArena.ClearArenaRefPtrs(aObjectID);
-  }
-
   nsIDocument* GetDocument() const { return mDocument; }
 
   nsPresContext* GetPresContext() const { return mPresContext; }
 
   nsViewManager* GetViewManager() const { return mViewManager; }
 
   nsRefreshDriver* GetRefreshDriver() const;
 
--- a/layout/base/nsPresArena.cpp
+++ b/layout/base/nsPresArena.cpp
@@ -23,71 +23,28 @@
 using namespace mozilla;
 
 nsPresArena::nsPresArena()
 {
 }
 
 nsPresArena::~nsPresArena()
 {
-  ClearArenaRefPtrs();
-
 #if defined(MOZ_HAVE_MEM_CHECKS)
   for (FreeList* entry = mFreeLists; entry != ArrayEnd(mFreeLists); ++entry) {
     nsTArray<void*>::index_type len;
     while ((len = entry->mEntries.Length())) {
       void* result = entry->mEntries.ElementAt(len - 1);
       entry->mEntries.RemoveElementAt(len - 1);
       MOZ_MAKE_MEM_UNDEFINED(result, entry->mEntrySize);
     }
   }
 #endif
 }
 
-/* inline */ void
-nsPresArena::ClearArenaRefPtrWithoutDeregistering(void* aPtr,
-                                                  ArenaObjectID aObjectID)
-{
-  switch (aObjectID) {
-    // We use ArenaRefPtr<ComputedStyle>, which can be ComputedStyle
-    // or GeckoComputedStyle. GeckoComputedStyle is actually arena managed,
-    // but ComputedStyle isn't.
-    case eArenaObjectID_GeckoComputedStyle:
-      static_cast<ArenaRefPtr<ComputedStyle>*>(aPtr)->ClearWithoutDeregistering();
-      return;
-    default:
-      MOZ_ASSERT(false, "unexpected ArenaObjectID value");
-      break;
-  }
-}
-
-void
-nsPresArena::ClearArenaRefPtrs()
-{
-  for (auto iter = mArenaRefPtrs.Iter(); !iter.Done(); iter.Next()) {
-    void* ptr = iter.Key();
-    ArenaObjectID id = iter.UserData();
-    ClearArenaRefPtrWithoutDeregistering(ptr, id);
-  }
-  mArenaRefPtrs.Clear();
-}
-
-void
-nsPresArena::ClearArenaRefPtrs(ArenaObjectID aObjectID)
-{
-  for (auto iter = mArenaRefPtrs.Iter(); !iter.Done(); iter.Next()) {
-    void* ptr = iter.Key();
-    ArenaObjectID id = iter.UserData();
-    if (id == aObjectID) {
-      ClearArenaRefPtrWithoutDeregistering(ptr, id);
-      iter.Remove();
-    }
-  }
-}
-
 void*
 nsPresArena::Allocate(uint32_t aCode, size_t aSize)
 {
   MOZ_ASSERT(aSize > 0, "PresArena cannot allocate zero bytes");
   MOZ_ASSERT(aCode < ArrayLength(mFreeLists));
 
   // We only hand out aligned sizes
   aSize = mPool.AlignedSize(aSize);
--- a/layout/base/nsPresArena.h
+++ b/layout/base/nsPresArena.h
@@ -7,17 +7,16 @@
 
 /* arena allocation for the frame tree and closely-related objects */
 
 #ifndef nsPresArena_h___
 #define nsPresArena_h___
 
 #include "mozilla/ArenaAllocator.h"
 #include "mozilla/ArenaObjectID.h"
-#include "mozilla/ArenaRefPtr.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/MemoryChecking.h" // Note: Do not remove this, needed for MOZ_HAVE_MEM_CHECKS below
 #include "mozilla/MemoryReporting.h"
 #include <stdint.h>
 #include "nscore.h"
 #include "nsDataHashtable.h"
 #include "nsHashKeys.h"
 #include "nsTArray.h"
@@ -61,55 +60,16 @@ public:
     return Allocate(aID, aSize);
   }
   void FreeByCustomID(uint32_t aID, void* ptr)
   {
     Free(aID, ptr);
   }
 
   /**
-   * Register an ArenaRefPtr to be cleared when this arena is about to
-   * be destroyed.
-   *
-   * (Defined in ArenaRefPtrInlines.h.)
-   *
-   * @param aPtr The ArenaRefPtr to clear.
-   * @param aObjectID The ArenaObjectID value that uniquely identifies
-   *   the type of object the ArenaRefPtr holds.
-   */
-  template<typename T>
-  void RegisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr);
-
-  /**
-   * Deregister an ArenaRefPtr that was previously registered with
-   * RegisterArenaRefPtr.
-   */
-  template<typename T>
-  void DeregisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr)
-  {
-    MOZ_ASSERT(mArenaRefPtrs.Contains(aPtr));
-    mArenaRefPtrs.Remove(aPtr);
-  }
-
-  /**
-   * Clears all currently registered ArenaRefPtrs.  This will be called during
-   * the destructor, but can be called by users of nsPresArena who want to
-   * ensure arena-allocated objects are released earlier.
-   */
-  void ClearArenaRefPtrs();
-
-  /**
-   * Clears all currently registered ArenaRefPtrs for the given ArenaObjectID.
-   * This is called when we reconstruct the rule tree so that style contexts
-   * pointing into the old rule tree aren't released afterwards, triggering an
-   * assertion in ~ComputedStyle.
-   */
-  void ClearArenaRefPtrs(mozilla::ArenaObjectID aObjectID);
-
-  /**
    * Increment nsWindowSizes with sizes of interesting objects allocated in this
    * arena.
    */
   void AddSizeOfExcludingThis(nsWindowSizes& aWindowSizes) const;
 
 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
   void Check() {
     mPool.Check();