Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/node/cppgc/internal/caged-heap-local-data.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright 2020 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_LOCAL_DATA_H_
0006 #define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
0007 
0008 #include <array>
0009 #include <cstddef>
0010 #include <cstdint>
0011 
0012 #include "cppgc/internal/api-constants.h"
0013 #include "cppgc/internal/caged-heap.h"
0014 #include "cppgc/internal/logging.h"
0015 #include "cppgc/platform.h"
0016 #include "v8config.h"  // NOLINT(build/include_directory)
0017 
0018 #if __cpp_lib_bitopts
0019 #include <bit>
0020 #endif  // __cpp_lib_bitopts
0021 
0022 #if defined(CPPGC_CAGED_HEAP)
0023 
0024 namespace cppgc {
0025 namespace internal {
0026 
0027 class HeapBase;
0028 class HeapBaseHandle;
0029 
0030 #if defined(CPPGC_YOUNG_GENERATION)
0031 
0032 // AgeTable is the bytemap needed for the fast generation check in the write
0033 // barrier. AgeTable contains entries that correspond to 4096 bytes memory
0034 // regions (cards). Each entry in the table represents generation of the objects
0035 // that reside on the corresponding card (young, old or mixed).
0036 class V8_EXPORT AgeTable final {
0037   static constexpr size_t kRequiredSize = 1 * api_constants::kMB;
0038   static constexpr size_t kAllocationGranularity =
0039       api_constants::kAllocationGranularity;
0040 
0041  public:
0042   // Represents age of the objects living on a single card.
0043   enum class Age : uint8_t { kOld, kYoung, kMixed };
0044   // When setting age for a range, consider or ignore ages of the adjacent
0045   // cards.
0046   enum class AdjacentCardsPolicy : uint8_t { kConsider, kIgnore };
0047 
0048   static constexpr size_t kCardSizeInBytes =
0049       api_constants::kCagedHeapDefaultReservationSize / kRequiredSize;
0050 
0051   static constexpr size_t CalculateAgeTableSizeForHeapSize(size_t heap_size) {
0052     return heap_size / kCardSizeInBytes;
0053   }
0054 
0055   void SetAge(uintptr_t cage_offset, Age age) {
0056     table_[card(cage_offset)] = age;
0057   }
0058 
0059   V8_INLINE Age GetAge(uintptr_t cage_offset) const {
0060     return table_[card(cage_offset)];
0061   }
0062 
0063   void SetAgeForRange(uintptr_t cage_offset_begin, uintptr_t cage_offset_end,
0064                       Age age, AdjacentCardsPolicy adjacent_cards_policy);
0065 
0066   Age GetAgeForRange(uintptr_t cage_offset_begin,
0067                      uintptr_t cage_offset_end) const;
0068 
0069   void ResetForTesting();
0070 
0071  private:
0072   V8_INLINE size_t card(uintptr_t offset) const {
0073     constexpr size_t kGranularityBits =
0074 #if __cpp_lib_bitopts
0075         std::countr_zero(static_cast<uint32_t>(kCardSizeInBytes));
0076 #elif V8_HAS_BUILTIN_CTZ
0077         __builtin_ctz(static_cast<uint32_t>(kCardSizeInBytes));
0078 #else   //! V8_HAS_BUILTIN_CTZ
0079         // Hardcode and check with assert.
0080 #if defined(CPPGC_2GB_CAGE)
0081         11;
0082 #else   // !defined(CPPGC_2GB_CAGE)
0083         12;
0084 #endif  // !defined(CPPGC_2GB_CAGE)
0085 #endif  // !V8_HAS_BUILTIN_CTZ
0086     static_assert((1 << kGranularityBits) == kCardSizeInBytes);
0087     const size_t entry = offset >> kGranularityBits;
0088     CPPGC_DCHECK(CagedHeapBase::GetAgeTableSize() > entry);
0089     return entry;
0090   }
0091 
0092 #if defined(V8_CC_GNU)
0093   // gcc disallows flexible arrays in otherwise empty classes.
0094   Age table_[0];
0095 #else   // !defined(V8_CC_GNU)
0096   Age table_[];
0097 #endif  // !defined(V8_CC_GNU)
0098 };
0099 
0100 #endif  // CPPGC_YOUNG_GENERATION
0101 
0102 struct CagedHeapLocalData final {
0103   V8_INLINE static CagedHeapLocalData& Get() {
0104     return *reinterpret_cast<CagedHeapLocalData*>(CagedHeapBase::GetBase());
0105   }
0106 
0107   static constexpr size_t CalculateLocalDataSizeForHeapSize(size_t heap_size) {
0108     return AgeTable::CalculateAgeTableSizeForHeapSize(heap_size);
0109   }
0110 
0111 #if defined(CPPGC_YOUNG_GENERATION)
0112   AgeTable age_table;
0113 #endif
0114 };
0115 
0116 }  // namespace internal
0117 }  // namespace cppgc
0118 
0119 #endif  // defined(CPPGC_CAGED_HEAP)
0120 
0121 #endif  // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_