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 | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 39 | // Each loader has a ClassTable |
| 40 | class ClassTable { |
| 41 | public: |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 42 | class ClassDescriptorHashEquals { |
| 43 | public: |
| 44 | // uint32_t for cross compilation. |
| 45 | uint32_t operator()(const GcRoot<mirror::Class>& root) const NO_THREAD_SAFETY_ANALYSIS; |
| 46 | // Same class loader and descriptor. |
| 47 | bool operator()(const GcRoot<mirror::Class>& a, const GcRoot<mirror::Class>& b) const |
| 48 | NO_THREAD_SAFETY_ANALYSIS;; |
| 49 | // Same descriptor. |
| 50 | bool operator()(const GcRoot<mirror::Class>& a, const char* descriptor) const |
| 51 | NO_THREAD_SAFETY_ANALYSIS; |
| 52 | // uint32_t for cross compilation. |
| 53 | uint32_t operator()(const char* descriptor) const NO_THREAD_SAFETY_ANALYSIS; |
| 54 | }; |
| 55 | class GcRootEmptyFn { |
| 56 | public: |
| 57 | void MakeEmpty(GcRoot<mirror::Class>& item) const { |
| 58 | item = GcRoot<mirror::Class>(); |
| 59 | } |
| 60 | bool IsEmpty(const GcRoot<mirror::Class>& item) const { |
| 61 | return item.IsNull(); |
| 62 | } |
| 63 | }; |
| 64 | // hash set which hashes class descriptor, and compares descriptors and class loaders. Results |
| 65 | // should be compared for a matching Class descriptor and class loader. |
| 66 | typedef HashSet<GcRoot<mirror::Class>, GcRootEmptyFn, ClassDescriptorHashEquals, |
| 67 | ClassDescriptorHashEquals, TrackingAllocator<GcRoot<mirror::Class>, kAllocatorTagClassTable>> |
| 68 | ClassSet; |
| 69 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 70 | ClassTable(); |
| 71 | |
| 72 | // Used by image writer for checking. |
| 73 | bool Contains(mirror::Class* klass) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 74 | REQUIRES(!lock_) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 75 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 76 | |
| 77 | // Freeze the current class tables by allocating a new table and never updating or modifying the |
| 78 | // existing table. This helps prevents dirty pages after caused by inserting after zygote fork. |
| 79 | void FreezeSnapshot() |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 80 | REQUIRES(!lock_) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 81 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 82 | |
| 83 | // Returns the number of classes in previous snapshots. |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 84 | size_t NumZygoteClasses() const REQUIRES(!lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 85 | |
| 86 | // Returns all off the classes in the lastest snapshot. |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 87 | size_t NumNonZygoteClasses() const REQUIRES(!lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 88 | |
| 89 | // Update a class in the table with the new class. Returns the existing class which was replaced. |
| 90 | mirror::Class* UpdateClass(const char* descriptor, mirror::Class* new_klass, size_t hash) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 91 | REQUIRES(!lock_) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 92 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 93 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 94 | // NO_THREAD_SAFETY_ANALYSIS for object marking requiring heap bitmap lock. |
| 95 | template<class Visitor> |
| 96 | void VisitRoots(Visitor& visitor) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 97 | NO_THREAD_SAFETY_ANALYSIS |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 98 | REQUIRES(!lock_) |
| 99 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 100 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 101 | template<class Visitor> |
| 102 | void VisitRoots(const Visitor& visitor) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 103 | NO_THREAD_SAFETY_ANALYSIS |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 104 | REQUIRES(!lock_) |
| 105 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 106 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 107 | // Stops visit if the visitor returns false. |
| 108 | template <typename Visitor> |
| 109 | bool Visit(Visitor& visitor) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 110 | REQUIRES(!lock_) |
| 111 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 112 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 113 | // Return the first class that matches the descriptor. Returns null if there are none. |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 114 | mirror::Class* Lookup(const char* descriptor, size_t hash) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 115 | REQUIRES(!lock_) |
| 116 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 117 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 118 | // Return the first class that matches the descriptor of klass. Returns null if there are none. |
| 119 | mirror::Class* LookupByDescriptor(mirror::Class* klass) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 120 | REQUIRES(!lock_) |
| 121 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 122 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 123 | void Insert(mirror::Class* klass) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 124 | REQUIRES(!lock_) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 125 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 126 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 127 | void InsertWithHash(mirror::Class* klass, size_t hash) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 128 | REQUIRES(!lock_) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 129 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 130 | |
| 131 | // Returns true if the class was found and removed, false otherwise. |
| 132 | bool Remove(const char* descriptor) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 133 | REQUIRES(!lock_) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 134 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 135 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 136 | // Return true if we inserted the strong root, false if it already exists. |
| 137 | bool InsertStrongRoot(mirror::Object* obj) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 138 | REQUIRES(!lock_) |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 139 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 140 | |
Mathieu Chartier | 41dc8ce | 2015-12-04 15:07:48 -0800 | [diff] [blame] | 141 | // Combines all of the tables into one class set. |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 142 | size_t WriteToMemory(uint8_t* ptr) const |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 143 | REQUIRES(!lock_) |
| 144 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 145 | |
| 146 | // Read a table from ptr and put it at the front of the class set. |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 147 | size_t ReadFromMemory(uint8_t* ptr) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 148 | REQUIRES(!lock_) |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 149 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 150 | |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 151 | // Add a class set to the front of classes. |
| 152 | void AddClassSet(ClassSet&& set) |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 153 | REQUIRES(!lock_) |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 154 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 155 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 156 | // Clear strong roots (other than classes themselves). |
| 157 | void ClearStrongRoots() |
| 158 | REQUIRES(!lock_) |
| 159 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 160 | |
Mathieu Chartier | 92091bd | 2016-05-10 18:13:20 -0700 | [diff] [blame] | 161 | ReaderWriterMutex& GetLock() { |
| 162 | return lock_; |
| 163 | } |
| 164 | |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 165 | private: |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 166 | // Lock to guard inserting and removing. |
| 167 | mutable ReaderWriterMutex lock_; |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 168 | // We have a vector to help prevent dirty pages after the zygote forks by calling FreezeSnapshot. |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 169 | std::vector<ClassSet> classes_ GUARDED_BY(lock_); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 170 | // Extra strong roots that can be either dex files or dex caches. Dex files used by the class |
| 171 | // loader which may not be owned by the class loader must be held strongly live. Also dex caches |
| 172 | // are held live to prevent them being unloading once they have classes in them. |
| 173 | std::vector<GcRoot<mirror::Object>> strong_roots_ GUARDED_BY(lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | } // namespace art |
| 177 | |
| 178 | #endif // ART_RUNTIME_CLASS_TABLE_H_ |