blob: 74a2532a5ded99d91770d86488c92a6344a139be [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"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080025#include "image-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010026#include "mirror/dex_cache-inl.h"
Ian Rogers7dfb28c2013-08-22 08:18:36 -070027#include "mirror/object_array-inl.h"
28#include "mirror/object-inl.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070029#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070031#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070032
33namespace art {
34
Ian Rogers7dfb28c2013-08-22 08:18:36 -070035InternTable::InternTable()
Mathieu Chartierea0831f2015-12-29 13:17:37 -080036 : images_added_to_intern_table_(false),
37 log_new_roots_(false),
Mathieu Chartier14c3bf92015-07-13 14:35:43 -070038 weak_intern_condition_("New intern condition", *Locks::intern_table_lock_),
39 weak_root_state_(gc::kWeakRootStateNormal) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070040}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070041
Brian Carlstroma663ea52011-08-19 23:33:41 -070042size_t InternTable::Size() const {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010043 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070044 return strong_interns_.Size() + weak_interns_.Size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070045}
46
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070047size_t InternTable::StrongSize() const {
48 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070049 return strong_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070050}
51
52size_t InternTable::WeakSize() const {
53 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070054 return weak_interns_.Size();
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070055}
56
Elliott Hughescac6cc72011-11-03 20:31:21 -070057void InternTable::DumpForSigQuit(std::ostream& os) const {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070058 os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070059}
60
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070061void InternTable::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010062 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080063 if ((flags & kVisitRootFlagAllRoots) != 0) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070064 strong_interns_.VisitRoots(visitor);
Mathieu Chartier893263b2014-03-04 11:07:42 -080065 } else if ((flags & kVisitRootFlagNewRoots) != 0) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070066 for (auto& root : new_strong_intern_roots_) {
67 mirror::String* old_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070068 root.VisitRoot(visitor, RootInfo(kRootInternedString));
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070069 mirror::String* new_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierc2e20622014-11-03 11:41:47 -080070 if (new_ref != old_ref) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070071 // The GC moved a root in the log. Need to search the strong interns and update the
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070072 // corresponding object. This is slow, but luckily for us, this may only happen with a
73 // concurrent moving GC.
Mathieu Chartiereb175f72014-10-31 11:49:27 -070074 strong_interns_.Remove(old_ref);
75 strong_interns_.Insert(new_ref);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070076 }
77 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080078 }
Mathieu Chartier893263b2014-03-04 11:07:42 -080079 if ((flags & kVisitRootFlagClearRootLog) != 0) {
80 new_strong_intern_roots_.clear();
81 }
82 if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
83 log_new_roots_ = true;
84 } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
85 log_new_roots_ = false;
Ian Rogers1d54e732013-05-02 21:10:01 -070086 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070087 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070088}
89
Mathieu Chartierfbc31082016-01-24 11:59:56 -080090mirror::String* InternTable::LookupWeak(Thread* self, mirror::String* s) {
91 MutexLock mu(self, *Locks::intern_table_lock_);
92 return LookupWeakLocked(s);
Mathieu Chartierf7fd9702015-11-09 11:16:49 -080093}
94
Mathieu Chartierfbc31082016-01-24 11:59:56 -080095mirror::String* InternTable::LookupStrong(Thread* self, mirror::String* s) {
96 MutexLock mu(self, *Locks::intern_table_lock_);
97 return LookupStrongLocked(s);
98}
99
100mirror::String* InternTable::LookupWeakLocked(mirror::String* s) {
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +0000101 return weak_interns_.Find(s);
102}
103
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800104mirror::String* InternTable::LookupStrongLocked(mirror::String* s) {
105 return strong_interns_.Find(s);
106}
107
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800108void InternTable::AddNewTable() {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700109 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800110 weak_interns_.AddNewTable();
111 strong_interns_.AddNewTable();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700112}
113
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700114mirror::String* InternTable::InsertStrong(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 Runtime* runtime = Runtime::Current();
116 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700117 runtime->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800119 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700120 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800121 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700122 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800123 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100124}
125
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700126mirror::String* InternTable::InsertWeak(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->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100130 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700131 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700132 return s;
133}
134
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700135void InternTable::RemoveStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700136 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700137}
138
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700139void InternTable::RemoveWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140 Runtime* runtime = Runtime::Current();
141 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700142 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100143 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700144 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700145}
146
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100147// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700148mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100149 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700150 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100151}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700152mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700154 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100155}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700156void InternTable::RemoveStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100157 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700158 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700160void InternTable::RemoveWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100161 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700162 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100163}
164
Mathieu Chartier205b7622016-01-06 15:47:09 -0800165void InternTable::AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700166 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800167 for (gc::space::ImageSpace* image_space : image_spaces) {
168 const ImageHeader* const header = &image_space->GetImageHeader();
169 // Check if we have the interned strings section.
170 const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings);
171 if (section.Size() > 0) {
172 AddTableFromMemoryLocked(image_space->Begin() + section.Offset());
173 } else {
174 // TODO: Delete this logic?
175 mirror::Object* root = header->GetImageRoot(ImageHeader::kDexCaches);
176 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
177 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
178 mirror::DexCache* dex_cache = dex_caches->Get(i);
179 const size_t num_strings = dex_cache->NumStrings();
180 for (size_t j = 0; j < num_strings; ++j) {
181 mirror::String* image_string = dex_cache->GetResolvedString(j);
182 if (image_string != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800183 mirror::String* found = LookupStrongLocked(image_string);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800184 if (found == nullptr) {
185 InsertStrong(image_string);
186 } else {
187 DCHECK_EQ(found, image_string);
188 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700189 }
190 }
191 }
192 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700193 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800194 images_added_to_intern_table_ = true;
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700195}
196
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700197mirror::String* InternTable::LookupStringFromImage(mirror::String* s) {
Mathieu Chartier205b7622016-01-06 15:47:09 -0800198 DCHECK(!images_added_to_intern_table_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800199 const std::vector<gc::space::ImageSpace*>& image_spaces =
Jeff Haodcdc85b2015-12-04 14:06:18 -0800200 Runtime::Current()->GetHeap()->GetBootImageSpaces();
201 if (image_spaces.empty()) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700202 return nullptr; // No image present.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700203 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700204 const std::string utf8 = s->ToModifiedUtf8();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800205 for (gc::space::ImageSpace* image_space : image_spaces) {
206 mirror::Object* root = image_space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
207 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
208 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
209 mirror::DexCache* dex_cache = dex_caches->Get(i);
210 const DexFile* dex_file = dex_cache->GetDexFile();
211 // Binary search the dex file for the string index.
212 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
213 if (string_id != nullptr) {
214 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
215 // GetResolvedString() contains a RB.
216 mirror::String* image_string = dex_cache->GetResolvedString(string_idx);
217 if (image_string != nullptr) {
218 return image_string;
219 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700220 }
221 }
222 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800223 return nullptr;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700224}
225
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700226void InternTable::BroadcastForNewInterns() {
227 CHECK(kUseReadBarrier);
228 Thread* self = Thread::Current();
229 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700230 weak_intern_condition_.Broadcast(self);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700231}
232
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700233void InternTable::WaitUntilAccessible(Thread* self) {
234 Locks::intern_table_lock_->ExclusiveUnlock(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700235 {
236 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
237 MutexLock mu(self, *Locks::intern_table_lock_);
238 while (weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) {
239 weak_intern_condition_.Wait(self);
240 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700241 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700242 Locks::intern_table_lock_->ExclusiveLock(self);
243}
244
245mirror::String* InternTable::Insert(mirror::String* s, bool is_strong, bool holding_locks) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800246 if (s == nullptr) {
247 return nullptr;
248 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700249 Thread* const self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700251 if (kDebugLocking && !holding_locks) {
252 Locks::mutator_lock_->AssertSharedHeld(self);
253 CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock";
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700254 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700255 while (true) {
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700256 if (holding_locks) {
257 if (!kUseReadBarrier) {
258 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
259 } else {
260 CHECK(self->GetWeakRefAccessEnabled());
261 }
262 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700263 // Check the strong table for a match.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800264 mirror::String* strong = LookupStrongLocked(s);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700265 if (strong != nullptr) {
266 return strong;
267 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700268 if ((!kUseReadBarrier && weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) ||
269 (kUseReadBarrier && self->GetWeakRefAccessEnabled())) {
270 break;
271 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700272 // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only
273 // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is
274 // cleared.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700275 CHECK(!holding_locks);
276 StackHandleScope<1> hs(self);
277 auto h = hs.NewHandleWrapper(&s);
278 WaitUntilAccessible(self);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700279 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700280 if (!kUseReadBarrier) {
281 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
282 } else {
283 CHECK(self->GetWeakRefAccessEnabled());
284 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800285 // There is no match in the strong table, check the weak table.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800286 mirror::String* weak = LookupWeakLocked(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800287 if (weak != nullptr) {
288 if (is_strong) {
289 // A match was found in the weak table. Promote to the strong table.
290 RemoveWeak(weak);
291 return InsertStrong(weak);
292 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700293 return weak;
294 }
nikolay serdjuka446d862015-04-17 19:27:56 +0600295 // Check the image for a match.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800296 if (!images_added_to_intern_table_) {
297 mirror::String* const image_string = LookupStringFromImage(s);
298 if (image_string != nullptr) {
299 return is_strong ? InsertStrong(image_string) : InsertWeak(image_string);
300 }
nikolay serdjuka446d862015-04-17 19:27:56 +0600301 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800302 // No match in the strong table or the weak table. Insert into the strong / weak table.
303 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700304}
305
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700306mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
307 DCHECK(utf8_data != nullptr);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700308 return InternStrong(mirror::String::AllocFromModifiedUtf8(
309 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700310}
311
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700313 DCHECK(utf8_data != nullptr);
314 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700315}
316
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700317mirror::String* InternTable::InternStrongImageString(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700318 // May be holding the heap bitmap lock.
319 return Insert(s, true, true);
320}
321
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322mirror::String* InternTable::InternStrong(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700323 return Insert(s, true, false);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700324}
325
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326mirror::String* InternTable::InternWeak(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700327 return Insert(s, false, false);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700328}
329
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330bool InternTable::ContainsWeak(mirror::String* s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800331 return LookupWeak(Thread::Current(), s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700332}
333
Mathieu Chartier97509952015-07-13 14:35:43 -0700334void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100335 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700336 weak_interns_.SweepWeaks(visitor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700337}
338
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800339size_t InternTable::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700340 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800341 return AddTableFromMemoryLocked(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700342}
343
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800344size_t InternTable::AddTableFromMemoryLocked(const uint8_t* ptr) {
345 return strong_interns_.AddTableFromMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700346}
347
348size_t InternTable::WriteToMemory(uint8_t* ptr) {
349 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800350 return strong_interns_.WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700351}
352
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800353std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700354 if (kIsDebugBuild) {
355 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
356 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800357 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700358}
359
360bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800361 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700362 if (kIsDebugBuild) {
363 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
364 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800365 return a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700366}
367
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800368size_t InternTable::Table::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700369 size_t read_count = 0;
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800370 UnorderedSet set(ptr, /*make copy*/false, &read_count);
371 // TODO: Disable this for app images if app images have intern tables.
372 static constexpr bool kCheckDuplicates = true;
373 if (kCheckDuplicates) {
374 for (GcRoot<mirror::String>& string : set) {
375 CHECK(Find(string.Read()) == nullptr) << "Already found " << string.Read()->ToModifiedUtf8();
376 }
377 }
378 // Insert at the front since we insert into the back.
379 tables_.insert(tables_.begin(), std::move(set));
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700380 return read_count;
381}
382
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800383size_t InternTable::Table::WriteToMemory(uint8_t* ptr) {
384 if (tables_.empty()) {
385 return 0;
386 }
387 UnorderedSet* table_to_write;
388 UnorderedSet combined;
389 if (tables_.size() > 1) {
390 table_to_write = &combined;
391 for (UnorderedSet& table : tables_) {
392 for (GcRoot<mirror::String>& string : table) {
393 combined.Insert(string);
394 }
395 }
396 } else {
397 table_to_write = &tables_.back();
398 }
399 return table_to_write->WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700400}
401
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700402void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800403 for (UnorderedSet& table : tables_) {
404 auto it = table.Find(GcRoot<mirror::String>(s));
405 if (it != table.end()) {
406 table.Erase(it);
407 return;
408 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700409 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800410 LOG(FATAL) << "Attempting to remove non-interned string " << s->ToModifiedUtf8();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700411}
412
413mirror::String* InternTable::Table::Find(mirror::String* s) {
414 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800415 for (UnorderedSet& table : tables_) {
416 auto it = table.Find(GcRoot<mirror::String>(s));
417 if (it != table.end()) {
418 return it->Read();
419 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700420 }
421 return nullptr;
422}
423
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800424void InternTable::Table::AddNewTable() {
425 tables_.push_back(UnorderedSet());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700426}
427
428void InternTable::Table::Insert(mirror::String* s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800429 // Always insert the last table, the image tables are before and we avoid inserting into these
430 // to prevent dirty pages.
431 DCHECK(!tables_.empty());
432 tables_.back().Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700433}
434
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700435void InternTable::Table::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700436 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
437 visitor, RootInfo(kRootInternedString));
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800438 for (UnorderedSet& table : tables_) {
439 for (auto& intern : table) {
440 buffered_visitor.VisitRoot(intern);
441 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700442 }
443}
444
Mathieu Chartier97509952015-07-13 14:35:43 -0700445void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800446 for (UnorderedSet& table : tables_) {
447 SweepWeaks(&table, visitor);
448 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700449}
450
Mathieu Chartier97509952015-07-13 14:35:43 -0700451void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700452 for (auto it = set->begin(), end = set->end(); it != end;) {
453 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800454 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -0700455 mirror::Object* new_object = visitor->IsMarked(object);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700456 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800457 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700458 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800459 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700460 ++it;
461 }
462 }
463}
464
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800465size_t InternTable::Table::Size() const {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800466 return std::accumulate(tables_.begin(),
467 tables_.end(),
Mathieu Chartier205b7622016-01-06 15:47:09 -0800468 0U,
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800469 [](size_t sum, const UnorderedSet& set) {
470 return sum + set.Size();
471 });
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800472}
473
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700474void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) {
475 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
476 ChangeWeakRootStateLocked(new_state);
477}
478
479void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700480 CHECK(!kUseReadBarrier);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700481 weak_root_state_ = new_state;
482 if (new_state != gc::kWeakRootStateNoReadsOrWrites) {
483 weak_intern_condition_.Broadcast(Thread::Current());
484 }
485}
486
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700487InternTable::Table::Table() {
488 Runtime* const runtime = Runtime::Current();
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800489 // Initial table.
490 tables_.push_back(UnorderedSet());
491 tables_.back().SetLoadFactor(runtime->GetHashTableMinLoadFactor(),
492 runtime->GetHashTableMaxLoadFactor());
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700493}
494
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700495} // namespace art