blob: 252a47dd2540d0ccc0fdf73667c799a7acaa7e28 [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
70 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
71 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
72
73 // Return false if the callback told us to exit.
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070074 bool Visit(ClassVisitor* visitor)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070075 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
76
77 mirror::Class* Lookup(const char* descriptor, size_t hash)
78 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
79
80 void Insert(mirror::Class* klass)
81 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
82 void InsertWithHash(mirror::Class* klass, size_t hash)
83 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
84
85 // Returns true if the class was found and removed, false otherwise.
86 bool Remove(const char* descriptor)
87 REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
88
89 private:
90 class ClassDescriptorHashEquals {
91 public:
92 // Same class loader and descriptor.
93 std::size_t operator()(const GcRoot<mirror::Class>& root) const NO_THREAD_SAFETY_ANALYSIS;
94 bool operator()(const GcRoot<mirror::Class>& a, const GcRoot<mirror::Class>& b) const
95 NO_THREAD_SAFETY_ANALYSIS;;
96 // Same descriptor.
97 bool operator()(const GcRoot<mirror::Class>& a, const char* descriptor) const
98 NO_THREAD_SAFETY_ANALYSIS;
99 std::size_t operator()(const char* descriptor) const NO_THREAD_SAFETY_ANALYSIS;
100 };
101 class GcRootEmptyFn {
102 public:
103 void MakeEmpty(GcRoot<mirror::Class>& item) const {
104 item = GcRoot<mirror::Class>();
105 }
106 bool IsEmpty(const GcRoot<mirror::Class>& item) const {
107 return item.IsNull();
108 }
109 };
110 // hash set which hashes class descriptor, and compares descriptors nad class loaders. Results
111 // should be compared for a matching Class descriptor and class loader.
112 typedef HashSet<GcRoot<mirror::Class>, GcRootEmptyFn, ClassDescriptorHashEquals,
113 ClassDescriptorHashEquals, TrackingAllocator<GcRoot<mirror::Class>, kAllocatorTagClassTable>>
114 ClassSet;
115
116 // TODO: shard lock to have one per class loader.
117 std::vector<ClassSet> classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
118};
119
120} // namespace art
121
122#endif // ART_RUNTIME_CLASS_TABLE_H_