blob: 3e1914604d22ea8d39a84f5013950d705c6329fe [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_) {
Mathieu Chartier9e868092016-10-31 14:58:04 -070066 ObjPtr<mirror::String> old_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070067 root.VisitRoot(visitor, RootInfo(kRootInternedString));
Mathieu Chartier9e868092016-10-31 14:58:04 -070068 ObjPtr<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 Chartier9e868092016-10-31 14:58:04 -070089ObjPtr<mirror::String> InternTable::LookupWeak(Thread* self, ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080090 MutexLock mu(self, *Locks::intern_table_lock_);
91 return LookupWeakLocked(s);
Mathieu Chartierf7fd9702015-11-09 11:16:49 -080092}
93
Mathieu Chartier9e868092016-10-31 14:58:04 -070094ObjPtr<mirror::String> InternTable::LookupStrong(Thread* self, ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080095 MutexLock mu(self, *Locks::intern_table_lock_);
96 return LookupStrongLocked(s);
97}
98
Mathieu Chartier9e868092016-10-31 14:58:04 -070099ObjPtr<mirror::String> InternTable::LookupStrong(Thread* self,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000100 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 Chartier9e868092016-10-31 14:58:04 -0700110ObjPtr<mirror::String> InternTable::LookupWeakLocked(ObjPtr<mirror::String> s) {
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +0000111 return weak_interns_.Find(s);
112}
113
Mathieu Chartier9e868092016-10-31 14:58:04 -0700114ObjPtr<mirror::String> InternTable::LookupStrongLocked(ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800115 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 Chartier9e868092016-10-31 14:58:04 -0700124ObjPtr<mirror::String> InternTable::InsertStrong(ObjPtr<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 Chartier9e868092016-10-31 14:58:04 -0700136ObjPtr<mirror::String> InternTable::InsertWeak(ObjPtr<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 Chartier9e868092016-10-31 14:58:04 -0700145void InternTable::RemoveStrong(ObjPtr<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 Chartier9e868092016-10-31 14:58:04 -0700149void InternTable::RemoveWeak(ObjPtr<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 Chartier9e868092016-10-31 14:58:04 -0700158ObjPtr<mirror::String> InternTable::InsertStrongFromTransaction(ObjPtr<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 Chartier9e868092016-10-31 14:58:04 -0700162
163ObjPtr<mirror::String> InternTable::InsertWeakFromTransaction(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100164 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700165 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100166}
Mathieu Chartier9e868092016-10-31 14:58:04 -0700167
168void InternTable::RemoveStrongFromTransaction(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100169 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700170 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100171}
Mathieu Chartier9e868092016-10-31 14:58:04 -0700172
173void InternTable::RemoveWeakFromTransaction(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100174 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700175 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100176}
177
Mathieu Chartier205b7622016-01-06 15:47:09 -0800178void InternTable::AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700179 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800180 for (gc::space::ImageSpace* image_space : image_spaces) {
181 const ImageHeader* const header = &image_space->GetImageHeader();
182 // Check if we have the interned strings section.
183 const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings);
184 if (section.Size() > 0) {
185 AddTableFromMemoryLocked(image_space->Begin() + section.Offset());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700186 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700187 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700188}
189
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700190void InternTable::BroadcastForNewInterns() {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700191 Thread* self = Thread::Current();
192 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700193 weak_intern_condition_.Broadcast(self);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700194}
195
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700196void InternTable::WaitUntilAccessible(Thread* self) {
197 Locks::intern_table_lock_->ExclusiveUnlock(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700198 {
199 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
200 MutexLock mu(self, *Locks::intern_table_lock_);
Hiroshi Yamauchi9e6f0972016-11-03 13:03:20 -0700201 while ((!kUseReadBarrier && weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) ||
202 (kUseReadBarrier && !self->GetWeakRefAccessEnabled())) {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700203 weak_intern_condition_.Wait(self);
204 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700205 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700206 Locks::intern_table_lock_->ExclusiveLock(self);
207}
208
Mathieu Chartier9e868092016-10-31 14:58:04 -0700209ObjPtr<mirror::String> InternTable::Insert(ObjPtr<mirror::String> s,
210 bool is_strong,
211 bool holding_locks) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800212 if (s == nullptr) {
213 return nullptr;
214 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700215 Thread* const self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100216 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700217 if (kDebugLocking && !holding_locks) {
218 Locks::mutator_lock_->AssertSharedHeld(self);
219 CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock";
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700220 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700221 while (true) {
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700222 if (holding_locks) {
223 if (!kUseReadBarrier) {
224 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
225 } else {
226 CHECK(self->GetWeakRefAccessEnabled());
227 }
228 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700229 // Check the strong table for a match.
Mathieu Chartier9e868092016-10-31 14:58:04 -0700230 ObjPtr<mirror::String> strong = LookupStrongLocked(s);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700231 if (strong != nullptr) {
232 return strong;
233 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700234 if ((!kUseReadBarrier && weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) ||
235 (kUseReadBarrier && self->GetWeakRefAccessEnabled())) {
236 break;
237 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700238 // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only
239 // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is
240 // cleared.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700241 CHECK(!holding_locks);
242 StackHandleScope<1> hs(self);
243 auto h = hs.NewHandleWrapper(&s);
244 WaitUntilAccessible(self);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700245 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700246 if (!kUseReadBarrier) {
247 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
248 } else {
249 CHECK(self->GetWeakRefAccessEnabled());
250 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800251 // There is no match in the strong table, check the weak table.
Mathieu Chartier9e868092016-10-31 14:58:04 -0700252 ObjPtr<mirror::String> weak = LookupWeakLocked(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800253 if (weak != nullptr) {
254 if (is_strong) {
255 // A match was found in the weak table. Promote to the strong table.
256 RemoveWeak(weak);
257 return InsertStrong(weak);
258 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700259 return weak;
260 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800261 // No match in the strong table or the weak table. Insert into the strong / weak table.
262 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700263}
264
Mathieu Chartier9e868092016-10-31 14:58:04 -0700265ObjPtr<mirror::String> InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700266 DCHECK(utf8_data != nullptr);
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100267 Thread* self = Thread::Current();
268 // Try to avoid allocation.
Mathieu Chartier9e868092016-10-31 14:58:04 -0700269 ObjPtr<mirror::String> s = LookupStrong(self, utf16_length, utf8_data);
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100270 if (s != nullptr) {
271 return s;
272 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700273 return InternStrong(mirror::String::AllocFromModifiedUtf8(
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100274 self, utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700275}
276
Mathieu Chartier9e868092016-10-31 14:58:04 -0700277ObjPtr<mirror::String> InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700278 DCHECK(utf8_data != nullptr);
279 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700280}
281
Mathieu Chartier9e868092016-10-31 14:58:04 -0700282ObjPtr<mirror::String> InternTable::InternStrongImageString(ObjPtr<mirror::String> s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700283 // May be holding the heap bitmap lock.
284 return Insert(s, true, true);
285}
286
Mathieu Chartier9e868092016-10-31 14:58:04 -0700287ObjPtr<mirror::String> InternTable::InternStrong(ObjPtr<mirror::String> s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700288 return Insert(s, true, false);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700289}
290
Mathieu Chartier9e868092016-10-31 14:58:04 -0700291ObjPtr<mirror::String> InternTable::InternWeak(ObjPtr<mirror::String> s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700292 return Insert(s, false, false);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700293}
294
Mathieu Chartier9e868092016-10-31 14:58:04 -0700295bool InternTable::ContainsWeak(ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800296 return LookupWeak(Thread::Current(), s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700297}
298
Mathieu Chartier97509952015-07-13 14:35:43 -0700299void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700301 weak_interns_.SweepWeaks(visitor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700302}
303
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800304size_t InternTable::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700305 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800306 return AddTableFromMemoryLocked(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700307}
308
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800309size_t InternTable::AddTableFromMemoryLocked(const uint8_t* ptr) {
310 return strong_interns_.AddTableFromMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700311}
312
313size_t InternTable::WriteToMemory(uint8_t* ptr) {
314 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800315 return strong_interns_.WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700316}
317
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800318std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700319 if (kIsDebugBuild) {
320 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
321 }
Alexey Grebenkin21f23642016-12-02 17:44:54 +0300322 // An additional cast to prevent undesired sign extension.
323 return static_cast<size_t>(
324 static_cast<uint32_t>(root.Read<kWithoutReadBarrier>()->GetHashCode()));
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700325}
326
327bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800328 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700329 if (kIsDebugBuild) {
330 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
331 }
Mathieu Chartier9e868092016-10-31 14:58:04 -0700332 return a.Read<kWithoutReadBarrier>()->Equals(b.Read<kWithoutReadBarrier>());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700333}
334
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000335bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
336 const Utf8String& b) const {
337 if (kIsDebugBuild) {
338 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
339 }
Mathieu Chartier9e868092016-10-31 14:58:04 -0700340 ObjPtr<mirror::String> a_string = a.Read<kWithoutReadBarrier>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000341 uint32_t a_length = static_cast<uint32_t>(a_string->GetLength());
342 if (a_length != b.GetUtf16Length()) {
343 return false;
344 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700345 if (a_string->IsCompressed()) {
346 size_t b_byte_count = strlen(b.GetUtf8Data());
347 size_t b_utf8_length = CountModifiedUtf8Chars(b.GetUtf8Data(), b_byte_count);
348 // Modified UTF-8 single byte character range is 0x01 .. 0x7f
349 // The string compression occurs on regular ASCII with same exact range,
350 // not on extended ASCII which up to 0xff
351 const bool is_b_regular_ascii = (b_byte_count == b_utf8_length);
352 if (is_b_regular_ascii) {
353 return memcmp(b.GetUtf8Data(),
354 a_string->GetValueCompressed(), a_length * sizeof(uint8_t)) == 0;
355 } else {
356 return false;
357 }
358 } else {
359 const uint16_t* a_value = a_string->GetValue();
360 return CompareModifiedUtf8ToUtf16AsCodePointValues(b.GetUtf8Data(), a_value, a_length) == 0;
361 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000362}
363
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800364size_t InternTable::Table::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700365 size_t read_count = 0;
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800366 UnorderedSet set(ptr, /*make copy*/false, &read_count);
Mathieu Chartier619a4572016-04-05 19:13:37 -0700367 if (set.Empty()) {
368 // Avoid inserting empty sets.
369 return read_count;
370 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800371 // 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 }
Mathieu Chartier619a4572016-04-05 19:13:37 -0700378 // Insert at the front since we add new interns into the back.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800379 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 Chartier9e868092016-10-31 14:58:04 -0700402void InternTable::Table::Remove(ObjPtr<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
Mathieu Chartier9e868092016-10-31 14:58:04 -0700413ObjPtr<mirror::String> InternTable::Table::Find(ObjPtr<mirror::String> s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700414 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 Chartier9e868092016-10-31 14:58:04 -0700424ObjPtr<mirror::String> InternTable::Table::Find(const Utf8String& string) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000425 Locks::intern_table_lock_->AssertHeld(Thread::Current());
426 for (UnorderedSet& table : tables_) {
427 auto it = table.Find(string);
428 if (it != table.end()) {
429 return it->Read();
430 }
431 }
432 return nullptr;
433}
434
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800435void InternTable::Table::AddNewTable() {
436 tables_.push_back(UnorderedSet());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700437}
438
Mathieu Chartier9e868092016-10-31 14:58:04 -0700439void InternTable::Table::Insert(ObjPtr<mirror::String> s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800440 // Always insert the last table, the image tables are before and we avoid inserting into these
441 // to prevent dirty pages.
442 DCHECK(!tables_.empty());
443 tables_.back().Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700444}
445
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700446void InternTable::Table::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700447 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
448 visitor, RootInfo(kRootInternedString));
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800449 for (UnorderedSet& table : tables_) {
450 for (auto& intern : table) {
451 buffered_visitor.VisitRoot(intern);
452 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700453 }
454}
455
Mathieu Chartier97509952015-07-13 14:35:43 -0700456void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800457 for (UnorderedSet& table : tables_) {
458 SweepWeaks(&table, visitor);
459 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700460}
461
Mathieu Chartier97509952015-07-13 14:35:43 -0700462void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700463 for (auto it = set->begin(), end = set->end(); it != end;) {
464 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800465 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -0700466 mirror::Object* new_object = visitor->IsMarked(object);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700467 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800468 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700469 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800470 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700471 ++it;
472 }
473 }
474}
475
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800476size_t InternTable::Table::Size() const {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800477 return std::accumulate(tables_.begin(),
478 tables_.end(),
Mathieu Chartier205b7622016-01-06 15:47:09 -0800479 0U,
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800480 [](size_t sum, const UnorderedSet& set) {
481 return sum + set.Size();
482 });
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800483}
484
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700485void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) {
486 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
487 ChangeWeakRootStateLocked(new_state);
488}
489
490void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700491 CHECK(!kUseReadBarrier);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700492 weak_root_state_ = new_state;
493 if (new_state != gc::kWeakRootStateNoReadsOrWrites) {
494 weak_intern_condition_.Broadcast(Thread::Current());
495 }
496}
497
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700498InternTable::Table::Table() {
499 Runtime* const runtime = Runtime::Current();
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800500 // Initial table.
501 tables_.push_back(UnorderedSet());
502 tables_.back().SetLoadFactor(runtime->GetHashTableMinLoadFactor(),
503 runtime->GetHashTableMaxLoadFactor());
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700504}
505
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700506} // namespace art