blob: 6b18d9009d6dea41a0a5e56a765ce62829d9d0ea [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
35namespace mirror {
36 class ClassLoader;
37} // namespace mirror
38
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070039class ClassVisitor {
40 public:
41 virtual ~ClassVisitor() {}
42 // Return true to continue visiting.
43 virtual bool Visit(mirror::Class* klass) = 0;
44};
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070045
46// Each loader has a ClassTable
47class ClassTable {
48 public:
49 ClassTable();
50
51 // Used by image writer for checking.
52 bool Contains(mirror::Class* klass)
53 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
54
55 // Freeze the current class tables by allocating a new table and never updating or modifying the
56 // existing table. This helps prevents dirty pages after caused by inserting after zygote fork.
57 void FreezeSnapshot()
58 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
59
60 // Returns the number of classes in previous snapshots.
61 size_t NumZygoteClasses() const REQUIRES(Locks::classlinker_classes_lock_);
62
63 // Returns all off the classes in the lastest snapshot.
64 size_t NumNonZygoteClasses() const REQUIRES(Locks::classlinker_classes_lock_);
65
66 // Update a class in the table with the new class. Returns the existing class which was replaced.
67 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* new_klass, size_t hash)
68 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
69
Mathieu Chartiere4275c02015-08-06 15:34:15 -070070 // NO_THREAD_SAFETY_ANALYSIS for object marking requiring heap bitmap lock.
71 template<class Visitor>
72 void VisitRoots(Visitor& visitor)
73 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_)
74 NO_THREAD_SAFETY_ANALYSIS;
75 template<class Visitor>
76 void VisitRoots(const Visitor& visitor)
77 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_)
78 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070079
80 // Return false if the callback told us to exit.
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070081 bool Visit(ClassVisitor* visitor)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070082 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
83
84 mirror::Class* Lookup(const char* descriptor, size_t hash)
85 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
86
87 void Insert(mirror::Class* klass)
88 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
89 void InsertWithHash(mirror::Class* klass, size_t hash)
90 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
91
92 // Returns true if the class was found and removed, false otherwise.
93 bool Remove(const char* descriptor)
94 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
95
96 private:
97 class ClassDescriptorHashEquals {
98 public:
99 // Same class loader and descriptor.
100 std::size_t operator()(const GcRoot<mirror::Class>& root) const NO_THREAD_SAFETY_ANALYSIS;
101 bool operator()(const GcRoot<mirror::Class>& a, const GcRoot<mirror::Class>& b) const
102 NO_THREAD_SAFETY_ANALYSIS;;
103 // Same descriptor.
104 bool operator()(const GcRoot<mirror::Class>& a, const char* descriptor) const
105 NO_THREAD_SAFETY_ANALYSIS;
106 std::size_t operator()(const char* descriptor) const NO_THREAD_SAFETY_ANALYSIS;
107 };
108 class GcRootEmptyFn {
109 public:
110 void MakeEmpty(GcRoot<mirror::Class>& item) const {
111 item = GcRoot<mirror::Class>();
112 }
113 bool IsEmpty(const GcRoot<mirror::Class>& item) const {
114 return item.IsNull();
115 }
116 };
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700117 // hash set which hashes class descriptor, and compares descriptors and class loaders. Results
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700118 // should be compared for a matching Class descriptor and class loader.
119 typedef HashSet<GcRoot<mirror::Class>, GcRootEmptyFn, ClassDescriptorHashEquals,
120 ClassDescriptorHashEquals, TrackingAllocator<GcRoot<mirror::Class>, kAllocatorTagClassTable>>
121 ClassSet;
122
123 // TODO: shard lock to have one per class loader.
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700124 // We have a vector to help prevent dirty pages after the zygote forks by calling FreezeSnapshot.
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700125 std::vector<ClassSet> classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
126};
127
128} // namespace art
129
130#endif // ART_RUNTIME_CLASS_TABLE_H_