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_CAGED_HEAP_H_ 6 #define V8_HEAP_CPPGC_CAGED_HEAP_H_ 7 8 #include <limits> 9 #include <memory> 10 11 #include "include/cppgc/platform.h" 12 #include "src/base/bounded-page-allocator.h" 13 #include "src/heap/cppgc/globals.h" 14 #include "src/heap/cppgc/virtual-memory.h" 15 16 namespace cppgc { 17 namespace internal { 18 19 struct CagedHeapLocalData; 20 class HeapBase; 21 22 class CagedHeap final { 23 public: 24 using AllocatorType = v8::base::BoundedPageAllocator; 25 26 template <typename RetType = uintptr_t> OffsetFromAddress(const void * address)27 static RetType OffsetFromAddress(const void* address) { 28 static_assert( 29 std::numeric_limits<RetType>::max() >= (kCagedHeapReservationSize - 1), 30 "The return type should be large enough"); 31 return reinterpret_cast<uintptr_t>(address) & 32 (kCagedHeapReservationAlignment - 1); 33 } 34 BaseFromAddress(const void * address)35 static uintptr_t BaseFromAddress(const void* address) { 36 return reinterpret_cast<uintptr_t>(address) & 37 ~(kCagedHeapReservationAlignment - 1); 38 } 39 40 CagedHeap(HeapBase& heap, PageAllocator& platform_allocator); 41 42 CagedHeap(const CagedHeap&) = delete; 43 CagedHeap& operator=(const CagedHeap&) = delete; 44 allocator()45 AllocatorType& allocator() { return *bounded_allocator_; } allocator()46 const AllocatorType& allocator() const { return *bounded_allocator_; } 47 local_data()48 CagedHeapLocalData& local_data() { 49 return *static_cast<CagedHeapLocalData*>(reserved_area_.address()); 50 } local_data()51 const CagedHeapLocalData& local_data() const { 52 return *static_cast<CagedHeapLocalData*>(reserved_area_.address()); 53 } 54 IsOnHeap(const void * address)55 bool IsOnHeap(const void* address) const { 56 return reinterpret_cast<void*>(BaseFromAddress(address)) == 57 reserved_area_.address(); 58 } 59 base()60 void* base() const { return reserved_area_.address(); } 61 62 private: 63 const VirtualMemory reserved_area_; 64 std::unique_ptr<AllocatorType> bounded_allocator_; 65 }; 66 67 } // namespace internal 68 } // namespace cppgc 69 70 #endif // V8_HEAP_CPPGC_CAGED_HEAP_H_ 71