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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/string.h" |
| 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 | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 32 | : log_new_roots_(false), allow_new_interns_(true), |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 33 | new_intern_condition_("New intern condition", *Locks::intern_table_lock_) { |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 34 | } |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 35 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 36 | size_t InternTable::Size() const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 37 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 38 | return strong_interns_.size() + weak_interns_.size(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 41 | size_t InternTable::StrongSize() const { |
| 42 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 43 | return strong_interns_.size(); |
| 44 | } |
| 45 | |
| 46 | size_t InternTable::WeakSize() const { |
| 47 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 48 | return weak_interns_.size(); |
| 49 | } |
| 50 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 51 | void InternTable::DumpForSigQuit(std::ostream& os) const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 52 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 53 | os << "Intern table: " << strong_interns_.size() << " strong; " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 54 | << weak_interns_.size() << " weak\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 57 | void InternTable::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 58 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 59 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | b307052 | 2013-09-17 14:18:21 -0700 | [diff] [blame] | 60 | for (auto& strong_intern : strong_interns_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 61 | strong_intern.second.VisitRoot(callback, arg, 0, kRootInternedString); |
| 62 | DCHECK(!strong_intern.second.IsNull()); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 63 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 64 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 65 | for (auto& pair : new_strong_intern_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 66 | mirror::String* old_ref = pair.second.Read<kWithoutReadBarrier>(); |
| 67 | pair.second.VisitRoot(callback, arg, 0, kRootInternedString); |
| 68 | mirror::String* new_ref = pair.second.Read<kWithoutReadBarrier>(); |
| 69 | if (UNLIKELY(new_ref != old_ref)) { |
| 70 | // Uh ohes, GC moved a root in the log. Need to search the strong interns and update the |
| 71 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 72 | // concurrent moving GC. |
| 73 | for (auto it = strong_interns_.lower_bound(pair.first), end = strong_interns_.end(); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 74 | it != end && it->first == pair.first; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 75 | // If the class stored matches the old class, update it to the new value. |
| 76 | if (old_ref == it->second.Read<kWithoutReadBarrier>()) { |
| 77 | it->second = GcRoot<mirror::String>(new_ref); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 85 | new_strong_intern_roots_.clear(); |
| 86 | } |
| 87 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 88 | log_new_roots_ = true; |
| 89 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 90 | log_new_roots_ = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 91 | } |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 92 | // 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] | 93 | } |
| 94 | |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 95 | mirror::String* InternTable::LookupStrong(mirror::String* s, int32_t hash_code) { |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 96 | return Lookup(&strong_interns_, s, hash_code); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | mirror::String* InternTable::LookupWeak(mirror::String* s, int32_t hash_code) { |
| 100 | // Weak interns need a read barrier because they are weak roots. |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 101 | return Lookup(&weak_interns_, s, hash_code); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 104 | mirror::String* InternTable::Lookup(Table* table, mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 105 | Locks::intern_table_lock_->AssertHeld(Thread::Current()); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 106 | for (auto it = table->lower_bound(hash_code), end = table->end(); |
Vladimir Marko | 53dc70c | 2014-05-22 12:16:44 +0100 | [diff] [blame] | 107 | it != end && it->first == hash_code; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 108 | mirror::String* existing_string = it->second.Read(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 109 | if (existing_string->Equals(s)) { |
| 110 | return existing_string; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 111 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 112 | } |
| 113 | return NULL; |
| 114 | } |
| 115 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 116 | mirror::String* InternTable::InsertStrong(mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 117 | Runtime* runtime = Runtime::Current(); |
| 118 | if (runtime->IsActiveTransaction()) { |
| 119 | runtime->RecordStrongStringInsertion(s, hash_code); |
| 120 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 121 | if (log_new_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 122 | new_strong_intern_roots_.push_back(std::make_pair(hash_code, GcRoot<mirror::String>(s))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 123 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 124 | strong_interns_.insert(std::make_pair(hash_code, GcRoot<mirror::String>(s))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 125 | return s; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 128 | mirror::String* InternTable::InsertWeak(mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 129 | Runtime* runtime = Runtime::Current(); |
| 130 | if (runtime->IsActiveTransaction()) { |
| 131 | runtime->RecordWeakStringInsertion(s, hash_code); |
| 132 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 133 | weak_interns_.insert(std::make_pair(hash_code, GcRoot<mirror::String>(s))); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 134 | return s; |
| 135 | } |
| 136 | |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 137 | void InternTable::RemoveStrong(mirror::String* s, int32_t hash_code) { |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 138 | Remove(&strong_interns_, s, hash_code); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 141 | void InternTable::RemoveWeak(mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 142 | Runtime* runtime = Runtime::Current(); |
| 143 | if (runtime->IsActiveTransaction()) { |
| 144 | runtime->RecordWeakStringRemoval(s, hash_code); |
| 145 | } |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 146 | Remove(&weak_interns_, s, hash_code); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 149 | void InternTable::Remove(Table* table, mirror::String* s, int32_t hash_code) { |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 150 | for (auto it = table->lower_bound(hash_code), end = table->end(); |
Vladimir Marko | 53dc70c | 2014-05-22 12:16:44 +0100 | [diff] [blame] | 151 | it != end && it->first == hash_code; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 152 | mirror::String* existing_string = it->second.Read(); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 153 | if (existing_string == s) { |
| 154 | table->erase(it); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 155 | return; |
| 156 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 160 | // Insert/remove methods used to undo changes made during an aborted transaction. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 161 | mirror::String* InternTable::InsertStrongFromTransaction(mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 162 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 163 | return InsertStrong(s, hash_code); |
| 164 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 165 | mirror::String* InternTable::InsertWeakFromTransaction(mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 166 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 167 | return InsertWeak(s, hash_code); |
| 168 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 169 | void InternTable::RemoveStrongFromTransaction(mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 170 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 171 | RemoveStrong(s, hash_code); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 172 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 173 | void InternTable::RemoveWeakFromTransaction(mirror::String* s, int32_t hash_code) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 174 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 175 | RemoveWeak(s, hash_code); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 178 | static mirror::String* LookupStringFromImage(mirror::String* s) |
| 179 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 180 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 181 | if (image == NULL) { |
| 182 | return NULL; // No image present. |
| 183 | } |
| 184 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 185 | mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>(); |
| 186 | const std::string utf8 = s->ToModifiedUtf8(); |
| 187 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 188 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 189 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 190 | // Binary search the dex file for the string index. |
| 191 | const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str()); |
| 192 | if (string_id != NULL) { |
| 193 | uint32_t string_idx = dex_file->GetIndexForStringId(*string_id); |
| 194 | mirror::String* image = dex_cache->GetResolvedString(string_idx); |
| 195 | if (image != NULL) { |
| 196 | return image; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | return NULL; |
| 201 | } |
| 202 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 203 | void InternTable::AllowNewInterns() { |
| 204 | Thread* self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 205 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 206 | allow_new_interns_ = true; |
| 207 | new_intern_condition_.Broadcast(self); |
| 208 | } |
| 209 | |
| 210 | void InternTable::DisallowNewInterns() { |
| 211 | Thread* self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 212 | MutexLock mu(self, *Locks::intern_table_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 213 | allow_new_interns_ = false; |
| 214 | } |
| 215 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 216 | mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) { |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 217 | Thread* self = Thread::Current(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 218 | MutexLock mu(self, *Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 219 | |
| 220 | DCHECK(s != NULL); |
| 221 | uint32_t hash_code = s->GetHashCode(); |
| 222 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 223 | while (UNLIKELY(!allow_new_interns_)) { |
| 224 | new_intern_condition_.WaitHoldingLocks(self); |
| 225 | } |
| 226 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 227 | if (is_strong) { |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 228 | // Check the strong table for a match. |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 229 | mirror::String* strong = LookupStrong(s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 230 | if (strong != NULL) { |
| 231 | return strong; |
| 232 | } |
| 233 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 234 | // Check the image for a match. |
| 235 | mirror::String* image = LookupStringFromImage(s); |
| 236 | if (image != NULL) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 237 | return InsertStrong(image, hash_code); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 240 | // There is no match in the strong table, check the weak table. |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 241 | mirror::String* weak = LookupWeak(s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 242 | if (weak != NULL) { |
| 243 | // A match was found in the weak table. Promote to the strong table. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 244 | RemoveWeak(weak, hash_code); |
| 245 | return InsertStrong(weak, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 248 | // No match in the strong table or the weak table. Insert into the strong |
| 249 | // table. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 250 | return InsertStrong(s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Check the strong table for a match. |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 254 | mirror::String* strong = LookupStrong(s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 255 | if (strong != NULL) { |
| 256 | return strong; |
| 257 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 258 | // Check the image for a match. |
| 259 | mirror::String* image = LookupStringFromImage(s); |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 260 | if (image != NULL) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 261 | return InsertWeak(image, hash_code); |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 262 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 263 | // Check the weak table for a match. |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 264 | mirror::String* weak = LookupWeak(s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 265 | if (weak != NULL) { |
| 266 | return weak; |
| 267 | } |
| 268 | // Insert into the weak table. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 269 | return InsertWeak(s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 272 | mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) { |
| 273 | DCHECK(utf8_data != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 274 | return InternStrong(mirror::String::AllocFromModifiedUtf8( |
| 275 | Thread::Current(), utf16_length, utf8_data)); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 278 | mirror::String* InternTable::InternStrong(const char* utf8_data) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 279 | DCHECK(utf8_data != nullptr); |
| 280 | return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 283 | mirror::String* InternTable::InternStrong(mirror::String* s) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 284 | if (s == nullptr) { |
| 285 | return nullptr; |
Elliott Hughes | 37d4e6b | 2011-10-13 12:05:20 -0700 | [diff] [blame] | 286 | } |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 287 | return Insert(s, true); |
| 288 | } |
| 289 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 290 | mirror::String* InternTable::InternWeak(mirror::String* s) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 291 | if (s == nullptr) { |
| 292 | return nullptr; |
Elliott Hughes | 37d4e6b | 2011-10-13 12:05:20 -0700 | [diff] [blame] | 293 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 294 | return Insert(s, false); |
| 295 | } |
| 296 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 297 | bool InternTable::ContainsWeak(mirror::String* s) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 298 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 299 | const mirror::String* found = LookupWeak(s, s->GetHashCode()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 300 | return found == s; |
| 301 | } |
| 302 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 303 | void InternTable::SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 304 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 305 | for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) { |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 306 | // This does not need a read barrier because this is called by GC. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 307 | mirror::Object* object = it->second.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 308 | mirror::Object* new_object = callback(object, arg); |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 309 | if (new_object == nullptr) { |
| 310 | // TODO: use it = weak_interns_.erase(it) when we get a c++11 stl. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 311 | weak_interns_.erase(it++); |
| 312 | } else { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 313 | it->second = GcRoot<mirror::String>(down_cast<mirror::String*>(new_object)); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 314 | ++it; |
| 315 | } |
| 316 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 319 | } // namespace art |