v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
caged-heap.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 V8_HEAP_CPPGC_CAGED_HEAP_H_
6#define V8_HEAP_CPPGC_CAGED_HEAP_H_
7
8#include <limits>
9#include <memory>
10
17
18namespace cppgc {
19namespace internal {
20
21namespace testing {
22class TestWithHeap;
23}
24
26 public:
28
29 template <typename RetType = uintptr_t>
30 static RetType OffsetFromAddress(const void* address) {
31 static_assert(std::numeric_limits<RetType>::max() >=
32 (api_constants::kCagedHeapMaxReservationSize - 1),
33 "The return type should be large enough");
34 return reinterpret_cast<uintptr_t>(address) &
35 (api_constants::kCagedHeapReservationAlignment - 1);
36 }
37
38 static uintptr_t BaseFromAddress(const void* address) {
39 return reinterpret_cast<uintptr_t>(address) &
40 ~(api_constants::kCagedHeapReservationAlignment - 1);
41 }
42
43 static void InitializeIfNeeded(PageAllocator& platform_allocator,
44 size_t desired_heap_size);
45
46 static void CommitAgeTable(PageAllocator& platform_allocator);
47
48 static CagedHeap& Instance();
49
50 CagedHeap(const CagedHeap&) = delete;
51 CagedHeap& operator=(const CagedHeap&) = delete;
52
53 AllocatorType& page_allocator() { return *page_bounded_allocator_; }
55 return *page_bounded_allocator_;
56 }
57
58 bool IsOnHeap(const void* address) const {
59 DCHECK_EQ(reserved_area_.address(),
60 reinterpret_cast<void*>(CagedHeapBase::GetBase()));
61 return reinterpret_cast<void*>(BaseFromAddress(address)) ==
62 reserved_area_.address();
63 }
64
65 void* base() const { return reserved_area_.address(); }
66
67 private:
68 friend class v8::base::LeakyObject<CagedHeap>;
69 friend class testing::TestWithHeap;
70
71 explicit CagedHeap(PageAllocator& platform_allocator,
72 size_t desired_heap_size);
73
75
77 // BoundedPageAllocator is thread-safe, no need to use external
78 // synchronization.
79 std::unique_ptr<AllocatorType> page_bounded_allocator_;
80};
81
82} // namespace internal
83} // namespace cppgc
84
85#endif // V8_HEAP_CPPGC_CAGED_HEAP_H_
CagedHeap & operator=(const CagedHeap &)=delete
std::unique_ptr< AllocatorType > page_bounded_allocator_
Definition caged-heap.h:79
AllocatorType & page_allocator()
Definition caged-heap.h:53
bool IsOnHeap(const void *address) const
Definition caged-heap.h:58
CagedHeap(const CagedHeap &)=delete
const AllocatorType & page_allocator() const
Definition caged-heap.h:54
static uintptr_t BaseFromAddress(const void *address)
Definition caged-heap.h:38
static CagedHeap * instance_
Definition caged-heap.h:74
const VirtualMemory reserved_area_
Definition caged-heap.h:76
static RetType OffsetFromAddress(const void *address)
Definition caged-heap.h:30
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460