blob: cc49d6713fc0e5a65259c8ef8743290d3cd8f422 [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
17#include "intern_table.h"
18
Ian Rogers7dfb28c2013-08-22 08:18:36 -070019#include "gc/space/image_space.h"
20#include "mirror/dex_cache.h"
21#include "mirror/object_array-inl.h"
22#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/string.h"
24#include "thread.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070025#include "UniquePtr.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027
28namespace art {
29
Ian Rogers7dfb28c2013-08-22 08:18:36 -070030InternTable::InternTable()
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010031 : is_dirty_(false), allow_new_interns_(true),
32 new_intern_condition_("New intern condition", *Locks::intern_table_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070033}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070034
Brian Carlstroma663ea52011-08-19 23:33:41 -070035size_t InternTable::Size() const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010036 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070037 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070038}
39
Elliott Hughescac6cc72011-11-03 20:31:21 -070040void InternTable::DumpForSigQuit(std::ostream& os) const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010041 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -070042 os << "Intern table: " << strong_interns_.size() << " strong; "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070043 << weak_interns_.size() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070044}
45
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080046void InternTable::VisitRoots(RootCallback* callback, void* arg,
Mathieu Chartierc4621982013-09-16 19:43:47 -070047 bool only_dirty, bool clean_dirty) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010048 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierc4621982013-09-16 19:43:47 -070049 if (!only_dirty || is_dirty_) {
Mathieu Chartierb3070522013-09-17 14:18:21 -070050 for (auto& strong_intern : strong_interns_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080051 strong_intern.second =
52 down_cast<mirror::String*>(callback(strong_intern.second, arg, 0,
53 kRootInternedString));
Mathieu Chartierb3070522013-09-17 14:18:21 -070054 DCHECK(strong_intern.second != nullptr);
Mathieu Chartierc4621982013-09-16 19:43:47 -070055 }
56 if (clean_dirty) {
57 is_dirty_ = false;
58 }
Ian Rogers1d54e732013-05-02 21:10:01 -070059 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070060 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070061}
62
Mathieu Chartier590fee92013-09-13 13:46:47 -070063mirror::String* InternTable::Lookup(Table& table, mirror::String* s, uint32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010064 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartier02e25112013-08-14 16:14:24 -070065 for (auto it = table.find(hash_code), end = table.end(); it != end; ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 mirror::String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 if (existing_string->Equals(s)) {
68 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070069 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070070 }
71 return NULL;
72}
73
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010074mirror::String* InternTable::InsertStrong(mirror::String* s, uint32_t hash_code) {
75 Runtime* runtime = Runtime::Current();
76 if (runtime->IsActiveTransaction()) {
77 runtime->RecordStrongStringInsertion(s, hash_code);
78 }
79 return Insert(strong_interns_, s, hash_code);
80}
81
82mirror::String* InternTable::InsertWeak(mirror::String* s, uint32_t hash_code) {
83 Runtime* runtime = Runtime::Current();
84 if (runtime->IsActiveTransaction()) {
85 runtime->RecordWeakStringInsertion(s, hash_code);
86 }
87 return Insert(weak_interns_, s, hash_code);
88}
89
Mathieu Chartier590fee92013-09-13 13:46:47 -070090mirror::String* InternTable::Insert(Table& table, mirror::String* s, uint32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010091 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070092 table.insert(std::make_pair(hash_code, s));
93 return s;
94}
95
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010096void InternTable::RemoveWeak(mirror::String* s, uint32_t hash_code) {
97 Runtime* runtime = Runtime::Current();
98 if (runtime->IsActiveTransaction()) {
99 runtime->RecordWeakStringRemoval(s, hash_code);
100 }
101 Remove(weak_interns_, s, hash_code);
102}
103
104void InternTable::Remove(Table& table, mirror::String* s, uint32_t hash_code) {
105 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartier02e25112013-08-14 16:14:24 -0700106 for (auto it = table.find(hash_code), end = table.end(); it != end; ++it) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700107 if (it->second == s) {
108 table.erase(it);
109 return;
110 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700111 }
112}
113
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114// Insert/remove methods used to undo changes made during an aborted transaction.
115mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s, uint32_t hash_code) {
116 DCHECK(!Runtime::Current()->IsActiveTransaction());
117 return InsertStrong(s, hash_code);
118}
119mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s, uint32_t hash_code) {
120 DCHECK(!Runtime::Current()->IsActiveTransaction());
121 return InsertWeak(s, hash_code);
122}
123void InternTable::RemoveStrongFromTransaction(mirror::String* s, uint32_t hash_code) {
124 DCHECK(!Runtime::Current()->IsActiveTransaction());
125 Remove(strong_interns_, s, hash_code);
126}
127void InternTable::RemoveWeakFromTransaction(mirror::String* s, uint32_t hash_code) {
128 DCHECK(!Runtime::Current()->IsActiveTransaction());
129 Remove(weak_interns_, s, hash_code);
130}
131
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700132static mirror::String* LookupStringFromImage(mirror::String* s)
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
134 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
135 if (image == NULL) {
136 return NULL; // No image present.
137 }
138 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
139 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
140 const std::string utf8 = s->ToModifiedUtf8();
141 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
142 mirror::DexCache* dex_cache = dex_caches->Get(i);
143 const DexFile* dex_file = dex_cache->GetDexFile();
144 // Binary search the dex file for the string index.
145 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
146 if (string_id != NULL) {
147 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
148 mirror::String* image = dex_cache->GetResolvedString(string_idx);
149 if (image != NULL) {
150 return image;
151 }
152 }
153 }
154 return NULL;
155}
156
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700157void InternTable::AllowNewInterns() {
158 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700160 allow_new_interns_ = true;
161 new_intern_condition_.Broadcast(self);
162}
163
164void InternTable::DisallowNewInterns() {
165 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100166 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700167 allow_new_interns_ = false;
168}
169
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700171 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100172 MutexLock mu(self, *Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700173
174 DCHECK(s != NULL);
175 uint32_t hash_code = s->GetHashCode();
176
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700177 while (UNLIKELY(!allow_new_interns_)) {
178 new_intern_condition_.WaitHoldingLocks(self);
179 }
180
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700181 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -0700182 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700184 if (strong != NULL) {
185 return strong;
186 }
187
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700188 // Mark as dirty so that we rescan the roots.
Mathieu Chartierc4621982013-09-16 19:43:47 -0700189 is_dirty_ = true;
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700190
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700191 // Check the image for a match.
192 mirror::String* image = LookupStringFromImage(s);
193 if (image != NULL) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100194 return InsertStrong(image, hash_code);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700195 }
196
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700197 // There is no match in the strong table, check the weak table.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700199 if (weak != NULL) {
200 // A match was found in the weak table. Promote to the strong table.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100201 RemoveWeak(weak, hash_code);
202 return InsertStrong(weak, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700203 }
204
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700205 // No match in the strong table or the weak table. Insert into the strong
206 // table.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100207 return InsertStrong(s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700208 }
209
210 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700212 if (strong != NULL) {
213 return strong;
214 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700215 // Check the image for a match.
216 mirror::String* image = LookupStringFromImage(s);
jeffhaob1c6f342012-03-20 17:57:26 -0700217 if (image != NULL) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100218 return InsertWeak(image, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700219 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700220 // Check the weak table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700222 if (weak != NULL) {
223 return weak;
224 }
225 // Insert into the weak table.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100226 return InsertWeak(s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700227}
228
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700229mirror::String* InternTable::InternStrong(int32_t utf16_length,
230 const char* utf8_data) {
231 return InternStrong(mirror::String::AllocFromModifiedUtf8(
232 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700233}
234
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235mirror::String* InternTable::InternStrong(const char* utf8_data) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700236 return InternStrong(
237 mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700238}
239
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240mirror::String* InternTable::InternStrong(mirror::String* s) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800241 if (s == nullptr) {
242 return nullptr;
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700243 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700244 return Insert(s, true);
245}
246
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247mirror::String* InternTable::InternWeak(mirror::String* s) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800248 if (s == nullptr) {
249 return nullptr;
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700250 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700251 return Insert(s, false);
252}
253
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100255 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700257 return found == s;
258}
259
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800260void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100261 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700262 for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 mirror::Object* object = it->second;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800264 mirror::Object* new_object = callback(object, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700265 if (new_object == nullptr) {
266 // TODO: use it = weak_interns_.erase(it) when we get a c++11 stl.
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700267 weak_interns_.erase(it++);
268 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700269 it->second = down_cast<mirror::String*>(new_object);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700270 ++it;
271 }
272 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700273}
274
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700275} // namespace art