blob: 96854dad0ef54538c33d02aa5503bf94f39e4243 [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"
Vladimir Marko05792b92015-08-03 11:56:49 +010025#include "mirror/dex_cache-inl.h"
Ian Rogers7dfb28c2013-08-22 08:18:36 -070026#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 Chartierea0831f2015-12-29 13:17:37 -080035 : images_added_to_intern_table_(false),
36 log_new_roots_(false),
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070037 weak_intern_condition_("New intern condition", *Locks::intern_table_lock_),
38 weak_root_state_(gc::kWeakRootStateNormal) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070039}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070040
Brian Carlstroma663ea52011-08-19 23:33:41 -070041size_t InternTable::Size() const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010042 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070043 return strong_interns_.Size() + weak_interns_.Size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070044}
45
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070046size_t InternTable::StrongSize() const {
47 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070048 return strong_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070049}
50
51size_t InternTable::WeakSize() const {
52 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070053 return weak_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070054}
55
Elliott Hughescac6cc72011-11-03 20:31:21 -070056void InternTable::DumpForSigQuit(std::ostream& os) const {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070057 os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070058}
59
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070060void InternTable::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010061 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080062 if ((flags & kVisitRootFlagAllRoots) != 0) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070063 strong_interns_.VisitRoots(visitor);
Mathieu Chartier893263b2014-03-04 11:07:42 -080064 } else if ((flags & kVisitRootFlagNewRoots) != 0) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070065 for (auto& root : new_strong_intern_roots_) {
66 mirror::String* old_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070067 root.VisitRoot(visitor, RootInfo(kRootInternedString));
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070068 mirror::String* new_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierc2e20622014-11-03 11:41:47 -080069 if (new_ref != old_ref) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070070 // The GC moved a root in the log. Need to search the strong interns and update the
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070071 // corresponding object. This is slow, but luckily for us, this may only happen with a
72 // concurrent moving GC.
Mathieu Chartiereb175f72014-10-31 11:49:27 -070073 strong_interns_.Remove(old_ref);
74 strong_interns_.Insert(new_ref);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070075 }
76 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080077 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080078 if ((flags & kVisitRootFlagClearRootLog) != 0) {
79 new_strong_intern_roots_.clear();
80 }
81 if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
82 log_new_roots_ = true;
83 } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
84 log_new_roots_ = false;
Ian Rogers1d54e732013-05-02 21:10:01 -070085 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070086 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070087}
88
Mathieu Chartierfbc31082016-01-24 11:59:56 -080089mirror::String* InternTable::LookupWeak(Thread* self, mirror::String* s) {
90 MutexLock mu(self, *Locks::intern_table_lock_);
91 return LookupWeakLocked(s);
Mathieu Chartierf7fd9702015-11-09 11:16:49 -080092}
93
Mathieu Chartierfbc31082016-01-24 11:59:56 -080094mirror::String* InternTable::LookupStrong(Thread* self, mirror::String* s) {
95 MutexLock mu(self, *Locks::intern_table_lock_);
96 return LookupStrongLocked(s);
97}
98
99mirror::String* InternTable::LookupWeakLocked(mirror::String* s) {
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +0000100 return weak_interns_.Find(s);
101}
102
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800103mirror::String* InternTable::LookupStrongLocked(mirror::String* s) {
104 return strong_interns_.Find(s);
105}
106
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800107void InternTable::AddNewTable() {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700108 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800109 weak_interns_.AddNewTable();
110 strong_interns_.AddNewTable();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700111}
112
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700113mirror::String* InternTable::InsertStrong(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->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800118 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700119 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800120 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700121 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800122 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100123}
124
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700125mirror::String* InternTable::InsertWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100126 Runtime* runtime = Runtime::Current();
127 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700128 runtime->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100129 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700130 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700131 return s;
132}
133
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700134void InternTable::RemoveStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700135 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700136}
137
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700138void InternTable::RemoveWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100139 Runtime* runtime = Runtime::Current();
140 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700141 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700143 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700144}
145
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700147mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700149 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700151mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100152 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700153 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700155void InternTable::RemoveStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100156 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700157 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100158}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700159void InternTable::RemoveWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100160 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700161 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100162}
163
Mathieu Chartier205b7622016-01-06 15:47:09 -0800164void InternTable::AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700165 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800166 for (gc::space::ImageSpace* image_space : image_spaces) {
167 const ImageHeader* const header = &image_space->GetImageHeader();
168 // Check if we have the interned strings section.
169 const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings);
170 if (section.Size() > 0) {
171 AddTableFromMemoryLocked(image_space->Begin() + section.Offset());
172 } else {
173 // TODO: Delete this logic?
174 mirror::Object* root = header->GetImageRoot(ImageHeader::kDexCaches);
175 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
176 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
177 mirror::DexCache* dex_cache = dex_caches->Get(i);
178 const size_t num_strings = dex_cache->NumStrings();
179 for (size_t j = 0; j < num_strings; ++j) {
180 mirror::String* image_string = dex_cache->GetResolvedString(j);
181 if (image_string != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800182 mirror::String* found = LookupStrongLocked(image_string);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800183 if (found == nullptr) {
184 InsertStrong(image_string);
185 } else {
186 DCHECK_EQ(found, image_string);
187 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700188 }
189 }
190 }
191 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700192 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800193 images_added_to_intern_table_ = true;
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700194}
195
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700196mirror::String* InternTable::LookupStringFromImage(mirror::String* s) {
Mathieu Chartier205b7622016-01-06 15:47:09 -0800197 DCHECK(!images_added_to_intern_table_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800198 const std::vector<gc::space::ImageSpace*>& image_spaces =
Jeff Haodcdc85b2015-12-04 14:06:18 -0800199 Runtime::Current()->GetHeap()->GetBootImageSpaces();
200 if (image_spaces.empty()) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700201 return nullptr; // No image present.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700202 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700203 const std::string utf8 = s->ToModifiedUtf8();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800204 for (gc::space::ImageSpace* image_space : image_spaces) {
205 mirror::Object* root = image_space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
206 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
207 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
208 mirror::DexCache* dex_cache = dex_caches->Get(i);
209 const DexFile* dex_file = dex_cache->GetDexFile();
210 // Binary search the dex file for the string index.
211 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
212 if (string_id != nullptr) {
213 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
214 // GetResolvedString() contains a RB.
215 mirror::String* image_string = dex_cache->GetResolvedString(string_idx);
216 if (image_string != nullptr) {
217 return image_string;
218 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700219 }
220 }
221 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800222 return nullptr;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700223}
224
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700225void InternTable::BroadcastForNewInterns() {
226 CHECK(kUseReadBarrier);
227 Thread* self = Thread::Current();
228 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700229 weak_intern_condition_.Broadcast(self);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700230}
231
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700232void InternTable::WaitUntilAccessible(Thread* self) {
233 Locks::intern_table_lock_->ExclusiveUnlock(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700234 {
235 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
236 MutexLock mu(self, *Locks::intern_table_lock_);
237 while (weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) {
238 weak_intern_condition_.Wait(self);
239 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700240 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700241 Locks::intern_table_lock_->ExclusiveLock(self);
242}
243
244mirror::String* InternTable::Insert(mirror::String* s, bool is_strong, bool holding_locks) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800245 if (s == nullptr) {
246 return nullptr;
247 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700248 Thread* const self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100249 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700250 if (kDebugLocking && !holding_locks) {
251 Locks::mutator_lock_->AssertSharedHeld(self);
252 CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock";
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700253 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700254 while (true) {
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700255 if (holding_locks) {
256 if (!kUseReadBarrier) {
257 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
258 } else {
259 CHECK(self->GetWeakRefAccessEnabled());
260 }
261 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700262 // Check the strong table for a match.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800263 mirror::String* strong = LookupStrongLocked(s);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700264 if (strong != nullptr) {
265 return strong;
266 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700267 if ((!kUseReadBarrier && weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) ||
268 (kUseReadBarrier && self->GetWeakRefAccessEnabled())) {
269 break;
270 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700271 // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only
272 // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is
273 // cleared.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700274 CHECK(!holding_locks);
275 StackHandleScope<1> hs(self);
276 auto h = hs.NewHandleWrapper(&s);
277 WaitUntilAccessible(self);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700278 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700279 if (!kUseReadBarrier) {
280 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
281 } else {
282 CHECK(self->GetWeakRefAccessEnabled());
283 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800284 // There is no match in the strong table, check the weak table.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800285 mirror::String* weak = LookupWeakLocked(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800286 if (weak != nullptr) {
287 if (is_strong) {
288 // A match was found in the weak table. Promote to the strong table.
289 RemoveWeak(weak);
290 return InsertStrong(weak);
291 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700292 return weak;
293 }
nikolay serdjuka446d862015-04-17 19:27:56 +0600294 // Check the image for a match.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800295 if (!images_added_to_intern_table_) {
296 mirror::String* const image_string = LookupStringFromImage(s);
297 if (image_string != nullptr) {
298 return is_strong ? InsertStrong(image_string) : InsertWeak(image_string);
299 }
nikolay serdjuka446d862015-04-17 19:27:56 +0600300 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800301 // No match in the strong table or the weak table. Insert into the strong / weak table.
302 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700303}
304
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700305mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
306 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700307 return InternStrong(mirror::String::AllocFromModifiedUtf8(
308 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700309}
310
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700312 DCHECK(utf8_data != nullptr);
313 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700314}
315
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700316mirror::String* InternTable::InternStrongImageString(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700317 // May be holding the heap bitmap lock.
318 return Insert(s, true, true);
319}
320
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800321mirror::String* InternTable::InternStrong(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700322 return Insert(s, true, false);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700323}
324
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325mirror::String* InternTable::InternWeak(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700326 return Insert(s, false, false);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700327}
328
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329bool InternTable::ContainsWeak(mirror::String* s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800330 return LookupWeak(Thread::Current(), s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700331}
332
Mathieu Chartier97509952015-07-13 14:35:43 -0700333void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100334 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700335 weak_interns_.SweepWeaks(visitor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700336}
337
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800338size_t InternTable::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700339 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800340 return AddTableFromMemoryLocked(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700341}
342
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800343size_t InternTable::AddTableFromMemoryLocked(const uint8_t* ptr) {
344 return strong_interns_.AddTableFromMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700345}
346
347size_t InternTable::WriteToMemory(uint8_t* ptr) {
348 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800349 return strong_interns_.WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700350}
351
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800352std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700353 if (kIsDebugBuild) {
354 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
355 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800356 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700357}
358
359bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800360 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700361 if (kIsDebugBuild) {
362 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
363 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800364 return a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700365}
366
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800367size_t InternTable::Table::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700368 size_t read_count = 0;
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800369 UnorderedSet set(ptr, /*make copy*/false, &read_count);
370 // TODO: Disable this for app images if app images have intern tables.
371 static constexpr bool kCheckDuplicates = true;
372 if (kCheckDuplicates) {
373 for (GcRoot<mirror::String>& string : set) {
374 CHECK(Find(string.Read()) == nullptr) << "Already found " << string.Read()->ToModifiedUtf8();
375 }
376 }
377 // Insert at the front since we insert into the back.
378 tables_.insert(tables_.begin(), std::move(set));
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700379 return read_count;
380}
381
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800382size_t InternTable::Table::WriteToMemory(uint8_t* ptr) {
383 if (tables_.empty()) {
384 return 0;
385 }
386 UnorderedSet* table_to_write;
387 UnorderedSet combined;
388 if (tables_.size() > 1) {
389 table_to_write = &combined;
390 for (UnorderedSet& table : tables_) {
391 for (GcRoot<mirror::String>& string : table) {
392 combined.Insert(string);
393 }
394 }
395 } else {
396 table_to_write = &tables_.back();
397 }
398 return table_to_write->WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700399}
400
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700401void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800402 for (UnorderedSet& table : tables_) {
403 auto it = table.Find(GcRoot<mirror::String>(s));
404 if (it != table.end()) {
405 table.Erase(it);
406 return;
407 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700408 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800409 LOG(FATAL) << "Attempting to remove non-interned string " << s->ToModifiedUtf8();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700410}
411
412mirror::String* InternTable::Table::Find(mirror::String* s) {
413 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800414 for (UnorderedSet& table : tables_) {
415 auto it = table.Find(GcRoot<mirror::String>(s));
416 if (it != table.end()) {
417 return it->Read();
418 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700419 }
420 return nullptr;
421}
422
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800423void InternTable::Table::AddNewTable() {
424 tables_.push_back(UnorderedSet());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700425}
426
427void InternTable::Table::Insert(mirror::String* s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800428 // Always insert the last table, the image tables are before and we avoid inserting into these
429 // to prevent dirty pages.
430 DCHECK(!tables_.empty());
431 tables_.back().Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700432}
433
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700434void InternTable::Table::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700435 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
436 visitor, RootInfo(kRootInternedString));
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800437 for (UnorderedSet& table : tables_) {
438 for (auto& intern : table) {
439 buffered_visitor.VisitRoot(intern);
440 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700441 }
442}
443
Mathieu Chartier97509952015-07-13 14:35:43 -0700444void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800445 for (UnorderedSet& table : tables_) {
446 SweepWeaks(&table, visitor);
447 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700448}
449
Mathieu Chartier97509952015-07-13 14:35:43 -0700450void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700451 for (auto it = set->begin(), end = set->end(); it != end;) {
452 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800453 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -0700454 mirror::Object* new_object = visitor->IsMarked(object);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700455 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800456 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700457 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800458 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700459 ++it;
460 }
461 }
462}
463
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800464size_t InternTable::Table::Size() const {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800465 return std::accumulate(tables_.begin(),
466 tables_.end(),
Mathieu Chartier205b7622016-01-06 15:47:09 -0800467 0U,
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800468 [](size_t sum, const UnorderedSet& set) {
469 return sum + set.Size();
470 });
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800471}
472
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700473void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) {
474 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
475 ChangeWeakRootStateLocked(new_state);
476}
477
478void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700479 CHECK(!kUseReadBarrier);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700480 weak_root_state_ = new_state;
481 if (new_state != gc::kWeakRootStateNoReadsOrWrites) {
482 weak_intern_condition_.Broadcast(Thread::Current());
483 }
484}
485
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700486InternTable::Table::Table() {
487 Runtime* const runtime = Runtime::Current();
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800488 // Initial table.
489 tables_.push_back(UnorderedSet());
490 tables_.back().SetLoadFactor(runtime->GetHashTableMinLoadFactor(),
491 runtime->GetHashTableMaxLoadFactor());
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700492}
493
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700494} // namespace art