Debian Codegraph @

js/src/gc/AllocKind.h

raw
1
2 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * vim: set ts=8 sts=2 et sw=2 tw=80:
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 /*
9 * GC-internal definition of GC cell kinds.
10 */
11
12 #ifndef gc_AllocKind_h
13 #define gc_AllocKind_h
14
15 #include "mozilla/EnumeratedArray.h"
16 #include "mozilla/EnumeratedRange.h"
17
18 #include <iterator>
19 #include <stdint.h>
20
21 #include "js/TraceKind.h"
22
23 class JSDependentString;
24 class JSExternalString;
25 class JSFatInlineString;
26 class JSLinearString;
27 class JSRope;
28 class JSThinInlineString;
29
30 namespace js {
31
32 class CompactPropMap;
33 class FatInlineAtom;
34 class ThinInlineAtom;
35 class NormalAtom;
36 class NormalPropMap;
37 class DictionaryPropMap;
38 class DictionaryShape;
39 class SharedShape;
40 class ProxyShape;
41 class WasmGCShape;
42
43 namespace gc {
44
45 // The GC allocation kinds.
46 //
47 // These are defined by macros which enumerate the different allocation kinds
48 // and supply the following information:
49 //
50 // - the corresponding AllocKind
51 // - their JS::TraceKind
52 // - their C++ base type
53 // - a C++ type of the correct size
54 // - their FinalizeKind (see above)
55 // - whether they can be allocated in the nursery (this is true for foreground
56 // finalized objects but these will can only actually be allocated in the
57 // nursery if JSCLASS_SKIP_NURSERY_FINALIZE is set)
58 // - whether they can be compacted
59
60 // clang-format off
61 #define FOR_EACH_OBJECT_ALLOCKIND(D) \
62 /* AllocKind TraceKind TypeName SizedType Finalize Nursery Compact */ \
63 D(FUNCTION, Object, JSObject, JSObject_Slots4, None, true, true) \
64 D(FUNCTION_EXTENDED, Object, JSObject, JSObject_Slots7, None, true, true) \
65 D(OBJECT0, Object, JSObject, JSObject_Slots0, None, true, true) \
66 D(OBJECT0_FOREGROUND, Object, JSObject, JSObject_Slots0, Foreground, true, true) \
67 D(OBJECT0_BACKGROUND, Object, JSObject, JSObject_Slots0, Background, true, true) \
68 D(OBJECT2, Object, JSObject, JSObject_Slots2, None, true, true) \
69 D(OBJECT2_FOREGROUND, Object, JSObject, JSObject_Slots2, Foreground, true, true) \
70 D(OBJECT2_BACKGROUND, Object, JSObject, JSObject_Slots2, Background, true, true) \
71 D(ARRAYBUFFER4, Object, JSObject, JSObject_Slots4, Background, true, true) \
72 D(OBJECT4, Object, JSObject, JSObject_Slots4, None, true, true) \
73 D(OBJECT4_FOREGROUND, Object, JSObject, JSObject_Slots4, Foreground, true, true) \
74 D(OBJECT4_BACKGROUND, Object, JSObject, JSObject_Slots4, Background, true, true) \
75 D(ARRAYBUFFER8, Object, JSObject, JSObject_Slots8, Background, true, true) \
76 D(OBJECT8, Object, JSObject, JSObject_Slots8, None, true, true) \
77 D(OBJECT8_FOREGROUND, Object, JSObject, JSObject_Slots8, Foreground, true, true) \
78 D(OBJECT8_BACKGROUND, Object, JSObject, JSObject_Slots8, Background, true, true) \
79 D(ARRAYBUFFER12, Object, JSObject, JSObject_Slots12, Background, true, true) \
80 D(OBJECT12, Object, JSObject, JSObject_Slots12, None, true, true) \
81 D(OBJECT12_FOREGROUND, Object, JSObject, JSObject_Slots12, Foreground, true, true) \
82 D(OBJECT12_BACKGROUND, Object, JSObject, JSObject_Slots12, Background, true, true) \
83 D(ARRAYBUFFER16, Object, JSObject, JSObject_Slots16, Background, true, true) \
84 D(OBJECT16, Object, JSObject, JSObject_Slots16, None, true, true) \
85 D(OBJECT16_FOREGROUND, Object, JSObject, JSObject_Slots16, Foreground, true, true) \
86 D(OBJECT16_BACKGROUND, Object, JSObject, JSObject_Slots16, Background, true, true)
87
88 #define FOR_EACH_NONOBJECT_NONNURSERY_ALLOCKIND(D) \
89 /* AllocKind TraceKind TypeName SizedType Finalize Nursery Compact */ \
90 D(SCRIPT, Script, js::BaseScript, js::BaseScript, Foreground, false, true) \
91 D(SHAPE, Shape, js::Shape, js::SizedShape, Background, false, true) \
92 D(BASE_SHAPE, BaseShape, js::BaseShape, js::BaseShape, None, false, true) \
93 D(GETTER_SETTER, GetterSetter, js::GetterSetter, js::GetterSetter, None, false, true) \
94 D(COMPACT_PROP_MAP, PropMap, js::CompactPropMap, js::CompactPropMap, Background, false, true) \
95 D(NORMAL_PROP_MAP, PropMap, js::NormalPropMap, js::NormalPropMap, Background, false, true) \
96 D(DICT_PROP_MAP, PropMap, js::DictionaryPropMap, js::DictionaryPropMap, Background, false, true) \
97 D(EXTERNAL_STRING, String, JSExternalString, JSExternalString, Background, false, true) \
98 D(FAT_INLINE_ATOM, String, js::FatInlineAtom, js::FatInlineAtom, None, false, false) \
99 D(ATOM, String, js::NormalAtom, js::NormalAtom, Background, false, false) \
100 D(SYMBOL, Symbol, JS::Symbol, JS::Symbol, None, false, false) \
101 D(JITCODE, JitCode, js::jit::JitCode, js::jit::JitCode, Foreground, false, false) \
102 D(SCOPE, Scope, js::Scope, js::Scope, Background, false, true) \
103 D(REGEXP_SHARED, RegExpShared, js::RegExpShared, js::RegExpShared, Background, false, true)
104
105 #define FOR_EACH_NONOBJECT_NURSERY_ALLOCKIND(D) \
106 /* AllocKind TraceKind TypeName SizedType Finalize Nursery Compact */ \
107 D(BIGINT, BigInt, JS::BigInt, JS::BigInt, Background, true, true)
108
109 #define FOR_EACH_NURSERY_STRING_ALLOCKIND(D) \
110 /* AllocKind TraceKind TypeName SizedType Finalize Nursery Compact */ \
111 D(FAT_INLINE_STRING, String, JSFatInlineString, JSFatInlineString, None, true, true) \
112 D(STRING, String, JSString, JSString, Background, true, true)
113
114 #define FOR_EACH_BUFFER_ALLOCKIND(D) \
115 /* AllocKind TraceKind TypeName SizedType Finalize Nursery Compact */ \
116 D(BUFFER16, SmallBuffer, js::gc::SmallBuffer, js::gc::SmallBufferN<16>, None, false, true) \
117 D(BUFFER32, SmallBuffer, js::gc::SmallBuffer, js::gc::SmallBufferN<32>, None, false, true) \
118 D(BUFFER64, SmallBuffer, js::gc::SmallBuffer, js::gc::SmallBufferN<64>, None, false, true) \
119 D(BUFFER128, SmallBuffer, js::gc::SmallBuffer, js::gc::SmallBufferN<128>, None, false, true)
120 // clang-format on
121
122 #define FOR_EACH_NONOBJECT_NONBUFFER_ALLOCKIND(D) \
123 FOR_EACH_NONOBJECT_NONNURSERY_ALLOCKIND(D) \
124 FOR_EACH_NONOBJECT_NURSERY_ALLOCKIND(D) \
125 FOR_EACH_NURSERY_STRING_ALLOCKIND(D)
126
127 #define FOR_EACH_ALLOCKIND(D) \
128 FOR_EACH_OBJECT_ALLOCKIND(D) \
129 FOR_EACH_BUFFER_ALLOCKIND(D) \
130 FOR_EACH_NONOBJECT_NONBUFFER_ALLOCKIND(D)
131
132 #define DEFINE_ALLOC_KIND(allocKind, _1, _2, _3, _4, _5, _6) allocKind,
133 enum class AllocKind : uint8_t {
134 // clang-format off
135 FOR_EACH_OBJECT_ALLOCKIND(DEFINE_ALLOC_KIND)
136
137 OBJECT_LIMIT,
138 OBJECT_LAST = OBJECT_LIMIT - 1,
139
140 FOR_EACH_BUFFER_ALLOCKIND(DEFINE_ALLOC_KIND)
141
142 BUFFER_LIMIT,
143 BUFFER_LAST = BUFFER_LIMIT - 1,
144
145 FOR_EACH_NONOBJECT_NONBUFFER_ALLOCKIND(DEFINE_ALLOC_KIND)
146
147 LIMIT,
148 LAST = LIMIT - 1,
149
150 INVALID = LIMIT,
151
152 FIRST = 0,
153 OBJECT_FIRST = FUNCTION, // Hardcoded to first object kind.
154
155 BUFFER_FIRST = BUFFER16
156 // clang-format on
157 };
158 #undef DEFINE_ALLOC_KIND
159
160 static_assert(int(AllocKind::FIRST) == 0,
161 "Various places depend on AllocKind starting at 0");
162 static_assert(int(AllocKind::OBJECT_FIRST) == 0,
163 "OBJECT_FIRST must be defined as the first object kind");
164
165 constexpr size_t AllocKindCount = size_t(AllocKind::LIMIT);
166
167 /*
168 * A flag specifying either the tenured heap or a default heap (which may be
169 * either the nursery or the tenured heap).
170 *
171 * This allows an allocation site to request a heap based upon the estimated
172 * lifetime or lifetime requirements of objects allocated from that site.
173 *
174 * Order is important as these are numerically compared.
175 */
176 enum class Heap : uint8_t { Default = 0, Tenured = 1 };
177
178 enum class FinalizeKind {
179 // Cells are not finalized. Arenas containing these cells are swept on a
180 // background thread.
181 None = 0,
182
183 // Requires foreground finalization. May have client-supplied finalizer.
184 Foreground,
185
186 // Does not require foreground finalization but is non-trivial. May have
187 // client-supplied finalizer. Finalized on a background thread.
188 Background
189 };
190
191 constexpr bool IsAllocKind(AllocKind kind) {
192 return kind >= AllocKind::FIRST && kind <= AllocKind::LIMIT;
193 }
194
195 constexpr bool IsValidAllocKind(AllocKind kind) {
196 return kind >= AllocKind::FIRST && kind <= AllocKind::LAST;
197 }
198
199 const char* AllocKindName(AllocKind kind);
200
201 constexpr bool IsObjectAllocKind(AllocKind kind) {
202 return kind >= AllocKind::OBJECT_FIRST && kind <= AllocKind::OBJECT_LAST;
203 }
204
205 constexpr bool IsBufferAllocKind(AllocKind kind) {
206 return kind > AllocKind::OBJECT_LAST && kind <= AllocKind::BUFFER_LAST;
207 }
208
209 constexpr bool IsShapeAllocKind(AllocKind kind) {
210 return kind == AllocKind::SHAPE;
211 }
212
213 // Returns a sequence for use in a range-based for loop,
214 // to iterate over all alloc kinds.
215 constexpr auto AllAllocKinds() {
216 return mozilla::MakeEnumeratedRange(AllocKind::FIRST, AllocKind::LIMIT);
217 }
218
219 // Returns a sequence for use in a range-based for loop,
220 // to iterate over all object alloc kinds.
221 constexpr auto ObjectAllocKinds() {
222 return mozilla::MakeEnumeratedRange(AllocKind::OBJECT_FIRST,
223 AllocKind::OBJECT_LIMIT);
224 }
225
226 // Returns a sequence for use in a range-based for loop,
227 // to iterate over alloc kinds from |first| to |limit|, exclusive.
228 constexpr auto SomeAllocKinds(AllocKind first = AllocKind::FIRST,
229 AllocKind limit = AllocKind::LIMIT) {
230 MOZ_ASSERT(IsAllocKind(first), "|first| is not a valid AllocKind!");
231 MOZ_ASSERT(IsAllocKind(limit), "|limit| is not a valid AllocKind!");
232 return mozilla::MakeEnumeratedRange(first, limit);
233 }
234
235 // AllAllocKindArray<ValueType> gives an enumerated array of ValueTypes,
236 // with each index corresponding to a particular alloc kind.
237 template <typename ValueType>
238 using AllAllocKindArray =
239 mozilla::EnumeratedArray<AllocKind, ValueType, size_t(AllocKind::LIMIT)>;
240
241 // ObjectAllocKindArray<ValueType> gives an enumerated array of ValueTypes,
242 // with each index corresponding to a particular object alloc kind.
243 template <typename ValueType>
244 using ObjectAllocKindArray =
245 mozilla::EnumeratedArray<AllocKind, ValueType,
246 size_t(AllocKind::OBJECT_LIMIT)>;
247
248 /*
249 * Map from C++ type to alloc kind for non-object types. JSObject does not have
250 * a 1:1 mapping, so must use Arena::thingSize.
251 *
252 * The AllocKind is available as MapTypeToAllocKind<SomeType>::kind.
253 *
254 * There are specializations for strings and shapes since more than one derived
255 * type shares the same alloc kind.
256 */
257 template <typename T>
258 struct MapTypeToAllocKind {};
259 #define EXPAND_MAPTYPETOALLOCKIND(allocKind, _1, type, _2, _3, _4, _5) \
260 template <> \
261 struct MapTypeToAllocKind<type> { \
262 static const AllocKind kind = AllocKind::allocKind; \
263 };
264 FOR_EACH_NONOBJECT_NONBUFFER_ALLOCKIND(EXPAND_MAPTYPETOALLOCKIND)
265 #undef EXPAND_MAPTYPETOALLOCKIND
266
267 template <>
268 struct MapTypeToAllocKind<JSDependentString> {
269 static const AllocKind kind = AllocKind::STRING;
270 };
271 template <>
272 struct MapTypeToAllocKind<JSRope> {
273 static const AllocKind kind = AllocKind::STRING;
274 };
275 template <>
276 struct MapTypeToAllocKind<JSLinearString> {
277 static const AllocKind kind = AllocKind::STRING;
278 };
279 template <>
280 struct MapTypeToAllocKind<JSThinInlineString> {
281 static const AllocKind kind = AllocKind::STRING;
282 };
283 template <>
284 struct MapTypeToAllocKind<js::ThinInlineAtom> {
285 static const AllocKind kind = AllocKind::ATOM;
286 };
287
288 template <>
289 struct MapTypeToAllocKind<js::SharedShape> {
290 static const AllocKind kind = AllocKind::SHAPE;
291 };
292 template <>
293 struct MapTypeToAllocKind<js::DictionaryShape> {
294 static const AllocKind kind = AllocKind::SHAPE;
295 };
296 template <>
297 struct MapTypeToAllocKind<js::ProxyShape> {
298 static const AllocKind kind = AllocKind::SHAPE;
299 };
300 template <>
301 struct MapTypeToAllocKind<js::WasmGCShape> {
302 static const AllocKind kind = AllocKind::SHAPE;
303 };
304
305 constexpr JS::TraceKind MapAllocToTraceKind(AllocKind kind) {
306 constexpr JS::TraceKind map[] = {
307 #define EXPAND_ELEMENT(_1, traceKind, _2, _3, _4, _5, _6) \
308 JS::TraceKind::traceKind,
309 FOR_EACH_ALLOCKIND(EXPAND_ELEMENT)
310 #undef EXPAND_ELEMENT
311 };
312
313 static_assert(std::size(map) == AllocKindCount,
314 "AllocKind-to-TraceKind mapping must be in sync");
315 return map[size_t(kind)];
316 }
317
318 constexpr bool IsNurseryAllocable(AllocKind kind) {
319 MOZ_ASSERT(IsValidAllocKind(kind));
320
321 constexpr bool map[] = {
322 #define DEFINE_NURSERY_ALLOCABLE(_1, _2, _3, _4, _5, nursery, _6) nursery,
323 FOR_EACH_ALLOCKIND(DEFINE_NURSERY_ALLOCABLE)
324 #undef DEFINE_NURSERY_ALLOCABLE
325 };
326
327 static_assert(std::size(map) == AllocKindCount,
328 "IsNurseryAllocable sanity check");
329 return map[size_t(kind)];
330 }
331
332 constexpr FinalizeKind GetFinalizeKind(AllocKind kind) {
333 MOZ_ASSERT(IsValidAllocKind(kind));
334
335 constexpr FinalizeKind map[] = {
336 #define DEFINE_FINALIZE_KIND(_1, _2, _3, _4, finalizeKind, _5, _6) \
337 FinalizeKind::finalizeKind,
338 FOR_EACH_ALLOCKIND(DEFINE_FINALIZE_KIND)
339 #undef DEFINE_FINALIZE_KIND
340 };
341
342 static_assert(std::size(map) == AllocKindCount);
343 return map[size_t(kind)];
344 }
345
346 constexpr bool IsFinalizedKind(AllocKind kind) {
347 return GetFinalizeKind(kind) != FinalizeKind::None;
348 }
349
350 constexpr bool IsForegroundFinalized(AllocKind kind) {
351 return GetFinalizeKind(kind) == FinalizeKind::Foreground;
352 }
353
354 constexpr bool IsBackgroundFinalized(AllocKind kind) {
355 return GetFinalizeKind(kind) == FinalizeKind::Background;
356 }
357
358 // Arenas containing cells of kind FinalizeKind::None and
359 // FinalizeKind::Background are swept on a background thread.
360 constexpr bool IsBackgroundSwept(AllocKind kind) {
361 return !IsForegroundFinalized(kind);
362 }
363
364 constexpr bool IsCompactingKind(AllocKind kind) {
365 MOZ_ASSERT(IsValidAllocKind(kind));
366
367 constexpr bool map[] = {
368 #define DEFINE_COMPACTING_KIND(_1, _2, _3, _4, _5, _6, compact) compact,
369 FOR_EACH_ALLOCKIND(DEFINE_COMPACTING_KIND)
370 #undef DEFINE_COMPACTING_KIND
371 };
372
373 static_assert(std::size(map) == AllocKindCount,
374 "IsCompactingKind sanity check");
375 return map[size_t(kind)];
376 }
377
378 constexpr bool IsMovableKind(AllocKind kind) {
379 return IsNurseryAllocable(kind) || IsCompactingKind(kind);
380 }
381
382 } /* namespace gc */
383 } /* namespace js */
384
385 #endif /* gc_AllocKind_h */