blob: a61a1878af2d90354fc18803286717806ce400db [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()
Vladimir Marko1a1de672016-10-13 12:53:15 +010036 : 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
Vladimir Markocac5a7e2016-02-22 10:39:50 +000099mirror::String* InternTable::LookupStrong(Thread* self,
100 uint32_t utf16_length,
101 const char* utf8_data) {
102 DCHECK_EQ(utf16_length, CountModifiedUtf8Chars(utf8_data));
103 Utf8String string(utf16_length,
104 utf8_data,
105 ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length));
106 MutexLock mu(self, *Locks::intern_table_lock_);
107 return strong_interns_.Find(string);
108}
109
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800110mirror::String* InternTable::LookupWeakLocked(mirror::String* s) {
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +0000111 return weak_interns_.Find(s);
112}
113
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800114mirror::String* InternTable::LookupStrongLocked(mirror::String* s) {
115 return strong_interns_.Find(s);
116}
117
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800118void InternTable::AddNewTable() {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700119 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800120 weak_interns_.AddNewTable();
121 strong_interns_.AddNewTable();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700122}
123
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700124mirror::String* InternTable::InsertStrong(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100125 Runtime* runtime = Runtime::Current();
126 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700127 runtime->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100128 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800129 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700130 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800131 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700132 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800133 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100134}
135
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700136mirror::String* InternTable::InsertWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137 Runtime* runtime = Runtime::Current();
138 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700139 runtime->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700141 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700142 return s;
143}
144
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700145void InternTable::RemoveStrong(mirror::String* s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700146 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700147}
148
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700149void InternTable::RemoveWeak(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150 Runtime* runtime = Runtime::Current();
151 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700152 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700154 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700155}
156
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100157// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700158mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700160 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100161}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700162mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100163 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700164 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100165}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700166void InternTable::RemoveStrongFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100167 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700168 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100169}
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700170void InternTable::RemoveWeakFromTransaction(mirror::String* s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100171 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700172 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100173}
174
Mathieu Chartier205b7622016-01-06 15:47:09 -0800175void InternTable::AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700176 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800177 for (gc::space::ImageSpace* image_space : image_spaces) {
178 const ImageHeader* const header = &image_space->GetImageHeader();
179 // Check if we have the interned strings section.
180 const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings);
181 if (section.Size() > 0) {
182 AddTableFromMemoryLocked(image_space->Begin() + section.Offset());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700183 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700184 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700185}
186
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700187void InternTable::BroadcastForNewInterns() {
188 CHECK(kUseReadBarrier);
189 Thread* self = Thread::Current();
190 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700191 weak_intern_condition_.Broadcast(self);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700192}
193
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700194void InternTable::WaitUntilAccessible(Thread* self) {
195 Locks::intern_table_lock_->ExclusiveUnlock(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700196 {
197 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
198 MutexLock mu(self, *Locks::intern_table_lock_);
199 while (weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) {
200 weak_intern_condition_.Wait(self);
201 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700202 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700203 Locks::intern_table_lock_->ExclusiveLock(self);
204}
205
206mirror::String* InternTable::Insert(mirror::String* s, bool is_strong, bool holding_locks) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800207 if (s == nullptr) {
208 return nullptr;
209 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700210 Thread* const self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100211 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700212 if (kDebugLocking && !holding_locks) {
213 Locks::mutator_lock_->AssertSharedHeld(self);
214 CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock";
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700215 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700216 while (true) {
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700217 if (holding_locks) {
218 if (!kUseReadBarrier) {
219 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
220 } else {
221 CHECK(self->GetWeakRefAccessEnabled());
222 }
223 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700224 // Check the strong table for a match.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800225 mirror::String* strong = LookupStrongLocked(s);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700226 if (strong != nullptr) {
227 return strong;
228 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700229 if ((!kUseReadBarrier && weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) ||
230 (kUseReadBarrier && self->GetWeakRefAccessEnabled())) {
231 break;
232 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700233 // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only
234 // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is
235 // cleared.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700236 CHECK(!holding_locks);
237 StackHandleScope<1> hs(self);
238 auto h = hs.NewHandleWrapper(&s);
239 WaitUntilAccessible(self);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700240 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700241 if (!kUseReadBarrier) {
242 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
243 } else {
244 CHECK(self->GetWeakRefAccessEnabled());
245 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800246 // There is no match in the strong table, check the weak table.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800247 mirror::String* weak = LookupWeakLocked(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800248 if (weak != nullptr) {
249 if (is_strong) {
250 // A match was found in the weak table. Promote to the strong table.
251 RemoveWeak(weak);
252 return InsertStrong(weak);
253 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700254 return weak;
255 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800256 // No match in the strong table or the weak table. Insert into the strong / weak table.
257 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700258}
259
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700260mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
261 DCHECK(utf8_data != nullptr);
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100262 Thread* self = Thread::Current();
263 // Try to avoid allocation.
264 mirror::String* s = LookupStrong(self, utf16_length, utf8_data);
265 if (s != nullptr) {
266 return s;
267 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700268 return InternStrong(mirror::String::AllocFromModifiedUtf8(
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100269 self, utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700270}
271
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272mirror::String* InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700273 DCHECK(utf8_data != nullptr);
274 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700275}
276
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700277mirror::String* InternTable::InternStrongImageString(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700278 // May be holding the heap bitmap lock.
279 return Insert(s, true, true);
280}
281
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282mirror::String* InternTable::InternStrong(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700283 return Insert(s, true, false);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700284}
285
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286mirror::String* InternTable::InternWeak(mirror::String* s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700287 return Insert(s, false, false);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700288}
289
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290bool InternTable::ContainsWeak(mirror::String* s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800291 return LookupWeak(Thread::Current(), s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700292}
293
Mathieu Chartier97509952015-07-13 14:35:43 -0700294void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100295 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700296 weak_interns_.SweepWeaks(visitor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700297}
298
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800299size_t InternTable::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700300 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800301 return AddTableFromMemoryLocked(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700302}
303
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800304size_t InternTable::AddTableFromMemoryLocked(const uint8_t* ptr) {
305 return strong_interns_.AddTableFromMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700306}
307
308size_t InternTable::WriteToMemory(uint8_t* ptr) {
309 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800310 return strong_interns_.WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700311}
312
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800313std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700314 if (kIsDebugBuild) {
315 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
316 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800317 return static_cast<size_t>(root.Read()->GetHashCode());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700318}
319
320bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800321 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700322 if (kIsDebugBuild) {
323 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
324 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800325 return a.Read()->Equals(b.Read());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700326}
327
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000328bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
329 const Utf8String& b) const {
330 if (kIsDebugBuild) {
331 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
332 }
333 mirror::String* a_string = a.Read();
334 uint32_t a_length = static_cast<uint32_t>(a_string->GetLength());
335 if (a_length != b.GetUtf16Length()) {
336 return false;
337 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700338 if (a_string->IsCompressed()) {
339 size_t b_byte_count = strlen(b.GetUtf8Data());
340 size_t b_utf8_length = CountModifiedUtf8Chars(b.GetUtf8Data(), b_byte_count);
341 // Modified UTF-8 single byte character range is 0x01 .. 0x7f
342 // The string compression occurs on regular ASCII with same exact range,
343 // not on extended ASCII which up to 0xff
344 const bool is_b_regular_ascii = (b_byte_count == b_utf8_length);
345 if (is_b_regular_ascii) {
346 return memcmp(b.GetUtf8Data(),
347 a_string->GetValueCompressed(), a_length * sizeof(uint8_t)) == 0;
348 } else {
349 return false;
350 }
351 } else {
352 const uint16_t* a_value = a_string->GetValue();
353 return CompareModifiedUtf8ToUtf16AsCodePointValues(b.GetUtf8Data(), a_value, a_length) == 0;
354 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000355}
356
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800357size_t InternTable::Table::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700358 size_t read_count = 0;
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800359 UnorderedSet set(ptr, /*make copy*/false, &read_count);
Mathieu Chartier619a4572016-04-05 19:13:37 -0700360 if (set.Empty()) {
361 // Avoid inserting empty sets.
362 return read_count;
363 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800364 // TODO: Disable this for app images if app images have intern tables.
365 static constexpr bool kCheckDuplicates = true;
366 if (kCheckDuplicates) {
367 for (GcRoot<mirror::String>& string : set) {
368 CHECK(Find(string.Read()) == nullptr) << "Already found " << string.Read()->ToModifiedUtf8();
369 }
370 }
Mathieu Chartier619a4572016-04-05 19:13:37 -0700371 // Insert at the front since we add new interns into the back.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800372 tables_.insert(tables_.begin(), std::move(set));
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700373 return read_count;
374}
375
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800376size_t InternTable::Table::WriteToMemory(uint8_t* ptr) {
377 if (tables_.empty()) {
378 return 0;
379 }
380 UnorderedSet* table_to_write;
381 UnorderedSet combined;
382 if (tables_.size() > 1) {
383 table_to_write = &combined;
384 for (UnorderedSet& table : tables_) {
385 for (GcRoot<mirror::String>& string : table) {
386 combined.Insert(string);
387 }
388 }
389 } else {
390 table_to_write = &tables_.back();
391 }
392 return table_to_write->WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700393}
394
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700395void InternTable::Table::Remove(mirror::String* s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800396 for (UnorderedSet& table : tables_) {
397 auto it = table.Find(GcRoot<mirror::String>(s));
398 if (it != table.end()) {
399 table.Erase(it);
400 return;
401 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700402 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800403 LOG(FATAL) << "Attempting to remove non-interned string " << s->ToModifiedUtf8();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700404}
405
406mirror::String* InternTable::Table::Find(mirror::String* s) {
407 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800408 for (UnorderedSet& table : tables_) {
409 auto it = table.Find(GcRoot<mirror::String>(s));
410 if (it != table.end()) {
411 return it->Read();
412 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700413 }
414 return nullptr;
415}
416
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000417mirror::String* InternTable::Table::Find(const Utf8String& string) {
418 Locks::intern_table_lock_->AssertHeld(Thread::Current());
419 for (UnorderedSet& table : tables_) {
420 auto it = table.Find(string);
421 if (it != table.end()) {
422 return it->Read();
423 }
424 }
425 return nullptr;
426}
427
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800428void InternTable::Table::AddNewTable() {
429 tables_.push_back(UnorderedSet());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700430}
431
432void InternTable::Table::Insert(mirror::String* s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800433 // Always insert the last table, the image tables are before and we avoid inserting into these
434 // to prevent dirty pages.
435 DCHECK(!tables_.empty());
436 tables_.back().Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700437}
438
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700439void InternTable::Table::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700440 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
441 visitor, RootInfo(kRootInternedString));
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800442 for (UnorderedSet& table : tables_) {
443 for (auto& intern : table) {
444 buffered_visitor.VisitRoot(intern);
445 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700446 }
447}
448
Mathieu Chartier97509952015-07-13 14:35:43 -0700449void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800450 for (UnorderedSet& table : tables_) {
451 SweepWeaks(&table, visitor);
452 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700453}
454
Mathieu Chartier97509952015-07-13 14:35:43 -0700455void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700456 for (auto it = set->begin(), end = set->end(); it != end;) {
457 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800458 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -0700459 mirror::Object* new_object = visitor->IsMarked(object);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700460 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800461 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700462 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800463 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700464 ++it;
465 }
466 }
467}
468
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800469size_t InternTable::Table::Size() const {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800470 return std::accumulate(tables_.begin(),
471 tables_.end(),
Mathieu Chartier205b7622016-01-06 15:47:09 -0800472 0U,
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800473 [](size_t sum, const UnorderedSet& set) {
474 return sum + set.Size();
475 });
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800476}
477
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700478void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) {
479 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
480 ChangeWeakRootStateLocked(new_state);
481}
482
483void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700484 CHECK(!kUseReadBarrier);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700485 weak_root_state_ = new_state;
486 if (new_state != gc::kWeakRootStateNoReadsOrWrites) {
487 weak_intern_condition_.Broadcast(Thread::Current());
488 }
489}
490
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700491InternTable::Table::Table() {
492 Runtime* const runtime = Runtime::Current();
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800493 // Initial table.
494 tables_.push_back(UnorderedSet());
495 tables_.back().SetLoadFactor(runtime->GetHashTableMinLoadFactor(),
496 runtime->GetHashTableMaxLoadFactor());
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700497}
498
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700499} // namespace art