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 12;
81#endif // !V8_HAS_BUILTIN_CTZ
82 static_assert((1 << kGranularityBits) == kCardSizeInBytes);
83 const size_t entry = offset >> kGranularityBits;
84 CPPGC_DCHECK(CagedHeapBase::GetAgeTableSize() > entry);
85 return entry;
86 }
87
88#if defined(V8_CC_GNU)
89 // gcc disallows flexible arrays in otherwise empty classes.
90 Age table_[0];
91#else // !defined(V8_CC_GNU)
92 Age table_[];
93#endif // !defined(V8_CC_GNU)
94};
95
96#endif // CPPGC_YOUNG_GENERATION
97
98struct CagedHeapLocalData final {
99 V8_INLINE static CagedHeapLocalData& Get() {
100 return *reinterpret_cast<CagedHeapLocalData*>(CagedHeapBase::GetBase());
101 }
102
103 static constexpr size_t CalculateLocalDataSizeForHeapSize(size_t heap_size) {
104 return AgeTable::CalculateAgeTableSizeForHeapSize(heap_size);
105 }
106
107#if defined(CPPGC_YOUNG_GENERATION)
108 AgeTable age_table;
109#endif
110};
111
112} // namespace internal
113} // namespace cppgc
114
115#endif // defined(CPPGC_CAGED_HEAP)
116
117#endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
#define CPPGC_DCHECK(condition)
Definition: logging.h:36
constexpr size_t kAllocationGranularity
Definition: api-constants.h:75
Definition: allocation.h:38
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499