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