blob: f3c82acd6e2656bd9c83c33713b8c817ced060b3 [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) {
Ian Rogers5d76c432011-10-31 21:42:49 -070089 // Check the strong tables 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 }
Ian Rogers5d76c432011-10-31 21:42:49 -070094 strong = Lookup(image_strong_interns_, s, hash_code);
95 if (strong != NULL) {
96 return strong;
97 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070098
99 // There is no match in the strong table, check the weak table.
Brian Carlstromc74255f2011-09-11 22:47:39 -0700100 String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700101 if (weak != NULL) {
102 // A match was found in the weak table. Promote to the strong table.
103 Remove(weak_interns_, weak, hash_code);
104 return Insert(strong_interns_, weak, hash_code);
105 }
106
107 // No match in the strong table or the weak table. Insert into the strong table.
108 return Insert(strong_interns_, s, hash_code);
109 }
110
111 // Check the strong table for a match.
Brian Carlstromc74255f2011-09-11 22:47:39 -0700112 String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700113 if (strong != NULL) {
114 return strong;
115 }
116 // Check the weak table for a match.
Brian Carlstromc74255f2011-09-11 22:47:39 -0700117 String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700118 if (weak != NULL) {
119 return weak;
120 }
121 // Insert into the weak table.
122 return Insert(weak_interns_, s, hash_code);
123}
124
Brian Carlstromc74255f2011-09-11 22:47:39 -0700125String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700126 return InternStrong(String::AllocFromModifiedUtf8(utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700127}
128
Brian Carlstromc74255f2011-09-11 22:47:39 -0700129String* InternTable::InternStrong(const char* utf8_data) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700130 return InternStrong(String::AllocFromModifiedUtf8(utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700131}
132
133String* InternTable::InternStrong(String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700134 if (s == NULL) {
135 return NULL;
136 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700137 return Insert(s, true);
138}
139
140String* InternTable::InternWeak(String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700141 if (s == NULL) {
142 return NULL;
143 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700144 return Insert(s, false);
145}
146
Brian Carlstromc74255f2011-09-11 22:47:39 -0700147bool InternTable::ContainsWeak(String* s) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700148 MutexLock mu(intern_table_lock_);
149 const String* found = Lookup(weak_interns_, s, s->GetHashCode());
150 return found == s;
151}
152
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700153void InternTable::SweepInternTableWeaks(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700154 MutexLock mu(intern_table_lock_);
Elliott Hughese5448b52012-01-18 16:44:06 -0800155 typedef Table::iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700156 for (It it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700157 Object* object = it->second;
158 if (!is_marked(object, arg)) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700159 weak_interns_.erase(it++);
160 } else {
161 ++it;
162 }
163 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700164}
165
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700166} // namespace art