• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_RUNTIME_CORETYPES_CLASS_H_
16 #define PANDA_RUNTIME_CORETYPES_CLASS_H_
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 
22 #include "libpandabase/utils/utf.h"
23 #include "runtime/include/class.h"
24 #include "runtime/include/object_header.h"
25 
26 namespace ark::coretypes {
27 
28 class Class : public ObjectHeader {
29 public:
Class(const uint8_t * descriptor,uint32_t vtableSize,uint32_t imtSize,uint32_t klassSize)30     Class(const uint8_t *descriptor, uint32_t vtableSize, uint32_t imtSize, uint32_t klassSize)
31         : ObjectHeader(), klass_(descriptor, panda_file::SourceLang::PANDA_ASSEMBLY, vtableSize, imtSize, klassSize)
32     {
33     }
34 
35     // We shouldn't init header_ here - because it has been memset(0) in object allocation,
36     // otherwise it may cause data race while visiting object's class concurrently in gc.
InitClass(const uint8_t * descriptor,uint32_t vtableSize,uint32_t imtSize,uint32_t klassSize)37     void InitClass(const uint8_t *descriptor, uint32_t vtableSize, uint32_t imtSize, uint32_t klassSize)
38     {
39         new (&klass_) ark::Class(descriptor, panda_file::SourceLang::PANDA_ASSEMBLY, vtableSize, imtSize, klassSize);
40     }
41 
GetRuntimeClass()42     ark::Class *GetRuntimeClass()
43     {
44         return &klass_;
45     }
46 
GetRuntimeClass()47     const ark::Class *GetRuntimeClass() const
48     {
49         return &klass_;
50     }
51 
52     template <class T>
GetFieldPrimitive(const Field & field)53     T GetFieldPrimitive(const Field &field) const
54     {
55         return klass_.GetFieldPrimitive<T>(field);
56     }
57 
58     template <class T>
SetFieldPrimitive(const Field & field,T value)59     void SetFieldPrimitive(const Field &field, T value)
60     {
61         klass_.SetFieldPrimitive(field, value);
62     }
63 
64     template <bool NEED_READ_BARRIER = true>
GetFieldObject(const Field & field)65     ObjectHeader *GetFieldObject(const Field &field) const
66     {
67         return klass_.GetFieldObject<NEED_READ_BARRIER>(field);
68     }
69 
70     template <bool NEED_WRITE_BARRIER = true>
SetFieldObject(const Field & field,ObjectHeader * value)71     void SetFieldObject(const Field &field, ObjectHeader *value)
72     {
73         klass_.SetFieldObject<NEED_WRITE_BARRIER>(field, value);
74     }
75 
GetSize(uint32_t klassSize)76     static size_t GetSize(uint32_t klassSize)
77     {
78         return GetRuntimeClassOffset() + klassSize;
79     }
80 
GetRuntimeClassOffset()81     static constexpr size_t GetRuntimeClassOffset()
82     {
83         return MEMBER_OFFSET(Class, klass_);
84     }
85 
FromRuntimeClass(ark::Class * klass)86     static Class *FromRuntimeClass(ark::Class *klass)
87     {
88         return reinterpret_cast<Class *>(reinterpret_cast<uintptr_t>(klass) - GetRuntimeClassOffset());
89     }
90 
91 private:
92     ark::Class klass_;
93 };
94 
95 // Klass field has variable size so it must be last
96 static_assert(Class::GetRuntimeClassOffset() + sizeof(ark::Class) == sizeof(Class));
97 
98 }  // namespace ark::coretypes
99 
100 #endif  // PANDA_RUNTIME_CORETYPES_CLASS_H_
101