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#include "include/cppgc/internal/caged-heap-local-data.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include <algorithm>
81cb0ef41Sopenharmony_ci#include <type_traits>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "include/cppgc/platform.h"
111cb0ef41Sopenharmony_ci#include "src/base/macros.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace cppgc {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciCagedHeapLocalData::CagedHeapLocalData(HeapBase& heap_base,
171cb0ef41Sopenharmony_ci                                       PageAllocator& allocator)
181cb0ef41Sopenharmony_ci    : heap_base(heap_base) {
191cb0ef41Sopenharmony_ci#if defined(CPPGC_YOUNG_GENERATION)
201cb0ef41Sopenharmony_ci  age_table.Reset(&allocator);
211cb0ef41Sopenharmony_ci#endif  // defined(CPPGC_YOUNG_GENERATION)
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci#if defined(CPPGC_YOUNG_GENERATION)
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cistatic_assert(
271cb0ef41Sopenharmony_ci    std::is_trivially_default_constructible<AgeTable>::value,
281cb0ef41Sopenharmony_ci    "To support lazy committing, AgeTable must be trivially constructible");
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_civoid AgeTable::Reset(PageAllocator* allocator) {
311cb0ef41Sopenharmony_ci  // TODO(chromium:1029379): Consider MADV_DONTNEED instead of MADV_FREE on
321cb0ef41Sopenharmony_ci  // POSIX platforms.
331cb0ef41Sopenharmony_ci  std::fill(table_.begin(), table_.end(), Age::kOld);
341cb0ef41Sopenharmony_ci  const uintptr_t begin = RoundUp(reinterpret_cast<uintptr_t>(table_.data()),
351cb0ef41Sopenharmony_ci                                  allocator->CommitPageSize());
361cb0ef41Sopenharmony_ci  const uintptr_t end =
371cb0ef41Sopenharmony_ci      RoundDown(reinterpret_cast<uintptr_t>(table_.data() + table_.size()),
381cb0ef41Sopenharmony_ci                allocator->CommitPageSize());
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  allocator->DiscardSystemPages(reinterpret_cast<void*>(begin), end - begin);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci#endif  // defined(CPPGC_YOUNG_GENERATION)
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci}  // namespace internal
461cb0ef41Sopenharmony_ci}  // namespace cppgc
47