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