blob: bfe4e1ccb7db2ddc529ab1c278abeb76dbfcc950 [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"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070030#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070032#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070033
34namespace art {
35
Ian Rogers7dfb28c2013-08-22 08:18:36 -070036InternTable::InternTable()
Vladimir Marko1a1de672016-10-13 12:53:15 +010037 : 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_) {
Mathieu Chartier9e868092016-10-31 14:58:04 -070067 ObjPtr<mirror::String> old_ref = root.Read<kWithoutReadBarrier>();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070068 root.VisitRoot(visitor, RootInfo(kRootInternedString));
Mathieu Chartier9e868092016-10-31 14:58:04 -070069 ObjPtr<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 Chartier9e868092016-10-31 14:58:04 -070090ObjPtr<mirror::String> InternTable::LookupWeak(Thread* self, ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080091 MutexLock mu(self, *Locks::intern_table_lock_);
92 return LookupWeakLocked(s);
Mathieu Chartierf7fd9702015-11-09 11:16:49 -080093}
94
Mathieu Chartier9e868092016-10-31 14:58:04 -070095ObjPtr<mirror::String> InternTable::LookupStrong(Thread* self, ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080096 MutexLock mu(self, *Locks::intern_table_lock_);
97 return LookupStrongLocked(s);
98}
99
Mathieu Chartier9e868092016-10-31 14:58:04 -0700100ObjPtr<mirror::String> InternTable::LookupStrong(Thread* self,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000101 uint32_t utf16_length,
102 const char* utf8_data) {
103 DCHECK_EQ(utf16_length, CountModifiedUtf8Chars(utf8_data));
104 Utf8String string(utf16_length,
105 utf8_data,
106 ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length));
107 MutexLock mu(self, *Locks::intern_table_lock_);
108 return strong_interns_.Find(string);
109}
110
Mathieu Chartier9e868092016-10-31 14:58:04 -0700111ObjPtr<mirror::String> InternTable::LookupWeakLocked(ObjPtr<mirror::String> s) {
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +0000112 return weak_interns_.Find(s);
113}
114
Mathieu Chartier9e868092016-10-31 14:58:04 -0700115ObjPtr<mirror::String> InternTable::LookupStrongLocked(ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800116 return strong_interns_.Find(s);
117}
118
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800119void InternTable::AddNewTable() {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700120 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800121 weak_interns_.AddNewTable();
122 strong_interns_.AddNewTable();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700123}
124
Mathieu Chartier9e868092016-10-31 14:58:04 -0700125ObjPtr<mirror::String> InternTable::InsertStrong(ObjPtr<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->RecordStrongStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100129 }
Mathieu Chartier893263b2014-03-04 11:07:42 -0800130 if (log_new_roots_) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700131 new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s));
Mathieu Chartier893263b2014-03-04 11:07:42 -0800132 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700133 strong_interns_.Insert(s);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800134 return s;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100135}
136
Mathieu Chartier9e868092016-10-31 14:58:04 -0700137ObjPtr<mirror::String> InternTable::InsertWeak(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138 Runtime* runtime = Runtime::Current();
139 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700140 runtime->RecordWeakStringInsertion(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100141 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700142 weak_interns_.Insert(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700143 return s;
144}
145
Mathieu Chartier9e868092016-10-31 14:58:04 -0700146void InternTable::RemoveStrong(ObjPtr<mirror::String> s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700147 strong_interns_.Remove(s);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700148}
149
Mathieu Chartier9e868092016-10-31 14:58:04 -0700150void InternTable::RemoveWeak(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100151 Runtime* runtime = Runtime::Current();
152 if (runtime->IsActiveTransaction()) {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700153 runtime->RecordWeakStringRemoval(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700155 weak_interns_.Remove(s);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700156}
157
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100158// Insert/remove methods used to undo changes made during an aborted transaction.
Mathieu Chartier9e868092016-10-31 14:58:04 -0700159ObjPtr<mirror::String> InternTable::InsertStrongFromTransaction(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100160 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700161 return InsertStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100162}
Mathieu Chartier9e868092016-10-31 14:58:04 -0700163
164ObjPtr<mirror::String> InternTable::InsertWeakFromTransaction(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100165 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700166 return InsertWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100167}
Mathieu Chartier9e868092016-10-31 14:58:04 -0700168
169void InternTable::RemoveStrongFromTransaction(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100170 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700171 RemoveStrong(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100172}
Mathieu Chartier9e868092016-10-31 14:58:04 -0700173
174void InternTable::RemoveWeakFromTransaction(ObjPtr<mirror::String> s) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100175 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700176 RemoveWeak(s);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100177}
178
Mathieu Chartier205b7622016-01-06 15:47:09 -0800179void InternTable::AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700180 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier205b7622016-01-06 15:47:09 -0800181 for (gc::space::ImageSpace* image_space : image_spaces) {
182 const ImageHeader* const header = &image_space->GetImageHeader();
183 // Check if we have the interned strings section.
184 const ImageSection& section = header->GetImageSection(ImageHeader::kSectionInternedStrings);
185 if (section.Size() > 0) {
186 AddTableFromMemoryLocked(image_space->Begin() + section.Offset());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700187 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700188 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700189}
190
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700191void InternTable::BroadcastForNewInterns() {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700192 Thread* self = Thread::Current();
193 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700194 weak_intern_condition_.Broadcast(self);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700195}
196
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700197void InternTable::WaitUntilAccessible(Thread* self) {
198 Locks::intern_table_lock_->ExclusiveUnlock(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700199 {
200 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
201 MutexLock mu(self, *Locks::intern_table_lock_);
Hiroshi Yamauchi9e6f0972016-11-03 13:03:20 -0700202 while ((!kUseReadBarrier && weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) ||
203 (kUseReadBarrier && !self->GetWeakRefAccessEnabled())) {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700204 weak_intern_condition_.Wait(self);
205 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700206 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700207 Locks::intern_table_lock_->ExclusiveLock(self);
208}
209
Mathieu Chartier9e868092016-10-31 14:58:04 -0700210ObjPtr<mirror::String> InternTable::Insert(ObjPtr<mirror::String> s,
211 bool is_strong,
212 bool holding_locks) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800213 if (s == nullptr) {
214 return nullptr;
215 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700216 Thread* const self = Thread::Current();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100217 MutexLock mu(self, *Locks::intern_table_lock_);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700218 if (kDebugLocking && !holding_locks) {
219 Locks::mutator_lock_->AssertSharedHeld(self);
220 CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock";
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700221 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700222 while (true) {
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700223 if (holding_locks) {
224 if (!kUseReadBarrier) {
225 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
226 } else {
227 CHECK(self->GetWeakRefAccessEnabled());
228 }
229 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700230 // Check the strong table for a match.
Mathieu Chartier9e868092016-10-31 14:58:04 -0700231 ObjPtr<mirror::String> strong = LookupStrongLocked(s);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700232 if (strong != nullptr) {
233 return strong;
234 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700235 if ((!kUseReadBarrier && weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) ||
236 (kUseReadBarrier && self->GetWeakRefAccessEnabled())) {
237 break;
238 }
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700239 // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only
240 // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is
241 // cleared.
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700242 CHECK(!holding_locks);
243 StackHandleScope<1> hs(self);
244 auto h = hs.NewHandleWrapper(&s);
245 WaitUntilAccessible(self);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700246 }
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700247 if (!kUseReadBarrier) {
248 CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal);
249 } else {
250 CHECK(self->GetWeakRefAccessEnabled());
251 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800252 // There is no match in the strong table, check the weak table.
Mathieu Chartier9e868092016-10-31 14:58:04 -0700253 ObjPtr<mirror::String> weak = LookupWeakLocked(s);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800254 if (weak != nullptr) {
255 if (is_strong) {
256 // A match was found in the weak table. Promote to the strong table.
257 RemoveWeak(weak);
258 return InsertStrong(weak);
259 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700260 return weak;
261 }
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800262 // No match in the strong table or the weak table. Insert into the strong / weak table.
263 return is_strong ? InsertStrong(s) : InsertWeak(s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700264}
265
Mathieu Chartier9e868092016-10-31 14:58:04 -0700266ObjPtr<mirror::String> InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700267 DCHECK(utf8_data != nullptr);
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100268 Thread* self = Thread::Current();
269 // Try to avoid allocation.
Mathieu Chartier9e868092016-10-31 14:58:04 -0700270 ObjPtr<mirror::String> s = LookupStrong(self, utf16_length, utf8_data);
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100271 if (s != nullptr) {
272 return s;
273 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700274 return InternStrong(mirror::String::AllocFromModifiedUtf8(
Vladimir Markod2bdb9b2016-07-08 17:23:22 +0100275 self, utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700276}
277
Mathieu Chartier9e868092016-10-31 14:58:04 -0700278ObjPtr<mirror::String> InternTable::InternStrong(const char* utf8_data) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700279 DCHECK(utf8_data != nullptr);
280 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700281}
282
Mathieu Chartier9e868092016-10-31 14:58:04 -0700283ObjPtr<mirror::String> InternTable::InternStrongImageString(ObjPtr<mirror::String> s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700284 // May be holding the heap bitmap lock.
285 return Insert(s, true, true);
286}
287
Mathieu Chartier9e868092016-10-31 14:58:04 -0700288ObjPtr<mirror::String> InternTable::InternStrong(ObjPtr<mirror::String> s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700289 return Insert(s, true, false);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700290}
291
Mathieu Chartier9e868092016-10-31 14:58:04 -0700292ObjPtr<mirror::String> InternTable::InternWeak(ObjPtr<mirror::String> s) {
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700293 return Insert(s, false, false);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700294}
295
Mathieu Chartier9e868092016-10-31 14:58:04 -0700296bool InternTable::ContainsWeak(ObjPtr<mirror::String> s) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800297 return LookupWeak(Thread::Current(), s) == s;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700298}
299
Mathieu Chartier97509952015-07-13 14:35:43 -0700300void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100301 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700302 weak_interns_.SweepWeaks(visitor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700303}
304
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800305size_t InternTable::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700306 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800307 return AddTableFromMemoryLocked(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700308}
309
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800310size_t InternTable::AddTableFromMemoryLocked(const uint8_t* ptr) {
311 return strong_interns_.AddTableFromMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700312}
313
314size_t InternTable::WriteToMemory(uint8_t* ptr) {
315 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800316 return strong_interns_.WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700317}
318
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800319std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700320 if (kIsDebugBuild) {
321 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
322 }
Alexey Grebenkin21f23642016-12-02 17:44:54 +0300323 // An additional cast to prevent undesired sign extension.
324 return static_cast<size_t>(
325 static_cast<uint32_t>(root.Read<kWithoutReadBarrier>()->GetHashCode()));
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700326}
327
328bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800329 const GcRoot<mirror::String>& b) const {
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700330 if (kIsDebugBuild) {
331 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
332 }
Mathieu Chartier9e868092016-10-31 14:58:04 -0700333 return a.Read<kWithoutReadBarrier>()->Equals(b.Read<kWithoutReadBarrier>());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700334}
335
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000336bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a,
337 const Utf8String& b) const {
338 if (kIsDebugBuild) {
339 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
340 }
Mathieu Chartier9e868092016-10-31 14:58:04 -0700341 ObjPtr<mirror::String> a_string = a.Read<kWithoutReadBarrier>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000342 uint32_t a_length = static_cast<uint32_t>(a_string->GetLength());
343 if (a_length != b.GetUtf16Length()) {
344 return false;
345 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700346 if (a_string->IsCompressed()) {
347 size_t b_byte_count = strlen(b.GetUtf8Data());
348 size_t b_utf8_length = CountModifiedUtf8Chars(b.GetUtf8Data(), b_byte_count);
349 // Modified UTF-8 single byte character range is 0x01 .. 0x7f
350 // The string compression occurs on regular ASCII with same exact range,
351 // not on extended ASCII which up to 0xff
352 const bool is_b_regular_ascii = (b_byte_count == b_utf8_length);
353 if (is_b_regular_ascii) {
354 return memcmp(b.GetUtf8Data(),
355 a_string->GetValueCompressed(), a_length * sizeof(uint8_t)) == 0;
356 } else {
357 return false;
358 }
359 } else {
360 const uint16_t* a_value = a_string->GetValue();
361 return CompareModifiedUtf8ToUtf16AsCodePointValues(b.GetUtf8Data(), a_value, a_length) == 0;
362 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000363}
364
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800365size_t InternTable::Table::AddTableFromMemory(const uint8_t* ptr) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700366 size_t read_count = 0;
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800367 UnorderedSet set(ptr, /*make copy*/false, &read_count);
Mathieu Chartier619a4572016-04-05 19:13:37 -0700368 if (set.Empty()) {
369 // Avoid inserting empty sets.
370 return read_count;
371 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800372 // TODO: Disable this for app images if app images have intern tables.
373 static constexpr bool kCheckDuplicates = true;
374 if (kCheckDuplicates) {
375 for (GcRoot<mirror::String>& string : set) {
376 CHECK(Find(string.Read()) == nullptr) << "Already found " << string.Read()->ToModifiedUtf8();
377 }
378 }
Mathieu Chartier619a4572016-04-05 19:13:37 -0700379 // Insert at the front since we add new interns into the back.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800380 tables_.insert(tables_.begin(), std::move(set));
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700381 return read_count;
382}
383
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800384size_t InternTable::Table::WriteToMemory(uint8_t* ptr) {
385 if (tables_.empty()) {
386 return 0;
387 }
388 UnorderedSet* table_to_write;
389 UnorderedSet combined;
390 if (tables_.size() > 1) {
391 table_to_write = &combined;
392 for (UnorderedSet& table : tables_) {
393 for (GcRoot<mirror::String>& string : table) {
394 combined.Insert(string);
395 }
396 }
397 } else {
398 table_to_write = &tables_.back();
399 }
400 return table_to_write->WriteToMemory(ptr);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700401}
402
Mathieu Chartier9e868092016-10-31 14:58:04 -0700403void InternTable::Table::Remove(ObjPtr<mirror::String> s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800404 for (UnorderedSet& table : tables_) {
405 auto it = table.Find(GcRoot<mirror::String>(s));
406 if (it != table.end()) {
407 table.Erase(it);
408 return;
409 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700410 }
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800411 LOG(FATAL) << "Attempting to remove non-interned string " << s->ToModifiedUtf8();
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700412}
413
Mathieu Chartier9e868092016-10-31 14:58:04 -0700414ObjPtr<mirror::String> InternTable::Table::Find(ObjPtr<mirror::String> s) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700415 Locks::intern_table_lock_->AssertHeld(Thread::Current());
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800416 for (UnorderedSet& table : tables_) {
417 auto it = table.Find(GcRoot<mirror::String>(s));
418 if (it != table.end()) {
419 return it->Read();
420 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700421 }
422 return nullptr;
423}
424
Mathieu Chartier9e868092016-10-31 14:58:04 -0700425ObjPtr<mirror::String> InternTable::Table::Find(const Utf8String& string) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000426 Locks::intern_table_lock_->AssertHeld(Thread::Current());
427 for (UnorderedSet& table : tables_) {
428 auto it = table.Find(string);
429 if (it != table.end()) {
430 return it->Read();
431 }
432 }
433 return nullptr;
434}
435
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800436void InternTable::Table::AddNewTable() {
437 tables_.push_back(UnorderedSet());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700438}
439
Mathieu Chartier9e868092016-10-31 14:58:04 -0700440void InternTable::Table::Insert(ObjPtr<mirror::String> s) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800441 // Always insert the last table, the image tables are before and we avoid inserting into these
442 // to prevent dirty pages.
443 DCHECK(!tables_.empty());
444 tables_.back().Insert(GcRoot<mirror::String>(s));
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700445}
446
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700447void InternTable::Table::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700448 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
449 visitor, RootInfo(kRootInternedString));
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800450 for (UnorderedSet& table : tables_) {
451 for (auto& intern : table) {
452 buffered_visitor.VisitRoot(intern);
453 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700454 }
455}
456
Mathieu Chartier97509952015-07-13 14:35:43 -0700457void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800458 for (UnorderedSet& table : tables_) {
459 SweepWeaks(&table, visitor);
460 }
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700461}
462
Mathieu Chartier97509952015-07-13 14:35:43 -0700463void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700464 for (auto it = set->begin(), end = set->end(); it != end;) {
465 // This does not need a read barrier because this is called by GC.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800466 mirror::Object* object = it->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -0700467 mirror::Object* new_object = visitor->IsMarked(object);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700468 if (new_object == nullptr) {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800469 it = set->Erase(it);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700470 } else {
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800471 *it = GcRoot<mirror::String>(new_object->AsString());
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700472 ++it;
473 }
474 }
475}
476
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800477size_t InternTable::Table::Size() const {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800478 return std::accumulate(tables_.begin(),
479 tables_.end(),
Mathieu Chartier205b7622016-01-06 15:47:09 -0800480 0U,
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800481 [](size_t sum, const UnorderedSet& set) {
482 return sum + set.Size();
483 });
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800484}
485
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700486void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) {
487 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
488 ChangeWeakRootStateLocked(new_state);
489}
490
491void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700492 CHECK(!kUseReadBarrier);
Mathieu Chartier14c3bf92015-07-13 14:35:43 -0700493 weak_root_state_ = new_state;
494 if (new_state != gc::kWeakRootStateNoReadsOrWrites) {
495 weak_intern_condition_.Broadcast(Thread::Current());
496 }
497}
498
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700499InternTable::Table::Table() {
500 Runtime* const runtime = Runtime::Current();
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800501 // Initial table.
502 tables_.push_back(UnorderedSet());
503 tables_.back().SetLoadFactor(runtime->GetHashTableMinLoadFactor(),
504 runtime->GetHashTableMaxLoadFactor());
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700505}
506
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700507} // namespace art