11cb0ef41Sopenharmony_ci// Copyright 2022 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_BASE_PAGE_HANDLE_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_INTERNAL_BASE_PAGE_HANDLE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "cppgc/heap-handle.h"
91cb0ef41Sopenharmony_ci#include "cppgc/internal/api-constants.h"
101cb0ef41Sopenharmony_ci#include "cppgc/internal/logging.h"
111cb0ef41Sopenharmony_ci#include "v8config.h"  // NOLINT(build/include_directory)
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace cppgc {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// The class is needed in the header to allow for fast access to HeapHandle in
171cb0ef41Sopenharmony_ci// the write barrier.
181cb0ef41Sopenharmony_ciclass BasePageHandle {
191cb0ef41Sopenharmony_ci public:
201cb0ef41Sopenharmony_ci  static V8_INLINE BasePageHandle* FromPayload(void* payload) {
211cb0ef41Sopenharmony_ci    return reinterpret_cast<BasePageHandle*>(
221cb0ef41Sopenharmony_ci        (reinterpret_cast<uintptr_t>(payload) &
231cb0ef41Sopenharmony_ci         ~(api_constants::kPageSize - 1)) +
241cb0ef41Sopenharmony_ci        api_constants::kGuardPageSize);
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci  static V8_INLINE const BasePageHandle* FromPayload(const void* payload) {
271cb0ef41Sopenharmony_ci    return FromPayload(const_cast<void*>(payload));
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  HeapHandle& heap_handle() { return heap_handle_; }
311cb0ef41Sopenharmony_ci  const HeapHandle& heap_handle() const { return heap_handle_; }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci protected:
341cb0ef41Sopenharmony_ci  explicit BasePageHandle(HeapHandle& heap_handle) : heap_handle_(heap_handle) {
351cb0ef41Sopenharmony_ci    CPPGC_DCHECK(reinterpret_cast<uintptr_t>(this) % api_constants::kPageSize ==
361cb0ef41Sopenharmony_ci                 api_constants::kGuardPageSize);
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  HeapHandle& heap_handle_;
401cb0ef41Sopenharmony_ci};
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci}  // namespace internal
431cb0ef41Sopenharmony_ci}  // namespace cppgc
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_INTERNAL_BASE_PAGE_HANDLE_H_
46