Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / base / nsCycleCollectionParticipant.h
blobe4d293f42e825f827724c2cdc097679f9cf050ee
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsCycleCollectionParticipant_h__
8 #define nsCycleCollectionParticipant_h__
10 #include <type_traits>
11 #include "js/HeapAPI.h"
12 #include "js/TypeDecls.h"
13 #include "mozilla/MacroForEach.h"
14 #include "nsCycleCollectionNoteChild.h"
15 #include "nsDebug.h"
16 #include "nsID.h"
17 #include "nscore.h"
19 /**
20 * Note: the following two IIDs only differ in one bit in the last byte. This
21 * is a hack and is intentional in order to speed up the comparison inside
22 * NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED.
24 #define NS_XPCOMCYCLECOLLECTIONPARTICIPANT_IID \
25 { \
26 0xc61eac14, 0x5f7a, 0x4481, { \
27 0x96, 0x5e, 0x7e, 0xaa, 0x6e, 0xff, 0xa8, 0x5e \
28 } \
31 /**
32 * Special IID to get at the base nsISupports for a class. Usually this is the
33 * canonical nsISupports pointer, but in the case of tearoffs for example it is
34 * the base nsISupports pointer of the tearoff. This allow the cycle collector
35 * to have separate nsCycleCollectionParticipant's for tearoffs or aggregated
36 * classes.
38 #define NS_CYCLECOLLECTIONISUPPORTS_IID \
39 { \
40 0xc61eac14, 0x5f7a, 0x4481, { \
41 0x96, 0x5e, 0x7e, 0xaa, 0x6e, 0xff, 0xa8, 0x5f \
42 } \
45 namespace mozilla {
46 enum class CCReason : uint8_t {
47 NO_REASON,
49 // Purple buffer "overflow": enough objects are suspected to be cycle
50 // collectable to trigger an immediate CC.
51 MANY_SUSPECTED,
53 // The previous collection was kCCForced ago, and there are at least
54 // kCCForcedPurpleLimit objects suspected of being cycle collectable.
55 TIMED,
57 // Run a CC after a GC has completed.
58 GC_FINISHED,
60 // Run a slice of a collection.
61 SLICE,
63 // Manual reasons are explicitly triggered with a custom listener (as opposed
64 // to exceeding some internal threshold.) If a CC is already in progress,
65 // continue it. Otherwise, start a new one.
66 FIRST_MANUAL_REASON = 128,
68 // We want to GC, but must finish any ongoing cycle collection first.
69 GC_WAITING = FIRST_MANUAL_REASON,
71 // CC requested via an API. Used by tests.
72 API,
74 // Collecting in order to dump the heap.
75 DUMP_HEAP,
77 // Low memory situation detected.
78 MEM_PRESSURE,
80 // IPC message to a content process to trigger a CC. The original reason is
81 // not tracked.
82 IPC_MESSAGE,
84 // Cycle collection on a worker. The triggering reason is not tracked.
85 WORKER,
87 // Used for finding leaks.
88 SHUTDOWN
91 #define FOR_EACH_CCREASON(MACRO) \
92 MACRO(NO_REASON) \
93 MACRO(MANY_SUSPECTED) \
94 MACRO(TIMED) \
95 MACRO(GC_FINISHED) \
96 MACRO(SLICE) \
97 MACRO(GC_WAITING) \
98 MACRO(API) \
99 MACRO(DUMP_HEAP) \
100 MACRO(MEM_PRESSURE) \
101 MACRO(IPC_MESSAGE) \
102 MACRO(WORKER) \
103 MACRO(SHUTDOWN)
105 static inline bool IsManualCCReason(CCReason reason) {
106 return reason >= CCReason::FIRST_MANUAL_REASON;
109 static inline const char* CCReasonToString(CCReason aReason) {
110 switch (aReason) {
111 #define SET_REASON_STR(name) \
112 case CCReason::name: \
113 return #name; \
114 break;
115 FOR_EACH_CCREASON(SET_REASON_STR)
116 #undef SET_REASON_STR
117 default:
118 return "<unknown-reason>";
122 } // namespace mozilla
125 * Just holds the IID so NS_GET_IID works.
127 class nsCycleCollectionISupports {
128 public:
129 NS_DECLARE_STATIC_IID_ACCESSOR(NS_CYCLECOLLECTIONISUPPORTS_IID)
132 NS_DEFINE_STATIC_IID_ACCESSOR(nsCycleCollectionISupports,
133 NS_CYCLECOLLECTIONISUPPORTS_IID)
135 class nsCycleCollectionTraversalCallback;
136 class nsISupports;
137 class nsWrapperCache;
139 namespace JS {
140 template <class T>
141 class Heap;
142 template <typename T>
143 class TenuredHeap;
144 } /* namespace JS */
147 * A struct defining pure virtual methods which are called when tracing cycle
148 * collection paticipants. The appropriate method is called depending on the
149 * type of JS GC thing.
151 struct TraceCallbacks {
152 virtual void Trace(JS::Heap<JS::Value>* aPtr, const char* aName,
153 void* aClosure) const = 0;
154 virtual void Trace(JS::Heap<jsid>* aPtr, const char* aName,
155 void* aClosure) const = 0;
156 virtual void Trace(JS::Heap<JSObject*>* aPtr, const char* aName,
157 void* aClosure) const = 0;
158 virtual void Trace(nsWrapperCache* aPtr, const char* aName,
159 void* aClosure) const = 0;
160 virtual void Trace(JS::TenuredHeap<JSObject*>* aPtr, const char* aName,
161 void* aClosure) const = 0;
162 virtual void Trace(JS::Heap<JSString*>* aPtr, const char* aName,
163 void* aClosure) const = 0;
164 virtual void Trace(JS::Heap<JSScript*>* aPtr, const char* aName,
165 void* aClosure) const = 0;
166 virtual void Trace(JS::Heap<JSFunction*>* aPtr, const char* aName,
167 void* aClosure) const = 0;
171 * An implementation of TraceCallbacks that calls a single function for all JS
172 * GC thing types encountered. Implemented in
173 * nsCycleCollectorTraceJSHelpers.cpp.
175 struct TraceCallbackFunc : public TraceCallbacks {
176 typedef void (*Func)(JS::GCCellPtr aPtr, const char* aName, void* aClosure);
178 explicit TraceCallbackFunc(Func aCb) : mCallback(aCb) {}
180 virtual void Trace(JS::Heap<JS::Value>* aPtr, const char* aName,
181 void* aClosure) const override;
182 virtual void Trace(JS::Heap<jsid>* aPtr, const char* aName,
183 void* aClosure) const override;
184 virtual void Trace(JS::Heap<JSObject*>* aPtr, const char* aName,
185 void* aClosure) const override;
186 virtual void Trace(nsWrapperCache* aPtr, const char* aName,
187 void* aClosure) const override;
188 virtual void Trace(JS::TenuredHeap<JSObject*>* aPtr, const char* aName,
189 void* aClosure) const override;
190 virtual void Trace(JS::Heap<JSString*>* aPtr, const char* aName,
191 void* aClosure) const override;
192 virtual void Trace(JS::Heap<JSScript*>* aPtr, const char* aName,
193 void* aClosure) const override;
194 virtual void Trace(JS::Heap<JSFunction*>* aPtr, const char* aName,
195 void* aClosure) const override;
197 private:
198 Func mCallback;
202 * Participant implementation classes
204 class NS_NO_VTABLE nsCycleCollectionParticipant {
205 public:
206 using Flags = uint8_t;
207 static constexpr Flags FlagMightSkip = 1u << 0;
208 static constexpr Flags FlagTraverseShouldTrace = 1u << 1;
209 // The object is a single zone js holder if FlagMaybeSingleZoneJSHolder is set
210 // and FlagMultiZoneJSHolder isn't set. This setup is needed so that
211 // inheriting classes can unset single zone behavior.
212 static constexpr Flags FlagMaybeSingleZoneJSHolder = 1u << 2;
213 static constexpr Flags FlagMultiZoneJSHolder = 1u << 3;
214 static constexpr Flags AllFlags = FlagMightSkip | FlagTraverseShouldTrace |
215 FlagMaybeSingleZoneJSHolder |
216 FlagMultiZoneJSHolder;
218 constexpr explicit nsCycleCollectionParticipant(Flags aFlags)
219 : mFlags(aFlags) {
220 MOZ_ASSERT((aFlags & ~AllFlags) == 0);
223 NS_IMETHOD TraverseNative(void* aPtr,
224 nsCycleCollectionTraversalCallback& aCb) = 0;
226 nsresult TraverseNativeAndJS(void* aPtr,
227 nsCycleCollectionTraversalCallback& aCb) {
228 nsresult rv = TraverseNative(aPtr, aCb);
229 if (TraverseShouldTrace()) {
230 // Note, we always call Trace, even if Traverse returned
231 // NS_SUCCESS_INTERRUPTED_TRAVERSE.
232 TraceCallbackFunc noteJsChild(&nsCycleCollectionParticipant::NoteJSChild);
233 Trace(aPtr, noteJsChild, &aCb);
235 return rv;
238 // Implemented in nsCycleCollectorTraceJSHelpers.cpp.
239 static void NoteJSChild(JS::GCCellPtr aGCThing, const char* aName,
240 void* aClosure);
242 NS_IMETHOD_(void) Root(void* aPtr) = 0;
243 NS_IMETHOD_(void) Unlink(void* aPtr) = 0;
244 NS_IMETHOD_(void) Unroot(void* aPtr) = 0;
245 NS_IMETHOD_(const char*) ClassName() = 0;
247 NS_IMETHOD_(void)
248 Trace(void* aPtr, const TraceCallbacks& aCb, void* aClosure) {}
250 // CanSkip is called during nsCycleCollector_forgetSkippable. If it returns
251 // true, aPtr is removed from the purple buffer and therefore might be left
252 // out from the cycle collector graph the next time that's constructed (unless
253 // it's reachable in some other way).
255 // CanSkip is allowed to expand the set of certainly-alive objects by removing
256 // other objects from the purple buffer, marking JS things black (in the GC
257 // sense), and so forth. Furthermore, if aRemovingAllowed is true, this call
258 // is allowed to remove aPtr itself from the purple buffer.
260 // Things can return true from CanSkip if either they know they have no
261 // outgoing edges at all in the cycle collection graph (because then they
262 // can't be parts of a cycle) or they know for sure they're alive.
263 bool CanSkip(void* aPtr, bool aRemovingAllowed) {
264 return MightSkip() ? CanSkipReal(aPtr, aRemovingAllowed) : false;
267 // CanSkipInCC is called during construction of the initial set of roots for
268 // the cycle collector graph. If it returns true, aPtr is left out of that
269 // set of roots. Note that the set of roots includes whatever is in the
270 // purple buffer (after earlier CanSkip calls) plus various other sources of
271 // roots, so an object can end up having CanSkipInCC called on it even if it
272 // returned true from CanSkip. One example of this would be an object that
273 // can potentially trace JS things.
275 // CanSkipInCC is allowed to remove other objects from the purple buffer but
276 // should not remove aPtr and should not mark JS things black. It should also
277 // not modify any reference counts.
279 // Things can return true from CanSkipInCC if either they know they have no
280 // outgoing edges at all in the cycle collection graph or they know for sure
281 // they're alive _and_ none of their outgoing edges are to gray (in the GC
282 // sense) gcthings. See also nsWrapperCache::HasNothingToTrace and
283 // nsWrapperCache::HasKnownLiveWrapperAndDoesNotNeedTracing. The restriction
284 // on not having outgoing edges to gray gcthings is because if we _do_ have
285 // them that means we have a "strong" edge to a JS thing and since we're alive
286 // we need to trace through it and mark keep them alive. Outgoing edges to
287 // C++ things don't matter here, because the criteria for when a CC
288 // participant is considered alive are slightly different for JS and C++
289 // things: JS things are only considered alive when reachable via an edge from
290 // a live thing, while C++ things are also considered alive when their
291 // refcount exceeds the number of edges via which they are reachable.
292 bool CanSkipInCC(void* aPtr) {
293 return MightSkip() ? CanSkipInCCReal(aPtr) : false;
296 // CanSkipThis is called during construction of the cycle collector graph,
297 // when we traverse an edge to aPtr and consider adding it to the graph. If
298 // it returns true, aPtr is not added to the graph.
300 // CanSkipThis is not allowed to change the liveness or reference count of any
301 // objects.
303 // Things can return true from CanSkipThis if either they know they have no
304 // outgoing edges at all in the cycle collection graph or they know for sure
305 // they're alive.
307 // Note that CanSkipThis doesn't have to worry about outgoing edges to gray GC
308 // things, because if this object could have those it already got added to the
309 // graph during root set construction. An object should never have
310 // CanSkipThis called on it if it has outgoing strong references to JS things.
311 bool CanSkipThis(void* aPtr) {
312 return MightSkip() ? CanSkipThisReal(aPtr) : false;
315 NS_IMETHOD_(void) DeleteCycleCollectable(void* aPtr) = 0;
317 bool IsSingleZoneJSHolder() const {
318 return (mFlags & FlagMaybeSingleZoneJSHolder) &&
319 !(mFlags & FlagMultiZoneJSHolder);
322 protected:
323 NS_IMETHOD_(bool) CanSkipReal(void* aPtr, bool aRemovingAllowed) {
324 NS_ASSERTION(false, "Forgot to implement CanSkipReal?");
325 return false;
327 NS_IMETHOD_(bool) CanSkipInCCReal(void* aPtr) {
328 NS_ASSERTION(false, "Forgot to implement CanSkipInCCReal?");
329 return false;
331 NS_IMETHOD_(bool) CanSkipThisReal(void* aPtr) {
332 NS_ASSERTION(false, "Forgot to implement CanSkipThisReal?");
333 return false;
336 private:
337 bool MightSkip() const { return mFlags & FlagMightSkip; }
338 bool TraverseShouldTrace() const { return mFlags & FlagTraverseShouldTrace; }
340 const Flags mFlags;
343 class NS_NO_VTABLE nsScriptObjectTracer : public nsCycleCollectionParticipant {
344 public:
345 constexpr explicit nsScriptObjectTracer(Flags aFlags)
346 : nsCycleCollectionParticipant(aFlags | FlagTraverseShouldTrace) {}
348 NS_IMETHOD_(void)
349 Trace(void* aPtr, const TraceCallbacks& aCb, void* aClosure) override = 0;
352 class NS_NO_VTABLE nsXPCOMCycleCollectionParticipant
353 : public nsScriptObjectTracer {
354 public:
355 constexpr explicit nsXPCOMCycleCollectionParticipant(Flags aFlags)
356 : nsScriptObjectTracer(aFlags) {}
358 NS_DECLARE_STATIC_IID_ACCESSOR(NS_XPCOMCYCLECOLLECTIONPARTICIPANT_IID)
360 NS_IMETHOD_(void) Root(void* aPtr) override;
361 NS_IMETHOD_(void) Unroot(void* aPtr) override;
363 NS_IMETHOD_(void)
364 Trace(void* aPtr, const TraceCallbacks& aCb, void* aClosure) override;
366 static bool CheckForRightISupports(nsISupports* aSupports);
369 NS_DEFINE_STATIC_IID_ACCESSOR(nsXPCOMCycleCollectionParticipant,
370 NS_XPCOMCYCLECOLLECTIONPARTICIPANT_IID)
372 ///////////////////////////////////////////////////////////////////////////////
373 // Helpers for implementing a QI to nsXPCOMCycleCollectionParticipant
374 ///////////////////////////////////////////////////////////////////////////////
376 #define NS_CYCLE_COLLECTION_CLASSNAME(_class) \
377 _class::NS_CYCLE_COLLECTION_INNERCLASS
379 // The IIDs for nsXPCOMCycleCollectionParticipant and nsCycleCollectionISupports
380 // are special in that they only differ in their last byte. This allows for the
381 // optimization below where we first check the first three words of the IID and
382 // if we find a match we check the last word to decide which case we have to
383 // deal with.
384 #define NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(_class) \
385 if (TopThreeWordsEquals( \
386 aIID, NS_GET_IID(nsXPCOMCycleCollectionParticipant), \
387 NS_GET_IID( \
388 nsCycleCollectionISupports)) && /* The calls to LowWordEquals \
389 here are repeated inside the \
390 if branch. This is due to the \
391 fact that we need to maintain \
392 the if/else chain for these \
393 macros, so that the control \
394 flow never enters the if \
395 branch unless if we're \
396 certain one of the \
397 LowWordEquals() branches will \
398 get executed. */ \
399 (LowWordEquals(aIID, NS_GET_IID(nsXPCOMCycleCollectionParticipant)) || \
400 LowWordEquals(aIID, NS_GET_IID(nsCycleCollectionISupports)))) { \
401 if (LowWordEquals(aIID, NS_GET_IID(nsXPCOMCycleCollectionParticipant))) { \
402 *aInstancePtr = NS_CYCLE_COLLECTION_PARTICIPANT(_class); \
403 return NS_OK; \
405 if (LowWordEquals(aIID, NS_GET_IID(nsCycleCollectionISupports))) { \
406 *aInstancePtr = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Upcast(this); \
407 return NS_OK; \
409 /* Avoid warnings about foundInterface being left uninitialized. */ \
410 foundInterface = nullptr; \
411 } else
413 #define NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(_class) \
414 NS_INTERFACE_MAP_BEGIN(_class) \
415 NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(_class)
417 #define NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(_class) \
418 if (rv == NS_OK) return rv; \
419 nsISupports* foundInterface; \
420 NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(_class)
422 // The IIDs for nsXPCOMCycleCollectionParticipant and nsCycleCollectionISupports
423 // are special in that they only differ in their last byte. This allows for the
424 // optimization below where we first check the first three words of the IID and
425 // if we find a match we check the last word to decide which case we have to
426 // deal with.
427 #define NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(_class) \
428 NS_IMETHODIMP _class::QueryInterface(REFNSIID aIID, void** aInstancePtr) { \
429 MOZ_ASSERT(aInstancePtr, "null out param"); \
431 if (TopThreeWordsEquals(aIID, \
432 NS_GET_IID(nsXPCOMCycleCollectionParticipant), \
433 NS_GET_IID(nsCycleCollectionISupports))) { \
434 if (LowWordEquals(aIID, \
435 NS_GET_IID(nsXPCOMCycleCollectionParticipant))) { \
436 *aInstancePtr = NS_CYCLE_COLLECTION_PARTICIPANT(_class); \
437 return NS_OK; \
439 if (LowWordEquals(aIID, NS_GET_IID(nsCycleCollectionISupports))) { \
440 *aInstancePtr = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Upcast(this); \
441 return NS_OK; \
444 nsresult rv = NS_ERROR_FAILURE;
446 #define NS_CYCLE_COLLECTION_UPCAST(obj, clazz) \
447 NS_CYCLE_COLLECTION_CLASSNAME(clazz)::Upcast(obj)
449 #ifdef DEBUG
450 # define NS_CHECK_FOR_RIGHT_PARTICIPANT(_ptr) _ptr->CheckForRightParticipant()
451 #else
452 # define NS_CHECK_FOR_RIGHT_PARTICIPANT(_ptr)
453 #endif
455 // The default implementation of this class template is empty, because it
456 // should never be used: see the partial specializations below.
457 template <typename T, bool IsXPCOM = std::is_base_of<nsISupports, T>::value>
458 struct DowncastCCParticipantImpl {};
460 // Specialization for XPCOM CC participants
461 template <typename T>
462 struct DowncastCCParticipantImpl<T, true> {
463 static T* Run(void* aPtr) {
464 nsISupports* s = static_cast<nsISupports*>(aPtr);
465 MOZ_ASSERT(NS_CYCLE_COLLECTION_CLASSNAME(T)::CheckForRightISupports(s),
466 "not the nsISupports pointer we expect");
467 T* rval = NS_CYCLE_COLLECTION_CLASSNAME(T)::Downcast(s);
468 NS_CHECK_FOR_RIGHT_PARTICIPANT(rval);
469 return rval;
473 // Specialization for native CC participants
474 template <typename T>
475 struct DowncastCCParticipantImpl<T, false> {
476 static T* Run(void* aPtr) { return static_cast<T*>(aPtr); }
479 template <typename T>
480 T* DowncastCCParticipant(void* aPtr) {
481 return DowncastCCParticipantImpl<T>::Run(aPtr);
484 ///////////////////////////////////////////////////////////////////////////////
485 // Helpers for implementing CanSkip methods
486 ///////////////////////////////////////////////////////////////////////////////
488 // See documentation for nsCycleCollectionParticipant::CanSkip for documentation
489 // about this method.
490 #define NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(_class) \
491 NS_IMETHODIMP_(bool) \
492 NS_CYCLE_COLLECTION_CLASSNAME(_class)::CanSkipReal(void* p, \
493 bool aRemovingAllowed) { \
494 _class* tmp = DowncastCCParticipant<_class>(p);
496 #define NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END \
497 (void)tmp; \
498 return false; \
501 // See documentation for nsCycleCollectionParticipant::CanSkipInCC for
502 // documentation about this method.
503 #define NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(_class) \
504 NS_IMETHODIMP_(bool) \
505 NS_CYCLE_COLLECTION_CLASSNAME(_class)::CanSkipInCCReal(void* p) { \
506 _class* tmp = DowncastCCParticipant<_class>(p);
508 #define NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END \
509 (void)tmp; \
510 return false; \
513 // See documentation for nsCycleCollectionParticipant::CanSkipThis for
514 // documentation about this method.
515 #define NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(_class) \
516 NS_IMETHODIMP_(bool) \
517 NS_CYCLE_COLLECTION_CLASSNAME(_class)::CanSkipThisReal(void* p) { \
518 _class* tmp = DowncastCCParticipant<_class>(p);
520 #define NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END \
521 (void)tmp; \
522 return false; \
525 ///////////////////////////////////////////////////////////////////////////////
526 // Helpers for implementing nsCycleCollectionParticipant::Unlink
528 // You need to use NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED if you want
529 // the base class Unlink version to be called before your own implementation.
530 // You can use NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED if you want the
531 // base class Unlink to get called after your own implementation. You should
532 // never use them together.
533 ///////////////////////////////////////////////////////////////////////////////
535 #define NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
536 NS_IMETHODIMP_(void) \
537 NS_CYCLE_COLLECTION_CLASSNAME(_class)::Unlink(void* p) { \
538 _class* tmp = DowncastCCParticipant<_class>(p);
540 #define NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(_class, _base_class) \
541 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
542 nsISupports* s = static_cast<nsISupports*>(p); \
543 NS_CYCLE_COLLECTION_CLASSNAME(_base_class)::Unlink(s);
545 #define NS_IMPL_CYCLE_COLLECTION_UNLINK_HELPER(_field) \
546 ImplCycleCollectionUnlink(tmp->_field);
548 #define NS_IMPL_CYCLE_COLLECTION_UNLINK(...) \
549 MOZ_FOR_EACH(NS_IMPL_CYCLE_COLLECTION_UNLINK_HELPER, (), (__VA_ARGS__))
551 #define NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
552 (void)tmp; \
555 #define NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(_base_class) \
556 nsISupports* s = static_cast<nsISupports*>(p); \
557 NS_CYCLE_COLLECTION_CLASSNAME(_base_class)::Unlink(s); \
558 (void)tmp; \
561 #define NS_IMPL_CYCLE_COLLECTION_UNLINK_0(_class) \
562 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
563 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
565 ///////////////////////////////////////////////////////////////////////////////
566 // Helpers for implementing nsCycleCollectionParticipant::Traverse
567 ///////////////////////////////////////////////////////////////////////////////
569 #define NS_IMPL_CYCLE_COLLECTION_DESCRIBE(_class, _refcnt) \
570 cb.DescribeRefCountedNode(_refcnt, #_class);
572 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(_class) \
573 NS_IMETHODIMP \
574 NS_CYCLE_COLLECTION_CLASSNAME(_class)::TraverseNative( \
575 void* p, nsCycleCollectionTraversalCallback& cb) { \
576 _class* tmp = DowncastCCParticipant<_class>(p);
578 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(_class) \
579 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(_class) \
580 NS_IMPL_CYCLE_COLLECTION_DESCRIBE(_class, tmp->mRefCnt.get())
582 // Base class' CC participant should return NS_SUCCESS_INTERRUPTED_TRAVERSE
583 // from Traverse if it wants derived classes to not traverse anything from
584 // their CC participant.
586 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(_class, _base_class) \
587 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(_class) \
588 nsISupports* s = static_cast<nsISupports*>(p); \
589 if (NS_CYCLE_COLLECTION_CLASSNAME(_base_class)::TraverseNative(s, cb) == \
590 NS_SUCCESS_INTERRUPTED_TRAVERSE) { \
591 return NS_SUCCESS_INTERRUPTED_TRAVERSE; \
594 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_HELPER(_field) \
595 ImplCycleCollectionTraverse(cb, tmp->_field, #_field, 0);
597 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE(...) \
598 MOZ_FOR_EACH(NS_IMPL_CYCLE_COLLECTION_TRAVERSE_HELPER, (), (__VA_ARGS__))
600 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(_field) \
601 CycleCollectionNoteChild(cb, tmp->_field, #_field);
603 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END \
604 (void)tmp; \
605 return NS_OK; \
608 ///////////////////////////////////////////////////////////////////////////////
609 // Helpers for implementing nsScriptObjectTracer::Trace
610 ///////////////////////////////////////////////////////////////////////////////
612 #define NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(_class) \
613 void NS_CYCLE_COLLECTION_CLASSNAME(_class)::Trace( \
614 void* p, const TraceCallbacks& aCallbacks, void* aClosure) { \
615 _class* tmp = DowncastCCParticipant<_class>(p);
617 #define NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(_class, _base_class) \
618 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(_class) \
619 nsISupports* s = static_cast<nsISupports*>(p); \
620 NS_CYCLE_COLLECTION_CLASSNAME(_base_class)::Trace(s, aCallbacks, aClosure);
622 #define NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(_field) \
623 aCallbacks.Trace(&tmp->_field, #_field, aClosure);
625 // NB: The (void)tmp; hack in the TRACE_END macro exists to support
626 // implementations that don't need to do anything in their Trace method.
627 // Without this hack, some compilers warn about the unused tmp local.
628 #define NS_IMPL_CYCLE_COLLECTION_TRACE_END \
629 (void)tmp; \
632 ///////////////////////////////////////////////////////////////////////////////
633 // Helpers for implementing a concrete nsCycleCollectionParticipant
634 ///////////////////////////////////////////////////////////////////////////////
636 // If a class defines a participant, then QIing an instance of that class to
637 // nsXPCOMCycleCollectionParticipant should produce that participant.
638 #ifdef DEBUG
639 # define NS_CHECK_FOR_RIGHT_PARTICIPANT_BASE \
640 virtual void CheckForRightParticipant()
641 # define NS_CHECK_FOR_RIGHT_PARTICIPANT_DERIVED \
642 virtual void CheckForRightParticipant() override
643 # define NS_CHECK_FOR_RIGHT_PARTICIPANT_BODY(_class) \
645 nsXPCOMCycleCollectionParticipant* p; \
646 CallQueryInterface(this, &p); \
647 MOZ_ASSERT(p == &NS_CYCLE_COLLECTION_INNERNAME, \
648 #_class " should QI to its own CC participant"); \
650 # define NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class) \
651 NS_CHECK_FOR_RIGHT_PARTICIPANT_BASE \
652 NS_CHECK_FOR_RIGHT_PARTICIPANT_BODY(_class)
653 # define NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL_INHERITED(_class) \
654 NS_CHECK_FOR_RIGHT_PARTICIPANT_DERIVED \
655 NS_CHECK_FOR_RIGHT_PARTICIPANT_BODY(_class)
656 #else
657 # define NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class)
658 # define NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL_INHERITED(_class)
659 #endif
661 #define NS_DECL_CYCLE_COLLECTION_CLASS_NAME_METHOD(_class) \
662 NS_IMETHOD_(const char*) ClassName() override { return #_class; };
664 #define NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base) \
665 public: \
666 NS_IMETHOD TraverseNative(void* p, nsCycleCollectionTraversalCallback& cb) \
667 override; \
668 NS_DECL_CYCLE_COLLECTION_CLASS_NAME_METHOD(_class) \
669 NS_IMETHOD_(void) DeleteCycleCollectable(void* p) override { \
670 DowncastCCParticipant<_class>(p)->DeleteCycleCollectable(); \
672 static _class* Downcast(nsISupports* s) { \
673 return static_cast<_class*>(static_cast<_base*>(s)); \
675 static nsISupports* Upcast(_class* p) { \
676 return NS_ISUPPORTS_CAST(_base*, p); \
678 template <typename T> \
679 friend nsISupports* ToSupports(T* p, NS_CYCLE_COLLECTION_INNERCLASS* dummy); \
680 NS_IMETHOD_(void) Unlink(void* p) override;
682 #define NS_PARTICIPANT_AS(type, participant) \
683 const_cast<type*>(reinterpret_cast<const type*>(participant))
685 #define NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
686 static constexpr nsXPCOMCycleCollectionParticipant* GetParticipant() { \
687 return &_class::NS_CYCLE_COLLECTION_INNERNAME; \
691 * We use this macro to force that classes that inherit from a ccable class and
692 * declare their own participant declare themselves as inherited cc classes.
693 * To avoid possibly unnecessary vtables we only do this checking in debug
694 * builds.
696 #ifdef DEBUG
697 # define NOT_INHERITED_CANT_OVERRIDE \
698 virtual void BaseCycleCollectable() final {}
699 #else
700 # define NOT_INHERITED_CANT_OVERRIDE
701 #endif
703 #define NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(_class, _base) \
704 class NS_CYCLE_COLLECTION_INNERCLASS \
705 : public nsXPCOMCycleCollectionParticipant { \
706 public: \
707 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
708 : nsXPCOMCycleCollectionParticipant(aFlags) {} \
710 private: \
711 NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base) \
712 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
713 }; \
714 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class) \
715 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME; \
716 NOT_INHERITED_CANT_OVERRIDE
718 #define NS_DECL_CYCLE_COLLECTION_CLASS(_class) \
719 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(_class, _class)
721 // Cycle collector helper for ambiguous classes that can sometimes be skipped.
722 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_CLASS_AMBIGUOUS(_class, _base) \
723 class NS_CYCLE_COLLECTION_INNERCLASS \
724 : public nsXPCOMCycleCollectionParticipant { \
725 public: \
726 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS( \
727 Flags aFlags = FlagMightSkip) /* We always want skippability. */ \
728 : nsXPCOMCycleCollectionParticipant(aFlags | FlagMightSkip) {} \
730 private: \
731 NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base) \
732 NS_IMETHOD_(bool) CanSkipReal(void* p, bool aRemovingAllowed) override; \
733 NS_IMETHOD_(bool) CanSkipInCCReal(void* p) override; \
734 NS_IMETHOD_(bool) CanSkipThisReal(void* p) override; \
735 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
736 }; \
737 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class) \
738 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME; \
739 NOT_INHERITED_CANT_OVERRIDE
741 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_CLASS(_class) \
742 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_CLASS_AMBIGUOUS(_class, _class)
744 #define NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(_class, _base) \
745 class NS_CYCLE_COLLECTION_INNERCLASS \
746 : public nsXPCOMCycleCollectionParticipant { \
747 public: \
748 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
749 : nsXPCOMCycleCollectionParticipant(aFlags) {} \
751 private: \
752 NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base) \
753 NS_IMETHOD_(void) \
754 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
755 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
756 }; \
757 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class) \
758 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME; \
759 NOT_INHERITED_CANT_OVERRIDE
761 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_AMBIGUOUS( \
762 _class, _base) \
763 class NS_CYCLE_COLLECTION_INNERCLASS \
764 : public nsXPCOMCycleCollectionParticipant { \
765 public: \
766 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS( \
767 Flags aFlags = FlagMightSkip) /* We always want skippability. */ \
768 : nsXPCOMCycleCollectionParticipant(aFlags | FlagMightSkip) {} \
770 private: \
771 NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base) \
772 NS_IMETHOD_(void) \
773 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
774 NS_IMETHOD_(bool) CanSkipReal(void* p, bool aRemovingAllowed) override; \
775 NS_IMETHOD_(bool) CanSkipInCCReal(void* p) override; \
776 NS_IMETHOD_(bool) CanSkipThisReal(void* p) override; \
777 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
778 }; \
779 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class) \
780 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME; \
781 NOT_INHERITED_CANT_OVERRIDE
783 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(_class) \
784 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_AMBIGUOUS(_class, \
785 _class)
787 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED( \
788 _class, _base_class) \
789 class NS_CYCLE_COLLECTION_INNERCLASS \
790 : public NS_CYCLE_COLLECTION_CLASSNAME(_base_class) { \
791 public: \
792 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS( \
793 Flags aFlags = FlagMightSkip | /* We always want skippability. */ \
794 FlagMultiZoneJSHolder) \
795 : NS_CYCLE_COLLECTION_CLASSNAME(_base_class)(aFlags | FlagMightSkip) { \
798 private: \
799 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_BODY(_class, _base_class) \
800 NS_IMETHOD_(void) \
801 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
802 NS_IMETHOD_(bool) CanSkipReal(void* p, bool aRemovingAllowed) override; \
803 NS_IMETHOD_(bool) CanSkipInCCReal(void* p) override; \
804 NS_IMETHOD_(bool) CanSkipThisReal(void* p) override; \
805 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
806 }; \
807 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL_INHERITED(_class) \
808 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
810 #define NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(_class) \
811 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(_class, _class)
813 #define NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_BODY(_class, _base_class) \
814 public: \
815 NS_IMETHOD TraverseNative(void* p, nsCycleCollectionTraversalCallback& cb) \
816 override; \
817 NS_DECL_CYCLE_COLLECTION_CLASS_NAME_METHOD(_class) \
818 static _class* Downcast(nsISupports* s) { \
819 return static_cast<_class*>(static_cast<_base_class*>( \
820 NS_CYCLE_COLLECTION_CLASSNAME(_base_class)::Downcast(s))); \
822 NS_IMETHOD_(void) Unlink(void* p) override;
824 #define NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(_class, _base_class) \
825 class NS_CYCLE_COLLECTION_INNERCLASS \
826 : public NS_CYCLE_COLLECTION_CLASSNAME(_base_class) { \
827 public: \
828 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
829 : NS_CYCLE_COLLECTION_CLASSNAME(_base_class)(aFlags) {} \
831 private: \
832 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_BODY(_class, _base_class) \
833 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
834 }; \
835 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL_INHERITED(_class) \
836 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
838 #define NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(_class, \
839 _base_class) \
840 class NS_CYCLE_COLLECTION_INNERCLASS \
841 : public NS_CYCLE_COLLECTION_CLASSNAME(_base_class) { \
842 public: \
843 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
844 : NS_CYCLE_COLLECTION_CLASSNAME(_base_class)(aFlags | \
845 FlagMultiZoneJSHolder) {} \
847 private: \
848 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_BODY(_class, _base_class) \
849 NS_IMETHOD_(void) \
850 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
851 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
852 }; \
853 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL_INHERITED(_class) \
854 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
856 // Cycle collector participant declarations.
858 #define NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS_BODY(_class) \
859 public: \
860 NS_IMETHOD_(void) Root(void* p) override { \
861 static_cast<_class*>(p)->AddRef(); \
863 NS_IMETHOD_(void) Unlink(void* n) override; \
864 NS_IMETHOD_(void) Unroot(void* p) override { \
865 static_cast<_class*>(p)->Release(); \
867 NS_IMETHOD TraverseNative(void* n, nsCycleCollectionTraversalCallback& cb) \
868 override; \
869 NS_DECL_CYCLE_COLLECTION_CLASS_NAME_METHOD(_class) \
870 NS_IMETHOD_(void) DeleteCycleCollectable(void* n) override { \
871 DowncastCCParticipant<_class>(n)->DeleteCycleCollectable(); \
873 static _class* Downcast(void* s) { \
874 return DowncastCCParticipant<_class>(s); \
876 static void* Upcast(_class* p) { return static_cast<void*>(p); }
878 #define NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(_class) \
879 void DeleteCycleCollectable(void) { delete this; } \
880 class NS_CYCLE_COLLECTION_INNERCLASS : public nsCycleCollectionParticipant { \
881 public: \
882 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
883 : nsCycleCollectionParticipant(aFlags) {} \
885 private: \
886 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS_BODY(_class) \
887 static constexpr nsCycleCollectionParticipant* GetParticipant() { \
888 return &_class::NS_CYCLE_COLLECTION_INNERNAME; \
890 }; \
891 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
893 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_NATIVE_CLASS(_class) \
894 void DeleteCycleCollectable(void) { delete this; } \
895 class NS_CYCLE_COLLECTION_INNERCLASS : public nsCycleCollectionParticipant { \
896 public: \
897 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS( \
898 Flags aFlags = FlagMightSkip) /* We always want skippability. */ \
899 : nsCycleCollectionParticipant(aFlags | FlagMightSkip) {} \
901 private: \
902 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS_BODY(_class) \
903 NS_IMETHOD_(bool) CanSkipReal(void* p, bool aRemovingAllowed) override; \
904 NS_IMETHOD_(bool) CanSkipInCCReal(void* p) override; \
905 NS_IMETHOD_(bool) CanSkipThisReal(void* p) override; \
906 static nsCycleCollectionParticipant* GetParticipant() { \
907 return &_class::NS_CYCLE_COLLECTION_INNERNAME; \
909 }; \
910 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
912 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_NATIVE_CLASS_WITH_CUSTOM_DELETE( \
913 _class) \
914 class NS_CYCLE_COLLECTION_INNERCLASS : public nsCycleCollectionParticipant { \
915 public: \
916 constexpr NS_CYCLE_COLLECTION_INNERCLASS() \
917 : nsCycleCollectionParticipant(true) {} \
919 private: \
920 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS_BODY(_class) \
921 NS_IMETHOD_(bool) CanSkipReal(void* p, bool aRemovingAllowed) override; \
922 NS_IMETHOD_(bool) CanSkipInCCReal(void* p) override; \
923 NS_IMETHOD_(bool) CanSkipThisReal(void* p) override; \
924 static nsCycleCollectionParticipant* GetParticipant() { \
925 return &_class::NS_CYCLE_COLLECTION_INNERNAME; \
927 }; \
928 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
930 #define NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(_class) \
931 void DeleteCycleCollectable(void) { delete this; } \
932 class NS_CYCLE_COLLECTION_INNERCLASS : public nsScriptObjectTracer { \
933 public: \
934 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
935 : nsScriptObjectTracer(aFlags) {} \
937 private: \
938 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS_BODY(_class) \
939 NS_IMETHOD_(void) \
940 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
941 static constexpr nsScriptObjectTracer* GetParticipant() { \
942 return &_class::NS_CYCLE_COLLECTION_INNERNAME; \
944 }; \
945 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
947 #define NS_IMPL_CYCLE_COLLECTION_CLASS(_class) \
948 _class::NS_CYCLE_COLLECTION_INNERCLASS _class::NS_CYCLE_COLLECTION_INNERNAME;
950 // By default JS holders are treated as if they may store pointers to multiple
951 // zones, but one may optimize GC handling by using single zone holders.
952 #define NS_IMPL_CYCLE_COLLECTION_SINGLE_ZONE_SCRIPT_HOLDER_CLASS(_class) \
953 _class::NS_CYCLE_COLLECTION_INNERCLASS \
954 _class::NS_CYCLE_COLLECTION_INNERNAME( \
955 nsCycleCollectionParticipant::FlagMaybeSingleZoneJSHolder);
957 // NB: This is not something you usually want to use. It is here to allow
958 // adding things to the CC graph to help debugging via CC logs, but it does not
959 // traverse or unlink anything, so it is useless for anything else.
960 #define NS_IMPL_CYCLE_COLLECTION_0(_class) \
961 NS_IMPL_CYCLE_COLLECTION_CLASS(_class) \
962 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
963 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
964 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(_class) \
965 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
967 #define NS_IMPL_CYCLE_COLLECTION(_class, ...) \
968 NS_IMPL_CYCLE_COLLECTION_CLASS(_class) \
969 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
970 NS_IMPL_CYCLE_COLLECTION_UNLINK(__VA_ARGS__) \
971 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
972 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(_class) \
973 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(__VA_ARGS__) \
974 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
976 // If you are looking for NS_IMPL_CYCLE_COLLECTION_INHERITED_0(_class, _base)
977 // you should instead not declare any cycle collected stuff in _class, so it
978 // will just inherit the CC declarations from _base.
980 #define NS_IMPL_CYCLE_COLLECTION_INHERITED(_class, _base, ...) \
981 NS_IMPL_CYCLE_COLLECTION_CLASS(_class) \
982 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(_class, _base) \
983 NS_IMPL_CYCLE_COLLECTION_UNLINK(__VA_ARGS__) \
984 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
985 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(_class, _base) \
986 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(__VA_ARGS__) \
987 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
989 #define NS_CYCLE_COLLECTION_NOTE_EDGE_NAME CycleCollectionNoteEdgeName
992 * Convenience macros for defining nISupports methods in a cycle collected
993 * class.
996 #define NS_IMPL_QUERY_INTERFACE_CYCLE_COLLECTION_INHERITED(aClass, aSuper, \
997 ...) \
998 NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(aClass) \
999 NS_INTERFACE_TABLE_INHERITED(aClass, __VA_ARGS__) \
1000 NS_INTERFACE_TABLE_TAIL_INHERITING(aSuper)
1002 #define NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(aClass, aSuper, ...) \
1003 NS_IMPL_QUERY_INTERFACE_CYCLE_COLLECTION_INHERITED(aClass, aSuper, \
1004 __VA_ARGS__) \
1005 NS_IMPL_ADDREF_INHERITED(aClass, aSuper) \
1006 NS_IMPL_RELEASE_INHERITED(aClass, aSuper)
1008 #define NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(aClass, aSuper) \
1009 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(aClass) \
1010 NS_INTERFACE_MAP_END_INHERITING(aSuper) \
1011 NS_IMPL_ADDREF_INHERITED(aClass, aSuper) \
1012 NS_IMPL_RELEASE_INHERITED(aClass, aSuper)
1015 * Equivalency of the high three words where two IIDs have the same
1016 * top three words but not the same low word.
1018 inline bool TopThreeWordsEquals(const nsID& aID, const nsID& aOther1,
1019 const nsID& aOther2) {
1020 MOZ_ASSERT((((uint32_t*)&aOther1.m0)[0] == ((uint32_t*)&aOther2.m0)[0]) &&
1021 (((uint32_t*)&aOther1.m0)[1] == ((uint32_t*)&aOther2.m0)[1]) &&
1022 (((uint32_t*)&aOther1.m0)[2] == ((uint32_t*)&aOther2.m0)[2]) &&
1023 (((uint32_t*)&aOther1.m0)[3] != ((uint32_t*)&aOther2.m0)[3]));
1025 return ((((uint32_t*)&aID.m0)[0] == ((uint32_t*)&aOther1.m0)[0]) &&
1026 (((uint32_t*)&aID.m0)[1] == ((uint32_t*)&aOther1.m0)[1]) &&
1027 (((uint32_t*)&aID.m0)[2] == ((uint32_t*)&aOther1.m0)[2]));
1031 * Equivalency of the fourth word where the two IIDs have the same
1032 * top three words but not the same low word.
1034 inline bool LowWordEquals(const nsID& aID, const nsID& aOther) {
1035 return (((uint32_t*)&aID.m0)[3] == ((uint32_t*)&aOther.m0)[3]);
1038 // Template magic to modify JS::Heap without including relevant headers, to
1039 // prevent excessive header dependency.
1040 template <typename T>
1041 inline void ImplCycleCollectionUnlink(JS::Heap<T>& aField) {
1042 aField.setNull();
1044 template <typename T>
1045 inline void ImplCycleCollectionUnlink(JS::Heap<T*>& aField) {
1046 aField = nullptr;
1049 #define NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBERS(...) \
1050 MOZ_ASSERT(!IsSingleZoneJSHolder()); \
1051 MOZ_FOR_EACH(NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK, (), \
1052 (__VA_ARGS__))
1054 #define NS_IMPL_CYCLE_COLLECTION_WITH_JS_MEMBERS(class_, native_members_, \
1055 js_members_) \
1056 NS_IMPL_CYCLE_COLLECTION_CLASS(class_) \
1057 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(class_) \
1058 using ::ImplCycleCollectionUnlink; \
1059 NS_IMPL_CYCLE_COLLECTION_UNLINK( \
1060 MOZ_FOR_EACH_EXPAND_HELPER native_members_) \
1061 NS_IMPL_CYCLE_COLLECTION_UNLINK(MOZ_FOR_EACH_EXPAND_HELPER js_members_) \
1062 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
1063 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(class_) \
1064 NS_IMPL_CYCLE_COLLECTION_TRAVERSE( \
1065 MOZ_FOR_EACH_EXPAND_HELPER native_members_) \
1066 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END \
1067 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(class_) \
1068 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBERS( \
1069 MOZ_FOR_EACH_EXPAND_HELPER js_members_) \
1070 NS_IMPL_CYCLE_COLLECTION_TRACE_END
1072 #define NS_IMPL_CYCLE_COLLECTION_INHERITED_WITH_JS_MEMBERS( \
1073 class_, _base, native_members_, js_members_) \
1074 NS_IMPL_CYCLE_COLLECTION_CLASS(class_) \
1075 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(class_, _base) \
1076 using ::ImplCycleCollectionUnlink; \
1077 NS_IMPL_CYCLE_COLLECTION_UNLINK( \
1078 MOZ_FOR_EACH_EXPAND_HELPER native_members_) \
1079 NS_IMPL_CYCLE_COLLECTION_UNLINK(MOZ_FOR_EACH_EXPAND_HELPER js_members_) \
1080 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
1081 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(class_, _base) \
1082 NS_IMPL_CYCLE_COLLECTION_TRAVERSE( \
1083 MOZ_FOR_EACH_EXPAND_HELPER native_members_) \
1084 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END \
1085 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(class_, _base) \
1086 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBERS( \
1087 MOZ_FOR_EACH_EXPAND_HELPER js_members_) \
1088 NS_IMPL_CYCLE_COLLECTION_TRACE_END
1090 template <typename... Elements>
1091 inline void ImplCycleCollectionUnlink(std::tuple<Elements...>& aField) {
1092 std::apply([](auto&&... aArgs) { (ImplCycleCollectionUnlink(aArgs), ...); },
1093 aField);
1095 template <typename... Elements>
1096 inline void ImplCycleCollectionTraverse(
1097 nsCycleCollectionTraversalCallback& aCallback,
1098 std::tuple<Elements...>& aField, const char* aName, uint32_t aFlags) {
1099 aFlags |= CycleCollectionEdgeNameArrayFlag;
1100 std::apply(
1101 [&](auto&&... aArgs) {
1102 (ImplCycleCollectionTraverse(aCallback, aArgs, aName, aFlags), ...);
1104 aField);
1107 #endif // nsCycleCollectionParticipant_h__