Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 16 | |
| 17 | #include "intern_table.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 21 | #include "gc/collector/garbage_collector.h" |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 22 | #include "gc/space/image_space.h" |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 23 | #include "gc/weak_root_state.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "gc_root-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 25 | #include "image-inl.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 26 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 29 | #include "mirror/string-inl.h" |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 30 | #include "object_callbacks.h" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "thread.h" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 33 | #include "utf.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 37 | InternTable::InternTable() |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 38 | : log_new_roots_(false), |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 39 | weak_intern_condition_("New intern condition", *Locks::intern_table_lock_), |
| 40 | weak_root_state_(gc::kWeakRootStateNormal) { |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 41 | } |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 42 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 43 | size_t InternTable::Size() const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 44 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 45 | return strong_interns_.Size() + weak_interns_.Size(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 48 | size_t InternTable::StrongSize() const { |
| 49 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 50 | return strong_interns_.Size(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | size_t InternTable::WeakSize() const { |
| 54 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 55 | return weak_interns_.Size(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 58 | void InternTable::DumpForSigQuit(std::ostream& os) const { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 59 | os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 62 | void InternTable::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 63 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 64 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 65 | strong_interns_.VisitRoots(visitor); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 66 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 67 | for (auto& root : new_strong_intern_roots_) { |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 68 | ObjPtr<mirror::String> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 69 | root.VisitRoot(visitor, RootInfo(kRootInternedString)); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 70 | ObjPtr<mirror::String> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 71 | if (new_ref != old_ref) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 72 | // The GC moved a root in the log. Need to search the strong interns and update the |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 73 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 74 | // concurrent moving GC. |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 75 | strong_interns_.Remove(old_ref); |
| 76 | strong_interns_.Insert(new_ref); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 79 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 80 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 81 | new_strong_intern_roots_.clear(); |
| 82 | } |
| 83 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 84 | log_new_roots_ = true; |
| 85 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 86 | log_new_roots_ = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 87 | } |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 88 | // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots. |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 91 | ObjPtr<mirror::String> InternTable::LookupWeak(Thread* self, ObjPtr<mirror::String> s) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 92 | MutexLock mu(self, *Locks::intern_table_lock_); |
| 93 | return LookupWeakLocked(s); |
Mathieu Chartier | f7fd970 | 2015-11-09 11:16:49 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 96 | ObjPtr<mirror::String> InternTable::LookupStrong(Thread* self, ObjPtr<mirror::String> s) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 97 | MutexLock mu(self, *Locks::intern_table_lock_); |
| 98 | return LookupStrongLocked(s); |
| 99 | } |
| 100 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 101 | ObjPtr<mirror::String> InternTable::LookupStrong(Thread* self, |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 102 | uint32_t utf16_length, |
| 103 | const char* utf8_data) { |
| 104 | DCHECK_EQ(utf16_length, CountModifiedUtf8Chars(utf8_data)); |
| 105 | Utf8String string(utf16_length, |
| 106 | utf8_data, |
| 107 | ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length)); |
| 108 | MutexLock mu(self, *Locks::intern_table_lock_); |
| 109 | return strong_interns_.Find(string); |
| 110 | } |
| 111 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 112 | ObjPtr<mirror::String> InternTable::LookupWeakLocked(ObjPtr<mirror::String> s) { |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 113 | return weak_interns_.Find(s); |
| 114 | } |
| 115 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 116 | ObjPtr<mirror::String> InternTable::LookupStrongLocked(ObjPtr<mirror::String> s) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 117 | return strong_interns_.Find(s); |
| 118 | } |
| 119 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 120 | void InternTable::AddNewTable() { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 121 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 122 | weak_interns_.AddNewTable(); |
| 123 | strong_interns_.AddNewTable(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 126 | ObjPtr<mirror::String> InternTable::InsertStrong(ObjPtr<mirror::String> s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 127 | Runtime* runtime = Runtime::Current(); |
| 128 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 129 | runtime->RecordStrongStringInsertion(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 130 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 131 | if (log_new_roots_) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 132 | new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 133 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 134 | strong_interns_.Insert(s); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 135 | return s; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 138 | ObjPtr<mirror::String> InternTable::InsertWeak(ObjPtr<mirror::String> s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 139 | Runtime* runtime = Runtime::Current(); |
| 140 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 141 | runtime->RecordWeakStringInsertion(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 142 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 143 | weak_interns_.Insert(s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 144 | return s; |
| 145 | } |
| 146 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 147 | void InternTable::RemoveStrong(ObjPtr<mirror::String> s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 148 | strong_interns_.Remove(s); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 151 | void InternTable::RemoveWeak(ObjPtr<mirror::String> s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 152 | Runtime* runtime = Runtime::Current(); |
| 153 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 154 | runtime->RecordWeakStringRemoval(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 155 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 156 | weak_interns_.Remove(s); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 159 | // Insert/remove methods used to undo changes made during an aborted transaction. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 160 | ObjPtr<mirror::String> InternTable::InsertStrongFromTransaction(ObjPtr<mirror::String> s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 161 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 162 | return InsertStrong(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 163 | } |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 164 | |
| 165 | ObjPtr<mirror::String> InternTable::InsertWeakFromTransaction(ObjPtr<mirror::String> s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 166 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 167 | return InsertWeak(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 168 | } |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 169 | |
| 170 | void InternTable::RemoveStrongFromTransaction(ObjPtr<mirror::String> s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 171 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 172 | RemoveStrong(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 173 | } |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 174 | |
| 175 | void InternTable::RemoveWeakFromTransaction(ObjPtr<mirror::String> s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 176 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 177 | RemoveWeak(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 180 | void InternTable::AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 181 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 182 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 183 | const ImageHeader* const header = &image_space->GetImageHeader(); |
| 184 | // Check if we have the interned strings section. |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 185 | const ImageSection& section = header->GetInternedStringsSection(); |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 186 | if (section.Size() > 0) { |
| 187 | AddTableFromMemoryLocked(image_space->Begin() + section.Offset()); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 188 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 189 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 192 | void InternTable::BroadcastForNewInterns() { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 193 | Thread* self = Thread::Current(); |
| 194 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 195 | weak_intern_condition_.Broadcast(self); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 198 | void InternTable::WaitUntilAccessible(Thread* self) { |
| 199 | Locks::intern_table_lock_->ExclusiveUnlock(self); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 200 | { |
| 201 | ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead); |
| 202 | MutexLock mu(self, *Locks::intern_table_lock_); |
Hiroshi Yamauchi | 9e6f097 | 2016-11-03 13:03:20 -0700 | [diff] [blame] | 203 | while ((!kUseReadBarrier && weak_root_state_ == gc::kWeakRootStateNoReadsOrWrites) || |
| 204 | (kUseReadBarrier && !self->GetWeakRefAccessEnabled())) { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 205 | weak_intern_condition_.Wait(self); |
| 206 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 207 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 208 | Locks::intern_table_lock_->ExclusiveLock(self); |
| 209 | } |
| 210 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 211 | ObjPtr<mirror::String> InternTable::Insert(ObjPtr<mirror::String> s, |
| 212 | bool is_strong, |
| 213 | bool holding_locks) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 214 | if (s == nullptr) { |
| 215 | return nullptr; |
| 216 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 217 | Thread* const self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 218 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 219 | if (kDebugLocking && !holding_locks) { |
| 220 | Locks::mutator_lock_->AssertSharedHeld(self); |
| 221 | CHECK_EQ(2u, self->NumberOfHeldMutexes()) << "may only safely hold the mutator lock"; |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 222 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 223 | while (true) { |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 224 | if (holding_locks) { |
| 225 | if (!kUseReadBarrier) { |
| 226 | CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal); |
| 227 | } else { |
| 228 | CHECK(self->GetWeakRefAccessEnabled()); |
| 229 | } |
| 230 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 231 | // Check the strong table for a match. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 232 | ObjPtr<mirror::String> strong = LookupStrongLocked(s); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 233 | if (strong != nullptr) { |
| 234 | return strong; |
| 235 | } |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 236 | if ((!kUseReadBarrier && weak_root_state_ != gc::kWeakRootStateNoReadsOrWrites) || |
| 237 | (kUseReadBarrier && self->GetWeakRefAccessEnabled())) { |
| 238 | break; |
| 239 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 240 | // weak_root_state_ is set to gc::kWeakRootStateNoReadsOrWrites in the GC pause but is only |
| 241 | // cleared after SweepSystemWeaks has completed. This is why we need to wait until it is |
| 242 | // cleared. |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 243 | CHECK(!holding_locks); |
| 244 | StackHandleScope<1> hs(self); |
| 245 | auto h = hs.NewHandleWrapper(&s); |
| 246 | WaitUntilAccessible(self); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 247 | } |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 248 | if (!kUseReadBarrier) { |
| 249 | CHECK_EQ(weak_root_state_, gc::kWeakRootStateNormal); |
| 250 | } else { |
| 251 | CHECK(self->GetWeakRefAccessEnabled()); |
| 252 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 253 | // There is no match in the strong table, check the weak table. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 254 | ObjPtr<mirror::String> weak = LookupWeakLocked(s); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 255 | if (weak != nullptr) { |
| 256 | if (is_strong) { |
| 257 | // A match was found in the weak table. Promote to the strong table. |
| 258 | RemoveWeak(weak); |
| 259 | return InsertStrong(weak); |
| 260 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 261 | return weak; |
| 262 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 263 | // No match in the strong table or the weak table. Insert into the strong / weak table. |
| 264 | return is_strong ? InsertStrong(s) : InsertWeak(s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 267 | ObjPtr<mirror::String> InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 268 | DCHECK(utf8_data != nullptr); |
Vladimir Marko | d2bdb9b | 2016-07-08 17:23:22 +0100 | [diff] [blame] | 269 | Thread* self = Thread::Current(); |
| 270 | // Try to avoid allocation. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 271 | ObjPtr<mirror::String> s = LookupStrong(self, utf16_length, utf8_data); |
Vladimir Marko | d2bdb9b | 2016-07-08 17:23:22 +0100 | [diff] [blame] | 272 | if (s != nullptr) { |
| 273 | return s; |
| 274 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 275 | return InternStrong(mirror::String::AllocFromModifiedUtf8( |
Vladimir Marko | d2bdb9b | 2016-07-08 17:23:22 +0100 | [diff] [blame] | 276 | self, utf16_length, utf8_data)); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 279 | ObjPtr<mirror::String> InternTable::InternStrong(const char* utf8_data) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 280 | DCHECK(utf8_data != nullptr); |
| 281 | return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 284 | ObjPtr<mirror::String> InternTable::InternStrongImageString(ObjPtr<mirror::String> s) { |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 285 | // May be holding the heap bitmap lock. |
| 286 | return Insert(s, true, true); |
| 287 | } |
| 288 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 289 | ObjPtr<mirror::String> InternTable::InternStrong(ObjPtr<mirror::String> s) { |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 290 | return Insert(s, true, false); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 293 | ObjPtr<mirror::String> InternTable::InternWeak(ObjPtr<mirror::String> s) { |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 294 | return Insert(s, false, false); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 297 | bool InternTable::ContainsWeak(ObjPtr<mirror::String> s) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 298 | return LookupWeak(Thread::Current(), s) == s; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 301 | void InternTable::SweepInternTableWeaks(IsMarkedVisitor* visitor) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 302 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 303 | weak_interns_.SweepWeaks(visitor); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 306 | size_t InternTable::AddTableFromMemory(const uint8_t* ptr) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 307 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 308 | return AddTableFromMemoryLocked(ptr); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 311 | size_t InternTable::AddTableFromMemoryLocked(const uint8_t* ptr) { |
| 312 | return strong_interns_.AddTableFromMemory(ptr); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | size_t InternTable::WriteToMemory(uint8_t* ptr) { |
| 316 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 317 | return strong_interns_.WriteToMemory(ptr); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 320 | std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 321 | if (kIsDebugBuild) { |
| 322 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 323 | } |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 324 | // An additional cast to prevent undesired sign extension. |
| 325 | return static_cast<size_t>( |
| 326 | static_cast<uint32_t>(root.Read<kWithoutReadBarrier>()->GetHashCode())); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 330 | const GcRoot<mirror::String>& b) const { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 331 | if (kIsDebugBuild) { |
| 332 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 333 | } |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 334 | return a.Read<kWithoutReadBarrier>()->Equals(b.Read<kWithoutReadBarrier>()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 337 | bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a, |
| 338 | const Utf8String& b) const { |
| 339 | if (kIsDebugBuild) { |
| 340 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 341 | } |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 342 | ObjPtr<mirror::String> a_string = a.Read<kWithoutReadBarrier>(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 343 | uint32_t a_length = static_cast<uint32_t>(a_string->GetLength()); |
| 344 | if (a_length != b.GetUtf16Length()) { |
| 345 | return false; |
| 346 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 347 | if (a_string->IsCompressed()) { |
| 348 | size_t b_byte_count = strlen(b.GetUtf8Data()); |
| 349 | size_t b_utf8_length = CountModifiedUtf8Chars(b.GetUtf8Data(), b_byte_count); |
| 350 | // Modified UTF-8 single byte character range is 0x01 .. 0x7f |
| 351 | // The string compression occurs on regular ASCII with same exact range, |
| 352 | // not on extended ASCII which up to 0xff |
| 353 | const bool is_b_regular_ascii = (b_byte_count == b_utf8_length); |
| 354 | if (is_b_regular_ascii) { |
| 355 | return memcmp(b.GetUtf8Data(), |
| 356 | a_string->GetValueCompressed(), a_length * sizeof(uint8_t)) == 0; |
| 357 | } else { |
| 358 | return false; |
| 359 | } |
| 360 | } else { |
| 361 | const uint16_t* a_value = a_string->GetValue(); |
| 362 | return CompareModifiedUtf8ToUtf16AsCodePointValues(b.GetUtf8Data(), a_value, a_length) == 0; |
| 363 | } |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 366 | size_t InternTable::Table::AddTableFromMemory(const uint8_t* ptr) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 367 | size_t read_count = 0; |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 368 | UnorderedSet set(ptr, /*make copy*/false, &read_count); |
Mathieu Chartier | 619a457 | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 369 | if (set.Empty()) { |
| 370 | // Avoid inserting empty sets. |
| 371 | return read_count; |
| 372 | } |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 373 | // TODO: Disable this for app images if app images have intern tables. |
| 374 | static constexpr bool kCheckDuplicates = true; |
| 375 | if (kCheckDuplicates) { |
| 376 | for (GcRoot<mirror::String>& string : set) { |
| 377 | CHECK(Find(string.Read()) == nullptr) << "Already found " << string.Read()->ToModifiedUtf8(); |
| 378 | } |
| 379 | } |
Mathieu Chartier | 619a457 | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 380 | // Insert at the front since we add new interns into the back. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 381 | tables_.insert(tables_.begin(), std::move(set)); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 382 | return read_count; |
| 383 | } |
| 384 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 385 | size_t InternTable::Table::WriteToMemory(uint8_t* ptr) { |
| 386 | if (tables_.empty()) { |
| 387 | return 0; |
| 388 | } |
| 389 | UnorderedSet* table_to_write; |
| 390 | UnorderedSet combined; |
| 391 | if (tables_.size() > 1) { |
| 392 | table_to_write = &combined; |
| 393 | for (UnorderedSet& table : tables_) { |
| 394 | for (GcRoot<mirror::String>& string : table) { |
| 395 | combined.Insert(string); |
| 396 | } |
| 397 | } |
| 398 | } else { |
| 399 | table_to_write = &tables_.back(); |
| 400 | } |
| 401 | return table_to_write->WriteToMemory(ptr); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 404 | void InternTable::Table::Remove(ObjPtr<mirror::String> s) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 405 | for (UnorderedSet& table : tables_) { |
| 406 | auto it = table.Find(GcRoot<mirror::String>(s)); |
| 407 | if (it != table.end()) { |
| 408 | table.Erase(it); |
| 409 | return; |
| 410 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 411 | } |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 412 | LOG(FATAL) << "Attempting to remove non-interned string " << s->ToModifiedUtf8(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 415 | ObjPtr<mirror::String> InternTable::Table::Find(ObjPtr<mirror::String> s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 416 | Locks::intern_table_lock_->AssertHeld(Thread::Current()); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 417 | for (UnorderedSet& table : tables_) { |
| 418 | auto it = table.Find(GcRoot<mirror::String>(s)); |
| 419 | if (it != table.end()) { |
| 420 | return it->Read(); |
| 421 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 422 | } |
| 423 | return nullptr; |
| 424 | } |
| 425 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 426 | ObjPtr<mirror::String> InternTable::Table::Find(const Utf8String& string) { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 427 | Locks::intern_table_lock_->AssertHeld(Thread::Current()); |
| 428 | for (UnorderedSet& table : tables_) { |
| 429 | auto it = table.Find(string); |
| 430 | if (it != table.end()) { |
| 431 | return it->Read(); |
| 432 | } |
| 433 | } |
| 434 | return nullptr; |
| 435 | } |
| 436 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 437 | void InternTable::Table::AddNewTable() { |
| 438 | tables_.push_back(UnorderedSet()); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 441 | void InternTable::Table::Insert(ObjPtr<mirror::String> s) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 442 | // Always insert the last table, the image tables are before and we avoid inserting into these |
| 443 | // to prevent dirty pages. |
| 444 | DCHECK(!tables_.empty()); |
| 445 | tables_.back().Insert(GcRoot<mirror::String>(s)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 448 | void InternTable::Table::VisitRoots(RootVisitor* visitor) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 449 | BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor( |
| 450 | visitor, RootInfo(kRootInternedString)); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 451 | for (UnorderedSet& table : tables_) { |
| 452 | for (auto& intern : table) { |
| 453 | buffered_visitor.VisitRoot(intern); |
| 454 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 458 | void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 459 | for (UnorderedSet& table : tables_) { |
| 460 | SweepWeaks(&table, visitor); |
| 461 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 464 | void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 465 | for (auto it = set->begin(), end = set->end(); it != end;) { |
| 466 | // This does not need a read barrier because this is called by GC. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 467 | mirror::Object* object = it->Read<kWithoutReadBarrier>(); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 468 | mirror::Object* new_object = visitor->IsMarked(object); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 469 | if (new_object == nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 470 | it = set->Erase(it); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 471 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 472 | *it = GcRoot<mirror::String>(new_object->AsString()); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 473 | ++it; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 478 | size_t InternTable::Table::Size() const { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 479 | return std::accumulate(tables_.begin(), |
| 480 | tables_.end(), |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 481 | 0U, |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 482 | [](size_t sum, const UnorderedSet& set) { |
| 483 | return sum + set.Size(); |
| 484 | }); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 487 | void InternTable::ChangeWeakRootState(gc::WeakRootState new_state) { |
| 488 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 489 | ChangeWeakRootStateLocked(new_state); |
| 490 | } |
| 491 | |
| 492 | void InternTable::ChangeWeakRootStateLocked(gc::WeakRootState new_state) { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 493 | CHECK(!kUseReadBarrier); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 494 | weak_root_state_ = new_state; |
| 495 | if (new_state != gc::kWeakRootStateNoReadsOrWrites) { |
| 496 | weak_intern_condition_.Broadcast(Thread::Current()); |
| 497 | } |
| 498 | } |
| 499 | |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 500 | InternTable::Table::Table() { |
| 501 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 502 | // Initial table. |
| 503 | tables_.push_back(UnorderedSet()); |
| 504 | tables_.back().SetLoadFactor(runtime->GetHashTableMinLoadFactor(), |
| 505 | runtime->GetHashTableMaxLoadFactor()); |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 508 | } // namespace art |