blob: 56a6d2cc5058ce657b9ca1b94995c28dc3b5984d [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>();
63 root.VisitRoot(callback, arg, 0, kRootInternedString);
64 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) {
151 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
152 if (!image_added_to_intern_table_) {
153 mirror::Object* root = image_space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
154 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
155 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
156 mirror::DexCache* dex_cache = dex_caches->Get(i);
157 const DexFile* dex_file = dex_cache->GetDexFile();
158 const size_t num_strings = dex_file->NumStringIds();
159 for (size_t j = 0; j < num_strings; ++j) {
160 mirror::String* image_string = dex_cache->GetResolvedString(j);
161 if (image_string != nullptr) {
162 mirror::String* found = LookupStrong(image_string);
163 if (found == nullptr) {
164 InsertStrong(image_string);
165 } else {
166 DCHECK_EQ(found, image_string);
167 }
168 }
169 }
170 }
171 image_added_to_intern_table_ = true;
172 }
173}
174
175mirror::String* InternTable::LookupStringFromImage(mirror::String* s)
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700177 if (image_added_to_intern_table_) {
178 return nullptr;
179 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700180 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700181 if (image == nullptr) {
182 return nullptr; // No image present.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700183 }
184 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
185 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
186 const std::string utf8 = s->ToModifiedUtf8();
187 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
188 mirror::DexCache* dex_cache = dex_caches->Get(i);
189 const DexFile* dex_file = dex_cache->GetDexFile();
190 // Binary search the dex file for the string index.
191 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800192 if (string_id != nullptr) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700193 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800194 mirror::String* image_string = dex_cache->GetResolvedString(string_idx);
195 if (image_string != NULL) {
196 return image_string;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700197 }
198 }
199 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800200 return nullptr;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700201}
202
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700203void InternTable::AllowNewInterns() {
204 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100205 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700206 allow_new_interns_ = true;
207 new_intern_condition_.Broadcast(self);
208}
209
210void InternTable::DisallowNewInterns() {
211 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100212 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700213 allow_new_interns_ = false;
214}
215
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800217 if (s == nullptr) {
218 return nullptr;
219 }
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700220 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100221 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700222 while (UNLIKELY(!allow_new_interns_)) {
223 new_intern_condition_.WaitHoldingLocks(self);
224 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700225 // Check the strong table for a match.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700226 mirror::String* strong = LookupStrong(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800227 if (strong != nullptr) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700228 return strong;
229 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700230 // Check the image for a match.
231 mirror::String* image = LookupStringFromImage(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800232 if (image != nullptr) {
233 return is_strong ? InsertStrong(image) : InsertWeak(image);
jeffhaob1c6f342012-03-20 17:57:26 -0700234 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800235 // There is no match in the strong table, check the weak table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700236 mirror::String* weak = LookupWeak(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800237 if (weak != nullptr) {
238 if (is_strong) {
239 // A match was found in the weak table. Promote to the strong table.
240 RemoveWeak(weak);
241 return InsertStrong(weak);
242 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700243 return weak;
244 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800245 // No match in the strong table or the weak table. Insert into the strong / weak table.
246 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700247}
248
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700249mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
250 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700251 return InternStrong(mirror::String::AllocFromModifiedUtf8(
252 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700253}
254
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700256 DCHECK(utf8_data != nullptr);
257 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700258}
259
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260mirror::String* InternTable::InternStrong(mirror::String* s) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700261 return Insert(s, true);
262}
263
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700265 return Insert(s, false);
266}
267
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100269 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700270 return LookupWeak(s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700271}
272
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800273void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100274 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700275 weak_interns_.SweepWeaks(callback, arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700276}
277
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800278std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700279 if (kIsDebugBuild) {
280 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
281 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800282 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700283}
284
285bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800286 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700287 if (kIsDebugBuild) {
288 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
289 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800290 return a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700291}
292
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700293void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800294 auto it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700295 if (it != post_zygote_table_.end()) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800296 post_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700297 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800298 it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700299 DCHECK(it != pre_zygote_table_.end());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800300 pre_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700301 }
302}
303
304mirror::String* InternTable::Table::Find(mirror::String* s) {
305 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800306 auto it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
307 if (it != pre_zygote_table_.end()) {
308 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700309 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800310 it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
311 if (it != post_zygote_table_.end()) {
312 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700313 }
314 return nullptr;
315}
316
317void InternTable::Table::SwapPostZygoteWithPreZygote() {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800318 CHECK(pre_zygote_table_.Empty());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700319 std::swap(pre_zygote_table_, post_zygote_table_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800320 VLOG(heap) << "Swapping " << pre_zygote_table_.Size() << " interns to the pre zygote table";
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700321}
322
323void InternTable::Table::Insert(mirror::String* s) {
324 // Always insert the post zygote table, this gets swapped when we create the zygote to be the
325 // pre zygote table.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800326 post_zygote_table_.Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700327}
328
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800329void InternTable::Table::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700330 for (auto& intern : pre_zygote_table_) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800331 intern.VisitRoot(callback, arg, 0, kRootInternedString);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700332 }
333 for (auto& intern : post_zygote_table_) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800334 intern.VisitRoot(callback, arg, 0, kRootInternedString);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700335 }
336}
337
338void InternTable::Table::SweepWeaks(IsMarkedCallback* callback, void* arg) {
339 SweepWeaks(&pre_zygote_table_, callback, arg);
340 SweepWeaks(&post_zygote_table_, callback, arg);
341}
342
343void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedCallback* callback, void* arg) {
344 for (auto it = set->begin(), end = set->end(); it != end;) {
345 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800346 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700347 mirror::Object* new_object = callback(object, arg);
348 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800349 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700350 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800351 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700352 ++it;
353 }
354 }
355}
356
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800357size_t InternTable::Table::Size() const {
358 return pre_zygote_table_.Size() + post_zygote_table_.Size();
359}
360
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700361} // namespace art