Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_TABLE_H_ |
| 18 | #define ART_RUNTIME_CLASS_TABLE_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "base/allocator.h" |
| 25 | #include "base/hash_set.h" |
| 26 | #include "base/macros.h" |
| 27 | #include "base/mutex.h" |
| 28 | #include "dex_file.h" |
| 29 | #include "gc_root.h" |
| 30 | #include "object_callbacks.h" |
| 31 | #include "runtime.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | namespace mirror { |
| 36 | class ClassLoader; |
| 37 | } // namespace mirror |
| 38 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 39 | class ClassVisitor { |
| 40 | public: |
| 41 | virtual ~ClassVisitor() {} |
| 42 | // Return true to continue visiting. |
| 43 | virtual bool Visit(mirror::Class* klass) = 0; |
| 44 | }; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 45 | |
| 46 | // Each loader has a ClassTable |
| 47 | class ClassTable { |
| 48 | public: |
| 49 | ClassTable(); |
| 50 | |
| 51 | // Used by image writer for checking. |
| 52 | bool Contains(mirror::Class* klass) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 53 | REQUIRES(Locks::classlinker_classes_lock_) |
| 54 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 55 | |
| 56 | // Freeze the current class tables by allocating a new table and never updating or modifying the |
| 57 | // existing table. This helps prevents dirty pages after caused by inserting after zygote fork. |
| 58 | void FreezeSnapshot() |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 59 | REQUIRES(Locks::classlinker_classes_lock_) |
| 60 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 61 | |
| 62 | // Returns the number of classes in previous snapshots. |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 63 | size_t NumZygoteClasses() const SHARED_REQUIRES(Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 64 | |
| 65 | // Returns all off the classes in the lastest snapshot. |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 66 | size_t NumNonZygoteClasses() const SHARED_REQUIRES(Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 67 | |
| 68 | // Update a class in the table with the new class. Returns the existing class which was replaced. |
| 69 | mirror::Class* UpdateClass(const char* descriptor, mirror::Class* new_klass, size_t hash) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 70 | REQUIRES(Locks::classlinker_classes_lock_) |
| 71 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 72 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 73 | // NO_THREAD_SAFETY_ANALYSIS for object marking requiring heap bitmap lock. |
| 74 | template<class Visitor> |
| 75 | void VisitRoots(Visitor& visitor) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 76 | NO_THREAD_SAFETY_ANALYSIS |
| 77 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 78 | template<class Visitor> |
| 79 | void VisitRoots(const Visitor& visitor) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 80 | NO_THREAD_SAFETY_ANALYSIS |
| 81 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 82 | |
| 83 | // Return false if the callback told us to exit. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 84 | bool Visit(ClassVisitor* visitor) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 85 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 86 | |
| 87 | mirror::Class* Lookup(const char* descriptor, size_t hash) |
| 88 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_); |
| 89 | |
| 90 | void Insert(mirror::Class* klass) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 91 | REQUIRES(Locks::classlinker_classes_lock_) |
| 92 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 93 | void InsertWithHash(mirror::Class* klass, size_t hash) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 94 | REQUIRES(Locks::classlinker_classes_lock_) |
| 95 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 96 | |
| 97 | // Returns true if the class was found and removed, false otherwise. |
| 98 | bool Remove(const char* descriptor) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 99 | REQUIRES(Locks::classlinker_classes_lock_) |
| 100 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 101 | |
| 102 | // Return true if we inserted the dex file, false if it already exists. |
| 103 | bool InsertDexFile(mirror::Object* dex_file) |
| 104 | REQUIRES(Locks::classlinker_classes_lock_) |
| 105 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 106 | |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 107 | // Combines all of the tables into one class set. |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 108 | size_t WriteToMemory(uint8_t* ptr) const |
| 109 | REQUIRES(Locks::classlinker_classes_lock_) |
| 110 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 111 | size_t ReadFromMemory(uint8_t* ptr) |
| 112 | REQUIRES(Locks::classlinker_classes_lock_) |
| 113 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 114 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 115 | private: |
| 116 | class ClassDescriptorHashEquals { |
| 117 | public: |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 118 | // uint32_t for cross compilation. |
| 119 | uint32_t operator()(const GcRoot<mirror::Class>& root) const NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 120 | // Same class loader and descriptor. |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 121 | bool operator()(const GcRoot<mirror::Class>& a, const GcRoot<mirror::Class>& b) const |
| 122 | NO_THREAD_SAFETY_ANALYSIS;; |
| 123 | // Same descriptor. |
| 124 | bool operator()(const GcRoot<mirror::Class>& a, const char* descriptor) const |
| 125 | NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 126 | // uint32_t for cross compilation. |
| 127 | uint32_t operator()(const char* descriptor) const NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 128 | }; |
| 129 | class GcRootEmptyFn { |
| 130 | public: |
| 131 | void MakeEmpty(GcRoot<mirror::Class>& item) const { |
| 132 | item = GcRoot<mirror::Class>(); |
| 133 | } |
| 134 | bool IsEmpty(const GcRoot<mirror::Class>& item) const { |
| 135 | return item.IsNull(); |
| 136 | } |
| 137 | }; |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 138 | // hash set which hashes class descriptor, and compares descriptors and class loaders. Results |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 139 | // should be compared for a matching Class descriptor and class loader. |
| 140 | typedef HashSet<GcRoot<mirror::Class>, GcRootEmptyFn, ClassDescriptorHashEquals, |
| 141 | ClassDescriptorHashEquals, TrackingAllocator<GcRoot<mirror::Class>, kAllocatorTagClassTable>> |
| 142 | ClassSet; |
| 143 | |
| 144 | // TODO: shard lock to have one per class loader. |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 145 | // We have a vector to help prevent dirty pages after the zygote forks by calling FreezeSnapshot. |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 146 | std::vector<ClassSet> classes_ GUARDED_BY(Locks::classlinker_classes_lock_); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 147 | // Dex files used by the class loader which may not be owned by the class loader. We keep these |
| 148 | // live so that we do not have issues closing any of the dex files. |
| 149 | std::vector<GcRoot<mirror::Object>> dex_files_ GUARDED_BY(Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | } // namespace art |
| 153 | |
| 154 | #endif // ART_RUNTIME_CLASS_TABLE_H_ |