• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 COMMON_INTERFACES_OBJECTS_BASE_OBJECT_H
17 #define COMMON_INTERFACES_OBJECTS_BASE_OBJECT_H
18 
19 #include <stdint.h>
20 #include <stdio.h>
21 
22 #include "objects/base_class.h"
23 #include "base/common.h"
24 #include "objects/base_object_operator.h"
25 #include "objects/base_state_word.h"
26 
27 namespace common {
28 class BaseObject {
29 public:
BaseObject()30     BaseObject() : state_(0) {}
Cast(MAddress address)31     static BaseObject *Cast(MAddress address)
32     {
33         return reinterpret_cast<BaseObject *>(address);
34     }
35     static void RegisterDynamic(BaseObjectOperatorInterfaces *dynamicObjOp);
36     static void RegisterStatic(BaseObjectOperatorInterfaces *staticObjOp);
37 
GetSize()38     inline size_t GetSize() const
39     {
40         if (IsForwarded()) {
41             return GetSizeForwarded();
42         } else {
43             return GetOperator()->GetSize(this);
44         }
45     }
46 
IsValidObject()47     inline bool IsValidObject() const
48     {
49         return GetOperator()->IsValidObject(this);
50     }
51 
ForEachRefField(const RefFieldVisitor & visitor)52     void ForEachRefField(const RefFieldVisitor &visitor)
53     {
54         GetOperator()->ForEachRefField(this, visitor);
55     }
56 
GetForwardingPointer()57     inline BaseObject *GetForwardingPointer() const
58     {
59         if (IsForwarded()) {
60             return GetOperator()->GetForwardingPointer(this);
61         }
62         return nullptr;
63     }
64 
SetForwardingPointerAfterExclusive(BaseObject * fwdPtr)65     inline void SetForwardingPointerAfterExclusive(BaseObject *fwdPtr)
66     {
67         CHECK_CC(IsForwarding());
68         GetOperator()->SetForwardingPointerAfterExclusive(this, fwdPtr);
69         state_.SetForwarded();
70     }
71 
SetSizeForwarded(size_t size)72     inline void SetSizeForwarded(size_t size)
73     {
74         // Set size in old obj when forwarded, forwardee obj size may changed.
75         size_t objectHeaderSize = sizeof(BaseStateWord);
76         DCHECK_CC(size >= objectHeaderSize + sizeof(size_t));
77         auto addr = reinterpret_cast<MAddress>(this) + objectHeaderSize;
78         *reinterpret_cast<size_t*>(addr) = size;
79     }
80 
GetSizeForwarded()81     inline size_t GetSizeForwarded() const
82     {
83         auto addr = reinterpret_cast<MAddress>(this) + sizeof(BaseStateWord);
84         return *reinterpret_cast<size_t*>(addr);
85     }
86 
IsForwarding()87     inline bool IsForwarding() const
88     {
89         return state_.IsForwarding();
90     }
91 
IsForwarded()92     inline bool IsForwarded() const
93     {
94         return state_.IsForwarded();
95     }
96 
IsDynamic()97     ALWAYS_INLINE_CC bool IsDynamic() const
98     {
99         return state_.IsDynamic();
100     }
101 
IsStatic()102     ALWAYS_INLINE_CC bool IsStatic() const
103     {
104         return state_.IsStatic();
105     }
106 
IsToVersion()107     inline bool IsToVersion() const
108     {
109         return state_.IsToVersion();
110     }
111 
SetLanguageType(LanguageType language)112     inline void SetLanguageType(LanguageType language)
113     {
114         state_.SetLanguageType(language);
115     }
116 
GetBaseStateWord()117     BaseStateWord GetBaseStateWord() const
118     {
119         return state_.AtomicGetBaseStateWord();
120     }
121 
SetForwardState(BaseStateWord::ForwardState state)122     void SetForwardState(BaseStateWord::ForwardState state)
123     {
124         state_.SetForwardState(state);
125     }
126 
127     template <typename T>
GetField(uint32_t offset)128     Field<T> &GetField(uint32_t offset) const
129     {
130         auto addr = reinterpret_cast<MAddress>(this) + offset;
131         return *reinterpret_cast<Field<T> *>(addr);
132     }
133 
134     // Locking means that this object forwardstate is forwarding.
135     // Any other gc coping thread or mutator thread will be wait if copy the same object.
TryLockExclusive(const BaseStateWord state)136     bool TryLockExclusive(const BaseStateWord state)
137     {
138         return state_.TryLockBaseStateWord(state);
139     }
140 
UnlockExclusive(const BaseStateWord::ForwardState newState)141     void UnlockExclusive(const BaseStateWord::ForwardState newState)
142     {
143         state_.UnlockStateWord(newState);
144     }
145 
FieldOffset(BaseObject * object,const void * field)146     static inline intptr_t FieldOffset(BaseObject *object, const void *field)
147     {
148         return reinterpret_cast<intptr_t>(field) - reinterpret_cast<intptr_t>(object);
149     }
150 
151     // The interfaces following only use for common code compiler. It will be deleted later.
GetComponentTypeInfo()152     TypeInfo *GetComponentTypeInfo() const
153     {
154         return reinterpret_cast<TypeInfo *>(const_cast<BaseObject *>(this));
155     }
156 
HasRefField()157     inline bool HasRefField() const
158     {
159         return true;
160     }
161 
GetTypeInfo()162     inline TypeInfo *GetTypeInfo() const
163     {
164         return reinterpret_cast<TypeInfo *>(const_cast<BaseObject *>(this));
165     }
166 
IsRawArray()167     bool IsRawArray() const
168     {
169         return true;
170     }
171 
ForEachRefInStruct(const RefFieldVisitor & visitor,MAddress aggStart,MAddress aggEnd)172     void ForEachRefInStruct([[maybe_unused]] const RefFieldVisitor &visitor, [[maybe_unused]] MAddress aggStart,
173                             [[maybe_unused]] MAddress aggEnd)
174     {
175     }
176     // The interfaces above only use for common code compiler. It will be deleted later.
177 
SetFullBaseClassWithoutBarrier(BaseClass * cls)178     void SetFullBaseClassWithoutBarrier(BaseClass* cls)
179     {
180         state_ = 0;
181         state_.SetFullBaseClassAddress(reinterpret_cast<common::StateWordType>(cls));
182     }
183 
GetBaseClass()184     BaseClass *GetBaseClass() const
185     {
186         return reinterpret_cast<BaseClass *>(state_.GetBaseClassAddress());
187     }
188 
189     // Size of object header
BaseObjectSize()190     static constexpr size_t BaseObjectSize()
191     {
192         return sizeof(BaseObject);
193     }
194 protected:
SetClassInfo(MAddress address,TypeInfo * klass)195     static BaseObject *SetClassInfo(MAddress address, TypeInfo *klass)
196     {
197         auto ref = reinterpret_cast<BaseObject *>(address);
198         return ref;
199     }
200 
GetOperator()201     inline BaseObjectOperatorInterfaces *GetOperator() const
202     {
203         if (state_.IsStatic()) {
204             return operator_.staticObjOp_;
205         }
206         return operator_.dynamicObjOp_;
207     }
208 
209     static PUBLIC_API BaseObjectOperator operator_;
210     BaseStateWord state_;
211 };
212 }  // namespace common
213 #endif  // COMMON_INTERFACES_OBJECTS_BASE_OBJECT_H
214