• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 V8_HEAP_CPPGC_RAW_HEAP_H_
6 #define V8_HEAP_CPPGC_RAW_HEAP_H_
7 
8 #include <iterator>
9 #include <memory>
10 #include <vector>
11 
12 #include "include/cppgc/heap.h"
13 #include "src/base/logging.h"
14 #include "src/base/macros.h"
15 
16 namespace cppgc {
17 namespace internal {
18 
19 class HeapBase;
20 class BaseSpace;
21 
22 // RawHeap is responsible for space management.
23 class V8_EXPORT_PRIVATE RawHeap final {
24  public:
25   // Normal spaces are used to store objects of different size classes:
26   // - kNormal1:  < 32 bytes
27   // - kNormal2:  < 64 bytes
28   // - kNormal3:  < 128 bytes
29   // - kNormal4: >= 128 bytes
30   //
31   // Objects of size greater than 2^16 get stored in the large space.
32   //
33   // Users can override where objects are allocated via cppgc::CustomSpace to
34   // force allocation in a custom space.
35   enum class RegularSpaceType : uint8_t {
36     kNormal1,
37     kNormal2,
38     kNormal3,
39     kNormal4,
40     kLarge,
41   };
42 
43   static constexpr size_t kNumberOfRegularSpaces =
44       static_cast<size_t>(RegularSpaceType::kLarge) + 1;
45 
46   using Spaces = std::vector<std::unique_ptr<BaseSpace>>;
47   using iterator = Spaces::iterator;
48   using const_iterator = Spaces::const_iterator;
49 
50   RawHeap(HeapBase* heap,
51           const std::vector<std::unique_ptr<CustomSpaceBase>>& custom_spaces);
52 
53   RawHeap(const RawHeap&) = delete;
54   RawHeap& operator=(const RawHeap&) = delete;
55 
56   ~RawHeap();
57 
58   // Space iteration support.
begin()59   iterator begin() { return spaces_.begin(); }
begin()60   const_iterator begin() const { return spaces_.begin(); }
end()61   iterator end() { return spaces_.end(); }
end()62   const_iterator end() const { return spaces_.end(); }
63 
custom_begin()64   iterator custom_begin() { return std::next(begin(), kNumberOfRegularSpaces); }
custom_end()65   iterator custom_end() { return end(); }
66 
size()67   size_t size() const { return spaces_.size(); }
68 
Space(RegularSpaceType type)69   BaseSpace* Space(RegularSpaceType type) {
70     const size_t index = static_cast<size_t>(type);
71     DCHECK_GT(kNumberOfRegularSpaces, index);
72     return Space(index);
73   }
Space(RegularSpaceType space)74   const BaseSpace* Space(RegularSpaceType space) const {
75     return const_cast<RawHeap&>(*this).Space(space);
76   }
77 
CustomSpace(CustomSpaceIndex space_index)78   BaseSpace* CustomSpace(CustomSpaceIndex space_index) {
79     return Space(SpaceIndexForCustomSpace(space_index));
80   }
CustomSpace(CustomSpaceIndex space_index)81   const BaseSpace* CustomSpace(CustomSpaceIndex space_index) const {
82     return const_cast<RawHeap&>(*this).CustomSpace(space_index);
83   }
84 
heap()85   HeapBase* heap() { return main_heap_; }
heap()86   const HeapBase* heap() const { return main_heap_; }
87 
88  private:
SpaceIndexForCustomSpace(CustomSpaceIndex space_index)89   size_t SpaceIndexForCustomSpace(CustomSpaceIndex space_index) const {
90     DCHECK_LT(space_index.value, spaces_.size() - kNumberOfRegularSpaces);
91     return kNumberOfRegularSpaces + space_index.value;
92   }
93 
Space(size_t space_index)94   BaseSpace* Space(size_t space_index) {
95     DCHECK_GT(spaces_.size(), space_index);
96     BaseSpace* space = spaces_[space_index].get();
97     DCHECK(space);
98     return space;
99   }
Space(size_t space_index)100   const BaseSpace* Space(size_t space_index) const {
101     return const_cast<RawHeap&>(*this).Space(space_index);
102   }
103 
104   HeapBase* main_heap_;
105   Spaces spaces_;
106 };
107 
108 }  // namespace internal
109 }  // namespace cppgc
110 
111 #endif  // V8_HEAP_CPPGC_RAW_HEAP_H_
112