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 #include "gc/AtomMarking.h"
9 #include "mozilla/Assertions.h"
11 #include <type_traits>
13 #include "vm/JSContext.h"
14 #include "vm/StringType.h"
15 #include "vm/SymbolType.h"
17 #include "gc/Heap-inl.h"
22 inline size_t GetAtomBit(TenuredCell
* thing
) {
23 MOZ_ASSERT(thing
->zoneFromAnyThread()->isAtomsZone());
24 Arena
* arena
= thing
->arena();
25 size_t arenaBit
= (reinterpret_cast<uintptr_t>(thing
) - arena
->address()) /
27 return arena
->atomBitmapStart() * JS_BITS_PER_WORD
+ arenaBit
;
30 template <typename T
, bool Fallible
>
31 MOZ_ALWAYS_INLINE
bool AtomMarkingRuntime::inlinedMarkAtomInternal(
32 JSContext
* cx
, T
* thing
) {
33 static_assert(std::is_same_v
<T
, JSAtom
> || std::is_same_v
<T
, JS::Symbol
>,
34 "Should only be called with JSAtom* or JS::Symbol* argument");
36 MOZ_ASSERT(cx
->zone());
37 MOZ_ASSERT(!cx
->zone()->isAtomsZone());
40 js::gc::TenuredCell
* cell
= &thing
->asTenured();
41 MOZ_ASSERT(cell
->zoneFromAnyThread()->isAtomsZone());
43 // This doesn't check for pinned atoms since that might require taking a
44 // lock. This is not required for correctness.
45 if (thing
->isPermanentAndMayBeShared()) {
49 size_t bit
= GetAtomBit(cell
);
50 MOZ_ASSERT(bit
/ JS_BITS_PER_WORD
< allocatedWords
);
53 if (!cx
->zone()->markedAtoms().setBitFallible(bit
)) {
57 cx
->zone()->markedAtoms().setBit(bit
);
60 // Trigger a read barrier on the atom, in case there is an incremental
61 // GC in progress. This is necessary if the atom is being marked
62 // because a reference to it was obtained from another zone which is
63 // not being collected by the incremental GC.
66 // Children of the thing also need to be marked in the context's zone.
67 // We don't have a JSTracer for this so manually handle the cases in which
68 // an atom can reference other atoms.
69 markChildren(cx
, thing
);
74 void AtomMarkingRuntime::markChildren(JSContext
* cx
, JSAtom
*) {}
76 void AtomMarkingRuntime::markChildren(JSContext
* cx
, JS::Symbol
* symbol
) {
77 if (JSAtom
* description
= symbol
->description()) {
78 markAtom(cx
, description
);
83 MOZ_ALWAYS_INLINE
void AtomMarkingRuntime::inlinedMarkAtom(JSContext
* cx
,
85 MOZ_ALWAYS_TRUE((inlinedMarkAtomInternal
<T
, false>(cx
, thing
)));
89 MOZ_ALWAYS_INLINE
bool AtomMarkingRuntime::inlinedMarkAtomFallible(
90 JSContext
* cx
, T
* thing
) {
91 return inlinedMarkAtomInternal
<T
, true>(cx
, thing
);