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