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 <atomic>
17 #include <cstdint>
18
19 #include "objects/composite_base_class.h"
20 #include "objects/base_object.h"
21
22 namespace common {
InitializeCompositeBaseClass(CompositeBaseClassAllocator & allocator)23 void BaseClassRoots::InitializeCompositeBaseClass(CompositeBaseClassAllocator &allocator)
24 {
25 if (initialized_.exchange(true)) {
26 return;
27 }
28 CreateCompositeBaseClass(CommonType::LINE_STRING, allocator);
29 CreateCompositeBaseClass(CommonType::SLICED_STRING, allocator);
30 CreateCompositeBaseClass(CommonType::TREE_STRING, allocator);
31 }
32
CreateCompositeBaseClass(CommonType type,CompositeBaseClassAllocator & allocator)33 void BaseClassRoots::CreateCompositeBaseClass(CommonType type, CompositeBaseClassAllocator& allocator)
34 {
35 CompositeBaseClass* classObject = allocator();
36 classObject->class_.ClearBitField();
37 classObject->class_.SetObjectType(type);
38 size_t index = TypeToIndex[static_cast<size_t>(type)];
39 compositeBaseClasses_[index] = classObject;
40 baseClasses_[index] = &classObject->class_;
41 }
42
GetBaseClass(CommonType type) const43 BaseClass* BaseClassRoots::GetBaseClass(CommonType type) const
44 {
45 return baseClasses_[TypeToIndex[static_cast<size_t>(type)]];
46 }
47
IterateCompositeBaseClass(const RefFieldVisitor & visitorFunc)48 void BaseClassRoots::IterateCompositeBaseClass(const RefFieldVisitor& visitorFunc)
49 {
50 if (!initialized_) {
51 return;
52 }
53 for (auto& it : compositeBaseClasses_) {
54 visitorFunc(reinterpret_cast<RefField<>&>(it));
55 }
56 }
57
58 } // namespace panda
59