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