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 | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | #include "mirror/string.h" |
| 20 | #include "thread.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 21 | #include "UniquePtr.h" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 22 | #include "utf.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 26 | InternTable::InternTable() : intern_table_lock_("InternTable lock"), is_dirty_(false) { |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 29 | size_t InternTable::Size() const { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 30 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 31 | return strong_interns_.size() + weak_interns_.size(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 34 | void InternTable::DumpForSigQuit(std::ostream& os) const { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 35 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 36 | os << "Intern table: " << strong_interns_.size() << " strong; " |
| 37 | << weak_interns_.size() << " weak; " |
| 38 | << image_strong_interns_.size() << " image strong\n"; |
| 39 | } |
| 40 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 41 | void InternTable::VisitRoots(RootVisitor* visitor, void* arg, bool clean_dirty) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 42 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 43 | for (const auto& strong_intern : strong_interns_) { |
| 44 | visitor(strong_intern.second, arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 45 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | if (clean_dirty) { |
| 47 | is_dirty_ = false; |
| 48 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 49 | // 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] | 50 | } |
| 51 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | 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] | 53 | intern_table_lock_.AssertHeld(Thread::Current()); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 54 | 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] | 55 | mirror::String* existing_string = it->second; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 56 | if (existing_string->Equals(s)) { |
| 57 | return existing_string; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 58 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 59 | } |
| 60 | return NULL; |
| 61 | } |
| 62 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | 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] | 64 | intern_table_lock_.AssertHeld(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 65 | table.insert(std::make_pair(hash_code, s)); |
| 66 | return s; |
| 67 | } |
| 68 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | void InternTable::RegisterStrong(mirror::String* s) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 70 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 71 | Insert(image_strong_interns_, s, s->GetHashCode()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | void InternTable::Remove(Table& table, const mirror::String* s, uint32_t hash_code) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 75 | intern_table_lock_.AssertHeld(Thread::Current()); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 76 | 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] | 77 | if (it->second == s) { |
| 78 | table.erase(it); |
| 79 | return; |
| 80 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 85 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 86 | |
| 87 | DCHECK(s != NULL); |
| 88 | uint32_t hash_code = s->GetHashCode(); |
| 89 | |
| 90 | if (is_strong) { |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 91 | // Check the strong table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 92 | mirror::String* strong = Lookup(strong_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 93 | if (strong != NULL) { |
| 94 | return strong; |
| 95 | } |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 96 | // Check the image table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | mirror::String* image = Lookup(image_strong_interns_, s, hash_code); |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 98 | if (image != NULL) { |
| 99 | return image; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 100 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 101 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 102 | // Mark as dirty so that we rescan the roots. |
| 103 | Dirty(); |
| 104 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 105 | // There is no match in the strong table, check the weak table. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | mirror::String* weak = Lookup(weak_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 107 | if (weak != NULL) { |
| 108 | // A match was found in the weak table. Promote to the strong table. |
| 109 | Remove(weak_interns_, weak, hash_code); |
| 110 | return Insert(strong_interns_, weak, hash_code); |
| 111 | } |
| 112 | |
| 113 | // No match in the strong table or the weak table. Insert into the strong table. |
| 114 | return Insert(strong_interns_, s, hash_code); |
| 115 | } |
| 116 | |
| 117 | // Check the strong table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 118 | mirror::String* strong = Lookup(strong_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 119 | if (strong != NULL) { |
| 120 | return strong; |
| 121 | } |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 122 | // Check the image table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 123 | mirror::String* image = Lookup(image_strong_interns_, s, hash_code); |
jeffhao | b1c6f34 | 2012-03-20 17:57:26 -0700 | [diff] [blame] | 124 | if (image != NULL) { |
| 125 | return image; |
| 126 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 127 | // Check the weak table for a match. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 128 | mirror::String* weak = Lookup(weak_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 129 | if (weak != NULL) { |
| 130 | return weak; |
| 131 | } |
| 132 | // Insert into the weak table. |
| 133 | return Insert(weak_interns_, s, hash_code); |
| 134 | } |
| 135 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 136 | mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) { |
| 137 | return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf16_length, utf8_data)); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 140 | mirror::String* InternTable::InternStrong(const char* utf8_data) { |
| 141 | return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 144 | mirror::String* InternTable::InternStrong(mirror::String* s) { |
Elliott Hughes | 37d4e6b | 2011-10-13 12:05:20 -0700 | [diff] [blame] | 145 | if (s == NULL) { |
| 146 | return NULL; |
| 147 | } |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 148 | return Insert(s, true); |
| 149 | } |
| 150 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | mirror::String* InternTable::InternWeak(mirror::String* s) { |
Elliott Hughes | 37d4e6b | 2011-10-13 12:05:20 -0700 | [diff] [blame] | 152 | if (s == NULL) { |
| 153 | return NULL; |
| 154 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 155 | return Insert(s, false); |
| 156 | } |
| 157 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 158 | bool InternTable::ContainsWeak(mirror::String* s) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 159 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 160 | const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 161 | return found == s; |
| 162 | } |
| 163 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 164 | void InternTable::SweepInternTableWeaks(IsMarkedTester is_marked, void* arg) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 165 | MutexLock mu(Thread::Current(), intern_table_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 166 | // TODO: std::remove_if + lambda. |
| 167 | for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 168 | mirror::Object* object = it->second; |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 169 | if (!is_marked(object, arg)) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 170 | weak_interns_.erase(it++); |
| 171 | } else { |
| 172 | ++it; |
| 173 | } |
| 174 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 177 | } // namespace art |