blob: 6ea047fbe4ae52f0e3b192d2688d5855e8b2ccdf [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "gc_root-inl.h"
Mathieu Chartier97509952015-07-13 14:35:43 -070022#include "gc/collector/garbage_collector.h"
Ian Rogers7dfb28c2013-08-22 08:18:36 -070023#include "gc/space/image_space.h"
24#include "mirror/dex_cache.h"
25#include "mirror/object_array-inl.h"
26#include "mirror/object-inl.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070027#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070029#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070030
31namespace art {
32
Ian Rogers7dfb28c2013-08-22 08:18:36 -070033InternTable::InternTable()
Mathieu Chartiereb175f72014-10-31 11:49:27 -070034 : image_added_to_intern_table_(false), log_new_roots_(false),
35 allow_new_interns_(true),
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010036 new_intern_condition_("New intern condition", *Locks::intern_table_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070037}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070038
Brian Carlstroma663ea52011-08-19 23:33:41 -070039size_t InternTable::Size() const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010040 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070041 return strong_interns_.Size() + weak_interns_.Size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070042}
43
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070044size_t InternTable::StrongSize() const {
45 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070046 return strong_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070047}
48
49size_t InternTable::WeakSize() const {
50 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070051 return weak_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070052}
53
Elliott Hughescac6cc72011-11-03 20:31:21 -070054void InternTable::DumpForSigQuit(std::ostream& os) const {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070055 os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070056}
57
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070058void InternTable::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010059 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080060 if ((flags & kVisitRootFlagAllRoots) != 0) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070061 strong_interns_.VisitRoots(visitor);
Mathieu Chartier893263b2014-03-04 11:07:42 -080062 } else if ((flags & kVisitRootFlagNewRoots) != 0) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070063 for (auto& root : new_strong_intern_roots_) {
64 mirror::String* old_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070065 root.VisitRoot(visitor, RootInfo(kRootInternedString));
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070066 mirror::String* new_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierc2e20622014-11-03 11:41:47 -080067 if (new_ref != old_ref) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070068 // The GC moved a root in the log. Need to search the strong interns and update the
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070069 // corresponding object. This is slow, but luckily for us, this may only happen with a
70 // concurrent moving GC.
Mathieu Chartiereb175f72014-10-31 11:49:27 -070071 strong_interns_.Remove(old_ref);
72 strong_interns_.Insert(new_ref);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070073 }
74 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080075 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080076 if ((flags & kVisitRootFlagClearRootLog) != 0) {
77 new_strong_intern_roots_.clear();
78 }
79 if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
80 log_new_roots_ = true;
81 } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
82 log_new_roots_ = false;
Ian Rogers1d54e732013-05-02 21:10:01 -070083 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070084 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070085}
86
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070087mirror::String* InternTable::LookupStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070088 return strong_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070089}
90
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070091mirror::String* InternTable::LookupWeak(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070092 return weak_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070093}
94
Mathieu Chartiereb175f72014-10-31 11:49:27 -070095void InternTable::SwapPostZygoteWithPreZygote() {
96 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
97 weak_interns_.SwapPostZygoteWithPreZygote();
98 strong_interns_.SwapPostZygoteWithPreZygote();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070099}
100
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700101mirror::String* InternTable::InsertStrong(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100102 Runtime* runtime = Runtime::Current();
103 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700104 runtime->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100105 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800106 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700107 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800108 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700109 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800110 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100111}
112
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700113mirror::String* InternTable::InsertWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114 Runtime* runtime = Runtime::Current();
115 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700116 runtime->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700118 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700119 return s;
120}
121
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700122void InternTable::RemoveStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700123 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700124}
125
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700126void InternTable::RemoveWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100127 Runtime* runtime = Runtime::Current();
128 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700129 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100130 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700131 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700132}
133
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100134// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700135mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100136 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700137 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700139mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700141 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700143void InternTable::RemoveStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100144 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700145 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700147void InternTable::RemoveWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700149 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150}
151
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700152void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space) {
Mathieu Chartierbc58ede2014-11-17 12:36:24 -0800153 CHECK(image_space != nullptr);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700154 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
155 if (!image_added_to_intern_table_) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700156 const ImageHeader* const header = &image_space->GetImageHeader();
157 // Check if we have the interned strings section.
158 const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings);
159 if (section.Size() > 0) {
160 ReadFromMemoryLocked(image_space->Begin() + section.Offset());
161 } else {
162 // TODO: Delete this logic?
163 mirror::Object* root = header->GetImageRoot(ImageHeader::kDexCaches);
164 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
165 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
166 mirror::DexCache* dex_cache = dex_caches->Get(i);
167 const DexFile* dex_file = dex_cache->GetDexFile();
168 const size_t num_strings = dex_file->NumStringIds();
169 for (size_t j = 0; j < num_strings; ++j) {
170 mirror::String* image_string = dex_cache->GetResolvedString(j);
171 if (image_string != nullptr) {
172 mirror::String* found = LookupStrong(image_string);
173 if (found == nullptr) {
174 InsertStrong(image_string);
175 } else {
176 DCHECK_EQ(found, image_string);
177 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700178 }
179 }
180 }
181 }
182 image_added_to_intern_table_ = true;
183 }
184}
185
186mirror::String* InternTable::LookupStringFromImage(mirror::String* s)
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700188 if (image_added_to_intern_table_) {
189 return nullptr;
190 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700191 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700192 if (image == nullptr) {
193 return nullptr; // No image present.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700194 }
195 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
196 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
197 const std::string utf8 = s->ToModifiedUtf8();
198 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
199 mirror::DexCache* dex_cache = dex_caches->Get(i);
200 const DexFile* dex_file = dex_cache->GetDexFile();
201 // Binary search the dex file for the string index.
202 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800203 if (string_id != nullptr) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700204 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800205 // GetResolvedString() contains a RB.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800206 mirror::String* image_string = dex_cache->GetResolvedString(string_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700207 if (image_string != nullptr) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800208 return image_string;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700209 }
210 }
211 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800212 return nullptr;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700213}
214
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700215void InternTable::AllowNewInterns() {
216 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100217 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700218 allow_new_interns_ = true;
219 new_intern_condition_.Broadcast(self);
220}
221
222void InternTable::DisallowNewInterns() {
223 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100224 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700225 allow_new_interns_ = false;
226}
227
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800228void InternTable::EnsureNewInternsDisallowed() {
229 // Lock and unlock once to ensure that no threads are still in the
230 // middle of adding new interns.
231 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
232 CHECK(!allow_new_interns_);
233}
234
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700235void InternTable::BroadcastForNewInterns() {
236 CHECK(kUseReadBarrier);
237 Thread* self = Thread::Current();
238 MutexLock mu(self, *Locks::intern_table_lock_);
239 new_intern_condition_.Broadcast(self);
240}
241
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800243 if (s == nullptr) {
244 return nullptr;
245 }
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700246 Thread* self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100247 MutexLock mu(self, *Locks::intern_table_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700248 while (UNLIKELY((!kUseReadBarrier && !allow_new_interns_) ||
249 (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700250 new_intern_condition_.WaitHoldingLocks(self);
251 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700252 // Check the strong table for a match.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700253 mirror::String* strong = LookupStrong(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800254 if (strong != nullptr) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700255 return strong;
256 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800257 // There is no match in the strong table, check the weak table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700258 mirror::String* weak = LookupWeak(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800259 if (weak != nullptr) {
260 if (is_strong) {
261 // A match was found in the weak table. Promote to the strong table.
262 RemoveWeak(weak);
263 return InsertStrong(weak);
264 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700265 return weak;
266 }
nikolay serdjuka446d862015-04-17 19:27:56 +0600267 // Check the image for a match.
268 mirror::String* image = LookupStringFromImage(s);
269 if (image != nullptr) {
270 return is_strong ? InsertStrong(image) : InsertWeak(image);
271 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800272 // No match in the strong table or the weak table. Insert into the strong / weak table.
273 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700274}
275
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700276mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
277 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700278 return InternStrong(mirror::String::AllocFromModifiedUtf8(
279 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700280}
281
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700283 DCHECK(utf8_data != nullptr);
284 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700285}
286
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287mirror::String* InternTable::InternStrong(mirror::String* s) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700288 return Insert(s, true);
289}
290
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700292 return Insert(s, false);
293}
294
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100296 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700297 return LookupWeak(s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700298}
299
Mathieu Chartier97509952015-07-13 14:35:43 -0700300void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100301 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700302 weak_interns_.SweepWeaks(visitor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700303}
304
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700305void InternTable::AddImageInternTable(gc::space::ImageSpace* image_space) {
306 const ImageSection& intern_section = image_space->GetImageHeader().GetImageSection(
307 ImageHeader::kSectionInternedStrings);
308 // Read the string tables from the image.
309 const uint8_t* ptr = image_space->Begin() + intern_section.Offset();
310 const size_t offset = ReadFromMemory(ptr);
311 CHECK_LE(offset, intern_section.Size());
312}
313
314size_t InternTable::ReadFromMemory(const uint8_t* ptr) {
315 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
316 return ReadFromMemoryLocked(ptr);
317}
318
319size_t InternTable::ReadFromMemoryLocked(const uint8_t* ptr) {
320 return strong_interns_.ReadIntoPreZygoteTable(ptr);
321}
322
323size_t InternTable::WriteToMemory(uint8_t* ptr) {
324 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
325 return strong_interns_.WriteFromPostZygoteTable(ptr);
326}
327
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800328std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700329 if (kIsDebugBuild) {
330 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
331 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800332 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700333}
334
335bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800336 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700337 if (kIsDebugBuild) {
338 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
339 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800340 return a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700341}
342
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700343size_t InternTable::Table::ReadIntoPreZygoteTable(const uint8_t* ptr) {
344 CHECK_EQ(pre_zygote_table_.Size(), 0u);
345 size_t read_count = 0;
346 pre_zygote_table_ = UnorderedSet(ptr, false /* make copy */, &read_count);
347 return read_count;
348}
349
350size_t InternTable::Table::WriteFromPostZygoteTable(uint8_t* ptr) {
351 return post_zygote_table_.WriteToMemory(ptr);
352}
353
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700354void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800355 auto it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700356 if (it != post_zygote_table_.end()) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800357 post_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700358 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800359 it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700360 DCHECK(it != pre_zygote_table_.end());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800361 pre_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700362 }
363}
364
365mirror::String* InternTable::Table::Find(mirror::String* s) {
366 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800367 auto it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
368 if (it != pre_zygote_table_.end()) {
369 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700370 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800371 it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
372 if (it != post_zygote_table_.end()) {
373 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700374 }
375 return nullptr;
376}
377
378void InternTable::Table::SwapPostZygoteWithPreZygote() {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700379 if (pre_zygote_table_.Empty()) {
380 std::swap(pre_zygote_table_, post_zygote_table_);
381 VLOG(heap) << "Swapping " << pre_zygote_table_.Size() << " interns to the pre zygote table";
382 } else {
383 // This case happens if read the intern table from the image.
384 VLOG(heap) << "Not swapping due to non-empty pre_zygote_table_";
385 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700386}
387
388void InternTable::Table::Insert(mirror::String* s) {
389 // Always insert the post zygote table, this gets swapped when we create the zygote to be the
390 // pre zygote table.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800391 post_zygote_table_.Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700392}
393
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700394void InternTable::Table::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700395 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
396 visitor, RootInfo(kRootInternedString));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700397 for (auto& intern : pre_zygote_table_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700398 buffered_visitor.VisitRoot(intern);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700399 }
400 for (auto& intern : post_zygote_table_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700401 buffered_visitor.VisitRoot(intern);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700402 }
403}
404
Mathieu Chartier97509952015-07-13 14:35:43 -0700405void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
406 SweepWeaks(&pre_zygote_table_, visitor);
407 SweepWeaks(&post_zygote_table_, visitor);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700408}
409
Mathieu Chartier97509952015-07-13 14:35:43 -0700410void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700411 for (auto it = set->begin(), end = set->end(); it != end;) {
412 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800413 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -0700414 mirror::Object* new_object = visitor->IsMarked(object);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700415 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800416 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700417 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800418 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700419 ++it;
420 }
421 }
422}
423
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800424size_t InternTable::Table::Size() const {
425 return pre_zygote_table_.Size() + post_zygote_table_.Size();
426}
427
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700428} // namespace art