11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <array>
91cb0ef41Sopenharmony_ci#include <cstddef>
101cb0ef41Sopenharmony_ci#include <cstdint>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "cppgc/internal/api-constants.h"
131cb0ef41Sopenharmony_ci#include "cppgc/internal/logging.h"
141cb0ef41Sopenharmony_ci#include "cppgc/platform.h"
151cb0ef41Sopenharmony_ci#include "v8config.h"  // NOLINT(build/include_directory)
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace cppgc {
181cb0ef41Sopenharmony_cinamespace internal {
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciclass HeapBase;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#if defined(CPPGC_YOUNG_GENERATION)
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// AgeTable is the bytemap needed for the fast generation check in the write
251cb0ef41Sopenharmony_ci// barrier. AgeTable contains entries that correspond to 512 bytes memory
261cb0ef41Sopenharmony_ci// regions (cards). Each entry in the table represents generation of the objects
271cb0ef41Sopenharmony_ci// that reside on the corresponding card (young, old or mixed).
281cb0ef41Sopenharmony_ciclass AgeTable final {
291cb0ef41Sopenharmony_ci  static constexpr size_t kRequiredSize = 1 * api_constants::kMB;
301cb0ef41Sopenharmony_ci  static constexpr size_t kAllocationGranularity =
311cb0ef41Sopenharmony_ci      api_constants::kAllocationGranularity;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci public:
341cb0ef41Sopenharmony_ci  enum class Age : uint8_t { kOld, kYoung, kMixed };
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  static constexpr size_t kCardSizeInBytes =
371cb0ef41Sopenharmony_ci      (api_constants::kCagedHeapReservationSize / kAllocationGranularity) /
381cb0ef41Sopenharmony_ci      kRequiredSize;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  void SetAge(uintptr_t cage_offset, Age age) {
411cb0ef41Sopenharmony_ci    table_[card(cage_offset)] = age;
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  V8_INLINE Age GetAge(uintptr_t cage_offset) const {
441cb0ef41Sopenharmony_ci    return table_[card(cage_offset)];
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  void Reset(PageAllocator* allocator);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci private:
501cb0ef41Sopenharmony_ci  V8_INLINE size_t card(uintptr_t offset) const {
511cb0ef41Sopenharmony_ci    constexpr size_t kGranularityBits =
521cb0ef41Sopenharmony_ci        __builtin_ctz(static_cast<uint32_t>(kCardSizeInBytes));
531cb0ef41Sopenharmony_ci    const size_t entry = offset >> kGranularityBits;
541cb0ef41Sopenharmony_ci    CPPGC_DCHECK(table_.size() > entry);
551cb0ef41Sopenharmony_ci    return entry;
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  std::array<Age, kRequiredSize> table_;
591cb0ef41Sopenharmony_ci};
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cistatic_assert(sizeof(AgeTable) == 1 * api_constants::kMB,
621cb0ef41Sopenharmony_ci              "Size of AgeTable is 1MB");
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci#endif  // CPPGC_YOUNG_GENERATION
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_cistruct CagedHeapLocalData final {
671cb0ef41Sopenharmony_ci  CagedHeapLocalData(HeapBase&, PageAllocator&);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  bool is_incremental_marking_in_progress = false;
701cb0ef41Sopenharmony_ci  HeapBase& heap_base;
711cb0ef41Sopenharmony_ci#if defined(CPPGC_YOUNG_GENERATION)
721cb0ef41Sopenharmony_ci  AgeTable age_table;
731cb0ef41Sopenharmony_ci#endif
741cb0ef41Sopenharmony_ci};
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci}  // namespace internal
771cb0ef41Sopenharmony_ci}  // namespace cppgc
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
80