Debian Codegraph @

js/src/gc/AtomMarking-inl.h

raw
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/. */
6
7 #include "gc/AtomMarking.h"
8
9 #include "mozilla/Assertions.h"
10
11 #include <type_traits>
12
13 #include "vm/JSContext.h"
14 #include "vm/StringType.h"
15 #include "vm/SymbolType.h"
16
17 #include "gc/Heap-inl.h"
18
19 namespace js {
20 namespace gc {
21
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()) /
26 CellBytesPerMarkBit;
27 return arena->atomBitmapStart() * JS_BITS_PER_WORD + arenaBit;
28 }
29
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");
35
36 MOZ_ASSERT(cx->zone());
37 MOZ_ASSERT(!cx->zone()->isAtomsZone());
38
39 MOZ_ASSERT(thing);
40 js::gc::TenuredCell* cell = &thing->asTenured();
41 MOZ_ASSERT(cell->zoneFromAnyThread()->isAtomsZone());
42
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()) {
46 return true;
47 }
48
49 size_t bit = GetAtomBit(cell);
50 MOZ_ASSERT(bit / JS_BITS_PER_WORD < allocatedWords);
51
52 if (Fallible) {
53 if (!cx->zone()->markedAtoms().setBitFallible(bit)) {
54 return false;
55 }
56 } else {
57 cx->zone()->markedAtoms().setBit(bit);
58 }
59
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.
64 ReadBarrier(thing);
65
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);
70
71 return true;
72 }
73
74 void AtomMarkingRuntime::markChildren(JSContext* cx, JSAtom*) {}
75
76 void AtomMarkingRuntime::markChildren(JSContext* cx, JS::Symbol* symbol) {
77 if (JSAtom* description = symbol->description()) {
78 markAtom(cx, description);
79 }
80 }
81
82 template <typename T>
83 MOZ_ALWAYS_INLINE void AtomMarkingRuntime::inlinedMarkAtom(JSContext* cx,
84 T* thing) {
85 MOZ_ALWAYS_TRUE((inlinedMarkAtomInternal<T, false>(cx, thing)));
86 }
87
88 template <typename T>
89 MOZ_ALWAYS_INLINE bool AtomMarkingRuntime::inlinedMarkAtomFallible(
90 JSContext* cx, T* thing) {
91 return inlinedMarkAtomInternal<T, true>(cx, thing);
92 }
93
94 } // namespace gc
95 } // namespace js