blob: d88b8e17105730dcaffb264d23287b6ce0f55af1 [file] [log] [blame]
Vladimir Marko5868ada2020-05-12 11:50:34 +01001/*
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
28namespace art {
29
30template <ReadBarrierOption kReadBarrierOption>
31inline 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
47template <ReadBarrierOption kReadBarrierOption>
48inline 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
53template <ReadBarrierOption kReadBarrierOption>
54inline ObjPtr<mirror::Class> GetClassRoot(ClassRoot class_root)
55 REQUIRES_SHARED(Locks::mutator_lock_) {
56 return GetClassRoot<kReadBarrierOption>(class_root, Runtime::Current()->GetClassLinker());
57}
58
59namespace detail {
60
61class ClassNotFoundExceptionTag;
62template <class Tag> struct NoMirrorType;
63
64template <class MirrorType>
65struct 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
73CLASS_ROOT_LIST(SPECIALIZE_CLASS_ROOT_SELECTOR)
74
75#undef SPECIALIZE_CLASS_ROOT_SELECTOR
76
77} // namespace detail
78
79template <class MirrorType, ReadBarrierOption kReadBarrierOption>
80inline 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
86template <class MirrorType, ReadBarrierOption kReadBarrierOption>
87inline ObjPtr<mirror::Class> GetClassRoot(ClassLinker* linker)
88 REQUIRES_SHARED(Locks::mutator_lock_) {
89 return GetClassRoot<kReadBarrierOption>(detail::ClassRootSelector<MirrorType>::value, linker);
90}
91
92template <class MirrorType, ReadBarrierOption kReadBarrierOption>
93inline 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_