blob: 339eb36178779786cc61e939850c40a6d13d68ce [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 Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
20
Ian Rogers7dfb28c2013-08-22 08:18:36 -070021#include "gc/space/image_space.h"
22#include "mirror/dex_cache.h"
23#include "mirror/object_array-inl.h"
24#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/string.h"
26#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070027#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070028
29namespace art {
30
Ian Rogers7dfb28c2013-08-22 08:18:36 -070031InternTable::InternTable()
Mathieu Chartier893263b2014-03-04 11:07:42 -080032 : log_new_roots_(false), allow_new_interns_(true),
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010033 new_intern_condition_("New intern condition", *Locks::intern_table_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070034}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070035
Brian Carlstroma663ea52011-08-19 23:33:41 -070036size_t InternTable::Size() const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010037 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070038 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070039}
40
Elliott Hughescac6cc72011-11-03 20:31:21 -070041void InternTable::DumpForSigQuit(std::ostream& os) const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010042 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -070043 os << "Intern table: " << strong_interns_.size() << " strong; "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070044 << weak_interns_.size() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070045}
46
Mathieu Chartier893263b2014-03-04 11:07:42 -080047void InternTable::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010048 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080049 if ((flags & kVisitRootFlagAllRoots) != 0) {
Mathieu Chartierb3070522013-09-17 14:18:21 -070050 for (auto& strong_intern : strong_interns_) {
Mathieu Chartier815873e2014-02-13 18:02:13 -080051 callback(reinterpret_cast<mirror::Object**>(&strong_intern.second), arg, 0,
52 kRootInternedString);
Mathieu Chartierb3070522013-09-17 14:18:21 -070053 DCHECK(strong_intern.second != nullptr);
Mathieu Chartierc4621982013-09-16 19:43:47 -070054 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080055 } else if ((flags & kVisitRootFlagNewRoots) != 0) {
56 for (auto& pair : new_strong_intern_roots_) {
57 mirror::String* old_ref = pair.second;
58 callback(reinterpret_cast<mirror::Object**>(&pair.second), arg, 0, kRootInternedString);
59 if (UNLIKELY(pair.second != old_ref)) {
60 // Uh ohes, GC moved a root in the log. Need to search the strong interns and update the
61 // corresponding object. This is slow, but luckily for us, this may only happen with a
62 // concurrent moving GC.
63 for (auto it = strong_interns_.lower_bound(pair.first), end = strong_interns_.end();
64 it != end && it->first == pair.first; ++it) {
65 // If the class stored matches the old class, update it to the new value.
66 if (old_ref == it->second) {
67 it->second = pair.second;
68 }
69 }
70 }
71 }
72 }
73
74 if ((flags & kVisitRootFlagClearRootLog) != 0) {
75 new_strong_intern_roots_.clear();
76 }
77 if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
78 log_new_roots_ = true;
79 } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
80 log_new_roots_ = false;
Ian Rogers1d54e732013-05-02 21:10:01 -070081 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070082 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070083}
84
Mathieu Chartier893263b2014-03-04 11:07:42 -080085mirror::String* InternTable::Lookup(Table& table, mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010086 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Vladimir Marko53dc70c2014-05-22 12:16:44 +010087 for (auto it = table.lower_bound(hash_code), end = table.end();
88 it != end && it->first == hash_code; ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 mirror::String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070090 if (existing_string->Equals(s)) {
91 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070092 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070093 }
94 return NULL;
95}
96
Mathieu Chartier893263b2014-03-04 11:07:42 -080097mirror::String* InternTable::InsertStrong(mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010098 Runtime* runtime = Runtime::Current();
99 if (runtime->IsActiveTransaction()) {
100 runtime->RecordStrongStringInsertion(s, hash_code);
101 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800102 if (log_new_roots_) {
103 new_strong_intern_roots_.push_back(std::make_pair(hash_code, s));
104 }
105 strong_interns_.insert(std::make_pair(hash_code, s));
106 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100107}
108
Mathieu Chartier893263b2014-03-04 11:07:42 -0800109mirror::String* InternTable::InsertWeak(mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100110 Runtime* runtime = Runtime::Current();
111 if (runtime->IsActiveTransaction()) {
112 runtime->RecordWeakStringInsertion(s, hash_code);
113 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800114 weak_interns_.insert(std::make_pair(hash_code, s));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700115 return s;
116}
117
Mathieu Chartier893263b2014-03-04 11:07:42 -0800118void InternTable::RemoveWeak(mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100119 Runtime* runtime = Runtime::Current();
120 if (runtime->IsActiveTransaction()) {
121 runtime->RecordWeakStringRemoval(s, hash_code);
122 }
123 Remove(weak_interns_, s, hash_code);
124}
125
Mathieu Chartier893263b2014-03-04 11:07:42 -0800126void InternTable::Remove(Table& table, mirror::String* s, int32_t hash_code) {
Vladimir Marko53dc70c2014-05-22 12:16:44 +0100127 for (auto it = table.lower_bound(hash_code), end = table.end();
128 it != end && it->first == hash_code; ++it) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700129 if (it->second == s) {
130 table.erase(it);
131 return;
132 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700133 }
134}
135
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100136// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartier893263b2014-03-04 11:07:42 -0800137mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138 DCHECK(!Runtime::Current()->IsActiveTransaction());
139 return InsertStrong(s, hash_code);
140}
Mathieu Chartier893263b2014-03-04 11:07:42 -0800141mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142 DCHECK(!Runtime::Current()->IsActiveTransaction());
143 return InsertWeak(s, hash_code);
144}
Mathieu Chartier893263b2014-03-04 11:07:42 -0800145void InternTable::RemoveStrongFromTransaction(mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146 DCHECK(!Runtime::Current()->IsActiveTransaction());
147 Remove(strong_interns_, s, hash_code);
148}
Mathieu Chartier893263b2014-03-04 11:07:42 -0800149void InternTable::RemoveWeakFromTransaction(mirror::String* s, int32_t hash_code) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150 DCHECK(!Runtime::Current()->IsActiveTransaction());
151 Remove(weak_interns_, s, hash_code);
152}
153
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700154static mirror::String* LookupStringFromImage(mirror::String* s)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
157 if (image == NULL) {
158 return NULL; // No image present.
159 }
160 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
161 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
162 const std::string utf8 = s->ToModifiedUtf8();
163 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
164 mirror::DexCache* dex_cache = dex_caches->Get(i);
165 const DexFile* dex_file = dex_cache->GetDexFile();
166 // Binary search the dex file for the string index.
167 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
168 if (string_id != NULL) {
169 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
170 mirror::String* image = dex_cache->GetResolvedString(string_idx);
171 if (image != NULL) {
172 return image;
173 }
174 }
175 }
176 return NULL;
177}
178
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700179void InternTable::AllowNewInterns() {
180 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100181 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700182 allow_new_interns_ = true;
183 new_intern_condition_.Broadcast(self);
184}
185
186void InternTable::DisallowNewInterns() {
187 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100188 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700189 allow_new_interns_ = false;
190}
191
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700193 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100194 MutexLock mu(self, *Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700195
196 DCHECK(s != NULL);
197 uint32_t hash_code = s->GetHashCode();
198
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700199 while (UNLIKELY(!allow_new_interns_)) {
200 new_intern_condition_.WaitHoldingLocks(self);
201 }
202
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700203 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -0700204 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700206 if (strong != NULL) {
207 return strong;
208 }
209
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700210 // Check the image for a match.
211 mirror::String* image = LookupStringFromImage(s);
212 if (image != NULL) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100213 return InsertStrong(image, hash_code);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700214 }
215
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700216 // There is no match in the strong table, check the weak table.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700218 if (weak != NULL) {
219 // A match was found in the weak table. Promote to the strong table.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100220 RemoveWeak(weak, hash_code);
221 return InsertStrong(weak, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700222 }
223
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700224 // No match in the strong table or the weak table. Insert into the strong
225 // table.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100226 return InsertStrong(s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700227 }
228
229 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700231 if (strong != NULL) {
232 return strong;
233 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700234 // Check the image for a match.
235 mirror::String* image = LookupStringFromImage(s);
jeffhaob1c6f342012-03-20 17:57:26 -0700236 if (image != NULL) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100237 return InsertWeak(image, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700238 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700239 // Check the weak table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700241 if (weak != NULL) {
242 return weak;
243 }
244 // Insert into the weak table.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100245 return InsertWeak(s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700246}
247
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700248mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
249 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700250 return InternStrong(mirror::String::AllocFromModifiedUtf8(
251 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700252}
253
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700255 DCHECK(utf8_data != nullptr);
256 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700257}
258
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259mirror::String* InternTable::InternStrong(mirror::String* s) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800260 if (s == nullptr) {
261 return nullptr;
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700262 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700263 return Insert(s, true);
264}
265
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266mirror::String* InternTable::InternWeak(mirror::String* s) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800267 if (s == nullptr) {
268 return nullptr;
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700269 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700270 return Insert(s, false);
271}
272
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100274 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700276 return found == s;
277}
278
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800279void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100280 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700281 for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 mirror::Object* object = it->second;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800283 mirror::Object* new_object = callback(object, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700284 if (new_object == nullptr) {
285 // TODO: use it = weak_interns_.erase(it) when we get a c++11 stl.
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700286 weak_interns_.erase(it++);
287 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700288 it->second = down_cast<mirror::String*>(new_object);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700289 ++it;
290 }
291 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700292}
293
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700294} // namespace art