v8 12.4.254 (node 22.4.1)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
caged-heap-local-data.h
Go to the documentation of this file.
1// Copyright 2020 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 INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
6#define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
7
8#include <array>
9#include <cstddef>
10#include <cstdint>
11
15#include "cppgc/platform.h"
16#include "v8config.h" // NOLINT(build/include_directory)
17
18#if __cpp_lib_bitopts
19#include <bit>
20#endif // __cpp_lib_bitopts
21
22#if defined(CPPGC_CAGED_HEAP)
23
24namespace cppgc {
25namespace internal {
26
27class HeapBase;
28class HeapBaseHandle;
29
30#if defined(CPPGC_YOUNG_GENERATION)
31
32// AgeTable is the bytemap needed for the fast generation check in the write
33// barrier. AgeTable contains entries that correspond to 4096 bytes memory
34// regions (cards). Each entry in the table represents generation of the objects
35// that reside on the corresponding card (young, old or mixed).
36class V8_EXPORT AgeTable final {
37 static constexpr size_t kRequiredSize = 1 * api_constants::kMB;
38 static constexpr size_t kAllocationGranularity =
39 api_constants::kAllocationGranularity;
40
41 public:
42 // Represents age of the objects living on a single card.
43 enum class Age : uint8_t { kOld, kYoung, kMixed };
44 // When setting age for a range, consider or ignore ages of the adjacent
45 // cards.
46 enum class AdjacentCardsPolicy : uint8_t { kConsider, kIgnore };
47
48 static constexpr size_t kCardSizeInBytes =
49 api_constants::kCagedHeapDefaultReservationSize / kRequiredSize;
50
51 static constexpr size_t CalculateAgeTableSizeForHeapSize(size_t heap_size) {
52 return heap_size / kCardSizeInBytes;
53 }
54
55 void SetAge(uintptr_t cage_offset, Age age) {
56 table_[card(cage_offset)] = age;
57 }
58
59 V8_INLINE Age GetAge(uintptr_t cage_offset) const {
60 return table_[card(cage_offset)];
61 }
62
63 void SetAgeForRange(uintptr_t cage_offset_begin, uintptr_t cage_offset_end,
64 Age age, AdjacentCardsPolicy adjacent_cards_policy);
65
66 Age GetAgeForRange(uintptr_t cage_offset_begin,
67 uintptr_t cage_offset_end) const;
68
69 void ResetForTesting();
70
71 private:
72 V8_INLINE size_t card(uintptr_t offset) const {
73 constexpr size_t kGranularityBits =
74#if __cpp_lib_bitopts
75 std::countr_zero(static_cast<uint32_t>(kCardSizeInBytes));
76#elif V8_HAS_BUILTIN_CTZ
77 __builtin_ctz(static_cast<uint32_t>(kCardSizeInBytes));
78#else
79 // Hardcode and check with assert.
80#if defined(CPPGC_2GB_CAGE)
81 11;
82#else // !defined(CPPGC_2GB_CAGE)
83 12;
84#endif // !defined(CPPGC_2GB_CAGE)
85#endif // !V8_HAS_BUILTIN_CTZ
86 static_assert((1 << kGranularityBits) == kCardSizeInBytes);
87 const size_t entry = offset >> kGranularityBits;
88 CPPGC_DCHECK(CagedHeapBase::GetAgeTableSize() > entry);
89 return entry;
90 }
91
92#if defined(V8_CC_GNU)
93 // gcc disallows flexible arrays in otherwise empty classes.
94 Age table_[0];
95#else // !defined(V8_CC_GNU)
96 Age table_[];
97#endif // !defined(V8_CC_GNU)
98};
99
100#endif // CPPGC_YOUNG_GENERATION
101
102struct CagedHeapLocalData final {
103 V8_INLINE static CagedHeapLocalData& Get() {
104 return *reinterpret_cast<CagedHeapLocalData*>(CagedHeapBase::GetBase());
105 }
106
107 static constexpr size_t CalculateLocalDataSizeForHeapSize(size_t heap_size) {
108 return AgeTable::CalculateAgeTableSizeForHeapSize(heap_size);
109 }
110
111#if defined(CPPGC_YOUNG_GENERATION)
112 AgeTable age_table;
113#endif
114};
115
116} // namespace internal
117} // namespace cppgc
118
119#endif // defined(CPPGC_CAGED_HEAP)
120
121#endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
#define CPPGC_DCHECK(condition)
Definition logging.h:36
constexpr size_t kAllocationGranularity
#define V8_EXPORT
Definition v8config.h:753
#define V8_INLINE
Definition v8config.h:477