Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / src / gc / AtomMarking-inl.h
blob908a11e7dad197af5701d9a6449584de440f9f09
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"
19 namespace js {
20 namespace gc {
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;
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());
39 MOZ_ASSERT(thing);
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()) {
46 return true;
49 size_t bit = GetAtomBit(cell);
50 MOZ_ASSERT(bit / JS_BITS_PER_WORD < allocatedWords);
52 if (Fallible) {
53 if (!cx->zone()->markedAtoms().setBitFallible(bit)) {
54 return false;
56 } else {
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.
64 ReadBarrier(thing);
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);
71 return true;
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);
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)));
88 template <typename T>
89 MOZ_ALWAYS_INLINE bool AtomMarkingRuntime::inlinedMarkAtomFallible(
90 JSContext* cx, T* thing) {
91 return inlinedMarkAtomInternal<T, true>(cx, thing);
94 } // namespace gc
95 } // namespace js