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