1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_TAGGED_OBJECT_HEADER_H 17 #define ECMASCRIPT_TAGGED_OBJECT_HEADER_H 18 19 #include "common_interfaces/base/mem.h" 20 #include "common_interfaces/objects/base_object.h" 21 #include "ecmascript/mem/mark_word.h" 22 #include "ecmascript/mem/shared_heap/shared_value_helper.h" 23 #include "ecmascript/mem/tagged_state_word.h" 24 25 namespace panda::ecmascript { 26 class JSHClass; 27 template<typename T> 28 class JSHandle; 29 class JSThread; 30 31 using ::common::BaseObject; 32 using ::common::ToUintPtr; 33 using ::common::ToVoidPtr; 34 35 class TaggedObject : public BaseObject { 36 public: Cast(const BaseObject * header)37 static TaggedObject *Cast(const BaseObject *header) 38 { 39 return static_cast<TaggedObject *>(const_cast<BaseObject *>(header)); 40 } 41 Cast(TaggedObject * header)42 static TaggedObject *Cast(TaggedObject *header) 43 { 44 return static_cast<TaggedObject *>(header); 45 } 46 TaggedObject() = default; 47 48 void SynchronizedTransitionClass(const JSThread *thread, JSHClass *hclass); 49 void SetClassWithoutBarrier(JSHClass *hclass); 50 void SetFreeObjectClass(JSHClass *hclass); 51 void TransitionClassWithoutBarrier(JSHClass *hclass); 52 53 JSHClass *SynchronizedGetClass() const; SetForwardingPointerAfterExclusive(BaseObject * fwdPtr)54 void SetForwardingPointerAfterExclusive(BaseObject *fwdPtr) 55 { 56 reinterpret_cast<TaggedStateWord *>(this)->SetForwardingAddress(reinterpret_cast<uintptr_t>(fwdPtr)); 57 } 58 GetForwardingPointer()59 BaseObject *GetForwardingPointer() const 60 { 61 return reinterpret_cast<BaseObject *>(reinterpret_cast<const TaggedStateWord *>(this)->GetForwardingAddress()); 62 } 63 GetClass()64 JSHClass *GetClass() const 65 { 66 return reinterpret_cast<JSHClass *>(reinterpret_cast<const TaggedStateWord *>(this)->GetClass()); 67 } 68 69 size_t GetSize(); 70 71 bool IsInSharedHeap() const; 72 73 // Size of object header TaggedObjectSize()74 static constexpr size_t TaggedObjectSize() 75 { 76 return sizeof(TaggedObject); 77 } 78 79 static constexpr uint64_t GC_STATE_MASK = 0x0FFFFFFFFFFFFFFF; 80 static constexpr int HCLASS_OFFSET = 0; 81 static constexpr int SIZE = sizeof(TaggedStateWord); 82 83 private: 84 void SetClass(const JSThread *thread, JSHClass *hclass); 85 86 friend class BaseHeap; 87 friend class Heap; 88 friend class SharedHeap; 89 friend class ObjectFactory; 90 friend class EcmaString; 91 }; 92 static_assert(TaggedObject::TaggedObjectSize() == sizeof(TaggedStateWord)); 93 } // namespace panda::ecmascript 94 95 #endif // ECMASCRIPT_TAGGED_OBJECT_HEADER_H 96