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 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 21 | #include "gc/space/image_space.h" |
| 22 | #include "mirror/dex_cache.h" |
| 23 | #include "mirror/object_array-inl.h" |
| 24 | #include "mirror/object-inl.h" |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 25 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "thread.h" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 27 | #include "utf.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 31 | InternTable::InternTable() |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 32 | : image_added_to_intern_table_(false), log_new_roots_(false), |
| 33 | allow_new_interns_(true), |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 34 | new_intern_condition_("New intern condition", *Locks::intern_table_lock_) { |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 35 | } |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 36 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 37 | size_t InternTable::Size() const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 38 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 39 | return strong_interns_.Size() + weak_interns_.Size(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 42 | size_t InternTable::StrongSize() const { |
| 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(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | size_t InternTable::WeakSize() const { |
| 48 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 49 | return weak_interns_.Size(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 52 | void InternTable::DumpForSigQuit(std::ostream& os) const { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 53 | os << "Intern table: " << StrongSize() << " strong; " << WeakSize() << " weak\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 56 | void InternTable::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 57 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 58 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 59 | strong_interns_.VisitRoots(callback, arg); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 60 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 61 | for (auto& root : new_strong_intern_roots_) { |
| 62 | mirror::String* old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 63 | root.VisitRoot(callback, arg, RootInfo(kRootInternedString)); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 64 | mirror::String* new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 65 | if (new_ref != old_ref) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 66 | // 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] | 67 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 68 | // concurrent moving GC. |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 69 | strong_interns_.Remove(old_ref); |
| 70 | strong_interns_.Insert(new_ref); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 73 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 74 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 75 | new_strong_intern_roots_.clear(); |
| 76 | } |
| 77 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 78 | log_new_roots_ = true; |
| 79 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 80 | log_new_roots_ = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 81 | } |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 82 | // 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] | 83 | } |
| 84 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 85 | mirror::String* InternTable::LookupStrong(mirror::String* s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 86 | return strong_interns_.Find(s); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 89 | mirror::String* InternTable::LookupWeak(mirror::String* s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 90 | return weak_interns_.Find(s); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 93 | void InternTable::SwapPostZygoteWithPreZygote() { |
| 94 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 95 | weak_interns_.SwapPostZygoteWithPreZygote(); |
| 96 | strong_interns_.SwapPostZygoteWithPreZygote(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 99 | mirror::String* InternTable::InsertStrong(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 100 | Runtime* runtime = Runtime::Current(); |
| 101 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 102 | runtime->RecordStrongStringInsertion(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 103 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 104 | if (log_new_roots_) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 105 | new_strong_intern_roots_.push_back(GcRoot<mirror::String>(s)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 106 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 107 | strong_interns_.Insert(s); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 108 | return s; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 111 | mirror::String* InternTable::InsertWeak(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 112 | Runtime* runtime = Runtime::Current(); |
| 113 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 114 | runtime->RecordWeakStringInsertion(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 115 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 116 | weak_interns_.Insert(s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 117 | return s; |
| 118 | } |
| 119 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 120 | void InternTable::RemoveStrong(mirror::String* s) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 121 | strong_interns_.Remove(s); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 124 | void InternTable::RemoveWeak(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 125 | Runtime* runtime = Runtime::Current(); |
| 126 | if (runtime->IsActiveTransaction()) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 127 | runtime->RecordWeakStringRemoval(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 128 | } |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 129 | weak_interns_.Remove(s); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 132 | // Insert/remove methods used to undo changes made during an aborted transaction. |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 133 | mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 134 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 135 | return InsertStrong(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 136 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 137 | mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 138 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 139 | return InsertWeak(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 140 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 141 | void InternTable::RemoveStrongFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 142 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 143 | RemoveStrong(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 144 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 145 | void InternTable::RemoveWeakFromTransaction(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 146 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 147 | RemoveWeak(s); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 150 | void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space) { |
Mathieu Chartier | bc58ede | 2014-11-17 12:36:24 -0800 | [diff] [blame] | 151 | CHECK(image_space != nullptr); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 152 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 153 | if (!image_added_to_intern_table_) { |
| 154 | mirror::Object* root = image_space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 155 | mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>(); |
| 156 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 157 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 158 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 159 | const size_t num_strings = dex_file->NumStringIds(); |
| 160 | for (size_t j = 0; j < num_strings; ++j) { |
| 161 | mirror::String* image_string = dex_cache->GetResolvedString(j); |
| 162 | if (image_string != nullptr) { |
| 163 | mirror::String* found = LookupStrong(image_string); |
| 164 | if (found == nullptr) { |
| 165 | InsertStrong(image_string); |
| 166 | } else { |
| 167 | DCHECK_EQ(found, image_string); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | image_added_to_intern_table_ = true; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | mirror::String* InternTable::LookupStringFromImage(mirror::String* s) |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 177 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 178 | if (image_added_to_intern_table_) { |
| 179 | return nullptr; |
| 180 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 181 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 182 | if (image == nullptr) { |
| 183 | return nullptr; // No image present. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 184 | } |
| 185 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 186 | mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>(); |
| 187 | const std::string utf8 = s->ToModifiedUtf8(); |
| 188 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 189 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 190 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 191 | // Binary search the dex file for the string index. |
| 192 | const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 193 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 194 | uint32_t string_idx = dex_file->GetIndexForStringId(*string_id); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 195 | // GetResolvedString() contains a RB. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 196 | mirror::String* image_string = dex_cache->GetResolvedString(string_idx); |
| 197 | if (image_string != NULL) { |
| 198 | return image_string; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 202 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 205 | void InternTable::AllowNewInterns() { |
| 206 | Thread* self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 207 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 208 | allow_new_interns_ = true; |
| 209 | new_intern_condition_.Broadcast(self); |
| 210 | } |
| 211 | |
| 212 | void InternTable::DisallowNewInterns() { |
| 213 | Thread* self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 214 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 215 | allow_new_interns_ = false; |
| 216 | } |
| 217 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 218 | void InternTable::EnsureNewInternsDisallowed() { |
| 219 | // Lock and unlock once to ensure that no threads are still in the |
| 220 | // middle of adding new interns. |
| 221 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 222 | CHECK(!allow_new_interns_); |
| 223 | } |
| 224 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 226 | if (s == nullptr) { |
| 227 | return nullptr; |
| 228 | } |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 229 | Thread* self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 230 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 231 | while (UNLIKELY(!allow_new_interns_)) { |
| 232 | new_intern_condition_.WaitHoldingLocks(self); |
| 233 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 234 | // Check the strong table for a match. |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 235 | mirror::String* strong = LookupStrong(s); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 236 | if (strong != nullptr) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 237 | return strong; |
| 238 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 239 | // Check the image for a match. |
| 240 | mirror::String* image = LookupStringFromImage(s); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 241 | if (image != nullptr) { |
| 242 | return is_strong ? InsertStrong(image) : InsertWeak(image); |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 243 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 244 | // There is no match in the strong table, check the weak table. |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 245 | mirror::String* weak = LookupWeak(s); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 246 | if (weak != nullptr) { |
| 247 | if (is_strong) { |
| 248 | // A match was found in the weak table. Promote to the strong table. |
| 249 | RemoveWeak(weak); |
| 250 | return InsertStrong(weak); |
| 251 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 252 | return weak; |
| 253 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 254 | // No match in the strong table or the weak table. Insert into the strong / weak table. |
| 255 | return is_strong ? InsertStrong(s) : InsertWeak(s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 258 | mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) { |
| 259 | DCHECK(utf8_data != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 260 | return InternStrong(mirror::String::AllocFromModifiedUtf8( |
| 261 | Thread::Current(), utf16_length, utf8_data)); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 264 | mirror::String* InternTable::InternStrong(const char* utf8_data) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 265 | DCHECK(utf8_data != nullptr); |
| 266 | return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 269 | mirror::String* InternTable::InternStrong(mirror::String* s) { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 270 | return Insert(s, true); |
| 271 | } |
| 272 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 273 | mirror::String* InternTable::InternWeak(mirror::String* s) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 274 | return Insert(s, false); |
| 275 | } |
| 276 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 277 | bool InternTable::ContainsWeak(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 278 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 279 | return LookupWeak(s) == s; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 282 | void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 283 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 284 | weak_interns_.SweepWeaks(callback, arg); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 287 | std::size_t InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& root) const { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 288 | if (kIsDebugBuild) { |
| 289 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 290 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 291 | return static_cast<size_t>(root.Read()->GetHashCode()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | bool InternTable::StringHashEquals::operator()(const GcRoot<mirror::String>& a, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 295 | const GcRoot<mirror::String>& b) const { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 296 | if (kIsDebugBuild) { |
| 297 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 298 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 299 | return a.Read()->Equals(b.Read()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 302 | void InternTable::Table::Remove(mirror::String* s) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 303 | auto it = post_zygote_table_.Find(GcRoot<mirror::String>(s)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 304 | if (it != post_zygote_table_.end()) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 305 | post_zygote_table_.Erase(it); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 306 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 307 | it = pre_zygote_table_.Find(GcRoot<mirror::String>(s)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 308 | DCHECK(it != pre_zygote_table_.end()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 309 | pre_zygote_table_.Erase(it); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | mirror::String* InternTable::Table::Find(mirror::String* s) { |
| 314 | Locks::intern_table_lock_->AssertHeld(Thread::Current()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 315 | auto it = pre_zygote_table_.Find(GcRoot<mirror::String>(s)); |
| 316 | if (it != pre_zygote_table_.end()) { |
| 317 | return it->Read(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 318 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 319 | it = post_zygote_table_.Find(GcRoot<mirror::String>(s)); |
| 320 | if (it != post_zygote_table_.end()) { |
| 321 | return it->Read(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 322 | } |
| 323 | return nullptr; |
| 324 | } |
| 325 | |
| 326 | void InternTable::Table::SwapPostZygoteWithPreZygote() { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 327 | CHECK(pre_zygote_table_.Empty()); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 328 | std::swap(pre_zygote_table_, post_zygote_table_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 329 | VLOG(heap) << "Swapping " << pre_zygote_table_.Size() << " interns to the pre zygote table"; |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void InternTable::Table::Insert(mirror::String* s) { |
| 333 | // Always insert the post zygote table, this gets swapped when we create the zygote to be the |
| 334 | // pre zygote table. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 335 | post_zygote_table_.Insert(GcRoot<mirror::String>(s)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 338 | void InternTable::Table::VisitRoots(RootCallback* callback, void* arg) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 339 | for (auto& intern : pre_zygote_table_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 340 | intern.VisitRoot(callback, arg, RootInfo(kRootInternedString)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 341 | } |
| 342 | for (auto& intern : post_zygote_table_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 343 | intern.VisitRoot(callback, arg, RootInfo(kRootInternedString)); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | void InternTable::Table::SweepWeaks(IsMarkedCallback* callback, void* arg) { |
| 348 | SweepWeaks(&pre_zygote_table_, callback, arg); |
| 349 | SweepWeaks(&post_zygote_table_, callback, arg); |
| 350 | } |
| 351 | |
| 352 | void InternTable::Table::SweepWeaks(UnorderedSet* set, IsMarkedCallback* callback, void* arg) { |
| 353 | for (auto it = set->begin(), end = set->end(); it != end;) { |
| 354 | // 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] | 355 | mirror::Object* object = it->Read<kWithoutReadBarrier>(); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 356 | mirror::Object* new_object = callback(object, arg); |
| 357 | if (new_object == nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 358 | it = set->Erase(it); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 359 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 360 | *it = GcRoot<mirror::String>(new_object->AsString()); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 361 | ++it; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 366 | size_t InternTable::Table::Size() const { |
| 367 | return pre_zygote_table_.Size() + post_zygote_table_.Size(); |
| 368 | } |
| 369 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 370 | } // namespace art |