Files
roytam1 39770f3213 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1274192 part 1 - make the MediaDecoderReaderWrapper as a proxy of WaitForData(); r=jwwang (9debdca9a9)
- Bug 1274192 part 2 - make MDSM and SeekTask to adopt new MediaDecoderReaderWrapper API; r=jwwang (156f924dc9)
- Bug 1276495: Don't reset audio promises for video only seek. r=jwwang,jya (c4632c7ff6)
- Bug 1276318, part 1 - Fix leading tabs in ipc/glue. r=billm (46bcc4ccb3)
- Bug 1035125 Part 6: Take Chromium commit 3181ba39ee787e1b40f4aea4be23f4f666ad0945 to add Windows 10 version to enumeration. r=aklotz (af62c8065f)
- Bug 1270752 - Fix lifetime of buffer passed to PR_SetEnv(). r=jduell (bfce06ebea)
- Bug 1275117 - Fix static strings leaks when mozlogging is on. r=jduell (8e9066c5d7)
- Bug 1273048 - Add MOZ_GCC_VERSION_AT_MOST macro. r=froydnj (cfb5acdbe6)
- Bug 1269968 - Remove unnecessary deduction on RefPtr.h. r=froydnj (30cf9bbf47)
- Bug 525063 - add attribute to mark member variables that should be skipped by clang plugin initialization checker. r=nfroyd (78aa863f88)
- Bug 1278391 - add move constructor to EnumeratedArray (r=njn) (5effe4e473)
- Bug 1268518: Update basic-integer tests for rotations + clean up; r=sunfish (46827b6211)
- Bug 1270370 - Part 1: Move SetIteratorObject to MapObject.h; r=jorendorff (01a27fbd50)
- Bug 1270370 - Part 2: Expose MapIterator and SetIterator through ESClassValues; r=jorendorff (3edb3c7551)
- Bug 1267551 (part 3) - Use MOZ_MUST_USE more in js/src/builtin/. r=jonco. (ea96d0fae0)
- Bug 1267551 (part 4) - Use MOZ_MUST_USE more in js/src/asmjs/. r=bbouvier. (3e622c736d)
- Bug 1268910: Refactor WebAssembly trap handling; r=luke (b59394a287)
- Bug 1268910: Trap on edge cases for integer div/mod; r=sunfish (3d85230cc8)
- Bug 1267551 (part 5) - Use MOZ_MUST_USE more in js/src/ctypes/. r=sfink. (e6580fb645)
- Bug 1267551 (part 6) - Remove dead GenerateBlockId declaration. r=jorendorff. (74afcc549e)
- Bug 1240072 - Add public JS APIs related to modules r=shu Bug 1267551 (part 7) - Use MOZ_MUST_USE more in js/src/frontend/. r=jorendorff. (5275c57768)
- Bug 1267551 (part 8) - Use MOZ_MUST_USE more in js/src/gc/. r=terrence. (2572cc1c58)
- Bug 1261063 - Part 1 - Remove public Telemetry functions that return raw histogram instances. r=chutten (3a68653c8f)
- Bug 1261063 - Part 2 - Expose C++ function to clear Telemetry histograms. r=chutten (1f42f6aa1e)
- Bug 1261063 - Part 3 - Make RasterImage use the public Telemetry API. r=tnikkel (0f564f3836)
- Bug 1261063 - Part 4 - Use a count histogram for WebRTC call counts. r=jesup (b6b7b649e0)
- Bug 1270073 - Point to Telemetry documentation in Telemetry.h. r=chutten (517279b284)
- Bug 1261052 - Move C++ histogram implementation code into a separate module. r=gfritzsche. (66bcb1e167)
- Bug 1145164 - Allow non-unitary increments to count histograms. r=gfritzsche (507e076e28)
- Bug 1173447 - Add test for incremental pre-barriers when storing things under roots, r=jonco (caf8bd16be)
- Bug 1267551 (part 10) - Use MOZ_MUST_USE in AutoVectorRooterBase. r=terrence. (2c5ed87a9f)
- This catches a missing check. (b2fcd30fc5)
- Bug 1267551 (part 11b) - Follow-up to fix Android bustage. r=me (f464ca7b98)
- Bug 1271854 - Part 1: Allow specifying multiple GC zeal levels; r=terrence (da3c3e1281)
- Bug 1271854 - Part 2: Allow specifying zeal modes by name as well; r=terrence (f63926e45b)
- Bug 1271854 - Part 3: Avoid saving the GC zeal string inside the JS shell; r=terrence (f09b262e55)
- Bug 1270062 - Unbust Windows full duplex. a=Tomcat (3c8b5887b6)
- Bug 1270062 - Update libcubeb. rs=jesup (71805cb0e0)
- Bug 1270004 - Update libcubeb. rs=jesup (2d90b9662f)
- Bug 1280280 - Update cubeb to 073c9f011114. r=kinetik (6bee6a7f21)
- Bug 1280280 - Update media/libcube/update.sh to add cubeb_jack.cpp, and sort the list of files. r=kinetik (430b7533c4)
- Bug 1272604 - Add a zeal mode to check the heap after a moving GC r=terrence (85c018d71f)
- Bug 1272604 - Fix bad implicit conversion constructor build error r=me (5c4e1dbc44)
- Bug 1272604 - Fix hazard analysis failure r=me (0591aa2dca)
- Bug 1232417 - Use a Variant to represent the CrossCompartmentWrapperMap key; r=jonco (512da0bab3)
- Bug 1269928 - Fold DirectProxyHandler into Wrapper, which is now its only (non-test) consumer. r=efaust (7c6bb6e898)
- Bug 1273639 - Add a nonunified spidermonkey build, r=terrence (d6fa13545c)
- Bug 1273639 - Fix nonunified spidermonkey builds, r=terrence (3e0544510d)
- Bug 1273180 - Trigger major GC if necessary after minor GC r=terrence (a9a9be8b17)
- Bug 1268992 - Assert that the heap is empty after a shutdown GC; r=jonco (fe3ac5bdd6)
- Bug 1273908 - Refactor GC to make it easier to add new kinds of GC thing r=terrence (51c92e1b05)
2024-10-09 21:36:44 +08:00

627 lines
14 KiB
C++

/* -*- 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/. */
#ifndef mozilla_RefPtr_h
#define mozilla_RefPtr_h
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
/*****************************************************************************/
// template <class T> class RefPtrGetterAddRefs;
class nsCOMPtr_helper;
namespace mozilla {
template<class T> class OwningNonNull;
// Traditionally, RefPtr supports automatic refcounting of any pointer type
// with AddRef() and Release() methods that follow the traditional semantics.
//
// This traits class can be specialized to operate on other pointer types. For
// example, we specialize this trait for opaque FFI types that represent
// refcounted objects in Rust.
//
// Given the use of ConstRemovingRefPtrTraits below, U should not be a const-
// qualified type.
template<class U>
struct RefPtrTraits
{
static void AddRef(U* aPtr) {
aPtr->AddRef();
}
static void Release(U* aPtr) {
aPtr->Release();
}
};
} // namespace mozilla
template <class T>
class RefPtr
{
private:
void
assign_with_AddRef(T* aRawPtr)
{
if (aRawPtr) {
ConstRemovingRefPtrTraits<T>::AddRef(aRawPtr);
}
assign_assuming_AddRef(aRawPtr);
}
void
assign_assuming_AddRef(T* aNewPtr)
{
T* oldPtr = mRawPtr;
mRawPtr = aNewPtr;
if (oldPtr) {
ConstRemovingRefPtrTraits<T>::Release(oldPtr);
}
}
private:
T* MOZ_OWNING_REF mRawPtr;
public:
typedef T element_type;
~RefPtr()
{
if (mRawPtr) {
ConstRemovingRefPtrTraits<T>::Release(mRawPtr);
}
}
// Constructors
RefPtr()
: mRawPtr(0)
// default constructor
{
}
RefPtr(const RefPtr<T>& aSmartPtr)
: mRawPtr(aSmartPtr.mRawPtr)
// copy-constructor
{
if (mRawPtr) {
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
}
}
RefPtr(RefPtr<T>&& aRefPtr)
: mRawPtr(aRefPtr.mRawPtr)
{
aRefPtr.mRawPtr = nullptr;
}
// construct from a raw pointer (of the right type)
MOZ_IMPLICIT RefPtr(T* aRawPtr)
: mRawPtr(aRawPtr)
{
if (mRawPtr) {
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
}
}
template <typename I>
MOZ_IMPLICIT RefPtr(already_AddRefed<I>& aSmartPtr)
: mRawPtr(aSmartPtr.take())
// construct from |already_AddRefed|
{
}
template <typename I>
MOZ_IMPLICIT RefPtr(already_AddRefed<I>&& aSmartPtr)
: mRawPtr(aSmartPtr.take())
// construct from |otherRefPtr.forget()|
{
}
template <typename I>
MOZ_IMPLICIT RefPtr(const RefPtr<I>& aSmartPtr)
: mRawPtr(aSmartPtr.get())
// copy-construct from a smart pointer with a related pointer type
{
if (mRawPtr) {
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
}
}
template <typename I>
MOZ_IMPLICIT RefPtr(RefPtr<I>&& aSmartPtr)
: mRawPtr(aSmartPtr.forget().take())
// construct from |Move(RefPtr<SomeSubclassOfT>)|.
{
}
MOZ_IMPLICIT RefPtr(const nsCOMPtr_helper& aHelper);
// Defined in OwningNonNull.h
template<class U>
MOZ_IMPLICIT RefPtr(const mozilla::OwningNonNull<U>& aOther);
// Assignment operators
RefPtr<T>&
operator=(const RefPtr<T>& aRhs)
// copy assignment operator
{
assign_with_AddRef(aRhs.mRawPtr);
return *this;
}
template <typename I>
RefPtr<T>&
operator=(const RefPtr<I>& aRhs)
// assign from an RefPtr of a related pointer type
{
assign_with_AddRef(aRhs.get());
return *this;
}
RefPtr<T>&
operator=(T* aRhs)
// assign from a raw pointer (of the right type)
{
assign_with_AddRef(aRhs);
return *this;
}
template <typename I>
RefPtr<T>&
operator=(already_AddRefed<I>& aRhs)
// assign from |already_AddRefed|
{
assign_assuming_AddRef(aRhs.take());
return *this;
}
template <typename I>
RefPtr<T>&
operator=(already_AddRefed<I> && aRhs)
// assign from |otherRefPtr.forget()|
{
assign_assuming_AddRef(aRhs.take());
return *this;
}
RefPtr<T>& operator=(const nsCOMPtr_helper& aHelper);
RefPtr<T>&
operator=(RefPtr<T> && aRefPtr)
{
assign_assuming_AddRef(aRefPtr.mRawPtr);
aRefPtr.mRawPtr = nullptr;
return *this;
}
// Defined in OwningNonNull.h
template<class U>
RefPtr<T>&
operator=(const mozilla::OwningNonNull<U>& aOther);
// Other pointer operators
void
swap(RefPtr<T>& aRhs)
// ...exchange ownership with |aRhs|; can save a pair of refcount operations
{
T* temp = aRhs.mRawPtr;
aRhs.mRawPtr = mRawPtr;
mRawPtr = temp;
}
void
swap(T*& aRhs)
// ...exchange ownership with |aRhs|; can save a pair of refcount operations
{
T* temp = aRhs;
aRhs = mRawPtr;
mRawPtr = temp;
}
already_AddRefed<T>
forget()
// return the value of mRawPtr and null out mRawPtr. Useful for
// already_AddRefed return values.
{
T* temp = 0;
swap(temp);
return already_AddRefed<T>(temp);
}
template <typename I>
void
forget(I** aRhs)
// Set the target of aRhs to the value of mRawPtr and null out mRawPtr.
// Useful to avoid unnecessary AddRef/Release pairs with "out"
// parameters where aRhs bay be a T** or an I** where I is a base class
// of T.
{
MOZ_ASSERT(aRhs, "Null pointer passed to forget!");
*aRhs = mRawPtr;
mRawPtr = 0;
}
T*
get() const
/*
Prefer the implicit conversion provided automatically by |operator T*() const|.
Use |get()| to resolve ambiguity or to get a castable pointer.
*/
{
return const_cast<T*>(mRawPtr);
}
operator T*() const
#ifdef MOZ_HAVE_REF_QUALIFIERS
&
#endif
/*
...makes an |RefPtr| act like its underlying raw pointer type whenever it
is used in a context where a raw pointer is expected. It is this operator
that makes an |RefPtr| substitutable for a raw pointer.
Prefer the implicit use of this operator to calling |get()|, except where
necessary to resolve ambiguity.
*/
{
return get();
}
#ifdef MOZ_HAVE_REF_QUALIFIERS
// Don't allow implicit conversion of temporary RefPtr to raw pointer,
// because the refcount might be one and the pointer will immediately become
// invalid.
operator T*() const && = delete;
// These are needed to avoid the deleted operator above. XXX Why is operator!
// needed separately? Shouldn't the compiler prefer using the non-deleted
// operator bool instead of the deleted operator T*?
explicit operator bool() const { return !!mRawPtr; }
bool operator!() const { return !mRawPtr; }
#endif
T*
operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
{
MOZ_ASSERT(mRawPtr != 0,
"You can't dereference a NULL RefPtr with operator->().");
return get();
}
template <typename R, typename... Args>
class Proxy
{
typedef R (T::*member_function)(Args...);
T* mRawPtr;
member_function mFunction;
public:
Proxy(T* aRawPtr, member_function aFunction)
: mRawPtr(aRawPtr),
mFunction(aFunction)
{
}
template<typename... ActualArgs>
R operator()(ActualArgs&&... aArgs)
{
return ((*mRawPtr).*mFunction)(mozilla::Forward<ActualArgs>(aArgs)...);
}
};
template <typename R, typename... Args>
Proxy<R, Args...> operator->*(R (T::*aFptr)(Args...)) const
{
MOZ_ASSERT(mRawPtr != 0,
"You can't dereference a NULL RefPtr with operator->*().");
return Proxy<R, Args...>(get(), aFptr);
}
RefPtr<T>*
get_address()
// This is not intended to be used by clients. See |address_of|
// below.
{
return this;
}
const RefPtr<T>*
get_address() const
// This is not intended to be used by clients. See |address_of|
// below.
{
return this;
}
public:
T&
operator*() const
{
MOZ_ASSERT(mRawPtr != 0,
"You can't dereference a NULL RefPtr with operator*().");
return *get();
}
T**
StartAssignment()
{
assign_assuming_AddRef(0);
return reinterpret_cast<T**>(&mRawPtr);
}
private:
// This helper class makes |RefPtr<const T>| possible by casting away
// the constness from the pointer when calling AddRef() and Release().
//
// This is necessary because AddRef() and Release() implementations can't
// generally expected to be const themselves (without heavy use of |mutable|
// and |const_cast| in their own implementations).
//
// This should be sound because while |RefPtr<const T>| provides a
// const view of an object, the object itself should not be const (it
// would have to be allocated as |new const T| or similar to be const).
template<class U>
struct ConstRemovingRefPtrTraits
{
static void AddRef(U* aPtr) {
mozilla::RefPtrTraits<U>::AddRef(aPtr);
}
static void Release(U* aPtr) {
mozilla::RefPtrTraits<U>::Release(aPtr);
}
};
template<class U>
struct ConstRemovingRefPtrTraits<const U>
{
static void AddRef(const U* aPtr) {
mozilla::RefPtrTraits<U>::AddRef(const_cast<U*>(aPtr));
}
static void Release(const U* aPtr) {
mozilla::RefPtrTraits<U>::Release(const_cast<U*>(aPtr));
}
};
};
class nsCycleCollectionTraversalCallback;
template <typename T>
void
CycleCollectionNoteChild(nsCycleCollectionTraversalCallback& aCallback,
T* aChild, const char* aName, uint32_t aFlags);
template <typename T>
inline void
ImplCycleCollectionUnlink(RefPtr<T>& aField)
{
aField = nullptr;
}
template <typename T>
inline void
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
RefPtr<T>& aField,
const char* aName,
uint32_t aFlags = 0)
{
CycleCollectionNoteChild(aCallback, aField.get(), aName, aFlags);
}
template <class T>
inline RefPtr<T>*
address_of(RefPtr<T>& aPtr)
{
return aPtr.get_address();
}
template <class T>
inline const RefPtr<T>*
address_of(const RefPtr<T>& aPtr)
{
return aPtr.get_address();
}
template <class T>
class RefPtrGetterAddRefs
/*
...
This class is designed to be used for anonymous temporary objects in the
argument list of calls that return COM interface pointers, e.g.,
RefPtr<IFoo> fooP;
...->GetAddRefedPointer(getter_AddRefs(fooP))
DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()| instead.
When initialized with a |RefPtr|, as in the example above, it returns
a |void**|, a |T**|, or an |nsISupports**| as needed, that the
outer call (|GetAddRefedPointer| in this case) can fill in.
This type should be a nested class inside |RefPtr<T>|.
*/
{
public:
explicit
RefPtrGetterAddRefs(RefPtr<T>& aSmartPtr)
: mTargetSmartPtr(aSmartPtr)
{
// nothing else to do
}
operator void**()
{
return reinterpret_cast<void**>(mTargetSmartPtr.StartAssignment());
}
operator T**()
{
return mTargetSmartPtr.StartAssignment();
}
T*&
operator*()
{
return *(mTargetSmartPtr.StartAssignment());
}
private:
RefPtr<T>& mTargetSmartPtr;
};
template <class T>
inline RefPtrGetterAddRefs<T>
getter_AddRefs(RefPtr<T>& aSmartPtr)
/*
Used around a |RefPtr| when
...makes the class |RefPtrGetterAddRefs<T>| invisible.
*/
{
return RefPtrGetterAddRefs<T>(aSmartPtr);
}
// Comparing two |RefPtr|s
template <class T, class U>
inline bool
operator==(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
{
return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs.get());
}
template <class T, class U>
inline bool
operator!=(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
{
return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs.get());
}
// Comparing an |RefPtr| to a raw pointer
template <class T, class U>
inline bool
operator==(const RefPtr<T>& aLhs, const U* aRhs)
{
return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs);
}
template <class T, class U>
inline bool
operator==(const U* aLhs, const RefPtr<T>& aRhs)
{
return static_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
}
template <class T, class U>
inline bool
operator!=(const RefPtr<T>& aLhs, const U* aRhs)
{
return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs);
}
template <class T, class U>
inline bool
operator!=(const U* aLhs, const RefPtr<T>& aRhs)
{
return static_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
}
template <class T, class U>
inline bool
operator==(const RefPtr<T>& aLhs, U* aRhs)
{
return static_cast<const T*>(aLhs.get()) == const_cast<const U*>(aRhs);
}
template <class T, class U>
inline bool
operator==(U* aLhs, const RefPtr<T>& aRhs)
{
return const_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
}
template <class T, class U>
inline bool
operator!=(const RefPtr<T>& aLhs, U* aRhs)
{
return static_cast<const T*>(aLhs.get()) != const_cast<const U*>(aRhs);
}
template <class T, class U>
inline bool
operator!=(U* aLhs, const RefPtr<T>& aRhs)
{
return const_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
}
// Comparing an |RefPtr| to |nullptr|
template <class T>
inline bool
operator==(const RefPtr<T>& aLhs, decltype(nullptr))
{
return aLhs.get() == nullptr;
}
template <class T>
inline bool
operator==(decltype(nullptr), const RefPtr<T>& aRhs)
{
return nullptr == aRhs.get();
}
template <class T>
inline bool
operator!=(const RefPtr<T>& aLhs, decltype(nullptr))
{
return aLhs.get() != nullptr;
}
template <class T>
inline bool
operator!=(decltype(nullptr), const RefPtr<T>& aRhs)
{
return nullptr != aRhs.get();
}
/*****************************************************************************/
template <class T>
inline already_AddRefed<T>
do_AddRef(T* aObj)
{
RefPtr<T> ref(aObj);
return ref.forget();
}
namespace mozilla {
/**
* Helper function to be able to conveniently write things like:
*
* already_AddRefed<T>
* f(...)
* {
* return MakeAndAddRef<T>(...);
* }
*/
template<typename T, typename... Args>
already_AddRefed<T>
MakeAndAddRef(Args&&... aArgs)
{
RefPtr<T> p(new T(Forward<Args>(aArgs)...));
return p.forget();
}
} // namespace mozilla
#endif /* mozilla_RefPtr_h */