Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-10 10:14:58

0001 // Copyright 2022 the V8 project authors. All rights reserved.
0002 // Use of this source code is governed by a BSD-style license that can be
0003 // found in the LICENSE file.
0004 
0005 #ifndef INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_
0006 #define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_
0007 
0008 #include <climits>
0009 #include <cstddef>
0010 
0011 #include "cppgc/internal/api-constants.h"
0012 #include "cppgc/internal/base-page-handle.h"
0013 #include "v8config.h"  // NOLINT(build/include_directory)
0014 
0015 #if defined(CPPGC_CAGED_HEAP)
0016 
0017 namespace cppgc {
0018 namespace internal {
0019 
0020 class V8_EXPORT CagedHeapBase {
0021  public:
0022   V8_INLINE static uintptr_t OffsetFromAddress(const void* address) {
0023     return reinterpret_cast<uintptr_t>(address) &
0024            (api_constants::kCagedHeapReservationAlignment - 1);
0025   }
0026 
0027   V8_INLINE static bool IsWithinCage(const void* address) {
0028     CPPGC_DCHECK(g_heap_base_);
0029     return (reinterpret_cast<uintptr_t>(address) &
0030             ~(api_constants::kCagedHeapReservationAlignment - 1)) ==
0031            g_heap_base_;
0032   }
0033 
0034   V8_INLINE static bool AreWithinCage(const void* addr1, const void* addr2) {
0035 #if defined(CPPGC_POINTER_COMPRESSION)
0036     static constexpr size_t kHeapBaseShift =
0037         31 + api_constants::kPointerCompressionShift;
0038 #else   // !defined(CPPGC_POINTER_COMPRESSION)
0039     static constexpr size_t kHeapBaseShift = sizeof(uint32_t) * CHAR_BIT;
0040 #endif  // !defined(CPPGC_POINTER_COMPRESSION)
0041     static_assert((static_cast<size_t>(1) << kHeapBaseShift) ==
0042                   api_constants::kCagedHeapMaxReservationSize);
0043     CPPGC_DCHECK(g_heap_base_);
0044     return !(((reinterpret_cast<uintptr_t>(addr1) ^ g_heap_base_) |
0045               (reinterpret_cast<uintptr_t>(addr2) ^ g_heap_base_)) >>
0046              kHeapBaseShift);
0047   }
0048 
0049   V8_INLINE static uintptr_t GetBase() { return g_heap_base_; }
0050   V8_INLINE static size_t GetAgeTableSize() { return g_age_table_size_; }
0051 
0052  private:
0053   friend class CagedHeap;
0054 
0055   static uintptr_t g_heap_base_;
0056   static size_t g_age_table_size_;
0057 };
0058 
0059 }  // namespace internal
0060 }  // namespace cppgc
0061 
0062 #endif  // defined(CPPGC_CAGED_HEAP)
0063 
0064 #endif  // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_