Debian Codegraph @

js/src/gc/Tenuring.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_Tenuring_h
8 #define gc_Tenuring_h
9
10 #include "mozilla/EnumeratedArray.h"
11 #include "mozilla/HashTable.h"
12 #include "mozilla/Maybe.h"
13
14 #include "gc/AllocKind.h"
15 #include "js/GCAPI.h"
16 #include "js/TracingAPI.h"
17 #include "js/UniquePtr.h"
18 #include "util/Text.h"
19
20 namespace js {
21
22 class NativeObject;
23 class Nursery;
24 class PlainObject;
25
26 namespace wasm {
27 class AnyRef;
28 } // namespace wasm
29
30 namespace gc {
31
32 class AllocSite;
33 class ArenaCellSet;
34 class RelocationOverlay;
35 class StringRelocationOverlay;
36 #ifdef JS_GC_ZEAL
37 class PromotionStats;
38 #endif
39
40 template <typename Key>
41 struct DeduplicationStringHasher {
42 using Lookup = Key;
43 static inline HashNumber hash(const Lookup& lookup);
44 static MOZ_ALWAYS_INLINE bool match(const Key& key, const Lookup& lookup);
45 };
46
47 class TenuringTracer final : public JSTracer {
48 Nursery& nursery_;
49
50 // Size of data promoted during collection.
51 size_t promotedSize = 0;
52 // Number of cells promoted during collection.
53 size_t promotedCells = 0;
54
55 // These lists are threaded through the Nursery using the space from
56 // already moved things. The lists are used to fix up the moved things and
57 // to find things held live by intra-Nursery pointers.
58 gc::RelocationOverlay* objHead = nullptr;
59 gc::StringRelocationOverlay* stringHead = nullptr;
60
61 using StringDeDupSet =
62 HashSet<JSString*, DeduplicationStringHasher<JSString*>,
63 SystemAllocPolicy>;
64
65 // deDupSet is emplaced at the beginning of the nursery collection and reset
66 // at the end of the nursery collection. It can also be reset during nursery
67 // collection when out of memory to insert new entries.
68 mozilla::Maybe<StringDeDupSet> stringDeDupSet;
69
70 bool tenureEverything;
71
72 // A flag set when a GC thing is promoted to the next nursery generation (as
73 // opposed to the tenured heap). This is used to check when we need to add an
74 // edge to the remembered set during nursery collection.
75 bool promotedToNursery = false;
76
77 #ifdef JS_GC_ZEAL
78 UniquePtr<PromotionStats> promotionStats;
79 #endif
80
81 public:
82 TenuringTracer(JSRuntime* rt, Nursery* nursery, bool tenureEverything);
83 ~TenuringTracer();
84
85 Nursery& nursery() { return nursery_; }
86
87 #ifdef JS_GC_ZEAL
88 void initPromotionReport();
89 void printPromotionReport(JSContext* cx, JS::GCReason reason,
90 const JS::AutoRequireNoGC& nogc) const;
91 #endif
92
93 // Promote all live objects and everything they can reach. Called after all
94 // roots have been traced.
95 void collectToObjectFixedPoint();
96
97 // Promote all live strings and all strings they can reach, and additionally
98 // do any fixups for when strings are pointing into memory that was
99 // deduplicated. Called after collectToObjectFixedPoint().
100 void collectToStringFixedPoint();
101
102 size_t getPromotedSize() const;
103 size_t getPromotedCells() const;
104
105 void traverse(JS::Value* thingp);
106 void traverse(wasm::AnyRef* thingp);
107
108 // The store buffers need to be able to call these directly.
109 void traceObject(JSObject* obj);
110 void traceObjectSlots(NativeObject* nobj, uint32_t start, uint32_t end);
111 void traceObjectElements(JS::Value* vp, uint32_t count);
112 void traceString(JSString* str);
113
114 // Methods to promote a live cell or get the pointer to its new location if
115 // that has already happened. The store buffers call these.
116 JSObject* promoteOrForward(JSObject* obj);
117 JSString* promoteOrForward(JSString* str);
118 JS::BigInt* promoteOrForward(JS::BigInt* bip);
119
120 // Returns whether any cells in the arena require sweeping.
121 template <typename T>
122 bool traceBufferedCells(Arena* arena, ArenaCellSet* cells);
123
124 class AutoPromotedAnyToNursery;
125
126 private:
127 #define DEFINE_ON_EDGE_METHOD(name, type, _1, _2) \
128 void on##name##Edge(type** thingp, const char* name) override;
129 JS_FOR_EACH_TRACEKIND(DEFINE_ON_EDGE_METHOD)
130 #undef DEFINE_ON_EDGE_METHOD
131
132 inline void insertIntoObjectFixupList(gc::RelocationOverlay* entry);
133 inline void insertIntoStringFixupList(gc::StringRelocationOverlay* entry);
134
135 template <typename T>
136 T* alloc(JS::Zone* zone, gc::AllocKind kind, gc::Cell* src);
137 template <JS::TraceKind traceKind>
138 void* allocCell(JS::Zone* zone, gc::AllocKind allocKind, gc::AllocSite* site,
139 gc::Cell* src);
140 JSString* allocString(JSString* src, JS::Zone* zone, gc::AllocKind dstKind);
141
142 bool shouldTenure(Zone* zone, JS::TraceKind traceKind, Cell* cell);
143
144 MOZ_ALWAYS_INLINE JSObject* promoteObject(JSObject* obj);
145 inline JSObject* promotePlainObject(PlainObject* src);
146 JSObject* promoteObjectSlow(JSObject* src);
147 JSString* promoteString(JSString* src);
148 JS::BigInt* promoteBigInt(JS::BigInt* src);
149
150 size_t moveElements(NativeObject* dst, NativeObject* src,
151 gc::AllocKind dstKind);
152 size_t moveSlots(NativeObject* dst, NativeObject* src);
153 size_t moveString(JSString* dst, JSString* src, gc::AllocKind dstKind);
154 size_t moveBigInt(JS::BigInt* dst, JS::BigInt* src, gc::AllocKind dstKind);
155
156 void traceSlots(JS::Value* vp, JS::Value* end);
157 };
158
159 } // namespace gc
160 } // namespace js
161
162 #endif // gc_Tenuring_h