11cb0ef41Sopenharmony_ci// Copyright 2021 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 V8_HEAP_CPPGC_OBJECT_VIEW_H_
61cb0ef41Sopenharmony_ci#define V8_HEAP_CPPGC_OBJECT_VIEW_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "include/v8config.h"
91cb0ef41Sopenharmony_ci#include "src/heap/cppgc/globals.h"
101cb0ef41Sopenharmony_ci#include "src/heap/cppgc/heap-object-header.h"
111cb0ef41Sopenharmony_ci#include "src/heap/cppgc/heap-page.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace cppgc {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// ObjectView allows accessing a header within the bounds of the actual object.
171cb0ef41Sopenharmony_ci// It is not exposed externally and does not keep the underlying object alive.
181cb0ef41Sopenharmony_citemplate <AccessMode = AccessMode::kNonAtomic>
191cb0ef41Sopenharmony_ciclass ObjectView final {
201cb0ef41Sopenharmony_ci public:
211cb0ef41Sopenharmony_ci  V8_INLINE explicit ObjectView(const HeapObjectHeader& header);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  V8_INLINE Address Start() const;
241cb0ef41Sopenharmony_ci  V8_INLINE ConstAddress End() const;
251cb0ef41Sopenharmony_ci  V8_INLINE size_t Size() const;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci private:
281cb0ef41Sopenharmony_ci  const HeapObjectHeader& header_;
291cb0ef41Sopenharmony_ci  const BasePage* base_page_;
301cb0ef41Sopenharmony_ci  const bool is_large_object_;
311cb0ef41Sopenharmony_ci};
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_citemplate <AccessMode access_mode>
341cb0ef41Sopenharmony_ciObjectView<access_mode>::ObjectView(const HeapObjectHeader& header)
351cb0ef41Sopenharmony_ci    : header_(header),
361cb0ef41Sopenharmony_ci      base_page_(
371cb0ef41Sopenharmony_ci          BasePage::FromPayload(const_cast<HeapObjectHeader*>(&header_))),
381cb0ef41Sopenharmony_ci      is_large_object_(header_.IsLargeObject<access_mode>()) {
391cb0ef41Sopenharmony_ci  DCHECK_EQ(Start() + Size(), End());
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_citemplate <AccessMode access_mode>
431cb0ef41Sopenharmony_ciAddress ObjectView<access_mode>::Start() const {
441cb0ef41Sopenharmony_ci  return header_.ObjectStart();
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_citemplate <AccessMode access_mode>
481cb0ef41Sopenharmony_ciConstAddress ObjectView<access_mode>::End() const {
491cb0ef41Sopenharmony_ci  return is_large_object_ ? LargePage::From(base_page_)->PayloadEnd()
501cb0ef41Sopenharmony_ci                          : header_.ObjectEnd();
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_citemplate <AccessMode access_mode>
541cb0ef41Sopenharmony_cisize_t ObjectView<access_mode>::Size() const {
551cb0ef41Sopenharmony_ci  return is_large_object_ ? LargePage::From(base_page_)->ObjectSize()
561cb0ef41Sopenharmony_ci                          : header_.ObjectSize<access_mode>();
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci}  // namespace internal
601cb0ef41Sopenharmony_ci}  // namespace cppgc
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci#endif  // V8_HEAP_CPPGC_OBJECT_VIEW_H_
63