1 // Copyright 2021 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_HEAP_CPPGC_OBJECT_VIEW_H_ 6 #define V8_HEAP_CPPGC_OBJECT_VIEW_H_ 7 8 #include "include/v8config.h" 9 #include "src/heap/cppgc/globals.h" 10 #include "src/heap/cppgc/heap-object-header.h" 11 #include "src/heap/cppgc/heap-page.h" 12 13 namespace cppgc { 14 namespace internal { 15 16 // ObjectView allows accessing a header within the bounds of the actual object. 17 // It is not exposed externally and does not keep the underlying object alive. 18 template <AccessMode = AccessMode::kNonAtomic> 19 class ObjectView final { 20 public: 21 V8_INLINE explicit ObjectView(const HeapObjectHeader& header); 22 23 V8_INLINE Address Start() const; 24 V8_INLINE ConstAddress End() const; 25 V8_INLINE size_t Size() const; 26 27 private: 28 const HeapObjectHeader& header_; 29 const BasePage* base_page_; 30 const bool is_large_object_; 31 }; 32 33 template <AccessMode access_mode> ObjectView(const HeapObjectHeader & header)34ObjectView<access_mode>::ObjectView(const HeapObjectHeader& header) 35 : header_(header), 36 base_page_( 37 BasePage::FromPayload(const_cast<HeapObjectHeader*>(&header_))), 38 is_large_object_(header_.IsLargeObject<access_mode>()) { 39 DCHECK_EQ(Start() + Size(), End()); 40 } 41 42 template <AccessMode access_mode> Start()43Address ObjectView<access_mode>::Start() const { 44 return header_.ObjectStart(); 45 } 46 47 template <AccessMode access_mode> End()48ConstAddress ObjectView<access_mode>::End() const { 49 return is_large_object_ ? LargePage::From(base_page_)->PayloadEnd() 50 : header_.ObjectEnd(); 51 } 52 53 template <AccessMode access_mode> Size()54size_t ObjectView<access_mode>::Size() const { 55 return is_large_object_ ? LargePage::From(base_page_)->ObjectSize() 56 : header_.ObjectSize<access_mode>(); 57 } 58 59 } // namespace internal 60 } // namespace cppgc 61 62 #endif // V8_HEAP_CPPGC_OBJECT_VIEW_H_ 63