blob: 0bff7b9ad265c8350defa8606751295112162bc0 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INTERN_TABLE_H_
18#define ART_RUNTIME_INTERN_TABLE_H_
Brian Carlstrom7e93b502011-08-04 14:16:22 -070019
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070020#include <unordered_set>
Brian Carlstrom7e93b502011-08-04 14:16:22 -070021
Mathieu Chartierbad02672014-08-25 13:08:22 -070022#include "base/allocator.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080023#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070024#include "gc_root.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080025#include "object_callbacks.h"
26
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027namespace art {
Mathieu Chartier893263b2014-03-04 11:07:42 -080028
Mathieu Chartiereb175f72014-10-31 11:49:27 -070029namespace gc {
30namespace space {
31class ImageSpace;
32} // namespace space
33} // namespace gc
34
Mathieu Chartier893263b2014-03-04 11:07:42 -080035enum VisitRootFlags : uint8_t;
36
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037namespace mirror {
38class String;
39} // namespace mirror
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010040class Transaction;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070041
Elliott Hughescf4c6c42011-09-01 15:16:42 -070042/**
43 * Used to intern strings.
44 *
45 * There are actually two tables: one that holds strong references to its strings, and one that
46 * holds weak references. The former is used for string literals, for which there is an effective
47 * reference from the constant pool. The latter is used for strings interned at runtime via
48 * String.intern. Some code (XML parsers being a prime example) relies on being able to intern
49 * arbitrarily many strings for the duration of a parse without permanently increasing the memory
50 * footprint.
51 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070052class InternTable {
53 public:
54 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070055
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070058 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070059
60 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 mirror::String* InternStrong(const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070062 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070063
64 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070066
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 // Interns a potentially new string in the 'weak' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070069
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070070 void SweepInternTableWeaks(IsMarkedCallback* callback, void* arg)
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070072
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070074
Mathieu Chartiereb175f72014-10-31 11:49:27 -070075 // Total number of interned strings.
76 size_t Size() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
77 // Total number of weakly live interned strings.
78 size_t StrongSize() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
79 // Total number of strongly live interned strings.
80 size_t WeakSize() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070081
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070082 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070084
Elliott Hughescac6cc72011-11-03 20:31:21 -070085 void DumpForSigQuit(std::ostream& os) const;
86
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070087 void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
88 void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89
Mathieu Chartiereb175f72014-10-31 11:49:27 -070090 // Adds all of the resolved image strings from the image space into the intern table. The
91 // advantage of doing this is preventing expensive DexFile::FindStringId calls.
92 void AddImageStringsToTable(gc::space::ImageSpace* image_space)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::intern_table_lock_);
94 // Copy the post zygote tables to pre zygote to save memory by preventing dirty pages.
95 void SwapPostZygoteWithPreZygote()
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::intern_table_lock_);
97
Brian Carlstrom7e93b502011-08-04 14:16:22 -070098 private:
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070099 class StringHashEquals {
100 public:
101 std::size_t operator()(const GcRoot<mirror::String>& root) NO_THREAD_SAFETY_ANALYSIS;
102 bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b)
103 NO_THREAD_SAFETY_ANALYSIS;
104 };
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700105
106 // Table which holds pre zygote and post zygote interned strings. There is one instance for
107 // weak interns and strong interns.
108 class Table {
109 public:
110 mirror::String* Find(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
111 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
112 void Insert(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
113 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
114 void Remove(mirror::String* s)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
116 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
117 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
119 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
120 void SweepWeaks(IsMarkedCallback* callback, void* arg)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
122 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
123 void SwapPostZygoteWithPreZygote() EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
124 size_t Size() const EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_) {
125 return pre_zygote_table_.size() + post_zygote_table_.size();
126 }
127
128 private:
129 typedef std::unordered_set<GcRoot<mirror::String>, StringHashEquals, StringHashEquals,
130 TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>> UnorderedSet;
131
132 void SweepWeaks(UnorderedSet* set, IsMarkedCallback* callback, void* arg)
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
134 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
135
136 // We call SwapPostZygoteWithPreZygote when we create the zygote to reduce private dirty pages
137 // caused by modifying the zygote intern table hash table. The pre zygote table are the
138 // interned strings which were interned before we created the zygote space. Post zygote is self
139 // explanatory.
140 UnorderedSet pre_zygote_table_;
141 UnorderedSet post_zygote_table_;
142 };
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700143
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 mirror::String* Insert(mirror::String* s, bool is_strong)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100145 LOCKS_EXCLUDED(Locks::intern_table_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700147
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700148 mirror::String* LookupStrong(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
150 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700151 mirror::String* LookupWeak(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
153 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700154 mirror::String* InsertStrong(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100156 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700157 mirror::String* InsertWeak(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700160 void RemoveStrong(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100162 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700163 void RemoveWeak(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
165 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700166
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100167 // Transaction rollback access.
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700168 mirror::String* LookupStringFromImage(mirror::String* s)
169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
170 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700171 mirror::String* InsertStrongFromTransaction(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100173 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700174 mirror::String* InsertWeakFromTransaction(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100176 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700177 void RemoveStrongFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100179 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700180 void RemoveWeakFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100182 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
183 friend class Transaction;
184
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700185 bool image_added_to_intern_table_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800186 bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100187 bool allow_new_interns_ GUARDED_BY(Locks::intern_table_lock_);
188 ConditionVariable new_intern_condition_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700189 // Since this contains (strong) roots, they need a read barrier to
190 // enable concurrent intern table (strong) root scan. Do not
191 // directly access the strings in it. Use functions that contain
192 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100193 Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700194 std::vector<GcRoot<mirror::String>> new_strong_intern_roots_
Mathieu Chartier893263b2014-03-04 11:07:42 -0800195 GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700196 // Since this contains (weak) roots, they need a read barrier. Do
197 // not directly access the strings in it. Use functions that contain
198 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100199 Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700200};
201
202} // namespace art
203
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700204#endif // ART_RUNTIME_INTERN_TABLE_H_