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 | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 31 | : intern_table_lock_("InternTable lock"), is_dirty_(false), allow_new_interns_(true), |
| 32 | new_intern_condition_("New intern condition", intern_table_lock_) { |
| 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 { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 36 | MutexLock mu(Thread::Current(), 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 { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 41 | MutexLock mu(Thread::Current(), 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 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 46 | void InternTable::VisitRoots(RootVisitor* visitor, void* arg, |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 47 | bool only_dirty, bool clean_dirty) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 48 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 49 | if (!only_dirty || is_dirty_) { |
Mathieu Chartier | b307052 | 2013-09-17 14:18:21 -0700 | [diff] [blame] | 50 | for (auto& strong_intern : strong_interns_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 51 | strong_intern.second = down_cast<mirror::String*>(visitor(strong_intern.second, arg)); |
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 | b307052 | 2013-09-17 14:18:21 -0700 | [diff] [blame] | 54 | |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 55 | if (clean_dirty) { |
| 56 | is_dirty_ = false; |
| 57 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 58 | } |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 59 | // 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] | 60 | } |
| 61 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 62 | mirror::String* InternTable::Lookup(Table& table, mirror::String* s, uint32_t hash_code) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 63 | intern_table_lock_.AssertHeld(Thread::Current()); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 64 | 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] | 65 | mirror::String* existing_string = it->second; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 66 | if (existing_string->Equals(s)) { |
| 67 | return existing_string; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 68 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 73 | mirror::String* InternTable::Insert(Table& table, mirror::String* s, uint32_t hash_code) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 74 | intern_table_lock_.AssertHeld(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 75 | table.insert(std::make_pair(hash_code, s)); |
| 76 | return s; |
| 77 | } |
| 78 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 79 | void InternTable::Remove(Table& table, const mirror::String* s, |
| 80 | uint32_t hash_code) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 81 | intern_table_lock_.AssertHeld(Thread::Current()); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 82 | 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] | 83 | if (it->second == s) { |
| 84 | table.erase(it); |
| 85 | return; |
| 86 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 90 | static mirror::String* LookupStringFromImage(mirror::String* s) |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 92 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 93 | if (image == NULL) { |
| 94 | return NULL; // No image present. |
| 95 | } |
| 96 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 97 | mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>(); |
| 98 | const std::string utf8 = s->ToModifiedUtf8(); |
| 99 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 100 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 101 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 102 | // Binary search the dex file for the string index. |
| 103 | const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str()); |
| 104 | if (string_id != NULL) { |
| 105 | uint32_t string_idx = dex_file->GetIndexForStringId(*string_id); |
| 106 | mirror::String* image = dex_cache->GetResolvedString(string_idx); |
| 107 | if (image != NULL) { |
| 108 | return image; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 115 | void InternTable::AllowNewInterns() { |
| 116 | Thread* self = Thread::Current(); |
| 117 | MutexLock mu(self, intern_table_lock_); |
| 118 | allow_new_interns_ = true; |
| 119 | new_intern_condition_.Broadcast(self); |
| 120 | } |
| 121 | |
| 122 | void InternTable::DisallowNewInterns() { |
| 123 | Thread* self = Thread::Current(); |
| 124 | MutexLock mu(self, intern_table_lock_); |
| 125 | allow_new_interns_ = false; |
| 126 | } |
| 127 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 128 | mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) { |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 129 | Thread* self = Thread::Current(); |
| 130 | MutexLock mu(self, intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 131 | |
| 132 | DCHECK(s != NULL); |
| 133 | uint32_t hash_code = s->GetHashCode(); |
| 134 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 135 | while (UNLIKELY(!allow_new_interns_)) { |
| 136 | new_intern_condition_.WaitHoldingLocks(self); |
| 137 | } |
| 138 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 139 | if (is_strong) { |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 140 | // Check the strong table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 141 | mirror::String* strong = Lookup(strong_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 142 | if (strong != NULL) { |
| 143 | return strong; |
| 144 | } |
| 145 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 146 | // Mark as dirty so that we rescan the roots. |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 147 | is_dirty_ = true; |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 148 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 149 | // Check the image for a match. |
| 150 | mirror::String* image = LookupStringFromImage(s); |
| 151 | if (image != NULL) { |
| 152 | return Insert(strong_interns_, image, hash_code); |
| 153 | } |
| 154 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 155 | // There is no match in the strong table, check the weak table. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | mirror::String* weak = Lookup(weak_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 157 | if (weak != NULL) { |
| 158 | // A match was found in the weak table. Promote to the strong table. |
| 159 | Remove(weak_interns_, weak, hash_code); |
| 160 | return Insert(strong_interns_, weak, hash_code); |
| 161 | } |
| 162 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 163 | // No match in the strong table or the weak table. Insert into the strong |
| 164 | // table. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 165 | return Insert(strong_interns_, s, hash_code); |
| 166 | } |
| 167 | |
| 168 | // Check the strong table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | mirror::String* strong = Lookup(strong_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 170 | if (strong != NULL) { |
| 171 | return strong; |
| 172 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 173 | // Check the image for a match. |
| 174 | mirror::String* image = LookupStringFromImage(s); |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 175 | if (image != NULL) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 176 | return Insert(weak_interns_, image, hash_code); |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 177 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 178 | // Check the weak table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 179 | mirror::String* weak = Lookup(weak_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 180 | if (weak != NULL) { |
| 181 | return weak; |
| 182 | } |
| 183 | // Insert into the weak table. |
| 184 | return Insert(weak_interns_, s, hash_code); |
| 185 | } |
| 186 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 187 | mirror::String* InternTable::InternStrong(int32_t utf16_length, |
| 188 | const char* utf8_data) { |
| 189 | return InternStrong(mirror::String::AllocFromModifiedUtf8( |
| 190 | Thread::Current(), utf16_length, utf8_data)); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 193 | mirror::String* InternTable::InternStrong(const char* utf8_data) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 194 | return InternStrong( |
| 195 | mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 198 | mirror::String* InternTable::InternStrong(mirror::String* s) { |
Elliott Hughes | 37d4e6b | 2011-10-13 12:05:20 -0700 | [diff] [blame] | 199 | if (s == NULL) { |
| 200 | return NULL; |
| 201 | } |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 202 | return Insert(s, true); |
| 203 | } |
| 204 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 205 | mirror::String* InternTable::InternWeak(mirror::String* s) { |
Elliott Hughes | 37d4e6b | 2011-10-13 12:05:20 -0700 | [diff] [blame] | 206 | if (s == NULL) { |
| 207 | return NULL; |
| 208 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 209 | return Insert(s, false); |
| 210 | } |
| 211 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 212 | bool InternTable::ContainsWeak(mirror::String* s) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 213 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 214 | const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 215 | return found == s; |
| 216 | } |
| 217 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 218 | void InternTable::SweepInternTableWeaks(RootVisitor visitor, void* arg) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 219 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 220 | for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 221 | mirror::Object* object = it->second; |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 222 | mirror::Object* new_object = visitor(object, arg); |
| 223 | if (new_object == nullptr) { |
| 224 | // 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] | 225 | weak_interns_.erase(it++); |
| 226 | } else { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 227 | it->second = down_cast<mirror::String*>(new_object); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 228 | ++it; |
| 229 | } |
| 230 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 233 | } // namespace art |