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 
10 #include "cppgc/internal/api-constants.h"
11 #include "cppgc/internal/logging.h"
12 #include "cppgc/platform.h"
13 #include "v8config.h"  // NOLINT(build/include_directory)
14 
15 namespace cppgc {
16 namespace internal {
17 
18 class HeapBase;
19 
20 #if defined(CPPGC_YOUNG_GENERATION)
21 
22 // AgeTable contains entries that correspond to 4KB memory regions. Each entry
23 // can be in one of three states: kOld, kYoung or kUnknown.
24 class AgeTable final {
25   static constexpr size_t kGranularityBits = 12;  // 4KiB per byte.
26 
27  public:
28   enum class Age : uint8_t { kOld, kYoung, kUnknown };
29 
30   static constexpr size_t kEntrySizeInBytes = 1 << kGranularityBits;
31 
32   Age& operator[](uintptr_t offset) { return table_[entry(offset)]; }
33   Age operator[](uintptr_t offset) const { return table_[entry(offset)]; }
34 
35   void Reset(PageAllocator* allocator);
36 
37  private:
38   static constexpr size_t kAgeTableSize =
39       api_constants::kCagedHeapReservationSize >> kGranularityBits;
40 
entry(uintptr_t offset)41   size_t entry(uintptr_t offset) const {
42     const size_t entry = offset >> kGranularityBits;
43     CPPGC_DCHECK(table_.size() > entry);
44     return entry;
45   }
46 
47   std::array<Age, kAgeTableSize> table_;
48 };
49 
50 static_assert(sizeof(AgeTable) == 1 * api_constants::kMB,
51               "Size of AgeTable is 1MB");
52 
53 #endif  // CPPGC_YOUNG_GENERATION
54 
55 struct CagedHeapLocalData final {
56   CagedHeapLocalData(HeapBase&, PageAllocator&);
57 
58   bool is_incremental_marking_in_progress = false;
59   HeapBase& heap_base;
60 #if defined(CPPGC_YOUNG_GENERATION)
61   AgeTable age_table;
62 #endif
63 };
64 
65 }  // namespace internal
66 }  // namespace cppgc
67 
68 #endif  // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
69