Loading...
Searching...
No Matches
caged-heap.h
Go to the documentation of this file.
1// Copyright 2022 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_H_
6#define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_
7
8#include <climits>
9#include <cstddef>
10
13#include "v8config.h" // NOLINT(build/include_directory)
14
15#if defined(CPPGC_CAGED_HEAP)
16
17namespace cppgc {
18namespace internal {
19
20class V8_EXPORT CagedHeapBase {
21 public:
22 V8_INLINE static uintptr_t OffsetFromAddress(const void* address) {
23 return reinterpret_cast<uintptr_t>(address) &
24 (api_constants::kCagedHeapReservationAlignment - 1);
25 }
26
27 V8_INLINE static bool IsWithinCage(const void* address) {
28 CPPGC_DCHECK(g_heap_base_);
29 return (reinterpret_cast<uintptr_t>(address) &
30 ~(api_constants::kCagedHeapReservationAlignment - 1)) ==
31 g_heap_base_;
32 }
33
34 V8_INLINE static bool AreWithinCage(const void* addr1, const void* addr2) {
35#if defined(CPPGC_POINTER_COMPRESSION)
36 static constexpr size_t kHeapBaseShift =
37 31 + api_constants::kPointerCompressionShift;
38#else // !defined(CPPGC_POINTER_COMPRESSION)
39 static constexpr size_t kHeapBaseShift = sizeof(uint32_t) * CHAR_BIT;
40#endif // !defined(CPPGC_POINTER_COMPRESSION)
41 static_assert((static_cast<size_t>(1) << kHeapBaseShift) ==
42 api_constants::kCagedHeapMaxReservationSize);
43 CPPGC_DCHECK(g_heap_base_);
44 return !(((reinterpret_cast<uintptr_t>(addr1) ^ g_heap_base_) |
45 (reinterpret_cast<uintptr_t>(addr2) ^ g_heap_base_)) >>
46 kHeapBaseShift);
47 }
48
49 V8_INLINE static uintptr_t GetBase() { return g_heap_base_; }
50 V8_INLINE static size_t GetAgeTableSize() { return g_age_table_size_; }
51
52 private:
53 friend class CagedHeap;
54
55 static uintptr_t g_heap_base_;
56 static size_t g_age_table_size_;
57};
58
59} // namespace internal
60} // namespace cppgc
61
62#endif // defined(CPPGC_CAGED_HEAP)
63
64#endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_
#define CPPGC_DCHECK(condition)
Definition: logging.h:36
Definition: allocation.h:38
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499