blob: acb15c787947f755eb8396517972af854da94acd [file] [log] [blame]
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -07001/*
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
33namespace art {
34
Vladimir Markoaad75c62016-10-03 08:46:48 +000035class OatFile;
36
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070037namespace mirror {
38 class ClassLoader;
39} // namespace mirror
40
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070041// Each loader has a ClassTable
42class ClassTable {
43 public:
Mathieu Chartier88027bd2016-03-02 16:08:31 -080044 class ClassDescriptorHashEquals {
45 public:
46 // uint32_t for cross compilation.
47 uint32_t operator()(const GcRoot<mirror::Class>& root) const NO_THREAD_SAFETY_ANALYSIS;
48 // Same class loader and descriptor.
49 bool operator()(const GcRoot<mirror::Class>& a, const GcRoot<mirror::Class>& b) const
50 NO_THREAD_SAFETY_ANALYSIS;;
51 // Same descriptor.
52 bool operator()(const GcRoot<mirror::Class>& a, const char* descriptor) const
53 NO_THREAD_SAFETY_ANALYSIS;
54 // uint32_t for cross compilation.
55 uint32_t operator()(const char* descriptor) const NO_THREAD_SAFETY_ANALYSIS;
56 };
57 class GcRootEmptyFn {
58 public:
59 void MakeEmpty(GcRoot<mirror::Class>& item) const {
60 item = GcRoot<mirror::Class>();
61 }
62 bool IsEmpty(const GcRoot<mirror::Class>& item) const {
63 return item.IsNull();
64 }
65 };
66 // hash set which hashes class descriptor, and compares descriptors and class loaders. Results
67 // should be compared for a matching Class descriptor and class loader.
68 typedef HashSet<GcRoot<mirror::Class>, GcRootEmptyFn, ClassDescriptorHashEquals,
69 ClassDescriptorHashEquals, TrackingAllocator<GcRoot<mirror::Class>, kAllocatorTagClassTable>>
70 ClassSet;
71
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070072 ClassTable();
73
74 // Used by image writer for checking.
75 bool Contains(mirror::Class* klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070076 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070078
79 // Freeze the current class tables by allocating a new table and never updating or modifying the
80 // existing table. This helps prevents dirty pages after caused by inserting after zygote fork.
81 void FreezeSnapshot()
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070082 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070084
85 // Returns the number of classes in previous snapshots.
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070086 size_t NumZygoteClasses() const REQUIRES(!lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070087
88 // Returns all off the classes in the lastest snapshot.
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070089 size_t NumNonZygoteClasses() const REQUIRES(!lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070090
91 // Update a class in the table with the new class. Returns the existing class which was replaced.
92 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* new_klass, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070093 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070094 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070095
Mathieu Chartiere4275c02015-08-06 15:34:15 -070096 // NO_THREAD_SAFETY_ANALYSIS for object marking requiring heap bitmap lock.
97 template<class Visitor>
98 void VisitRoots(Visitor& visitor)
Mathieu Chartier00310e02015-10-17 12:46:42 -070099 NO_THREAD_SAFETY_ANALYSIS
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700100 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700102
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700103 template<class Visitor>
104 void VisitRoots(const Visitor& visitor)
Mathieu Chartier00310e02015-10-17 12:46:42 -0700105 NO_THREAD_SAFETY_ANALYSIS
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700106 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700108
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800109 // Stops visit if the visitor returns false.
110 template <typename Visitor>
111 bool Visit(Visitor& visitor)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700112 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700114
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800115 // Return the first class that matches the descriptor. Returns null if there are none.
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700116 mirror::Class* Lookup(const char* descriptor, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700117 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700119
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800120 // Return the first class that matches the descriptor of klass. Returns null if there are none.
121 mirror::Class* LookupByDescriptor(mirror::Class* klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700122 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800124
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700125 void Insert(mirror::Class* klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700126 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700127 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700128
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700129 void InsertWithHash(mirror::Class* klass, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700130 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700132
133 // Returns true if the class was found and removed, false otherwise.
134 bool Remove(const char* descriptor)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700135 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700137
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700138 // Return true if we inserted the strong root, false if it already exists.
139 bool InsertStrongRoot(mirror::Object* obj)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700140 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700142
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800143 // Combines all of the tables into one class set.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800144 size_t WriteToMemory(uint8_t* ptr) const
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700145 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800147
148 // Read a table from ptr and put it at the front of the class set.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800149 size_t ReadFromMemory(uint8_t* ptr)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700150 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800152
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800153 // Add a class set to the front of classes.
154 void AddClassSet(ClassSet&& set)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700155 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700156 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700157
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700158 // Clear strong roots (other than classes themselves).
159 void ClearStrongRoots()
160 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700161 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700162
Mathieu Chartier92091bd2016-05-10 18:13:20 -0700163 ReaderWriterMutex& GetLock() {
164 return lock_;
165 }
166
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800167 private:
Mathieu Chartier496577f2016-09-20 15:33:31 -0700168 void InsertWithoutLocks(mirror::Class* klass) NO_THREAD_SAFETY_ANALYSIS;
169
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700170 // Lock to guard inserting and removing.
171 mutable ReaderWriterMutex lock_;
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700172 // We have a vector to help prevent dirty pages after the zygote forks by calling FreezeSnapshot.
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700173 std::vector<ClassSet> classes_ GUARDED_BY(lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700174 // Extra strong roots that can be either dex files or dex caches. Dex files used by the class
175 // loader which may not be owned by the class loader must be held strongly live. Also dex caches
176 // are held live to prevent them being unloading once they have classes in them.
177 std::vector<GcRoot<mirror::Object>> strong_roots_ GUARDED_BY(lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000178 // Keep track of oat files with GC roots associated with dex caches in `strong_roots_`.
179 std::vector<const OatFile*> oat_files_ GUARDED_BY(lock_);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700180
181 friend class ImageWriter; // for InsertWithoutLocks.
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700182};
183
184} // namespace art
185
186#endif // ART_RUNTIME_CLASS_TABLE_H_