blob: 10dd7f020a0800a2eb29184980b18814b3d5a99e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom7e93b502011-08-04 14:16:22 -070016
17#include "intern_table.h"
18
Elliott Hughes90a33692011-08-30 13:27:07 -070019#include "UniquePtr.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070020#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070021
22namespace art {
23
Elliott Hughes8daa0922011-09-11 13:46:25 -070024InternTable::InternTable() : intern_table_lock_("InternTable lock") {
Elliott Hughesde69d7f2011-08-18 16:49:37 -070025}
26
Brian Carlstroma663ea52011-08-19 23:33:41 -070027size_t InternTable::Size() const {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070028 MutexLock mu(intern_table_lock_);
29 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070030}
31
Elliott Hughescac6cc72011-11-03 20:31:21 -070032void 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 Hughes410c0c82011-09-01 17:58:25 -070039void InternTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -070040 MutexLock mu(intern_table_lock_);
41 typedef Table::const_iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -070042 for (It it = strong_interns_.begin(), end = strong_interns_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070043 visitor(it->second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070044 }
Ian Rogers5d76c432011-10-31 21:42:49 -070045 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070046}
47
Brian Carlstromc74255f2011-09-11 22:47:39 -070048String* InternTable::Lookup(Table& table, String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070049 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070050 typedef Table::const_iterator It; // TODO: C++0x auto
51 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
Brian Carlstromc74255f2011-09-11 22:47:39 -070052 String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070053 if (existing_string->Equals(s)) {
54 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070055 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056 }
57 return NULL;
58}
59
Brian Carlstromc74255f2011-09-11 22:47:39 -070060String* InternTable::Insert(Table& table, String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070061 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070062 table.insert(std::make_pair(hash_code, s));
63 return s;
64}
65
Brian Carlstromc74255f2011-09-11 22:47:39 -070066void InternTable::RegisterStrong(String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 MutexLock mu(intern_table_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -070068 Insert(image_strong_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070069}
70
71void InternTable::Remove(Table& table, const String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070072 intern_table_lock_.AssertHeld();
Elliott Hughese5448b52012-01-18 16:44:06 -080073 typedef Table::iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074 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 Carlstrom7e93b502011-08-04 14:16:22 -070079 }
80}
81
Brian Carlstromc74255f2011-09-11 22:47:39 -070082String* InternTable::Insert(String* s, bool is_strong) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070083 MutexLock mu(intern_table_lock_);
84
85 DCHECK(s != NULL);
86 uint32_t hash_code = s->GetHashCode();
87
88 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -070089 // Check the strong table for a match.
Brian Carlstromc74255f2011-09-11 22:47:39 -070090 String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070091 if (strong != NULL) {
92 return strong;
93 }
jeffhaob1c6f342012-03-20 17:57:26 -070094 // 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 Rogers5d76c432011-10-31 21:42:49 -070098 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070099
100 // There is no match in the strong table, check the weak table.
Brian Carlstromc74255f2011-09-11 22:47:39 -0700101 String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700102 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 Carlstromc74255f2011-09-11 22:47:39 -0700113 String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700114 if (strong != NULL) {
115 return strong;
116 }
jeffhaob1c6f342012-03-20 17:57:26 -0700117 // 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 Hughescf4c6c42011-09-01 15:16:42 -0700122 // Check the weak table for a match.
Brian Carlstromc74255f2011-09-11 22:47:39 -0700123 String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700124 if (weak != NULL) {
125 return weak;
126 }
127 // Insert into the weak table.
128 return Insert(weak_interns_, s, hash_code);
129}
130
Brian Carlstromc74255f2011-09-11 22:47:39 -0700131String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700132 return InternStrong(String::AllocFromModifiedUtf8(utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700133}
134
Brian Carlstromc74255f2011-09-11 22:47:39 -0700135String* InternTable::InternStrong(const char* utf8_data) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700136 return InternStrong(String::AllocFromModifiedUtf8(utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700137}
138
139String* InternTable::InternStrong(String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700140 if (s == NULL) {
141 return NULL;
142 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700143 return Insert(s, true);
144}
145
146String* InternTable::InternWeak(String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700147 if (s == NULL) {
148 return NULL;
149 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700150 return Insert(s, false);
151}
152
Brian Carlstromc74255f2011-09-11 22:47:39 -0700153bool InternTable::ContainsWeak(String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700154 MutexLock mu(intern_table_lock_);
155 const String* found = Lookup(weak_interns_, s, s->GetHashCode());
156 return found == s;
157}
158
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700159void InternTable::SweepInternTableWeaks(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700160 MutexLock mu(intern_table_lock_);
Elliott Hughese5448b52012-01-18 16:44:06 -0800161 typedef Table::iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700162 for (It it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700163 Object* object = it->second;
164 if (!is_marked(object, arg)) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700165 weak_interns_.erase(it++);
166 } else {
167 ++it;
168 }
169 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700170}
171
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700172} // namespace art