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_HEAP_SPACE_H_ 6 #define V8_HEAP_CPPGC_HEAP_SPACE_H_ 7 8 #include <vector> 9 10 #include "src/base/logging.h" 11 #include "src/base/macros.h" 12 #include "src/base/platform/mutex.h" 13 #include "src/heap/cppgc/free-list.h" 14 15 namespace cppgc { 16 namespace internal { 17 18 class RawHeap; 19 class BasePage; 20 21 // BaseSpace is responsible for page management. 22 class V8_EXPORT_PRIVATE BaseSpace { 23 public: 24 using Pages = std::vector<BasePage*>; 25 26 using iterator = Pages::iterator; 27 using const_iterator = Pages::const_iterator; 28 29 BaseSpace(const BaseSpace&) = delete; 30 BaseSpace& operator=(const BaseSpace&) = delete; 31 virtual ~BaseSpace(); 32 begin()33 iterator begin() { return pages_.begin(); } begin()34 const_iterator begin() const { return pages_.begin(); } end()35 iterator end() { return pages_.end(); } end()36 const_iterator end() const { return pages_.end(); } 37 size()38 size_t size() const { return pages_.size(); } 39 is_large()40 bool is_large() const { return type_ == PageType::kLarge; } index()41 size_t index() const { return index_; } 42 raw_heap()43 RawHeap* raw_heap() { return heap_; } raw_heap()44 const RawHeap* raw_heap() const { return heap_; } 45 46 // Page manipulation functions. 47 void AddPage(BasePage*); 48 void RemovePage(BasePage*); 49 Pages RemoveAllPages(); pages_mutex()50 v8::base::Mutex& pages_mutex() const { return pages_mutex_; } 51 is_compactable()52 bool is_compactable() const { return is_compactable_; } 53 54 protected: 55 enum class PageType { kNormal, kLarge }; 56 explicit BaseSpace(RawHeap* heap, size_t index, PageType type, 57 bool is_compactable); 58 59 private: 60 RawHeap* heap_; 61 Pages pages_; 62 mutable v8::base::Mutex pages_mutex_; 63 const size_t index_; 64 const PageType type_; 65 const bool is_compactable_; 66 }; 67 68 class V8_EXPORT_PRIVATE NormalPageSpace final : public BaseSpace { 69 public: 70 class LinearAllocationBuffer { 71 public: Allocate(size_t alloc_size)72 Address Allocate(size_t alloc_size) { 73 DCHECK_GE(size_, alloc_size); 74 Address result = start_; 75 start_ += alloc_size; 76 size_ -= alloc_size; 77 return result; 78 } 79 Set(Address ptr,size_t size)80 void Set(Address ptr, size_t size) { 81 start_ = ptr; 82 size_ = size; 83 } 84 start()85 Address start() const { return start_; } size()86 size_t size() const { return size_; } 87 88 private: 89 Address start_ = nullptr; 90 size_t size_ = 0; 91 }; 92 From(BaseSpace & space)93 static NormalPageSpace& From(BaseSpace& space) { 94 DCHECK(!space.is_large()); 95 return static_cast<NormalPageSpace&>(space); 96 } From(const BaseSpace & space)97 static const NormalPageSpace& From(const BaseSpace& space) { 98 return From(const_cast<BaseSpace&>(space)); 99 } 100 101 NormalPageSpace(RawHeap* heap, size_t index, bool is_compactable); 102 linear_allocation_buffer()103 LinearAllocationBuffer& linear_allocation_buffer() { return current_lab_; } linear_allocation_buffer()104 const LinearAllocationBuffer& linear_allocation_buffer() const { 105 return current_lab_; 106 } 107 free_list()108 FreeList& free_list() { return free_list_; } free_list()109 const FreeList& free_list() const { return free_list_; } 110 111 private: 112 LinearAllocationBuffer current_lab_; 113 FreeList free_list_; 114 }; 115 116 class V8_EXPORT_PRIVATE LargePageSpace final : public BaseSpace { 117 public: From(BaseSpace & space)118 static LargePageSpace& From(BaseSpace& space) { 119 DCHECK(space.is_large()); 120 return static_cast<LargePageSpace&>(space); 121 } From(const BaseSpace & space)122 static const LargePageSpace& From(const BaseSpace& space) { 123 return From(const_cast<BaseSpace&>(space)); 124 } 125 126 LargePageSpace(RawHeap* heap, size_t index); 127 }; 128 129 } // namespace internal 130 } // namespace cppgc 131 132 #endif // V8_HEAP_CPPGC_HEAP_SPACE_H_ 133