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