blob: ae521b164e4bac2165c99af0f950c9595704ebfe [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"
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070024#include "gc/weak_root_state.h"
Ian Rogers7dfb28c2013-08-22 08:18:36 -070025#include "mirror/dex_cache.h"
26#include "mirror/object_array-inl.h"
27#include "mirror/object-inl.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070028#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070030#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070031
32namespace art {
33
Ian Rogers7dfb28c2013-08-22 08:18:36 -070034InternTable::InternTable()
Mathieu Chartiereb175f72014-10-31 11:49:27 -070035 : image_added_to_intern_table_(false), log_new_roots_(false),
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070036 weak_intern_condition_("New intern condition", *Locks::intern_table_lock_),
37 weak_root_state_(gc::kWeakRootStateNormal) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070038}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070039
Brian Carlstroma663ea52011-08-19 23:33:41 -070040size_t InternTable::Size() const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010041 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070042 return strong_interns_.Size() + weak_interns_.Size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070043}
44
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070045size_t InternTable::StrongSize() const {
46 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070047 return strong_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070048}
49
50size_t InternTable::WeakSize() const {
51 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070052 return weak_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070053}
54
Elliott Hughescac6cc72011-11-03 20:31:21 -070055void InternTable::DumpForSigQuit(std::ostream& os) const {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070056 os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070057}
58
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070059void InternTable::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010060 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080061 if ((flags & kVisitRootFlagAllRoots) != 0) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070062 strong_interns_.VisitRoots(visitor);
Mathieu Chartier893263b2014-03-04 11:07:42 -080063 } else if ((flags & kVisitRootFlagNewRoots) != 0) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070064 for (auto& root : new_strong_intern_roots_) {
65 mirror::String* old_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070066 root.VisitRoot(visitor, RootInfo(kRootInternedString));
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070067 mirror::String* new_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierc2e20622014-11-03 11:41:47 -080068 if (new_ref != old_ref) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070069 // The GC moved a root in the log. Need to search the strong interns and update the
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070070 // corresponding object. This is slow, but luckily for us, this may only happen with a
71 // concurrent moving GC.
Mathieu Chartiereb175f72014-10-31 11:49:27 -070072 strong_interns_.Remove(old_ref);
73 strong_interns_.Insert(new_ref);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070074 }
75 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080076 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080077 if ((flags & kVisitRootFlagClearRootLog) != 0) {
78 new_strong_intern_roots_.clear();
79 }
80 if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
81 log_new_roots_ = true;
82 } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
83 log_new_roots_ = false;
Ian Rogers1d54e732013-05-02 21:10:01 -070084 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070085 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070086}
87
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070088mirror::String* InternTable::LookupStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070089 return strong_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070090}
91
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070092mirror::String* InternTable::LookupWeak(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070093 // TODO: Return only if marked.
Mathieu Chartiereb175f72014-10-31 11:49:27 -070094 return weak_interns_.Find(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070095}
96
Mathieu Chartiereb175f72014-10-31 11:49:27 -070097void InternTable::SwapPostZygoteWithPreZygote() {
98 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
99 weak_interns_.SwapPostZygoteWithPreZygote();
100 strong_interns_.SwapPostZygoteWithPreZygote();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700101}
102
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700103mirror::String* InternTable::InsertStrong(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104 Runtime* runtime = Runtime::Current();
105 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700106 runtime->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100107 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800108 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700109 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800110 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700111 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800112 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113}
114
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700115mirror::String* InternTable::InsertWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 Runtime* runtime = Runtime::Current();
117 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700118 runtime->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100119 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700120 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700121 return s;
122}
123
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700124void InternTable::RemoveStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700125 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700126}
127
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700128void InternTable::RemoveWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100129 Runtime* runtime = Runtime::Current();
130 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700131 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100132 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700133 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700134}
135
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100136// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700137mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700139 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700141mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700143 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100144}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700145void InternTable::RemoveStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700147 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700149void InternTable::RemoveWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700151 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100152}
153
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700154void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space) {
Mathieu Chartierbc58ede2014-11-17 12:36:24 -0800155 CHECK(image_space != nullptr);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700156 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
157 if (!image_added_to_intern_table_) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700158 const ImageHeader* const header = &image_space->GetImageHeader();
159 // Check if we have the interned strings section.
160 const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings);
161 if (section.Size() > 0) {
162 ReadFromMemoryLocked(image_space->Begin() + section.Offset());
163 } else {
164 // TODO: Delete this logic?
165 mirror::Object* root = header->GetImageRoot(ImageHeader::kDexCaches);
166 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
167 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
168 mirror::DexCache* dex_cache = dex_caches->Get(i);
169 const DexFile* dex_file = dex_cache->GetDexFile();
170 const size_t num_strings = dex_file->NumStringIds();
171 for (size_t j = 0; j < num_strings; ++j) {
172 mirror::String* image_string = dex_cache->GetResolvedString(j);
173 if (image_string != nullptr) {
174 mirror::String* found = LookupStrong(image_string);
175 if (found == nullptr) {
176 InsertStrong(image_string);
177 } else {
178 DCHECK_EQ(found, image_string);
179 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700180 }
181 }
182 }
183 }
184 image_added_to_intern_table_ = true;
185 }
186}
187
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700188mirror::String* InternTable::LookupStringFromImage(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700189 if (image_added_to_intern_table_) {
190 return nullptr;
191 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700192 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700193 if (image == nullptr) {
194 return nullptr; // No image present.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700195 }
196 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
197 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
198 const std::string utf8 = s->ToModifiedUtf8();
199 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
200 mirror::DexCache* dex_cache = dex_caches->Get(i);
201 const DexFile* dex_file = dex_cache->GetDexFile();
202 // Binary search the dex file for the string index.
203 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800204 if (string_id != nullptr) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700205 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800206 // GetResolvedString() contains a RB.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800207 mirror::String* image_string = dex_cache->GetResolvedString(string_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700208 if (image_string != nullptr) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800209 return image_string;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700210 }
211 }
212 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800213 return nullptr;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700214}
215
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700216void InternTable::EnsureNewWeakInternsDisallowed() {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800217 // Lock and unlock once to ensure that no threads are still in the
218 // middle of adding new interns.
219 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700220 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNoReadsOrWrites);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800221}
222
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700223void InternTable::BroadcastForNewInterns() {
224 CHECK(kUseReadBarrier);
225 Thread* self = Thread::Current();
226 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700227 weak_intern_condition_.Broadcast(self);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700228}
229
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700230void InternTable::WaitUntilAccessible(Thread* self) {
231 Locks::intern_table_lock_->ExclusiveUnlock(self);
232 self->TransitionFromRunnableToSuspended(kWaitingWeakRootRead);
233 Locks::intern_table_lock_->ExclusiveLock(self);
234 while (weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) {
235 weak_intern_condition_.Wait(self);
236 }
237 Locks::intern_table_lock_->ExclusiveUnlock(self);
238 self->TransitionFromSuspendedToRunnable();
239 Locks::intern_table_lock_->ExclusiveLock(self);
240}
241
242mirror::String* InternTable::Insert(mirror::String* s, bool is_strong, bool holding_locks) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800243 if (s == nullptr) {
244 return nullptr;
245 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700246 Thread* const self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100247 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700248 if (kDebugLocking && !holding_locks) {
249 Locks::mutator_lock_->AssertSharedHeld(self);
250 CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock";
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700251 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700252 while (true) {
253 // Check the strong table for a match.
254 mirror::String* strong = LookupStrong(s);
255 if (strong != nullptr) {
256 return strong;
257 }
258 // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only
259 // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is
260 // cleared.
261 if (weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) {
262 break;
263 }
264 CHECK(!holding_locks);
265 StackHandleScope<1> hs(self);
266 auto h = hs.NewHandleWrapper(&s);
267 WaitUntilAccessible(self);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700268 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700269 CHECK_NE(weak_root_state_, gc::kWeakRootStateNoReadsOrWrites);
270 DCHECK_NE(weak_root_state_, gc::kWeakRootStateMarkNewRoots) << "Unsupported";
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800271 // There is no match in the strong table, check the weak table.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700272 mirror::String* weak = LookupWeak(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800273 if (weak != nullptr) {
274 if (is_strong) {
275 // A match was found in the weak table. Promote to the strong table.
276 RemoveWeak(weak);
277 return InsertStrong(weak);
278 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700279 return weak;
280 }
nikolay serdjuka446d862015-04-17 19:27:56 +0600281 // Check the image for a match.
282 mirror::String* image = LookupStringFromImage(s);
283 if (image != nullptr) {
284 return is_strong ? InsertStrong(image) : InsertWeak(image);
285 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800286 // No match in the strong table or the weak table. Insert into the strong / weak table.
287 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700288}
289
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700290mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
291 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700292 return InternStrong(mirror::String::AllocFromModifiedUtf8(
293 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700294}
295
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700297 DCHECK(utf8_data != nullptr);
298 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700299}
300
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700301mirror::String* InternTable::InternImageString(mirror::String* s) {
302 // May be holding the heap bitmap lock.
303 return Insert(s, true, true);
304}
305
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306mirror::String* InternTable::InternStrong(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700307 return Insert(s, true, false);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700308}
309
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310mirror::String* InternTable::InternWeak(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700311 return Insert(s, false, false);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700312}
313
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314bool InternTable::ContainsWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100315 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700316 return LookupWeak(s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700317}
318
Mathieu Chartier97509952015-07-13 14:35:43 -0700319void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100320 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700321 weak_interns_.SweepWeaks(visitor);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700322 // Done sweeping, back to a normal state.
323 ChangeWeakRootStateLocked(gc::kWeakRootStateNormal);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700324}
325
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700326void InternTable::AddImageInternTable(gc::space::ImageSpace* image_space) {
327 const ImageSection& intern_section = image_space->GetImageHeader().GetImageSection(
328 ImageHeader::kSectionInternedStrings);
329 // Read the string tables from the image.
330 const uint8_t* ptr = image_space->Begin() + intern_section.Offset();
331 const size_t offset = ReadFromMemory(ptr);
332 CHECK_LE(offset, intern_section.Size());
333}
334
335size_t InternTable::ReadFromMemory(const uint8_t* ptr) {
336 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
337 return ReadFromMemoryLocked(ptr);
338}
339
340size_t InternTable::ReadFromMemoryLocked(const uint8_t* ptr) {
341 return strong_interns_.ReadIntoPreZygoteTable(ptr);
342}
343
344size_t InternTable::WriteToMemory(uint8_t* ptr) {
345 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
346 return strong_interns_.WriteFromPostZygoteTable(ptr);
347}
348
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800349std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700350 if (kIsDebugBuild) {
351 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
352 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800353 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700354}
355
356bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800357 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700358 if (kIsDebugBuild) {
359 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
360 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800361 return a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700362}
363
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700364size_t InternTable::Table::ReadIntoPreZygoteTable(const uint8_t* ptr) {
365 CHECK_EQ(pre_zygote_table_.Size(), 0u);
366 size_t read_count = 0;
367 pre_zygote_table_ = UnorderedSet(ptr, false /* make copy */, &read_count);
368 return read_count;
369}
370
371size_t InternTable::Table::WriteFromPostZygoteTable(uint8_t* ptr) {
372 return post_zygote_table_.WriteToMemory(ptr);
373}
374
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700375void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800376 auto it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700377 if (it != post_zygote_table_.end()) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800378 post_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700379 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800380 it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700381 DCHECK(it != pre_zygote_table_.end());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800382 pre_zygote_table_.Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700383 }
384}
385
386mirror::String* InternTable::Table::Find(mirror::String* s) {
387 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800388 auto it = pre_zygote_table_.Find(GcRoot<mirror::String>(s));
389 if (it != pre_zygote_table_.end()) {
390 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700391 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800392 it = post_zygote_table_.Find(GcRoot<mirror::String>(s));
393 if (it != post_zygote_table_.end()) {
394 return it->Read();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700395 }
396 return nullptr;
397}
398
399void InternTable::Table::SwapPostZygoteWithPreZygote() {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700400 if (pre_zygote_table_.Empty()) {
401 std::swap(pre_zygote_table_, post_zygote_table_);
402 VLOG(heap) << "Swapping " << pre_zygote_table_.Size() << " interns to the pre zygote table";
403 } else {
404 // This case happens if read the intern table from the image.
405 VLOG(heap) << "Not swapping due to non-empty pre_zygote_table_";
406 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700407}
408
409void InternTable::Table::Insert(mirror::String* s) {
410 // Always insert the post zygote table, this gets swapped when we create the zygote to be the
411 // pre zygote table.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800412 post_zygote_table_.Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700413}
414
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700415void InternTable::Table::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700416 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
417 visitor, RootInfo(kRootInternedString));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700418 for (auto& intern : pre_zygote_table_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700419 buffered_visitor.VisitRoot(intern);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700420 }
421 for (auto& intern : post_zygote_table_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700422 buffered_visitor.VisitRoot(intern);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700423 }
424}
425
Mathieu Chartier97509952015-07-13 14:35:43 -0700426void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
427 SweepWeaks(&pre_zygote_table_, visitor);
428 SweepWeaks(&post_zygote_table_, visitor);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700429}
430
Mathieu Chartier97509952015-07-13 14:35:43 -0700431void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700432 for (auto it = set->begin(), end = set->end(); it != end;) {
433 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800434 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -0700435 mirror::Object* new_object = visitor->IsMarked(object);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700436 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800437 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700438 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800439 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700440 ++it;
441 }
442 }
443}
444
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800445size_t InternTable::Table::Size() const {
446 return pre_zygote_table_.Size() + post_zygote_table_.Size();
447}
448
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700449void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) {
450 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
451 ChangeWeakRootStateLocked(new_state);
452}
453
454void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) {
455 weak_root_state_ = new_state;
456 if (new_state != gc::kWeakRootStateNoReadsOrWrites) {
457 weak_intern_condition_.Broadcast(Thread::Current());
458 }
459}
460
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700461} // namespace art