blob: 371d3f7d2cc544b931a473527f7a7b10681ad23b [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"
Mathieu Chartierc2e20622014-11-03 11:41:47 -080023#include "base/hash_set.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080024#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070025#include "gc_root.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080026#include "object_callbacks.h"
27
Brian Carlstrom7e93b502011-08-04 14:16:22 -070028namespace art {
Mathieu Chartier893263b2014-03-04 11:07:42 -080029
Mathieu Chartiereb175f72014-10-31 11:49:27 -070030namespace gc {
31namespace space {
32class ImageSpace;
33} // namespace space
34} // namespace gc
35
Mathieu Chartier893263b2014-03-04 11:07:42 -080036enum VisitRootFlags : uint8_t;
37
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038namespace mirror {
39class String;
40} // namespace mirror
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010041class Transaction;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070042
Elliott Hughescf4c6c42011-09-01 15:16:42 -070043/**
44 * Used to intern strings.
45 *
46 * There are actually two tables: one that holds strong references to its strings, and one that
47 * holds weak references. The former is used for string literals, for which there is an effective
48 * reference from the constant pool. The latter is used for strings interned at runtime via
49 * String.intern. Some code (XML parsers being a prime example) relies on being able to intern
50 * arbitrarily many strings for the duration of a parse without permanently increasing the memory
51 * footprint.
52 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070053class InternTable {
54 public:
55 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070056
Elliott Hughescf4c6c42011-09-01 15:16:42 -070057 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070060
61 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 mirror::String* InternStrong(const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070063 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070064
65 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070067
Elliott Hughescf4c6c42011-09-01 15:16:42 -070068 // Interns a potentially new string in the 'weak' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070070
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070071 void SweepInternTableWeaks(IsMarkedCallback* callback, void* arg)
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070073
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070075
Mathieu Chartiereb175f72014-10-31 11:49:27 -070076 // Total number of interned strings.
77 size_t Size() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
78 // Total number of weakly live interned strings.
79 size_t StrongSize() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
80 // Total number of strongly live interned strings.
81 size_t WeakSize() const LOCKS_EXCLUDED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070082
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070083 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
84 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070085
Elliott Hughescac6cc72011-11-03 20:31:21 -070086 void DumpForSigQuit(std::ostream& os) const;
87
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070088 void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
89 void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90
Mathieu Chartiereb175f72014-10-31 11:49:27 -070091 // Adds all of the resolved image strings from the image space into the intern table. The
92 // advantage of doing this is preventing expensive DexFile::FindStringId calls.
93 void AddImageStringsToTable(gc::space::ImageSpace* image_space)
94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::intern_table_lock_);
95 // Copy the post zygote tables to pre zygote to save memory by preventing dirty pages.
96 void SwapPostZygoteWithPreZygote()
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::intern_table_lock_);
98
Brian Carlstrom7e93b502011-08-04 14:16:22 -070099 private:
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700100 class StringHashEquals {
101 public:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800102 std::size_t operator()(const GcRoot<mirror::String>& root) const NO_THREAD_SAFETY_ANALYSIS;
103 bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b) const
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700104 NO_THREAD_SAFETY_ANALYSIS;
105 };
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800106 class GcRootEmptyFn {
107 public:
108 void MakeEmpty(GcRoot<mirror::String>& item) const {
109 item = GcRoot<mirror::String>();
110 }
111 bool IsEmpty(const GcRoot<mirror::String>& item) const {
112 return item.IsNull();
113 }
114 };
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700115
116 // Table which holds pre zygote and post zygote interned strings. There is one instance for
117 // weak interns and strong interns.
118 class Table {
119 public:
120 mirror::String* Find(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
121 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
122 void Insert(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
123 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
124 void Remove(mirror::String* s)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
126 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800127 void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
129 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
130 void SweepWeaks(IsMarkedCallback* callback, void* arg)
131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
132 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
133 void SwapPostZygoteWithPreZygote() EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800134 size_t Size() const EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700135
136 private:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800137 typedef HashSet<GcRoot<mirror::String>, GcRootEmptyFn, StringHashEquals, StringHashEquals,
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700138 TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>> UnorderedSet;
139
140 void SweepWeaks(UnorderedSet* set, IsMarkedCallback* callback, void* arg)
141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
142 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
143
144 // We call SwapPostZygoteWithPreZygote when we create the zygote to reduce private dirty pages
145 // caused by modifying the zygote intern table hash table. The pre zygote table are the
146 // interned strings which were interned before we created the zygote space. Post zygote is self
147 // explanatory.
148 UnorderedSet pre_zygote_table_;
149 UnorderedSet post_zygote_table_;
150 };
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700151
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800152 // Insert if non null, otherwise return nullptr.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 mirror::String* Insert(mirror::String* s, bool is_strong)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154 LOCKS_EXCLUDED(Locks::intern_table_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700156
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700157 mirror::String* LookupStrong(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
159 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700160 mirror::String* LookupWeak(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
162 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700163 mirror::String* InsertStrong(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100165 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700166 mirror::String* InsertWeak(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100168 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700169 void RemoveStrong(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100171 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700172 void RemoveWeak(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
174 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700175
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100176 // Transaction rollback access.
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700177 mirror::String* LookupStringFromImage(mirror::String* s)
178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
179 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700180 mirror::String* InsertStrongFromTransaction(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100182 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700183 mirror::String* InsertWeakFromTransaction(mirror::String* s)
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100185 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700186 void RemoveStrongFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100188 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700189 void RemoveWeakFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100191 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
192 friend class Transaction;
193
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700194 bool image_added_to_intern_table_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800195 bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100196 bool allow_new_interns_ GUARDED_BY(Locks::intern_table_lock_);
197 ConditionVariable new_intern_condition_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700198 // Since this contains (strong) roots, they need a read barrier to
199 // enable concurrent intern table (strong) root scan. Do not
200 // directly access the strings in it. Use functions that contain
201 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100202 Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700203 std::vector<GcRoot<mirror::String>> new_strong_intern_roots_
Mathieu Chartier893263b2014-03-04 11:07:42 -0800204 GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700205 // Since this contains (weak) roots, they need a read barrier. Do
206 // not directly access the strings in it. Use functions that contain
207 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100208 Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700209};
210
211} // namespace art
212
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700213#endif // ART_RUNTIME_INTERN_TABLE_H_