gecko-dev/layout/base/ArenaRefPtr.h

168 lines
4.0 KiB
C
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=2 sw=2 et tw=78:
* 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"
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
#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); }
#ifdef MOZ_HAVE_REF_QUALIFIERS
operator T*() const & { return get(); }
operator T*() const && = delete;
explicit operator bool() const { return !!mPtr; }
bool operator!() const { return !mPtr; }
#else
operator T*() const { return get(); }
#endif
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)
{
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<T> newPtr(aSmartPtr);
assignFrom(newPtr);
}
template<typename I>
void assign(already_AddRefed<I>&& aSmartPtr)
{
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
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);
}
}
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<T> mPtr;
};
} // namespace mozilla
#endif // mozilla_ArenaRefPtr_h