/* -*- 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/. */ // IWYU pragma: private, include "nsISupports.h"
#define MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(X) \
static_assert(!std::is_destructible_v<X>, \ "Reference-counted class "#X \ " should not have a public destructor. " \ "Make this class's destructor non-public");
# include "prthread.h"/* needed for thread-safety checks */
class nsAutoOwningThread { public:
nsAutoOwningThread();
// We move the actual assertion checks out-of-line to minimize code bloat, // but that means we have to pass a non-literal string to MOZ_CRASH_UNSAFE. // To make that more safe, the public interface requires a literal string // and passes that to the private interface; we can then be assured that we // effectively are passing a literal string to MOZ_CRASH_UNSAFE. template <int N> void AssertOwnership(constchar (&aMsg)[N]) const {
AssertCurrentThreadOwnsMe(aMsg);
}
// We move the actual assertion checks out-of-line to minimize code bloat, // but that means we have to pass a non-literal string to MOZ_CRASH_UNSAFE. // To make that more safe, the public interface requires a literal string // and passes that to the private interface; we can then be assured that we // effectively are passing a literal string to MOZ_CRASH_UNSAFE. template <int N> void AssertOwnership(constchar (&aMsg)[N]) const {
AssertCurrentThreadOwnsMe(aMsg);
}
# define MOZ_ASSERT_CLASSNAME(_type) \
static_assert(std::is_class_v<_type>, \ "Token '"#_type "' is not a class type.")
# define MOZ_ASSERT_NOT_ISUPPORTS(_type) \
static_assert(!std::is_base_of<nsISupports, _type>::value, \ "nsISupports classes don't need to call MOZ_COUNT_CTOR or " \ "MOZ_COUNT_DTOR");
// Note that the following constructor/destructor logging macros are redundant // for refcounted objects that log via the NS_LOG_ADDREF/NS_LOG_RELEASE macros. // Refcount logging is preferred. # define MOZ_COUNT_CTOR(_type) \ do { \
MOZ_ASSERT_CLASSNAME(_type); \
MOZ_ASSERT_NOT_ISUPPORTS(_type); \
NS_LogCtor((void*)this, #_type, sizeof(*this)); \
} while (0)
/* nsCOMPtr.h allows these macros to be defined by clients * These logging functions require dynamic_cast<void*>, so they don't * do anything useful if we don't have dynamic_cast<void*>. * Note: The explicit comparison to nullptr is needed to avoid warnings
* when _p is a nullptr itself. */ # define NSCAP_LOG_ASSIGNMENT(_c, _p) \ if (_p != nullptr) NS_LogCOMPtrAddRef((_c), ToSupports(_p))
# define NSCAP_LOG_RELEASE(_c, _p) \ if (_p) NS_LogCOMPtrRelease((_c), ToSupports(_p))
MOZ_ALWAYS_INLINE uintptr_t incr(void* aOwner,
nsCycleCollectionParticipant* aCp) {
mRefCntAndFlags += NS_REFCOUNT_CHANGE;
mRefCntAndFlags &= ~NS_IS_PURPLE; // For incremental cycle collection, use the purple buffer to track objects // that have been AddRef'd. if (!IsInPurpleBuffer()) {
mRefCntAndFlags |= NS_IN_PURPLE_BUFFER; // Refcount isn't zero, so Suspect won't delete anything.
MOZ_ASSERT(get() > 0);
NS_CycleCollectorSuspect3(aOwner, aCp, this, nullptr);
} return NS_REFCOUNT_VALUE(mRefCntAndFlags);
}
MOZ_ALWAYS_INLINE void stabilizeForDeletion() { // Set refcnt to 1 and mark us to be in the purple buffer. // This way decr won't call suspect again.
mRefCntAndFlags = NS_REFCOUNT_CHANGE | NS_IN_PURPLE_BUFFER;
}
// Cycle collected objects can only be used on a single thread, so if we ever // see an object on the main thread, it will always be on the main thread. // The cycle collector then uses this information to utilize faster nursery // purple buffer on the main thread. // The method works only on 64 bit systems.
MOZ_ALWAYS_INLINE void SetIsOnMainThread() { #ifdef HAVE_64BIT_BUILD
mRefCntAndFlags |= NS_IS_ON_MAINTHREAD; #endif
}
#ifdef HAVE_64BIT_BUILD // The NS_IS_ON_MAINTHREAD flag only exists on 64-bit builds.
MOZ_ALWAYS_INLINE bool IsOnMainThread() { return !!(mRefCntAndFlags & NS_IS_ON_MAINTHREAD);
} #endif
// only support prefix increment/decrement
MOZ_ALWAYS_INLINE nsrefcnt operator++() { // Memory synchronization is not required when incrementing a // reference count. The first increment of a reference count on a // thread is not important, since the first use of the object on a // thread can happen before it. What is important is the transfer // of the pointer to that thread, which may happen prior to the // first increment on that thread. The necessary memory // synchronization is done by the mechanism that transfers the // pointer between threads. return mValue.fetch_add(1, std::memory_order_relaxed) + 1;
}
MOZ_ALWAYS_INLINE nsrefcnt operator--() { // Since this may be the last release on this thread, we need // release semantics so that prior writes on this thread are visible // to the thread that destroys the object when it reads mValue with // acquire semantics.
nsrefcnt result = mValue.fetch_sub(1, std::memory_order_release) - 1; if (result == 0) { // We're going to destroy the object on this thread, so we need // acquire semantics to synchronize with the memory released by // the last release on other threads, that is, to ensure that // writes prior to that release are now visible on this thread. #ifdef MOZ_TSAN // TSan doesn't understand std::atomic_thread_fence, so in order // to avoid a false positive for every time a refcounted object // is deleted, we replace the fence with an atomic operation.
mValue.load(std::memory_order_acquire); #else
std::atomic_thread_fence(std::memory_order_acquire); #endif
} return result;
}
MOZ_ALWAYS_INLINE nsrefcnt operator=(nsrefcnt aValue) { // Use release semantics since we're not sure what the caller is // doing.
mValue.store(aValue, std::memory_order_release); return aValue;
}
MOZ_ALWAYS_INLINE operator nsrefcnt() const { return get(); }
MOZ_ALWAYS_INLINE nsrefcnt get() const { // Use acquire semantics since we're not sure what the caller is // doing. return mValue.load(std::memory_order_acquire);
}
// Type trait indicating whether a given XPCOM interface class may only be // implemented by types with threadsafe refcounts. This is specialized for // classes with the `rust_sync` annotation within XPIDL-generated header files, // and checked within macro-generated QueryInterface implementations. template <typename T> class InterfaceNeedsThreadSafeRefCnt : public std::false_type {};
/** * Use this macro to declare and implement the AddRef & Release methods for a * given non-XPCOM <i>_class</i>. * * @param _class The name of the class implementing the method * @param _destroy A statement that is executed when the object's * refcount drops to zero. * @param _decl Name of the macro to be used for the return type of the * AddRef & Release methods (typically NS_IMETHOD_ or NS_METHOD_). * @param optional override Mark the AddRef & Release methods as overrides.
*/ #define NS_INLINE_DECL_REFCOUNTING_META(_class, _decl, _destroy, _owning, ...) \ public: \
_decl(MozExternalRefCountType) AddRef(void) __VA_ARGS__ { \
MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(_class) \
MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
NS_ASSERT_OWNINGTHREAD(_class); \
++mRefCnt; \
NS_LOG_ADDREF(this, mRefCnt, #_class, sizeof(*this)); \ return mRefCnt; \
} \
_decl(MozExternalRefCountType) Release(void) __VA_ARGS__ { \
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
NS_ASSERT_OWNINGTHREAD(_class); \
--mRefCnt; \
NS_LOG_RELEASE(this, mRefCnt, #_class); \ if (mRefCnt == 0) { \
mRefCnt = 1; /* stabilize */ \
_destroy; \ return 0; \
} \ return mRefCnt; \
} \ using HasThreadSafeRefCnt = std::false_type; \
\ protected: \
nsAutoRefCnt mRefCnt; \
_owning public:
/** * Use this macro to declare and implement the AddRef & Release methods for a * given non-XPCOM <i>_class</i>. * * @param _class The name of the class implementing the method * @param _destroy A statement that is executed when the object's * refcount drops to zero. * @param optional override Mark the AddRef & Release methods as overrides.
*/ #define NS_INLINE_DECL_REFCOUNTING_WITH_DESTROY(_class, _destroy, ...) \
NS_INLINE_DECL_REFCOUNTING_META(_class, NS_METHOD_, _destroy, \
NS_DECL_OWNINGTHREAD, __VA_ARGS__)
/** * Use this macro to declare and implement the AddRef & Release methods for a * given non-XPCOM <i>_class</i>. * * @param _class The name of the class implementing the method * @param optional override Mark the AddRef & Release methods as overrides.
*/ #define NS_INLINE_DECL_REFCOUNTING(_class, ...) \
NS_INLINE_DECL_REFCOUNTING_WITH_DESTROY(_class, delete (this), __VA_ARGS__)
/** * Like NS_INLINE_DECL_REFCOUNTING, however the thread safety check will work * with any nsISerialEventTarget. This is a workaround until bug 1648031 is * properly resolved. Once this is done, it will be possible to use * NS_INLINE_DECL_REFCOUNTING under all circumstances.
*/ #define NS_INLINE_DECL_REFCOUNTING_ONEVENTTARGET(_class, ...) \
NS_INLINE_DECL_REFCOUNTING_META(_class, NS_METHOD_, delete (this), \
NS_DECL_OWNINGEVENTTARGET, __VA_ARGS__)
/** * Use this macro to declare and implement the AddRef & Release methods for a * given non-XPCOM <i>_class</i> in a threadsafe manner. * * DOES NOT DO REFCOUNT STABILIZATION! * * @param _class The name of the class implementing the method * @param _destroy A statement that is executed when the object's * refcount drops to zero. * @param optional override Mark the AddRef & Release methods as overrides.
*/ #define NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(_class, _destroy, \
...) \
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_META(_class, NS_METHOD_, _destroy, \
__VA_ARGS__)
/** * Use this macro to declare and implement the AddRef & Release methods for a * given non-XPCOM <i>_class</i> in a threadsafe manner. * * DOES NOT DO REFCOUNT STABILIZATION! * * @param _class The name of the class implementing the method * @param optional override Mark the AddRef & Release methods as overrides.
*/ #define NS_INLINE_DECL_THREADSAFE_REFCOUNTING(_class, ...) \
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(_class, delete (this), \
__VA_ARGS__)
#if !defined(XPCOM_GLUE_AVOID_NSPR) class nsISerialEventTarget; namespace mozilla { // Forward-declare `GetMainThreadSerialEventTarget`, as `nsISupportsImpl.h` // cannot include `nsThreadUtils.h`.
nsISerialEventTarget* GetMainThreadSerialEventTarget();
/** * Helper for _WITH_DELETE_ON_EVENT_TARGET threadsafe refcounting macros which * provides an implementation of `_destroy`
*/ # define NS_PROXY_DELETE_TO_EVENT_TARGET(_class, _target) \
::mozilla::detail::ProxyDeleteVoid( \ "ProxyDelete "#_class, _target, this, \
[](void* self) { deletestatic_cast<_class*>(self); })
/** * Use this macro to declare and implement the AddRef & Release methods for a * given non-XPCOM <i>_class</i> in a threadsafe manner, ensuring the * destructor runs on a specific nsISerialEventTarget. * * DOES NOT DO REFCOUNT STABILIZATION! * * @param _class The name of the class implementing the method * @param _target nsISerialEventTarget to run the class's destructor on * @param optional override Mark the AddRef & Release methods as overrides
*/ # define NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_EVENT_TARGET( \
_class, _target, ...) \
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY( \
_class, NS_PROXY_DELETE_TO_EVENT_TARGET(_class, _target), __VA_ARGS__)
/** * Use this macro to declare and implement the AddRef & Release methods for a * given non-XPCOM <i>_class</i> in a threadsafe manner, ensuring the * destructor runs on the main thread. * * DOES NOT DO REFCOUNT STABILIZATION! * * @param _class The name of the class implementing the method * @param optional override Mark the AddRef & Release methods as overrides
*/ # define NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_MAIN_THREAD( \
_class, ...) \
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_EVENT_TARGET( \
_class, ::mozilla::GetMainThreadSerialEventTarget(), __VA_ARGS__) #endif
/** * Use this macro in interface classes that you want to be able to reference * using RefPtr, but don't want to provide a refcounting implemenation. The * refcounting implementation can be provided by concrete subclasses that * implement the interface.
*/ #define NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING \ public: \
NS_IMETHOD_(MozExternalRefCountType) AddRef(void) = 0; \
NS_IMETHOD_(MozExternalRefCountType) Release(void) = 0; \
\ public:
/** * Use this macro to implement the AddRef method for a given <i>_class</i> * @param _class The name of the class implementing the method * @param _name The class name to be passed to XPCOM leak checking
*/ #define NS_IMPL_NAMED_ADDREF(_class, _name) \
NS_IMETHODIMP_(MozExternalRefCountType) _class::AddRef(void) { \
MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(_class) \
MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
MOZ_ASSERT(_name != nullptr, "Must specify a name"); \ if (!mRefCnt.isThreadSafe) NS_ASSERT_OWNINGTHREAD(_class); \
nsrefcnt count = ++mRefCnt; \
NS_LOG_ADDREF(this, count, _name, sizeof(*this)); \ return count; \
}
/** * Use this macro to implement the AddRef method for a given <i>_class</i> * @param _class The name of the class implementing the method
*/ #define NS_IMPL_ADDREF(_class) NS_IMPL_NAMED_ADDREF(_class, #_class)
/** * Use this macro to implement the AddRef method for a given <i>_class</i> * implemented as a wholly owned aggregated object intended to implement * interface(s) for its owner * @param _class The name of the class implementing the method * @param _aggregator the owning/containing object
*/ #define NS_IMPL_ADDREF_USING_AGGREGATOR(_class, _aggregator) \
NS_IMETHODIMP_(MozExternalRefCountType) _class::AddRef(void) { \
MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(_class) \
MOZ_ASSERT(_aggregator, "null aggregator"); \ return (_aggregator)->AddRef(); \
}
// We decrement the refcnt before logging the actual release, but when logging // named things, accessing the name may not be valid after the refcnt // decrement, because the object may have been destroyed on a different thread. // Use this macro to ensure that we have a local copy of the name prior to // the refcnt decrement. (We use a macro to make absolutely sure the name // isn't loaded in builds where it wouldn't be used.) #ifdef NS_BUILD_REFCNT_LOGGING # define NS_LOAD_NAME_BEFORE_RELEASE(localname, _name) \ constchar* const localname = _name #else # define NS_LOAD_NAME_BEFORE_RELEASE(localname, _name) #endif
/** * Use this macro to implement the Release method for a given * <i>_class</i>. * @param _class The name of the class implementing the method * @param _name The class name to be passed to XPCOM leak checking * @param _destroy A statement that is executed when the object's * refcount drops to zero. * * For example, * * NS_IMPL_RELEASE_WITH_DESTROY(Foo, "Foo", Destroy(this)) * * will cause * * Destroy(this); * * to be invoked when the object's refcount drops to zero. This * allows for arbitrary teardown activity to occur (e.g., deallocation * of object allocated with placement new).
*/ #define NS_IMPL_NAMED_RELEASE_WITH_DESTROY(_class, _name, _destroy) \
NS_IMETHODIMP_(MozExternalRefCountType) _class::Release(void) { \
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
MOZ_ASSERT(_name != nullptr, "Must specify a name"); \ if (!mRefCnt.isThreadSafe) NS_ASSERT_OWNINGTHREAD(_class); \
NS_LOAD_NAME_BEFORE_RELEASE(nametmp, _name); \
nsrefcnt count = --mRefCnt; \
NS_LOG_RELEASE(this, count, nametmp); \ if (count == 0) { \
mRefCnt = 1; /* stabilize */ \
_destroy; \ return 0; \
} \ return count; \
}
/** * Use this macro to implement the Release method for a given <i>_class</i> * @param _class The name of the class implementing the method * * A note on the 'stabilization' of the refcnt to one. At that point, * the object's refcount will have gone to zero. The object's * destructor may trigger code that attempts to QueryInterface() and * Release() 'this' again. Doing so will temporarily increment and * decrement the refcount. (Only a logic error would make one try to * keep a permanent hold on 'this'.) To prevent re-entering the * destructor, we make sure that no balanced refcounting can return * the refcount to |0|.
*/ #define NS_IMPL_RELEASE(_class) \
NS_IMPL_RELEASE_WITH_DESTROY(_class, delete (this))
/** * Use this macro to implement the Release method for a given <i>_class</i> * implemented as a wholly owned aggregated object intended to implement * interface(s) for its owner * @param _class The name of the class implementing the method * @param _aggregator the owning/containing object
*/ #define NS_IMPL_RELEASE_USING_AGGREGATOR(_class, _aggregator) \
NS_IMETHODIMP_(MozExternalRefCountType) _class::Release(void) { \
MOZ_ASSERT(_aggregator, "null aggregator"); \ return (_aggregator)->Release(); \
}
// _LAST_RELEASE can be useful when certain resources should be released // as soon as we know the object will be deleted. #define NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(_class, _last) \
NS_IMETHODIMP_(MozExternalRefCountType) _class::Release(void) { \
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
NS_ASSERT_OWNINGTHREAD(_class); \ bool shouldDelete = false; \
nsISupports* base = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Upcast(this); \
nsrefcnt count = mRefCnt.decr(base, &shouldDelete); \
NS_LOG_RELEASE(this, count, #_class); \ if (count == 0) { \
mRefCnt.incr(base); \
_last; \
mRefCnt.decr(base); \ if (shouldDelete) { \
mRefCnt.stabilizeForDeletion(); \
DeleteCycleCollectable(); \
} \
} \ return count; \
} \
NS_IMETHODIMP_(void) _class::DeleteCycleCollectable(void) { deletethis; }
// This macro is same as NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE // except it doesn't have DeleteCycleCollectable. #define NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE_AND_DESTROY( \
_class, _last, _destroy) \
NS_IMETHODIMP_(MozExternalRefCountType) _class::Release(void) { \
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
NS_ASSERT_OWNINGTHREAD(_class); \ bool shouldDelete = false; \
nsISupports* base = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Upcast(this); \
nsrefcnt count = mRefCnt.decr(base, &shouldDelete); \
NS_LOG_RELEASE(this, count, #_class); \ if (count == 0) { \
mRefCnt.incr(base); \
_last; \
mRefCnt.decr(base); \ if (shouldDelete) { \
mRefCnt.stabilizeForDeletion(); \
DeleteCycleCollectable(); \
} \
} \ return count; \
} \
NS_IMETHODIMP_(void) _class::DeleteCycleCollectable(void) { _destroy; }
// _WITH_INTERRUPTABLE_LAST_RELEASE can be useful when certain resources // should be released as soon as we know the object will be deleted and the // instance may be cached for reuse. // _last is performed for cleaning up its resources. Then, _maybeInterrupt is // tested and when it returns true, this stops deleting the instance. // (Note that it's not allowed to grab the instance with nsCOMPtr or RefPtr // during _last is performed.) // Therefore, when _maybeInterrupt returns true, the instance has to be grabbed // by nsCOMPtr or RefPtr. #define NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_INTERRUPTABLE_LAST_RELEASE( \
_class, _last, _maybeInterrupt) \
NS_IMETHODIMP_(MozExternalRefCountType) _class::Release(void) { \
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
NS_ASSERT_OWNINGTHREAD(_class); \ bool shouldDelete = false; \
nsISupports* base = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Upcast(this); \
nsrefcnt count = mRefCnt.decr(base, &shouldDelete); \
NS_LOG_RELEASE(this, count, #_class); \ if (count == 0) { \
mRefCnt.incr(base); \
_last; \
mRefCnt.decr(base); \ if (_maybeInterrupt) { \
MOZ_ASSERT(mRefCnt.get() > 0); \ return mRefCnt.get(); \
} \ if (shouldDelete) { \
mRefCnt.stabilizeForDeletion(); \
DeleteCycleCollectable(); \
} \
} \ return count; \
} \
NS_IMETHODIMP_(void) _class::DeleteCycleCollectable(void) { deletethis; }
// Helper which is roughly equivalent to NS_GET_IID, which also performs static // assertions that `Class` is allowed to implement the given XPCOM interface. // // These assertions are done like this to allow them to be used within the // `NS_INTERFACE_TABLE_ENTRY` macro, though they are also used in // `NS_IMPL_QUERY_BODY`. template <typenameClass, typename Interface>
constexpr const nsIID& GetImplementedIID() { if constexpr (mozilla::detail::InterfaceNeedsThreadSafeRefCnt<
Interface>::value) {
static_assert(Class::HasThreadSafeRefCnt::value, "Cannot implement a threadsafe interface with " "non-threadsafe refcounting!");
} return NS_GET_TEMPLATE_IID(Interface);
}
/** * There are two ways of implementing QueryInterface, and we use both: * * Table-driven QueryInterface uses a static table of IID->offset mappings * and a shared helper function. Using it tends to reduce codesize and improve * runtime performance (due to processor cache hits). * * Macro-driven QueryInterface generates a QueryInterface function directly * using common macros. This is necessary if special QueryInterface features * are being used (such as tearoffs and conditional interfaces). * * These methods can be combined into a table-driven function call followed * by custom code for tearoffs and conditionals.
*/
struct QITableEntry { const nsIID* iid; // null indicates end of the QITableEntry array
int32_t offset;
};
/** * This implements query interface with two assumptions: First, the * class in question implements nsISupports and its own interface and * nothing else. Second, the implementation of the class's primary * inheritance chain leads to its own interface. * * @param _class The name of the class implementing the method * @param _classiiddef The name of the #define symbol that defines the IID * for the class (e.g. NS_ISUPPORTS_IID)
*/
// Use this for querying to concrete class types which cannot be unambiguously // cast to nsISupports. See also nsQueryObject.h. #define NS_IMPL_QUERY_BODY_CONCRETE(_class) \ if (aIID.Equals(NS_IMPL_QUERY_BODY_IID(_class))) { \
*aInstancePtr = do_AddRef(static_cast<_class*>(this)).take(); \ return NS_OK; \
} else
/* This is the new scheme. Using this notation now will allow us to switch to a table driven mechanism when it's ready. Note the difference between this and the (currently) underlying NS_IMPL_QUERY_INTERFACE mechanism. You must explicitly mention |nsISupports| when using the interface maps.
*/ #define NS_INTERFACE_MAP_BEGIN(_implClass) NS_IMPL_QUERY_HEAD(_implClass) #define NS_INTERFACE_MAP_ENTRY(_interface) NS_IMPL_QUERY_BODY(_interface) #define NS_INTERFACE_MAP_ENTRY_CONDITIONAL(_interface, condition) \
NS_IMPL_QUERY_BODY_CONDITIONAL(_interface, condition) #define NS_INTERFACE_MAP_ENTRY_AGGREGATED(_interface, _aggregate) \
NS_IMPL_QUERY_BODY_AGGREGATED(_interface, _aggregate)
/** * Declare that you're going to inherit from something that already * implements nsISupports, but also implements an additional interface, thus * causing an ambiguity. In this case you don't need another mRefCnt, you * just need to forward the definitions to the appropriate superclass. E.g. * * class Bar : public Foo, public nsIBar { // both provide nsISupports * public: * NS_DECL_ISUPPORTS_INHERITED * ...other nsIBar and Bar methods... * };
*/ #define NS_DECL_ISUPPORTS_INHERITED \ public: \
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override; \
NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override; \
NS_IMETHOD_(MozExternalRefCountType) Release(void) override;
/** * These macros can be used in conjunction with NS_DECL_ISUPPORTS_INHERITED * to implement the nsISupports methods, forwarding the invocations to a * superclass that already implements nsISupports. Don't do anything for * subclasses of Runnable because it deals with subclass logging in its own * way, using the mName field. * * Note that I didn't make these inlined because they're virtual methods.
*/
namespace mozilla { class Runnable; namespace detail { class SupportsThreadSafeWeakPtrBase;
// Don't NS_LOG_{ADDREF,RELEASE} when inheriting from `Runnable*` or types with // thread safe weak references, as it will generate incorrect refcnt logs due to // the thread-safe `Upgrade()` call's refcount modifications not calling through // the derived class' `AddRef()` and `Release()` methods. template <typename T>
constexpr bool ShouldLogInheritedRefcnt =
!std::is_convertible_v<T*, Runnable*> &&
!std::is_base_of_v<SupportsThreadSafeWeakPtrBase, T>;
}
} // namespace mozilla
#define NS_IMPL_ADDREF_INHERITED_GUTS(Class, Super) \
MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(Class) \
nsrefcnt r = Super::AddRef(); \ if constexpr (::mozilla::detail::ShouldLogInheritedRefcnt<Class>) { \
NS_LOG_ADDREF(this, r, #Class, sizeof(*this)); \
} \ return r /* Purposefully no trailing semicolon */
/** * As above but not logging the addref/release; needed if the base * class might be aggregated.
*/ #define NS_IMPL_NONLOGGING_ADDREF_INHERITED(Class, Super) \
NS_IMETHODIMP_(MozExternalRefCountType) Class::AddRef(void) { \
MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(Class) \ return Super::AddRef(); \
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.