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 #include "runtime/mem/object_helpers.h"
17 #include "runtime/mem/object-references-iterator-inl.h"
18 #include "plugins/ets/runtime/hybrid/mem/static_object_operator.h"
19
20 namespace ark::mem {
21
22 StaticObjectOperator StaticObjectOperator::instance_;
23
24 class Handler {
25 public:
Handler(const panda::RefFieldVisitor & visitor)26 explicit Handler(const panda::RefFieldVisitor &visitor) : visitor_(visitor) {}
27
28 ~Handler() = default;
29
ProcessObjectPointer(ObjectPointerType * p)30 bool ProcessObjectPointer(ObjectPointerType *p)
31 {
32 if (*p != 0) {
33 visitor_(reinterpret_cast<panda::RefField<> &>(*reinterpret_cast<panda::BaseObject **>(p)));
34 }
35 return true;
36 }
37
38 private:
39 const panda::RefFieldVisitor &visitor_;
40 };
41
Initialize()42 void StaticObjectOperator::Initialize()
43 {
44 #if defined(ARK_HYBRID)
45 panda::BaseObject::RegisterStatic(&instance_);
46 #endif
47 }
48
ForEachRefField(const panda::BaseObject * object,const panda::RefFieldVisitor & visitor) const49 void StaticObjectOperator::ForEachRefField(const panda::BaseObject *object, const panda::RefFieldVisitor &visitor) const
50 {
51 Handler handler(visitor);
52 auto *objHeader = reinterpret_cast<ObjectHeader *>(const_cast<panda::BaseObject *>(object));
53 ObjectIterator<LANG_TYPE_STATIC>::template Iterate<false>(objHeader->ClassAddr<Class>(), objHeader, &handler);
54 }
55
GetForwardingPointer(const panda::BaseObject * object) const56 panda::BaseObject *StaticObjectOperator::GetForwardingPointer(const panda::BaseObject *object) const
57 {
58 // Overwrite class by forwarding address. Read barrier must be called before reading class.
59 uint64_t fwdAddr = *reinterpret_cast<const uint64_t *>(object);
60 return reinterpret_cast<panda::BaseObject *>(fwdAddr & ObjectHeader::GetClassMask());
61 }
62
SetForwardingPointerAfterExclusive(panda::BaseObject * object,panda::BaseObject * fwdPtr)63 void StaticObjectOperator::SetForwardingPointerAfterExclusive(panda::BaseObject *object, panda::BaseObject *fwdPtr)
64 {
65 auto &word = *reinterpret_cast<uint64_t *>(object);
66 uint64_t flags = word & (~ObjectHeader::GetClassMask());
67 word = flags | (reinterpret_cast<uint64_t>(fwdPtr) & ObjectHeader::GetClassMask());
68 }
69
70 } // namespace ark::mem
71