blob: 5de70d86980f4b31a4f3848a9a2b965d57d15296 [file] [log] [blame]
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "intern_table.h"
4
Elliott Hughes90a33692011-08-30 13:27:07 -07005#include "UniquePtr.h"
Elliott Hughes814e4032011-08-23 12:07:56 -07006#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -07007
8namespace art {
9
Elliott Hughes8daa0922011-09-11 13:46:25 -070010InternTable::InternTable() : intern_table_lock_("InternTable lock") {
Elliott Hughesde69d7f2011-08-18 16:49:37 -070011}
12
Brian Carlstroma663ea52011-08-19 23:33:41 -070013size_t InternTable::Size() const {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070014 MutexLock mu(intern_table_lock_);
15 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070016}
17
Elliott Hughes410c0c82011-09-01 17:58:25 -070018void InternTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -070019 MutexLock mu(intern_table_lock_);
20 typedef Table::const_iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -070021 for (It it = strong_interns_.begin(), end = strong_interns_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070022 visitor(it->second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070023 }
Ian Rogers5d76c432011-10-31 21:42:49 -070024 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070025}
26
Brian Carlstromc74255f2011-09-11 22:47:39 -070027String* InternTable::Lookup(Table& table, String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070028 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070029 typedef Table::const_iterator It; // TODO: C++0x auto
30 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
Brian Carlstromc74255f2011-09-11 22:47:39 -070031 String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032 if (existing_string->Equals(s)) {
33 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070034 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035 }
36 return NULL;
37}
38
Brian Carlstromc74255f2011-09-11 22:47:39 -070039String* InternTable::Insert(Table& table, String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070040 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070041 table.insert(std::make_pair(hash_code, s));
42 return s;
43}
44
Brian Carlstromc74255f2011-09-11 22:47:39 -070045void InternTable::RegisterStrong(String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070046 MutexLock mu(intern_table_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -070047 Insert(image_strong_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070048}
49
50void InternTable::Remove(Table& table, const String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070051 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052 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 Carlstrom7e93b502011-08-04 14:16:22 -070058 }
59}
60
Brian Carlstromc74255f2011-09-11 22:47:39 -070061String* InternTable::Insert(String* s, bool is_strong) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070062 MutexLock mu(intern_table_lock_);
63
64 DCHECK(s != NULL);
65 uint32_t hash_code = s->GetHashCode();
66
67 if (is_strong) {
Ian Rogers5d76c432011-10-31 21:42:49 -070068 // Check the strong tables for a match.
Brian Carlstromc74255f2011-09-11 22:47:39 -070069 String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070070 if (strong != NULL) {
71 return strong;
72 }
Ian Rogers5d76c432011-10-31 21:42:49 -070073 strong = Lookup(image_strong_interns_, s, hash_code);
74 if (strong != NULL) {
75 return strong;
76 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077
78 // There is no match in the strong table, check the weak table.
Brian Carlstromc74255f2011-09-11 22:47:39 -070079 String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070080 if (weak != NULL) {
81 // A match was found in the weak table. Promote to the strong table.
82 Remove(weak_interns_, weak, hash_code);
83 return Insert(strong_interns_, weak, hash_code);
84 }
85
86 // No match in the strong table or the weak table. Insert into the strong table.
87 return Insert(strong_interns_, s, hash_code);
88 }
89
90 // Check the strong table for a match.
Brian Carlstromc74255f2011-09-11 22:47:39 -070091 String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070092 if (strong != NULL) {
93 return strong;
94 }
95 // Check the weak table for a match.
Brian Carlstromc74255f2011-09-11 22:47:39 -070096 String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070097 if (weak != NULL) {
98 return weak;
99 }
100 // Insert into the weak table.
101 return Insert(weak_interns_, s, hash_code);
102}
103
Brian Carlstromc74255f2011-09-11 22:47:39 -0700104String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700105 return InternStrong(String::AllocFromModifiedUtf8(utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700106}
107
Brian Carlstromc74255f2011-09-11 22:47:39 -0700108String* InternTable::InternStrong(const char* utf8_data) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700109 return InternStrong(String::AllocFromModifiedUtf8(utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700110}
111
112String* InternTable::InternStrong(String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700113 if (s == NULL) {
114 return NULL;
115 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700116 return Insert(s, true);
117}
118
119String* InternTable::InternWeak(String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700120 if (s == NULL) {
121 return NULL;
122 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700123 return Insert(s, false);
124}
125
Brian Carlstromc74255f2011-09-11 22:47:39 -0700126bool InternTable::ContainsWeak(String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700127 MutexLock mu(intern_table_lock_);
128 const String* found = Lookup(weak_interns_, s, s->GetHashCode());
129 return found == s;
130}
131
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700132void InternTable::SweepInternTableWeaks(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700133 MutexLock mu(intern_table_lock_);
134 typedef Table::const_iterator It; // TODO: C++0x auto
135 for (It it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700136 Object* object = it->second;
137 if (!is_marked(object, arg)) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700138 weak_interns_.erase(it++);
139 } else {
140 ++it;
141 }
142 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700143}
144
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700145} // namespace art