1 /*
2  * Copyright (C) 2011 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_MIRROR_CLASS_LOADER_H_
18 #define ART_RUNTIME_MIRROR_CLASS_LOADER_H_
19 
20 #include "base/locks.h"
21 #include "obj_ptr.h"
22 #include "object.h"
23 #include "object_reference.h"
24 
25 namespace art {
26 
27 struct ClassLoaderOffsets;
28 class ClassTable;
29 class LinearAlloc;
30 
31 namespace mirror {
32 
33 class Class;
34 
35 // C++ mirror of java.lang.ClassLoader
36 class MANAGED ClassLoader : public Object {
37  public:
38   // Size of an instance of java.lang.ClassLoader.
InstanceSize()39   static constexpr uint32_t InstanceSize() {
40     return sizeof(ClassLoader);
41   }
42 
43   ObjPtr<ClassLoader> GetParent() REQUIRES_SHARED(Locks::mutator_lock_);
44 
45   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
GetClassTable()46   ClassTable* GetClassTable() REQUIRES_SHARED(Locks::mutator_lock_) {
47     return reinterpret_cast<ClassTable*>(
48         GetField64<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(ClassLoader, class_table_)));
49   }
50 
SetClassTable(ClassTable * class_table)51   void SetClassTable(ClassTable* class_table) REQUIRES_SHARED(Locks::mutator_lock_) {
52     SetField64<false>(OFFSET_OF_OBJECT_MEMBER(ClassLoader, class_table_),
53                       reinterpret_cast<uint64_t>(class_table));
54   }
55 
GetAllocator()56   LinearAlloc* GetAllocator() REQUIRES_SHARED(Locks::mutator_lock_) {
57     return reinterpret_cast<LinearAlloc*>(
58         GetField64(OFFSET_OF_OBJECT_MEMBER(ClassLoader, allocator_)));
59   }
60 
SetAllocator(LinearAlloc * allocator)61   void SetAllocator(LinearAlloc* allocator) REQUIRES_SHARED(Locks::mutator_lock_) {
62     SetField64<false>(OFFSET_OF_OBJECT_MEMBER(ClassLoader, allocator_),
63                       reinterpret_cast<uint64_t>(allocator));
64   }
65 
66  private:
67   // Visit instance fields of the class loader as well as its associated classes.
68   // Null class loader is handled by ClassLinker::VisitClassRoots.
69   template <bool kVisitClasses,
70             VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
71             ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
72             typename Visitor>
73   void VisitReferences(ObjPtr<Class> klass, const Visitor& visitor)
74       REQUIRES_SHARED(Locks::mutator_lock_)
75       REQUIRES(!Locks::classlinker_classes_lock_);
76 
77   // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
78   HeapReference<Object> packages_;
79   HeapReference<ClassLoader> parent_;
80   HeapReference<Object> proxyCache_;
81   // Native pointer to class table, need to zero this out when image writing.
82   uint32_t padding_ ATTRIBUTE_UNUSED;
83   uint64_t allocator_;
84   uint64_t class_table_;
85 
86   friend struct art::ClassLoaderOffsets;  // for verifying offset information
87   friend class Object;  // For VisitReferences
88   DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
89 };
90 
91 }  // namespace mirror
92 }  // namespace art
93 
94 #endif  // ART_RUNTIME_MIRROR_CLASS_LOADER_H_
95