Debian Codegraph @

js/src/gc/AtomMarking.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 #ifndef gc_AtomMarking_h
8 #define gc_AtomMarking_h
9
10 #include "mozilla/Atomics.h"
11
12 #include "NamespaceImports.h"
13 #include "js/Vector.h"
14 #include "threading/ProtectedData.h"
15
16 namespace js {
17
18 class AutoLockGC;
19 class DenseBitmap;
20
21 namespace gc {
22
23 class Arena;
24 class GCRuntime;
25
26 // This class manages state used for marking atoms during GCs.
27 // See AtomMarking.cpp for details.
28 class AtomMarkingRuntime {
29 // Unused arena atom bitmap indexes.
30 js::MainThreadData<Vector<size_t, 0, SystemAllocPolicy>> freeArenaIndexes;
31
32 // Background sweep state for |freeArenaIndexes|.
33 js::GCLockData<Vector<size_t, 0, SystemAllocPolicy>> pendingFreeArenaIndexes;
34 mozilla::Atomic<bool, mozilla::Relaxed> hasPendingFreeArenaIndexes;
35
36 inline void markChildren(JSContext* cx, JSAtom*);
37 inline void markChildren(JSContext* cx, JS::Symbol* symbol);
38
39 public:
40 // The extent of all allocated and free words in atom mark bitmaps.
41 // This monotonically increases and may be read from without locking.
42 mozilla::Atomic<size_t, mozilla::SequentiallyConsistent> allocatedWords;
43
44 AtomMarkingRuntime() : allocatedWords(0) {}
45
46 // Allocate an index in the atom marking bitmap for a new arena.
47 size_t allocateIndex(GCRuntime* gc);
48
49 // Free an index in the atom marking bitmap.
50 void freeIndex(size_t index, const AutoLockGC& lock);
51
52 void mergePendingFreeArenaIndexes(GCRuntime* gc);
53
54 // Update the atom marking bitmaps in all collected zones according to the
55 // atoms zone mark bits.
56 void refineZoneBitmapsForCollectedZones(GCRuntime* gc, size_t collectedZones);
57
58 // Set any bits in the chunk mark bitmaps for atoms which are marked in any
59 // uncollected zone in the runtime.
60 void markAtomsUsedByUncollectedZones(GCRuntime* gc, size_t uncollectedZones);
61
62 private:
63 // Fill |bitmap| with an atom marking bitmap based on the things that are
64 // currently marked in the chunks used by atoms zone arenas. This returns
65 // false on an allocation failure (but does not report an exception).
66 bool computeBitmapFromChunkMarkBits(GCRuntime* gc, DenseBitmap& bitmap);
67
68 // Update the atom marking bitmap in |zone| according to another
69 // overapproximation of the reachable atoms in |bitmap|.
70 void refineZoneBitmapForCollectedZone(Zone* zone, const DenseBitmap& bitmap);
71
72 public:
73 // Mark an atom or id as being newly reachable by the context's zone.
74 template <typename T>
75 void markAtom(JSContext* cx, T* thing);
76
77 // Version of markAtom that's always inlined, for performance-sensitive
78 // callers.
79 template <typename T, bool Fallible>
80 MOZ_ALWAYS_INLINE bool inlinedMarkAtomInternal(JSContext* cx, T* thing);
81 template <typename T>
82 MOZ_ALWAYS_INLINE void inlinedMarkAtom(JSContext* cx, T* thing);
83 template <typename T>
84 MOZ_ALWAYS_INLINE bool inlinedMarkAtomFallible(JSContext* cx, T* thing);
85
86 void markId(JSContext* cx, jsid id);
87 void markAtomValue(JSContext* cx, const Value& value);
88
89 #ifdef DEBUG
90 // Return whether |thing/id| is in the atom marking bitmap for |zone|.
91 template <typename T>
92 bool atomIsMarked(Zone* zone, T* thing);
93 bool idIsMarked(Zone* zone, jsid id);
94 bool valueIsMarked(Zone* zone, const Value& value);
95 #endif
96 };
97
98 } // namespace gc
99 } // namespace js
100
101 #endif // gc_AtomMarking_h