blob: f92f20930629baa3e05ca67329cde14abcd830ae [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);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800195 mirror::String* image_string = dex_cache->GetResolvedString(string_idx);
196 if (image_string != NULL) {
197 return image_string;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700198 }
199 }
200 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800201 return nullptr;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700202}
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 Chartierc2e20622014-11-03 11:41:47 -0800218 if (s == nullptr) {
219 return nullptr;
220 }
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700221 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700223 while (UNLIKELY(!allow_new_interns_)) {
224 new_intern_condition_.WaitHoldingLocks(self);
225 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700226 // Check the strong table for a match.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700227 mirror::String* strong = LookupStrong(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800228 if (strong != nullptr) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700229 return strong;
230 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700231 // Check the image for a match.
232 mirror::String* image = LookupStringFromImage(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800233 if (image != nullptr) {
234 return is_strong ? InsertStrong(image) : InsertWeak(image);
jeffhaob1c6f342012-03-20 17:57:26 -0700235 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800236 // There is no match in the strong table, check the weak table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700237 mirror::String* weak = LookupWeak(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800238 if (weak != nullptr) {
239 if (is_strong) {
240 // A match was found in the weak table. Promote to the strong table.
241 RemoveWeak(weak);
242 return InsertStrong(weak);
243 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700244 return weak;
245 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800246 // No match in the strong table or the weak table. Insert into the strong / weak table.
247 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700248}
249
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700250mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
251 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700252 return InternStrong(mirror::String::AllocFromModifiedUtf8(
253 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700254}
255
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700257 DCHECK(utf8_data != nullptr);
258 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700259}
260
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261mirror::String* InternTable::InternStrong(mirror::String* s) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700262 return Insert(s, true);
263}
264
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700266 return Insert(s, false);
267}
268
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100270 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700271 return LookupWeak(s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700272}
273
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800274void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100275 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700276 weak_interns_.SweepWeaks(callback, arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700277}
278
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800279std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700280 if (kIsDebugBuild) {
281 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
282 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800283 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700284}
285
286bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800287 const GcRoot<mirror::String>& b) 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 a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700292}
293
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700294void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800295 auto it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700296 if (it != post_zygote_table_.end()) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800297 post_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700298 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800299 it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700300 DCHECK(it != pre_zygote_table_.end());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800301 pre_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700302 }
303}
304
305mirror::String* InternTable::Table::Find(mirror::String* s) {
306 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800307 auto it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
308 if (it != pre_zygote_table_.end()) {
309 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700310 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800311 it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
312 if (it != post_zygote_table_.end()) {
313 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700314 }
315 return nullptr;
316}
317
318void InternTable::Table::SwapPostZygoteWithPreZygote() {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800319 CHECK(pre_zygote_table_.Empty());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700320 std::swap(pre_zygote_table_, post_zygote_table_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800321 VLOG(heap) << "Swapping " << pre_zygote_table_.Size() << " interns to the pre zygote table";
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700322}
323
324void InternTable::Table::Insert(mirror::String* s) {
325 // Always insert the post zygote table, this gets swapped when we create the zygote to be the
326 // pre zygote table.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800327 post_zygote_table_.Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700328}
329
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800330void InternTable::Table::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700331 for (auto& intern : pre_zygote_table_) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800332 intern.VisitRoot(callback, arg, RootInfo(kRootInternedString));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700333 }
334 for (auto& intern : post_zygote_table_) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800335 intern.VisitRoot(callback, arg, RootInfo(kRootInternedString));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700336 }
337}
338
339void InternTable::Table::SweepWeaks(IsMarkedCallback* callback, void* arg) {
340 SweepWeaks(&pre_zygote_table_, callback, arg);
341 SweepWeaks(&post_zygote_table_, callback, arg);
342}
343
344void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedCallback* callback, void* arg) {
345 for (auto it = set->begin(), end = set->end(); it != end;) {
346 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800347 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700348 mirror::Object* new_object = callback(object, arg);
349 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800350 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700351 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800352 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700353 ++it;
354 }
355 }
356}
357
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800358size_t InternTable::Table::Size() const {
359 return pre_zygote_table_.Size() + post_zygote_table_.Size();
360}
361
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700362} // namespace art