blob: 19bfc4e3534656022e18de59269ee4eaff81dca0 [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 Chartierc2e20622014-11-03 11:41:47 -080059 strong_interns_.VisitRoots(callback, arg);
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>();
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080063 root.VisitRoot(callback, arg, RootInfo(kRootInternedString));
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070064 mirror::String* new_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierc2e20622014-11-03 11:41:47 -080065 if (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 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080074 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 Chartiercdfd39f2014-08-29 18:16:58 -070085mirror::String* InternTable::LookupStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070086 return strong_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070087}
88
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070089mirror::String* InternTable::LookupWeak(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070090 return weak_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070091}
92
Mathieu Chartiereb175f72014-10-31 11:49:27 -070093void InternTable::SwapPostZygoteWithPreZygote() {
94 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
95 weak_interns_.SwapPostZygoteWithPreZygote();
96 strong_interns_.SwapPostZygoteWithPreZygote();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070097}
98
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070099mirror::String* InternTable::InsertStrong(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100100 Runtime* runtime = Runtime::Current();
101 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700102 runtime->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100103 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800104 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700105 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800106 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700107 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800108 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100109}
110
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700111mirror::String* InternTable::InsertWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100112 Runtime* runtime = Runtime::Current();
113 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700114 runtime->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700116 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700117 return s;
118}
119
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700120void InternTable::RemoveStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700121 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700122}
123
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700124void InternTable::RemoveWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100125 Runtime* runtime = Runtime::Current();
126 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700127 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100128 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700129 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700130}
131
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100132// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700133mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100134 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700135 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100136}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700137mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700139 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700141void InternTable::RemoveStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700143 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100144}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700145void InternTable::RemoveWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700147 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148}
149
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700150void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space) {
Mathieu Chartierbc58ede2014-11-17 12:36:24 -0800151 CHECK(image_space != nullptr);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700152 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());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800193 if (string_id != nullptr) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700194 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800195 // GetResolvedString() contains a RB.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800196 mirror::String* image_string = dex_cache->GetResolvedString(string_idx);
197 if (image_string != NULL) {
198 return image_string;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700199 }
200 }
201 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800202 return nullptr;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700203}
204
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700205void InternTable::AllowNewInterns() {
206 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100207 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700208 allow_new_interns_ = true;
209 new_intern_condition_.Broadcast(self);
210}
211
212void InternTable::DisallowNewInterns() {
213 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100214 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700215 allow_new_interns_ = false;
216}
217
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800218void InternTable::EnsureNewInternsDisallowed() {
219 // Lock and unlock once to ensure that no threads are still in the
220 // middle of adding new interns.
221 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
222 CHECK(!allow_new_interns_);
223}
224
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800226 if (s == nullptr) {
227 return nullptr;
228 }
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700229 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100230 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700231 while (UNLIKELY(!allow_new_interns_)) {
232 new_intern_condition_.WaitHoldingLocks(self);
233 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700234 // Check the strong table for a match.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700235 mirror::String* strong = LookupStrong(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800236 if (strong != nullptr) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700237 return strong;
238 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700239 // Check the image for a match.
240 mirror::String* image = LookupStringFromImage(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800241 if (image != nullptr) {
242 return is_strong ? InsertStrong(image) : InsertWeak(image);
jeffhaob1c6f342012-03-20 17:57:26 -0700243 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800244 // There is no match in the strong table, check the weak table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700245 mirror::String* weak = LookupWeak(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800246 if (weak != nullptr) {
247 if (is_strong) {
248 // A match was found in the weak table. Promote to the strong table.
249 RemoveWeak(weak);
250 return InsertStrong(weak);
251 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700252 return weak;
253 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800254 // No match in the strong table or the weak table. Insert into the strong / weak table.
255 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700256}
257
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700258mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
259 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700260 return InternStrong(mirror::String::AllocFromModifiedUtf8(
261 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700262}
263
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700265 DCHECK(utf8_data != nullptr);
266 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700267}
268
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269mirror::String* InternTable::InternStrong(mirror::String* s) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700270 return Insert(s, true);
271}
272
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700274 return Insert(s, false);
275}
276
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100278 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700279 return LookupWeak(s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700280}
281
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800282void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100283 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700284 weak_interns_.SweepWeaks(callback, arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700285}
286
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800287std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700288 if (kIsDebugBuild) {
289 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
290 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800291 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700292}
293
294bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800295 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700296 if (kIsDebugBuild) {
297 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
298 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800299 return a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700300}
301
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700302void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800303 auto it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700304 if (it != post_zygote_table_.end()) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800305 post_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700306 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800307 it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700308 DCHECK(it != pre_zygote_table_.end());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800309 pre_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700310 }
311}
312
313mirror::String* InternTable::Table::Find(mirror::String* s) {
314 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800315 auto it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
316 if (it != pre_zygote_table_.end()) {
317 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700318 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800319 it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
320 if (it != post_zygote_table_.end()) {
321 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700322 }
323 return nullptr;
324}
325
326void InternTable::Table::SwapPostZygoteWithPreZygote() {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800327 CHECK(pre_zygote_table_.Empty());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700328 std::swap(pre_zygote_table_, post_zygote_table_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800329 VLOG(heap) << "Swapping " << pre_zygote_table_.Size() << " interns to the pre zygote table";
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700330}
331
332void InternTable::Table::Insert(mirror::String* s) {
333 // Always insert the post zygote table, this gets swapped when we create the zygote to be the
334 // pre zygote table.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800335 post_zygote_table_.Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700336}
337
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800338void InternTable::Table::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700339 for (auto& intern : pre_zygote_table_) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800340 intern.VisitRoot(callback, arg, RootInfo(kRootInternedString));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700341 }
342 for (auto& intern : post_zygote_table_) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800343 intern.VisitRoot(callback, arg, RootInfo(kRootInternedString));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700344 }
345}
346
347void InternTable::Table::SweepWeaks(IsMarkedCallback* callback, void* arg) {
348 SweepWeaks(&pre_zygote_table_, callback, arg);
349 SweepWeaks(&post_zygote_table_, callback, arg);
350}
351
352void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedCallback* callback, void* arg) {
353 for (auto it = set->begin(), end = set->end(); it != end;) {
354 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800355 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700356 mirror::Object* new_object = callback(object, arg);
357 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800358 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700359 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800360 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700361 ++it;
362 }
363 }
364}
365
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800366size_t InternTable::Table::Size() const {
367 return pre_zygote_table_.Size() + post_zygote_table_.Size();
368}
369
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700370} // namespace art