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