1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_CLASS_ROOT_INL_H_
18 #define ART_RUNTIME_CLASS_ROOT_INL_H_
19 
20 #include "class_root.h"
21 
22 #include "class_linker-inl.h"
23 #include "mirror/class.h"
24 #include "mirror/object_array-inl.h"
25 #include "obj_ptr-inl.h"
26 #include "runtime.h"
27 
28 namespace art {
29 
30 template <ReadBarrierOption kReadBarrierOption>
GetClassRoot(ClassRoot class_root,ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots)31 inline ObjPtr<mirror::Class> GetClassRoot(ClassRoot class_root,
32                                           ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots) {
33   DCHECK(class_roots != nullptr);
34   if (kReadBarrierOption == kWithReadBarrier) {
35     // With read barrier all references must point to the to-space.
36     // Without read barrier, this check could fail.
37     DCHECK_EQ(class_roots, Runtime::Current()->GetClassLinker()->GetClassRoots());
38   }
39   DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax));
40   int32_t index = static_cast<int32_t>(class_root);
41   ObjPtr<mirror::Class> klass =
42       class_roots->GetWithoutChecks<kDefaultVerifyFlags, kReadBarrierOption>(index);
43   DCHECK(klass != nullptr);
44   return klass;
45 }
46 
47 template <ReadBarrierOption kReadBarrierOption>
GetClassRoot(ClassRoot class_root,ClassLinker * linker)48 inline ObjPtr<mirror::Class> GetClassRoot(ClassRoot class_root, ClassLinker* linker)
49     REQUIRES_SHARED(Locks::mutator_lock_) {
50   return GetClassRoot<kReadBarrierOption>(class_root, linker->GetClassRoots<kReadBarrierOption>());
51 }
52 
53 template <ReadBarrierOption kReadBarrierOption>
GetClassRoot(ClassRoot class_root)54 inline ObjPtr<mirror::Class> GetClassRoot(ClassRoot class_root)
55     REQUIRES_SHARED(Locks::mutator_lock_) {
56   return GetClassRoot<kReadBarrierOption>(class_root, Runtime::Current()->GetClassLinker());
57 }
58 
59 namespace detail {
60 
61 class ClassNotFoundExceptionTag;
62 template <class Tag> struct NoMirrorType;
63 
64 template <class MirrorType>
65 struct ClassRootSelector;  // No definition for unspecialized ClassRoot selector.
66 
67 #define SPECIALIZE_CLASS_ROOT_SELECTOR(name, descriptor, mirror_type) \
68   template <>                                                         \
69   struct ClassRootSelector<mirror_type> {                             \
70     static constexpr ClassRoot value = ClassRoot::name;               \
71   };
72 
73 CLASS_ROOT_LIST(SPECIALIZE_CLASS_ROOT_SELECTOR)
74 
75 #undef SPECIALIZE_CLASS_ROOT_SELECTOR
76 
77 }  // namespace detail
78 
79 template <class MirrorType, ReadBarrierOption kReadBarrierOption>
GetClassRoot(ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots)80 inline ObjPtr<mirror::Class> GetClassRoot(ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots)
81     REQUIRES_SHARED(Locks::mutator_lock_) {
82   return GetClassRoot<kReadBarrierOption>(detail::ClassRootSelector<MirrorType>::value,
83                                           class_roots);
84 }
85 
86 template <class MirrorType, ReadBarrierOption kReadBarrierOption>
GetClassRoot(ClassLinker * linker)87 inline ObjPtr<mirror::Class> GetClassRoot(ClassLinker* linker)
88     REQUIRES_SHARED(Locks::mutator_lock_) {
89   return GetClassRoot<kReadBarrierOption>(detail::ClassRootSelector<MirrorType>::value, linker);
90 }
91 
92 template <class MirrorType, ReadBarrierOption kReadBarrierOption>
GetClassRoot()93 inline ObjPtr<mirror::Class> GetClassRoot() REQUIRES_SHARED(Locks::mutator_lock_) {
94   return GetClassRoot<kReadBarrierOption>(detail::ClassRootSelector<MirrorType>::value);
95 }
96 
97 }  // namespace art
98 
99 #endif  // ART_RUNTIME_CLASS_ROOT_INL_H_
100