V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
roots.h
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_ROOTS_H_
6 #define V8_ROOTS_H_
7 
8 #include "src/accessors.h"
9 #include "src/globals.h"
10 #include "src/heap-symbols.h"
11 #include "src/objects-definitions.h"
12 #include "src/objects/slots.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 // Forward declarations.
18 enum ElementsKind : uint8_t;
19 class FixedTypedArrayBase;
20 template <typename T>
21 class Handle;
22 class Heap;
23 class Isolate;
24 class Map;
25 class String;
26 class Symbol;
27 class RootVisitor;
28 
29 // Defines all the read-only roots in Heap.
30 #define STRONG_READ_ONLY_ROOT_LIST(V) \
31  /* Cluster the most popular ones in a few cache lines here at the top. */ \
32  /* The first 32 entries are most often used in the startup snapshot and */ \
33  /* can use a shorter representation in the serialization format. */ \
34  V(Map, free_space_map, FreeSpaceMap) \
35  V(Map, one_pointer_filler_map, OnePointerFillerMap) \
36  V(Map, two_pointer_filler_map, TwoPointerFillerMap) \
37  V(Oddball*, uninitialized_value, UninitializedValue) \
38  V(Oddball*, undefined_value, UndefinedValue) \
39  V(Oddball*, the_hole_value, TheHoleValue) \
40  V(Oddball*, null_value, NullValue) \
41  V(Oddball*, true_value, TrueValue) \
42  V(Oddball*, false_value, FalseValue) \
43  V(String, empty_string, empty_string) \
44  V(Map, meta_map, MetaMap) \
45  V(Map, byte_array_map, ByteArrayMap) \
46  V(Map, fixed_array_map, FixedArrayMap) \
47  V(Map, fixed_cow_array_map, FixedCOWArrayMap) \
48  V(Map, hash_table_map, HashTableMap) \
49  V(Map, symbol_map, SymbolMap) \
50  V(Map, one_byte_string_map, OneByteStringMap) \
51  V(Map, one_byte_internalized_string_map, OneByteInternalizedStringMap) \
52  V(Map, scope_info_map, ScopeInfoMap) \
53  V(Map, shared_function_info_map, SharedFunctionInfoMap) \
54  V(Map, code_map, CodeMap) \
55  V(Map, function_context_map, FunctionContextMap) \
56  V(Map, cell_map, CellMap) \
57  V(Map, global_property_cell_map, GlobalPropertyCellMap) \
58  V(Map, foreign_map, ForeignMap) \
59  V(Map, heap_number_map, HeapNumberMap) \
60  V(Map, transition_array_map, TransitionArrayMap) \
61  V(Map, feedback_vector_map, FeedbackVectorMap) \
62  V(ScopeInfo, empty_scope_info, EmptyScopeInfo) \
63  V(FixedArray, empty_fixed_array, EmptyFixedArray) \
64  V(DescriptorArray*, empty_descriptor_array, EmptyDescriptorArray) \
65  /* Entries beyond the first 32 */ \
66  /* Oddballs */ \
67  V(Oddball*, arguments_marker, ArgumentsMarker) \
68  V(Oddball*, exception, Exception) \
69  V(Oddball*, termination_exception, TerminationException) \
70  V(Oddball*, optimized_out, OptimizedOut) \
71  V(Oddball*, stale_register, StaleRegister) \
72  /* Context maps */ \
73  V(Map, native_context_map, NativeContextMap) \
74  V(Map, module_context_map, ModuleContextMap) \
75  V(Map, eval_context_map, EvalContextMap) \
76  V(Map, script_context_map, ScriptContextMap) \
77  V(Map, await_context_map, AwaitContextMap) \
78  V(Map, block_context_map, BlockContextMap) \
79  V(Map, catch_context_map, CatchContextMap) \
80  V(Map, with_context_map, WithContextMap) \
81  V(Map, debug_evaluate_context_map, DebugEvaluateContextMap) \
82  V(Map, script_context_table_map, ScriptContextTableMap) \
83  /* Maps */ \
84  V(Map, feedback_metadata_map, FeedbackMetadataArrayMap) \
85  V(Map, array_list_map, ArrayListMap) \
86  V(Map, bigint_map, BigIntMap) \
87  V(Map, object_boilerplate_description_map, ObjectBoilerplateDescriptionMap) \
88  V(Map, bytecode_array_map, BytecodeArrayMap) \
89  V(Map, code_data_container_map, CodeDataContainerMap) \
90  V(Map, descriptor_array_map, DescriptorArrayMap) \
91  V(Map, fixed_double_array_map, FixedDoubleArrayMap) \
92  V(Map, global_dictionary_map, GlobalDictionaryMap) \
93  V(Map, many_closures_cell_map, ManyClosuresCellMap) \
94  V(Map, module_info_map, ModuleInfoMap) \
95  V(Map, mutable_heap_number_map, MutableHeapNumberMap) \
96  V(Map, name_dictionary_map, NameDictionaryMap) \
97  V(Map, no_closures_cell_map, NoClosuresCellMap) \
98  V(Map, no_feedback_cell_map, NoFeedbackCellMap) \
99  V(Map, number_dictionary_map, NumberDictionaryMap) \
100  V(Map, one_closure_cell_map, OneClosureCellMap) \
101  V(Map, ordered_hash_map_map, OrderedHashMapMap) \
102  V(Map, ordered_hash_set_map, OrderedHashSetMap) \
103  V(Map, ordered_name_dictionary_map, OrderedNameDictionaryMap) \
104  V(Map, pre_parsed_scope_data_map, PreParsedScopeDataMap) \
105  V(Map, property_array_map, PropertyArrayMap) \
106  V(Map, side_effect_call_handler_info_map, SideEffectCallHandlerInfoMap) \
107  V(Map, side_effect_free_call_handler_info_map, \
108  SideEffectFreeCallHandlerInfoMap) \
109  V(Map, next_call_side_effect_free_call_handler_info_map, \
110  NextCallSideEffectFreeCallHandlerInfoMap) \
111  V(Map, simple_number_dictionary_map, SimpleNumberDictionaryMap) \
112  V(Map, sloppy_arguments_elements_map, SloppyArgumentsElementsMap) \
113  V(Map, small_ordered_hash_map_map, SmallOrderedHashMapMap) \
114  V(Map, small_ordered_hash_set_map, SmallOrderedHashSetMap) \
115  V(Map, small_ordered_name_dictionary_map, SmallOrderedNameDictionaryMap) \
116  V(Map, string_table_map, StringTableMap) \
117  V(Map, uncompiled_data_without_pre_parsed_scope_map, \
118  UncompiledDataWithoutPreParsedScopeMap) \
119  V(Map, uncompiled_data_with_pre_parsed_scope_map, \
120  UncompiledDataWithPreParsedScopeMap) \
121  V(Map, weak_fixed_array_map, WeakFixedArrayMap) \
122  V(Map, weak_array_list_map, WeakArrayListMap) \
123  V(Map, ephemeron_hash_table_map, EphemeronHashTableMap) \
124  V(Map, embedder_data_array_map, EmbedderDataArrayMap) \
125  /* String maps */ \
126  V(Map, native_source_string_map, NativeSourceStringMap) \
127  V(Map, string_map, StringMap) \
128  V(Map, cons_one_byte_string_map, ConsOneByteStringMap) \
129  V(Map, cons_string_map, ConsStringMap) \
130  V(Map, thin_one_byte_string_map, ThinOneByteStringMap) \
131  V(Map, thin_string_map, ThinStringMap) \
132  V(Map, sliced_string_map, SlicedStringMap) \
133  V(Map, sliced_one_byte_string_map, SlicedOneByteStringMap) \
134  V(Map, external_string_map, ExternalStringMap) \
135  V(Map, external_string_with_one_byte_data_map, \
136  ExternalStringWithOneByteDataMap) \
137  V(Map, external_one_byte_string_map, ExternalOneByteStringMap) \
138  V(Map, uncached_external_string_map, UncachedExternalStringMap) \
139  V(Map, uncached_external_string_with_one_byte_data_map, \
140  UncachedExternalStringWithOneByteDataMap) \
141  V(Map, internalized_string_map, InternalizedStringMap) \
142  V(Map, external_internalized_string_map, ExternalInternalizedStringMap) \
143  V(Map, external_internalized_string_with_one_byte_data_map, \
144  ExternalInternalizedStringWithOneByteDataMap) \
145  V(Map, external_one_byte_internalized_string_map, \
146  ExternalOneByteInternalizedStringMap) \
147  V(Map, uncached_external_internalized_string_map, \
148  UncachedExternalInternalizedStringMap) \
149  V(Map, uncached_external_internalized_string_with_one_byte_data_map, \
150  UncachedExternalInternalizedStringWithOneByteDataMap) \
151  V(Map, uncached_external_one_byte_internalized_string_map, \
152  UncachedExternalOneByteInternalizedStringMap) \
153  V(Map, uncached_external_one_byte_string_map, \
154  UncachedExternalOneByteStringMap) \
155  /* Array element maps */ \
156  V(Map, fixed_uint8_array_map, FixedUint8ArrayMap) \
157  V(Map, fixed_int8_array_map, FixedInt8ArrayMap) \
158  V(Map, fixed_uint16_array_map, FixedUint16ArrayMap) \
159  V(Map, fixed_int16_array_map, FixedInt16ArrayMap) \
160  V(Map, fixed_uint32_array_map, FixedUint32ArrayMap) \
161  V(Map, fixed_int32_array_map, FixedInt32ArrayMap) \
162  V(Map, fixed_float32_array_map, FixedFloat32ArrayMap) \
163  V(Map, fixed_float64_array_map, FixedFloat64ArrayMap) \
164  V(Map, fixed_uint8_clamped_array_map, FixedUint8ClampedArrayMap) \
165  V(Map, fixed_biguint64_array_map, FixedBigUint64ArrayMap) \
166  V(Map, fixed_bigint64_array_map, FixedBigInt64ArrayMap) \
167  /* Oddball maps */ \
168  V(Map, undefined_map, UndefinedMap) \
169  V(Map, the_hole_map, TheHoleMap) \
170  V(Map, null_map, NullMap) \
171  V(Map, boolean_map, BooleanMap) \
172  V(Map, uninitialized_map, UninitializedMap) \
173  V(Map, arguments_marker_map, ArgumentsMarkerMap) \
174  V(Map, exception_map, ExceptionMap) \
175  V(Map, termination_exception_map, TerminationExceptionMap) \
176  V(Map, optimized_out_map, OptimizedOutMap) \
177  V(Map, stale_register_map, StaleRegisterMap) \
178  V(Map, self_reference_marker_map, SelfReferenceMarkerMap) \
179  /* Canonical empty values */ \
180  V(EnumCache*, empty_enum_cache, EmptyEnumCache) \
181  V(PropertyArray, empty_property_array, EmptyPropertyArray) \
182  V(ByteArray, empty_byte_array, EmptyByteArray) \
183  V(ObjectBoilerplateDescription, empty_object_boilerplate_description, \
184  EmptyObjectBoilerplateDescription) \
185  V(ArrayBoilerplateDescription*, empty_array_boilerplate_description, \
186  EmptyArrayBoilerplateDescription) \
187  V(FixedTypedArrayBase, empty_fixed_uint8_array, EmptyFixedUint8Array) \
188  V(FixedTypedArrayBase, empty_fixed_int8_array, EmptyFixedInt8Array) \
189  V(FixedTypedArrayBase, empty_fixed_uint16_array, EmptyFixedUint16Array) \
190  V(FixedTypedArrayBase, empty_fixed_int16_array, EmptyFixedInt16Array) \
191  V(FixedTypedArrayBase, empty_fixed_uint32_array, EmptyFixedUint32Array) \
192  V(FixedTypedArrayBase, empty_fixed_int32_array, EmptyFixedInt32Array) \
193  V(FixedTypedArrayBase, empty_fixed_float32_array, EmptyFixedFloat32Array) \
194  V(FixedTypedArrayBase, empty_fixed_float64_array, EmptyFixedFloat64Array) \
195  V(FixedTypedArrayBase, empty_fixed_uint8_clamped_array, \
196  EmptyFixedUint8ClampedArray) \
197  V(FixedTypedArrayBase, empty_fixed_biguint64_array, \
198  EmptyFixedBigUint64Array) \
199  V(FixedTypedArrayBase, empty_fixed_bigint64_array, EmptyFixedBigInt64Array) \
200  V(FixedArray, empty_sloppy_arguments_elements, EmptySloppyArgumentsElements) \
201  V(NumberDictionary, empty_slow_element_dictionary, \
202  EmptySlowElementDictionary) \
203  V(FixedArray, empty_ordered_hash_map, EmptyOrderedHashMap) \
204  V(FixedArray, empty_ordered_hash_set, EmptyOrderedHashSet) \
205  V(FeedbackMetadata*, empty_feedback_metadata, EmptyFeedbackMetadata) \
206  V(PropertyCell*, empty_property_cell, EmptyPropertyCell) \
207  V(NameDictionary, empty_property_dictionary, EmptyPropertyDictionary) \
208  V(InterceptorInfo*, noop_interceptor_info, NoOpInterceptorInfo) \
209  V(WeakFixedArray*, empty_weak_fixed_array, EmptyWeakFixedArray) \
210  V(WeakArrayList*, empty_weak_array_list, EmptyWeakArrayList) \
211  /* Special numbers */ \
212  V(HeapNumber*, nan_value, NanValue) \
213  V(HeapNumber*, hole_nan_value, HoleNanValue) \
214  V(HeapNumber*, infinity_value, InfinityValue) \
215  V(HeapNumber*, minus_zero_value, MinusZeroValue) \
216  V(HeapNumber*, minus_infinity_value, MinusInfinityValue) \
217  /* Marker for self-references during code-generation */ \
218  V(HeapObject*, self_reference_marker, SelfReferenceMarker) \
219  /* Canonical trampoline RelocInfo */ \
220  V(ByteArray, off_heap_trampoline_relocation_info, \
221  OffHeapTrampolineRelocationInfo) \
222  /* Hash seed */ \
223  V(ByteArray, hash_seed, HashSeed)
224 
225 // Mutable roots that are known to be immortal immovable, for which we can
226 // safely skip write barriers.
227 #define STRONG_MUTABLE_IMMOVABLE_ROOT_LIST(V) \
228  ACCESSOR_INFO_ROOT_LIST(V) \
229  /* Maps */ \
230  V(Map, external_map, ExternalMap) \
231  V(Map, message_object_map, JSMessageObjectMap) \
232  /* Canonical empty values */ \
233  V(Script*, empty_script, EmptyScript) \
234  V(FeedbackCell*, many_closures_cell, ManyClosuresCell) \
235  V(FeedbackCell*, no_feedback_cell, NoFeedbackCell) \
236  V(Cell*, invalid_prototype_validity_cell, InvalidPrototypeValidityCell) \
237  /* Protectors */ \
238  V(Cell*, array_constructor_protector, ArrayConstructorProtector) \
239  V(PropertyCell*, no_elements_protector, NoElementsProtector) \
240  V(Cell*, is_concat_spreadable_protector, IsConcatSpreadableProtector) \
241  V(PropertyCell*, array_species_protector, ArraySpeciesProtector) \
242  V(PropertyCell*, typed_array_species_protector, TypedArraySpeciesProtector) \
243  V(PropertyCell*, regexp_species_protector, RegExpSpeciesProtector) \
244  V(PropertyCell*, promise_species_protector, PromiseSpeciesProtector) \
245  V(Cell*, string_length_protector, StringLengthProtector) \
246  V(PropertyCell*, array_iterator_protector, ArrayIteratorProtector) \
247  V(PropertyCell*, array_buffer_neutering_protector, \
248  ArrayBufferNeuteringProtector) \
249  V(PropertyCell*, promise_hook_protector, PromiseHookProtector) \
250  V(Cell*, promise_resolve_protector, PromiseResolveProtector) \
251  V(PropertyCell*, map_iterator_protector, MapIteratorProtector) \
252  V(PropertyCell*, promise_then_protector, PromiseThenProtector) \
253  V(PropertyCell*, set_iterator_protector, SetIteratorProtector) \
254  V(PropertyCell*, string_iterator_protector, StringIteratorProtector) \
255  /* Caches */ \
256  V(FixedArray, single_character_string_cache, SingleCharacterStringCache) \
257  V(FixedArray, string_split_cache, StringSplitCache) \
258  V(FixedArray, regexp_multiple_cache, RegExpMultipleCache) \
259  /* Indirection lists for isolate-independent builtins */ \
260  V(FixedArray, builtins_constants_table, BuiltinsConstantsTable) \
261  /* JS Entries */ \
262  V(Code, js_entry_code, JsEntryCode) \
263  V(Code, js_construct_entry_code, JsConstructEntryCode) \
264  V(Code, js_run_microtasks_entry_code, JsRunMicrotasksEntryCode)
265 
266 // These root references can be updated by the mutator.
267 #define STRONG_MUTABLE_MOVABLE_ROOT_LIST(V) \
268  /* Caches */ \
269  V(FixedArray, number_string_cache, NumberStringCache) \
270  /* Lists and dictionaries */ \
271  V(NameDictionary, public_symbol_table, PublicSymbolTable) \
272  V(NameDictionary, api_symbol_table, ApiSymbolTable) \
273  V(NameDictionary, api_private_symbol_table, ApiPrivateSymbolTable) \
274  V(WeakArrayList*, script_list, ScriptList) \
275  V(SimpleNumberDictionary, code_stubs, CodeStubs) \
276  V(FixedArray, materialized_objects, MaterializedObjects) \
277  V(WeakArrayList*, detached_contexts, DetachedContexts) \
278  V(WeakArrayList*, retaining_path_targets, RetainingPathTargets) \
279  V(WeakArrayList*, retained_maps, RetainedMaps) \
280  /* Feedback vectors that we need for code coverage or type profile */ \
281  V(Object*, feedback_vectors_for_profiling_tools, \
282  FeedbackVectorsForProfilingTools) \
283  V(WeakArrayList*, noscript_shared_function_infos, \
284  NoScriptSharedFunctionInfos) \
285  V(FixedArray, serialized_objects, SerializedObjects) \
286  V(FixedArray, serialized_global_proxy_sizes, SerializedGlobalProxySizes) \
287  V(TemplateList, message_listeners, MessageListeners) \
288  /* Support for async stack traces */ \
289  V(HeapObject*, current_microtask, CurrentMicrotask) \
290  /* JSWeakFactory objects which need cleanup */ \
291  V(Object*, dirty_js_weak_factories, DirtyJSWeakFactories) \
292  /* KeepDuringJob set for JS WeakRefs */ \
293  V(HeapObject*, weak_refs_keep_during_job, WeakRefsKeepDuringJob) \
294  V(HeapObject*, interpreter_entry_trampoline_for_profiling, \
295  InterpreterEntryTrampolineForProfiling)
296 
297 // Entries in this list are limited to Smis and are not visited during GC.
298 #define SMI_ROOT_LIST(V) \
299  V(Smi, stack_limit, StackLimit) \
300  V(Smi, real_stack_limit, RealStackLimit) \
301  V(Smi, last_script_id, LastScriptId) \
302  V(Smi, last_debugging_id, LastDebuggingId) \
303  /* To distinguish the function templates, so that we can find them in the */ \
304  /* function cache of the native context. */ \
305  V(Smi, next_template_serial_number, NextTemplateSerialNumber) \
306  V(Smi, arguments_adaptor_deopt_pc_offset, ArgumentsAdaptorDeoptPCOffset) \
307  V(Smi, construct_stub_create_deopt_pc_offset, \
308  ConstructStubCreateDeoptPCOffset) \
309  V(Smi, construct_stub_invoke_deopt_pc_offset, \
310  ConstructStubInvokeDeoptPCOffset) \
311  V(Smi, interpreter_entry_return_pc_offset, InterpreterEntryReturnPCOffset)
312 
313 // Adapts one INTERNALIZED_STRING_LIST_GENERATOR entry to
314 // the ROOT_LIST-compatible entry
315 #define INTERNALIZED_STRING_LIST_ADAPTER(V, name, ...) V(String, name, name)
316 
317 // Produces (String, name, CamelCase) entries
318 #define INTERNALIZED_STRING_ROOT_LIST(V) \
319  INTERNALIZED_STRING_LIST_GENERATOR(INTERNALIZED_STRING_LIST_ADAPTER, V)
320 
321 // Adapts one XXX_SYMBOL_LIST_GENERATOR entry to the ROOT_LIST-compatible entry
322 #define SYMBOL_ROOT_LIST_ADAPTER(V, name, ...) V(Symbol, name, name)
323 
324 // Produces (Symbol, name, CamelCase) entries
325 #define PRIVATE_SYMBOL_ROOT_LIST(V) \
326  PRIVATE_SYMBOL_LIST_GENERATOR(SYMBOL_ROOT_LIST_ADAPTER, V)
327 #define PUBLIC_SYMBOL_ROOT_LIST(V) \
328  PUBLIC_SYMBOL_LIST_GENERATOR(SYMBOL_ROOT_LIST_ADAPTER, V)
329 #define WELL_KNOWN_SYMBOL_ROOT_LIST(V) \
330  WELL_KNOWN_SYMBOL_LIST_GENERATOR(SYMBOL_ROOT_LIST_ADAPTER, V)
331 
332 // Adapts one ACCESSOR_INFO_LIST_GENERATOR entry to the ROOT_LIST-compatible
333 // entry
334 #define ACCESSOR_INFO_ROOT_LIST_ADAPTER(V, name, CamelName, ...) \
335  V(AccessorInfo*, name##_accessor, CamelName##Accessor)
336 
337 // Produces (AccessorInfo*, name, CamelCase) entries
338 #define ACCESSOR_INFO_ROOT_LIST(V) \
339  ACCESSOR_INFO_LIST_GENERATOR(ACCESSOR_INFO_ROOT_LIST_ADAPTER, V)
340 
341 #define READ_ONLY_ROOT_LIST(V) \
342  STRONG_READ_ONLY_ROOT_LIST(V) \
343  INTERNALIZED_STRING_ROOT_LIST(V) \
344  PRIVATE_SYMBOL_ROOT_LIST(V) \
345  PUBLIC_SYMBOL_ROOT_LIST(V) \
346  WELL_KNOWN_SYMBOL_ROOT_LIST(V) \
347  STRUCT_MAPS_LIST(V) \
348  ALLOCATION_SITE_MAPS_LIST(V) \
349  DATA_HANDLER_MAPS_LIST(V)
350 
351 #define MUTABLE_ROOT_LIST(V) \
352  STRONG_MUTABLE_IMMOVABLE_ROOT_LIST(V) \
353  STRONG_MUTABLE_MOVABLE_ROOT_LIST(V) \
354  V(StringTable, string_table, StringTable) \
355  SMI_ROOT_LIST(V)
356 
357 #define ROOT_LIST(V) \
358  READ_ONLY_ROOT_LIST(V) \
359  MUTABLE_ROOT_LIST(V)
360 
361 // Declare all the root indices. This defines the root list order.
362 // clang-format off
363 enum class RootIndex : uint16_t {
364 #define DECL(type, name, CamelName) k##CamelName,
365  ROOT_LIST(DECL)
366 #undef DECL
367 
368  kRootListLength,
369 
370  // Helper aliases for inclusive regions of root indices.
371  kFirstRoot = 0,
372  kLastRoot = kRootListLength - 1,
373 
374 #define ROOT(...) +1
375  kReadOnlyRootsCount = 0 READ_ONLY_ROOT_LIST(ROOT),
376  kImmortalImmovableRootsCount =
377  kReadOnlyRootsCount STRONG_MUTABLE_IMMOVABLE_ROOT_LIST(ROOT),
378 #undef ROOT
379  kFirstReadOnlyRoot = kFirstRoot,
380  kLastReadOnlyRoot = kFirstReadOnlyRoot + kReadOnlyRootsCount - 1,
381 
382  // The strong roots visited by the garbage collector (not including read-only
383  // roots).
384  kFirstStrongRoot = kLastReadOnlyRoot + 1,
385  // (kStringTable is not a strong root).
386  kLastStrongRoot = kStringTable - 1,
387 
388  // All of the strong roots plus the read-only roots.
389  kFirstStrongOrReadOnlyRoot = kFirstRoot,
390  kLastStrongOrReadOnlyRoot = kLastStrongRoot,
391 
392  // All immortal immovable roots including read only ones.
393  kFirstImmortalImmovableRoot = kFirstReadOnlyRoot,
394  kLastImmortalImmovableRoot =
395  kFirstImmortalImmovableRoot + kImmortalImmovableRootsCount - 1,
396 
397  kFirstSmiRoot = kStringTable + 1,
398  kLastSmiRoot = kLastRoot
399 };
400 // clang-format on
401 
402 // Represents a storage of V8 heap roots.
403 class RootsTable {
404  public:
405  static constexpr size_t kEntriesCount =
406  static_cast<size_t>(RootIndex::kRootListLength);
407 
408  RootsTable() : roots_{} {}
409 
410  inline bool IsRootHandleLocation(Address* handle_location,
411  RootIndex* index) const;
412 
413  template <typename T>
414  bool IsRootHandle(Handle<T> handle, RootIndex* index) const;
415 
416  Object* const& operator[](RootIndex root_index) const {
417  size_t index = static_cast<size_t>(root_index);
418  DCHECK_LT(index, kEntriesCount);
419  return roots_[index];
420  }
421 
422  static const char* name(RootIndex root_index) {
423  size_t index = static_cast<size_t>(root_index);
424  DCHECK_LT(index, kEntriesCount);
425  return root_names_[index];
426  }
427 
428  static constexpr int offset_of(RootIndex root_index) {
429  return static_cast<int>(root_index) * kPointerSize;
430  }
431 
432  static RootIndex RootIndexForFixedTypedArray(ExternalArrayType array_type);
433  static RootIndex RootIndexForFixedTypedArray(ElementsKind elements_kind);
434  static RootIndex RootIndexForEmptyFixedTypedArray(ElementsKind elements_kind);
435 
436  // Immortal immovable root objects are allocated in OLD space and GC never
437  // moves them and the root table entries are guaranteed to not be modified
438  // after initialization. Note, however, that contents of those root objects
439  // that are allocated in writable space can still be modified after
440  // initialization.
441  // Generated code can treat direct references to these roots as constants.
442  static constexpr bool IsImmortalImmovable(RootIndex root_index) {
443  STATIC_ASSERT(static_cast<int>(RootIndex::kFirstImmortalImmovableRoot) ==
444  0);
445  return static_cast<unsigned>(root_index) <=
446  static_cast<unsigned>(RootIndex::kLastImmortalImmovableRoot);
447  }
448 
449  private:
450  ObjectSlot begin() {
451  return ObjectSlot(&roots_[static_cast<size_t>(RootIndex::kFirstRoot)]);
452  }
453  ObjectSlot end() {
454  return ObjectSlot(&roots_[static_cast<size_t>(RootIndex::kLastRoot) + 1]);
455  }
456 
457  // Used for iterating over all of the read-only and mutable strong roots.
458  ObjectSlot strong_or_read_only_roots_begin() {
459  STATIC_ASSERT(static_cast<size_t>(RootIndex::kLastReadOnlyRoot) ==
460  static_cast<size_t>(RootIndex::kFirstStrongRoot) - 1);
461  return ObjectSlot(
462  &roots_[static_cast<size_t>(RootIndex::kFirstStrongOrReadOnlyRoot)]);
463  }
464  ObjectSlot strong_or_read_only_roots_end() {
465  return ObjectSlot(
466  &roots_[static_cast<size_t>(RootIndex::kLastStrongOrReadOnlyRoot) + 1]);
467  }
468 
469  // The read-only, strong and Smi roots as defined by these accessors are all
470  // disjoint.
471  ObjectSlot read_only_roots_begin() {
472  return ObjectSlot(
473  &roots_[static_cast<size_t>(RootIndex::kFirstReadOnlyRoot)]);
474  }
475  ObjectSlot read_only_roots_end() {
476  return ObjectSlot(
477  &roots_[static_cast<size_t>(RootIndex::kLastReadOnlyRoot) + 1]);
478  }
479 
480  ObjectSlot strong_roots_begin() {
481  return ObjectSlot(
482  &roots_[static_cast<size_t>(RootIndex::kFirstStrongRoot)]);
483  }
484  ObjectSlot strong_roots_end() {
485  return ObjectSlot(
486  &roots_[static_cast<size_t>(RootIndex::kLastStrongRoot) + 1]);
487  }
488 
489  ObjectSlot smi_roots_begin() {
490  return ObjectSlot(&roots_[static_cast<size_t>(RootIndex::kFirstSmiRoot)]);
491  }
492  ObjectSlot smi_roots_end() {
493  return ObjectSlot(
494  &roots_[static_cast<size_t>(RootIndex::kLastSmiRoot) + 1]);
495  }
496 
497  Object*& operator[](RootIndex root_index) {
498  size_t index = static_cast<size_t>(root_index);
499  DCHECK_LT(index, kEntriesCount);
500  return roots_[index];
501  }
502 
503  Object* roots_[kEntriesCount];
504  static const char* root_names_[kEntriesCount];
505 
506  friend class Isolate;
507  friend class Heap;
508  friend class Factory;
509  friend class ReadOnlyRoots;
510  friend class RootsSerializer;
511 };
512 
514  public:
515  V8_INLINE explicit ReadOnlyRoots(Heap* heap);
516  V8_INLINE explicit ReadOnlyRoots(Isolate* isolate);
517 
518 // TODO(jkummerow): Drop std::remove_pointer after the migration to ObjectPtr.
519 #define ROOT_ACCESSOR(Type, name, CamelName) \
520  V8_INLINE class Type name() const; \
521  V8_INLINE Handle<std::remove_pointer<Type>::type> name##_handle() const;
522 
523  READ_ONLY_ROOT_LIST(ROOT_ACCESSOR)
524 #undef ROOT_ACCESSOR
525 
526  V8_INLINE Map MapForFixedTypedArray(ExternalArrayType array_type);
527  V8_INLINE Map MapForFixedTypedArray(ElementsKind elements_kind);
528  V8_INLINE FixedTypedArrayBase EmptyFixedTypedArrayForMap(const Map map);
529 
530  // Iterate over all the read-only roots. This is not necessary for garbage
531  // collection and is usually only performed as part of (de)serialization or
532  // heap verification.
533  void Iterate(RootVisitor* visitor);
534 
535  private:
536  RootsTable& roots_table_;
537 };
538 
539 } // namespace internal
540 } // namespace v8
541 
542 #endif // V8_ROOTS_H_
Definition: libplatform.h:13