Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "intern_table.h" |
| 4 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 5 | #include "UniquePtr.h" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 6 | #include "utf.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 7 | |
| 8 | namespace art { |
| 9 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 10 | InternTable::InternTable() : intern_table_lock_("InternTable lock") { |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 11 | } |
| 12 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 13 | size_t InternTable::Size() const { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 14 | MutexLock mu(intern_table_lock_); |
| 15 | return strong_interns_.size() + weak_interns_.size(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 16 | } |
| 17 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 18 | void InternTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 19 | MutexLock mu(intern_table_lock_); |
| 20 | typedef Table::const_iterator It; // TODO: C++0x auto |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 21 | 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] | 22 | visitor(it->second, arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 23 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 24 | // Note: we deliberately don't visit the weak_interns_ table. |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 27 | String* InternTable::Lookup(Table& table, String* s, uint32_t hash_code) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | intern_table_lock_.AssertHeld(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 29 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 30 | 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] | 31 | String* existing_string = it->second; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 32 | if (existing_string->Equals(s)) { |
| 33 | return existing_string; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 34 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 35 | } |
| 36 | return NULL; |
| 37 | } |
| 38 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 39 | String* InternTable::Insert(Table& table, String* s, uint32_t hash_code) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | intern_table_lock_.AssertHeld(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 41 | table.insert(std::make_pair(hash_code, s)); |
| 42 | return s; |
| 43 | } |
| 44 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 45 | void InternTable::RegisterStrong(String* s) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 46 | MutexLock mu(intern_table_lock_); |
| 47 | Insert(strong_interns_, s, s->GetHashCode()); |
| 48 | } |
| 49 | |
| 50 | void InternTable::Remove(Table& table, const String* s, uint32_t hash_code) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 51 | intern_table_lock_.AssertHeld(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 52 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 53 | for (It it = table.find(hash_code), end = table.end(); it != end; ++it) { |
| 54 | if (it->second == s) { |
| 55 | table.erase(it); |
| 56 | return; |
| 57 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 61 | String* InternTable::Insert(String* s, bool is_strong) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 62 | MutexLock mu(intern_table_lock_); |
| 63 | |
| 64 | DCHECK(s != NULL); |
| 65 | uint32_t hash_code = s->GetHashCode(); |
| 66 | |
| 67 | if (is_strong) { |
| 68 | // Check the strong table for a match. |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 69 | String* strong = Lookup(strong_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 70 | if (strong != NULL) { |
| 71 | return strong; |
| 72 | } |
| 73 | |
| 74 | // There is no match in the strong table, check the weak table. |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 75 | String* weak = Lookup(weak_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 76 | if (weak != NULL) { |
| 77 | // A match was found in the weak table. Promote to the strong table. |
| 78 | Remove(weak_interns_, weak, hash_code); |
| 79 | return Insert(strong_interns_, weak, hash_code); |
| 80 | } |
| 81 | |
| 82 | // No match in the strong table or the weak table. Insert into the strong table. |
| 83 | return Insert(strong_interns_, s, hash_code); |
| 84 | } |
| 85 | |
| 86 | // Check the strong table for a match. |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 87 | String* strong = Lookup(strong_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 88 | if (strong != NULL) { |
| 89 | return strong; |
| 90 | } |
| 91 | // Check the weak table for a match. |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 92 | String* weak = Lookup(weak_interns_, s, hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 93 | if (weak != NULL) { |
| 94 | return weak; |
| 95 | } |
| 96 | // Insert into the weak table. |
| 97 | return Insert(weak_interns_, s, hash_code); |
| 98 | } |
| 99 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 100 | String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 101 | return Insert(String::AllocFromModifiedUtf8(utf16_length, utf8_data), true); |
| 102 | } |
| 103 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 104 | String* InternTable::InternStrong(const char* utf8_data) { |
| 105 | return Insert(String::AllocFromModifiedUtf8(utf8_data), true); |
| 106 | } |
| 107 | |
| 108 | String* InternTable::InternStrong(String* s) { |
| 109 | return Insert(s, true); |
| 110 | } |
| 111 | |
| 112 | String* InternTable::InternWeak(String* s) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 113 | return Insert(s, false); |
| 114 | } |
| 115 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 116 | bool InternTable::ContainsWeak(String* s) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 117 | MutexLock mu(intern_table_lock_); |
| 118 | const String* found = Lookup(weak_interns_, s, s->GetHashCode()); |
| 119 | return found == s; |
| 120 | } |
| 121 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 122 | void InternTable::RemoveWeakIf(const Predicate& predicate) { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 123 | MutexLock mu(intern_table_lock_); |
| 124 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 125 | for (It it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) { |
| 126 | if (predicate(it->second)) { |
| 127 | weak_interns_.erase(it++); |
| 128 | } else { |
| 129 | ++it; |
| 130 | } |
| 131 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 134 | } // namespace art |