blob: 23324a6e75544b1843b3d7314db09261deea7ce9 [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"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070025#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#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 Chartiereb175f72014-10-31 11:49:27 -070032 : image_added_to_intern_table_(false), log_new_roots_(false),
33 allow_new_interns_(true),
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010034 new_intern_condition_("New intern condition", *Locks::intern_table_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070035}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070036
Brian Carlstroma663ea52011-08-19 23:33:41 -070037size_t InternTable::Size() const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010038 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070039 return strong_interns_.Size() + weak_interns_.Size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070040}
41
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070042size_t InternTable::StrongSize() const {
43 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070044 return strong_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070045}
46
47size_t InternTable::WeakSize() const {
48 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070049 return weak_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070050}
51
Elliott Hughescac6cc72011-11-03 20:31:21 -070052void InternTable::DumpForSigQuit(std::ostream& os) const {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070053 os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070054}
55
Mathieu Chartier893263b2014-03-04 11:07:42 -080056void InternTable::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010057 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080058 if ((flags & kVisitRootFlagAllRoots) != 0) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070059 strong_interns_.VisitRoots(callback, arg, flags);
Mathieu Chartier893263b2014-03-04 11:07:42 -080060 } else if ((flags & kVisitRootFlagNewRoots) != 0) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070061 for (auto& root : new_strong_intern_roots_) {
62 mirror::String* old_ref = root.Read<kWithoutReadBarrier>();
63 root.VisitRoot(callback, arg, 0, kRootInternedString);
64 mirror::String* new_ref = root.Read<kWithoutReadBarrier>();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070065 if (UNLIKELY(new_ref != old_ref)) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070066 // The GC moved a root in the log. Need to search the strong interns and update the
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070067 // corresponding object. This is slow, but luckily for us, this may only happen with a
68 // concurrent moving GC.
Mathieu Chartiereb175f72014-10-31 11:49:27 -070069 strong_interns_.Remove(old_ref);
70 strong_interns_.Insert(new_ref);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070071 }
72 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080073 }
74
75 if ((flags & kVisitRootFlagClearRootLog) != 0) {
76 new_strong_intern_roots_.clear();
77 }
78 if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
79 log_new_roots_ = true;
80 } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
81 log_new_roots_ = false;
Ian Rogers1d54e732013-05-02 21:10:01 -070082 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070083 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070084}
85
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070086mirror::String* InternTable::LookupStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070087 return strong_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070088}
89
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070090mirror::String* InternTable::LookupWeak(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070091 return weak_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070092}
93
Mathieu Chartiereb175f72014-10-31 11:49:27 -070094void InternTable::SwapPostZygoteWithPreZygote() {
95 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
96 weak_interns_.SwapPostZygoteWithPreZygote();
97 strong_interns_.SwapPostZygoteWithPreZygote();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070098}
99
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700100mirror::String* InternTable::InsertStrong(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100101 Runtime* runtime = Runtime::Current();
102 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700103 runtime->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800105 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700106 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800107 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700108 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800109 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100110}
111
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700112mirror::String* InternTable::InsertWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 Runtime* runtime = Runtime::Current();
114 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700115 runtime->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700117 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700118 return s;
119}
120
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700121void InternTable::RemoveStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700122 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700123}
124
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700125void InternTable::RemoveWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100126 Runtime* runtime = Runtime::Current();
127 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700128 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100129 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700130 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700131}
132
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100133// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700134mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100135 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700136 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700138mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100139 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700140 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100141}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700142void InternTable::RemoveStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100143 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700144 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100145}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700146void InternTable::RemoveWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100147 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700148 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100149}
150
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700151void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space) {
152 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
153 if (!image_added_to_intern_table_) {
154 mirror::Object* root = image_space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
155 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
156 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
157 mirror::DexCache* dex_cache = dex_caches->Get(i);
158 const DexFile* dex_file = dex_cache->GetDexFile();
159 const size_t num_strings = dex_file->NumStringIds();
160 for (size_t j = 0; j < num_strings; ++j) {
161 mirror::String* image_string = dex_cache->GetResolvedString(j);
162 if (image_string != nullptr) {
163 mirror::String* found = LookupStrong(image_string);
164 if (found == nullptr) {
165 InsertStrong(image_string);
166 } else {
167 DCHECK_EQ(found, image_string);
168 }
169 }
170 }
171 }
172 image_added_to_intern_table_ = true;
173 }
174}
175
176mirror::String* InternTable::LookupStringFromImage(mirror::String* s)
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700178 if (image_added_to_intern_table_) {
179 return nullptr;
180 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700181 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700182 if (image == nullptr) {
183 return nullptr; // No image present.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700184 }
185 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
186 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
187 const std::string utf8 = s->ToModifiedUtf8();
188 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
189 mirror::DexCache* dex_cache = dex_caches->Get(i);
190 const DexFile* dex_file = dex_cache->GetDexFile();
191 // Binary search the dex file for the string index.
192 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
193 if (string_id != NULL) {
194 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
195 mirror::String* image = dex_cache->GetResolvedString(string_idx);
196 if (image != NULL) {
197 return image;
198 }
199 }
200 }
201 return NULL;
202}
203
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700204void InternTable::AllowNewInterns() {
205 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100206 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700207 allow_new_interns_ = true;
208 new_intern_condition_.Broadcast(self);
209}
210
211void InternTable::DisallowNewInterns() {
212 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100213 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700214 allow_new_interns_ = false;
215}
216
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700218 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100219 MutexLock mu(self, *Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700220
221 DCHECK(s != NULL);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700222
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700223 while (UNLIKELY(!allow_new_interns_)) {
224 new_intern_condition_.WaitHoldingLocks(self);
225 }
226
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700227 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -0700228 // Check the strong table for a match.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700229 mirror::String* strong = LookupStrong(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700230 if (strong != NULL) {
231 return strong;
232 }
233
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700234 // Check the image for a match.
235 mirror::String* image = LookupStringFromImage(s);
236 if (image != NULL) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700237 return InsertStrong(image);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700238 }
239
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700240 // There is no match in the strong table, check the weak table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700241 mirror::String* weak = LookupWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700242 if (weak != NULL) {
243 // A match was found in the weak table. Promote to the strong table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700244 RemoveWeak(weak);
245 return InsertStrong(weak);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700246 }
247
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700248 // No match in the strong table or the weak table. Insert into the strong
249 // table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700250 return InsertStrong(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700251 }
252
253 // Check the strong table for a match.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700254 mirror::String* strong = LookupStrong(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700255 if (strong != NULL) {
256 return strong;
257 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700258 // Check the image for a match.
259 mirror::String* image = LookupStringFromImage(s);
jeffhaob1c6f342012-03-20 17:57:26 -0700260 if (image != NULL) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700261 return InsertWeak(image);
jeffhaob1c6f342012-03-20 17:57:26 -0700262 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700263 // Check the weak table for a match.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700264 mirror::String* weak = LookupWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700265 if (weak != NULL) {
266 return weak;
267 }
268 // Insert into the weak table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700269 return InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700270}
271
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700272mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
273 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700274 return InternStrong(mirror::String::AllocFromModifiedUtf8(
275 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700276}
277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700279 DCHECK(utf8_data != nullptr);
280 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700281}
282
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283mirror::String* InternTable::InternStrong(mirror::String* s) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800284 if (s == nullptr) {
285 return nullptr;
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700286 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700287 return Insert(s, true);
288}
289
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290mirror::String* InternTable::InternWeak(mirror::String* s) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800291 if (s == nullptr) {
292 return nullptr;
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700293 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700294 return Insert(s, false);
295}
296
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700299 return LookupWeak(s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700300}
301
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800302void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100303 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700304 weak_interns_.SweepWeaks(callback, arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700305}
306
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700307std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) {
308 if (kIsDebugBuild) {
309 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
310 }
Mathieu Chartierc2bda532014-09-02 16:20:45 -0700311 return static_cast<size_t>(const_cast<GcRoot<mirror::String>&>(root).Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700312}
313
314bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
315 const GcRoot<mirror::String>& b) {
316 if (kIsDebugBuild) {
317 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
318 }
Mathieu Chartierc2bda532014-09-02 16:20:45 -0700319 return const_cast<GcRoot<mirror::String>&>(a).Read()->Equals(
320 const_cast<GcRoot<mirror::String>&>(b).Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700321}
322
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700323void InternTable::Table::Remove(mirror::String* s) {
324 auto it = post_zygote_table_.find(GcRoot<mirror::String>(s));
325 if (it != post_zygote_table_.end()) {
326 post_zygote_table_.erase(it);
327 } else {
328 it = pre_zygote_table_.find(GcRoot<mirror::String>(s));
329 DCHECK(it != pre_zygote_table_.end());
330 pre_zygote_table_.erase(it);
331 }
332}
333
334mirror::String* InternTable::Table::Find(mirror::String* s) {
335 Locks::intern_table_lock_->AssertHeld(Thread::Current());
336 auto it = pre_zygote_table_.find(GcRoot<mirror::String>(s));
337 if (LIKELY(it != pre_zygote_table_.end())) {
338 return const_cast<GcRoot<mirror::String>&>(*it).Read();
339 }
340 it = post_zygote_table_.find(GcRoot<mirror::String>(s));
341 if (LIKELY(it != post_zygote_table_.end())) {
342 return const_cast<GcRoot<mirror::String>&>(*it).Read();
343 }
344 return nullptr;
345}
346
347void InternTable::Table::SwapPostZygoteWithPreZygote() {
348 CHECK(pre_zygote_table_.empty());
349 std::swap(pre_zygote_table_, post_zygote_table_);
350}
351
352void InternTable::Table::Insert(mirror::String* s) {
353 // Always insert the post zygote table, this gets swapped when we create the zygote to be the
354 // pre zygote table.
355 post_zygote_table_.insert(GcRoot<mirror::String>(s));
356}
357
358void InternTable::Table::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
359 for (auto& intern : pre_zygote_table_) {
360 const_cast<GcRoot<mirror::String>&>(intern).VisitRoot(callback, arg, 0, kRootInternedString);
361 }
362 for (auto& intern : post_zygote_table_) {
363 const_cast<GcRoot<mirror::String>&>(intern).VisitRoot(callback, arg, 0, kRootInternedString);
364 }
365}
366
367void InternTable::Table::SweepWeaks(IsMarkedCallback* callback, void* arg) {
368 SweepWeaks(&pre_zygote_table_, callback, arg);
369 SweepWeaks(&post_zygote_table_, callback, arg);
370}
371
372void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedCallback* callback, void* arg) {
373 for (auto it = set->begin(), end = set->end(); it != end;) {
374 // This does not need a read barrier because this is called by GC.
375 GcRoot<mirror::String>& root = const_cast<GcRoot<mirror::String>&>(*it);
376 mirror::Object* object = root.Read<kWithoutReadBarrier>();
377 mirror::Object* new_object = callback(object, arg);
378 if (new_object == nullptr) {
379 it = set->erase(it);
380 } else {
381 root = GcRoot<mirror::String>(new_object->AsString());
382 ++it;
383 }
384 }
385}
386
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700387} // namespace art