blob: 8f9e072093db6169e01c4ebf779cd19a662d1845 [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
Ian Rogers7dfb28c2013-08-22 08:18:36 -070019#include "gc/space/image_space.h"
20#include "mirror/dex_cache.h"
21#include "mirror/object_array-inl.h"
22#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/string.h"
24#include "thread.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070025#include "UniquePtr.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027
28namespace art {
29
Ian Rogers7dfb28c2013-08-22 08:18:36 -070030InternTable::InternTable()
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070031 : intern_table_lock_("InternTable lock"), is_dirty_(false), allow_new_interns_(true),
32 new_intern_condition_("New intern condition", intern_table_lock_) {
33}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070034
Brian Carlstroma663ea52011-08-19 23:33:41 -070035size_t InternTable::Size() const {
Ian Rogers50b35e22012-10-04 10:09:15 -070036 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070037 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070038}
39
Elliott Hughescac6cc72011-11-03 20:31:21 -070040void InternTable::DumpForSigQuit(std::ostream& os) const {
Ian Rogers50b35e22012-10-04 10:09:15 -070041 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -070042 os << "Intern table: " << strong_interns_.size() << " strong; "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070043 << weak_interns_.size() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070044}
45
Ian Rogers7dfb28c2013-08-22 08:18:36 -070046void InternTable::VisitRoots(RootVisitor* visitor, void* arg,
Mathieu Chartierc4621982013-09-16 19:43:47 -070047 bool only_dirty, bool clean_dirty) {
Ian Rogers50b35e22012-10-04 10:09:15 -070048 MutexLock mu(Thread::Current(), intern_table_lock_);
Mathieu Chartierc4621982013-09-16 19:43:47 -070049 if (!only_dirty || is_dirty_) {
Mathieu Chartierb3070522013-09-17 14:18:21 -070050 for (auto& strong_intern : strong_interns_) {
51 strong_intern.second = reinterpret_cast<mirror::String*>(visitor(strong_intern.second, arg));
52 DCHECK(strong_intern.second != nullptr);
Mathieu Chartierc4621982013-09-16 19:43:47 -070053 }
Mathieu Chartierb3070522013-09-17 14:18:21 -070054
Mathieu Chartierc4621982013-09-16 19:43:47 -070055 if (clean_dirty) {
56 is_dirty_ = false;
57 }
Ian Rogers1d54e732013-05-02 21:10:01 -070058 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -070059 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070060}
61
Ian Rogers7dfb28c2013-08-22 08:18:36 -070062mirror::String* InternTable::Lookup(Table& table, mirror::String* s,
63 uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070064 intern_table_lock_.AssertHeld(Thread::Current());
Mathieu Chartier02e25112013-08-14 16:14:24 -070065 for (auto it = table.find(hash_code), end = table.end(); it != end; ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 mirror::String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 if (existing_string->Equals(s)) {
68 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070069 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070070 }
71 return NULL;
72}
73
Ian Rogers7dfb28c2013-08-22 08:18:36 -070074mirror::String* InternTable::Insert(Table& table, mirror::String* s,
75 uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070076 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077 table.insert(std::make_pair(hash_code, s));
78 return s;
79}
80
Ian Rogers7dfb28c2013-08-22 08:18:36 -070081void InternTable::Remove(Table& table, const mirror::String* s,
82 uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070083 intern_table_lock_.AssertHeld(Thread::Current());
Mathieu Chartier02e25112013-08-14 16:14:24 -070084 for (auto it = table.find(hash_code), end = table.end(); it != end; ++it) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070085 if (it->second == s) {
86 table.erase(it);
87 return;
88 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -070089 }
90}
91
Ian Rogers7dfb28c2013-08-22 08:18:36 -070092static mirror::String* LookupStringFromImage(mirror::String* s)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
94 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
95 if (image == NULL) {
96 return NULL; // No image present.
97 }
98 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
99 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
100 const std::string utf8 = s->ToModifiedUtf8();
101 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
102 mirror::DexCache* dex_cache = dex_caches->Get(i);
103 const DexFile* dex_file = dex_cache->GetDexFile();
104 // Binary search the dex file for the string index.
105 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
106 if (string_id != NULL) {
107 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
108 mirror::String* image = dex_cache->GetResolvedString(string_idx);
109 if (image != NULL) {
110 return image;
111 }
112 }
113 }
114 return NULL;
115}
116
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700117void InternTable::AllowNewInterns() {
118 Thread* self = Thread::Current();
119 MutexLock mu(self, intern_table_lock_);
120 allow_new_interns_ = true;
121 new_intern_condition_.Broadcast(self);
122}
123
124void InternTable::DisallowNewInterns() {
125 Thread* self = Thread::Current();
126 MutexLock mu(self, intern_table_lock_);
127 allow_new_interns_ = false;
128}
129
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700131 Thread* self = Thread::Current();
132 MutexLock mu(self, intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700133
134 DCHECK(s != NULL);
135 uint32_t hash_code = s->GetHashCode();
136
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700137 while (UNLIKELY(!allow_new_interns_)) {
138 new_intern_condition_.WaitHoldingLocks(self);
139 }
140
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700141 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -0700142 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700144 if (strong != NULL) {
145 return strong;
146 }
147
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700148 // Mark as dirty so that we rescan the roots.
Mathieu Chartierc4621982013-09-16 19:43:47 -0700149 is_dirty_ = true;
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700150
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700151 // Check the image for a match.
152 mirror::String* image = LookupStringFromImage(s);
153 if (image != NULL) {
154 return Insert(strong_interns_, image, hash_code);
155 }
156
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700157 // There is no match in the strong table, check the weak table.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700159 if (weak != NULL) {
160 // A match was found in the weak table. Promote to the strong table.
161 Remove(weak_interns_, weak, hash_code);
162 return Insert(strong_interns_, weak, hash_code);
163 }
164
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700165 // No match in the strong table or the weak table. Insert into the strong
166 // table.
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700167 return Insert(strong_interns_, s, hash_code);
168 }
169
170 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700172 if (strong != NULL) {
173 return strong;
174 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700175 // Check the image for a match.
176 mirror::String* image = LookupStringFromImage(s);
jeffhaob1c6f342012-03-20 17:57:26 -0700177 if (image != NULL) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700178 return Insert(weak_interns_, image, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700179 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700180 // Check the weak table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700182 if (weak != NULL) {
183 return weak;
184 }
185 // Insert into the weak table.
186 return Insert(weak_interns_, s, hash_code);
187}
188
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700189mirror::String* InternTable::InternStrong(int32_t utf16_length,
190 const char* utf8_data) {
191 return InternStrong(mirror::String::AllocFromModifiedUtf8(
192 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700193}
194
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195mirror::String* InternTable::InternStrong(const char* utf8_data) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700196 return InternStrong(
197 mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700198}
199
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200mirror::String* InternTable::InternStrong(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700201 if (s == NULL) {
202 return NULL;
203 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700204 return Insert(s, true);
205}
206
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700208 if (s == NULL) {
209 return NULL;
210 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700211 return Insert(s, false);
212}
213
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214bool InternTable::ContainsWeak(mirror::String* s) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700215 MutexLock mu(Thread::Current(), intern_table_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700217 return found == s;
218}
219
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700220void InternTable::SweepInternTableWeaks(RootVisitor visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700221 MutexLock mu(Thread::Current(), intern_table_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700222 for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 mirror::Object* object = it->second;
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700224 mirror::Object* new_object = visitor(object, arg);
225 if (new_object == nullptr) {
226 // TODO: use it = weak_interns_.erase(it) when we get a c++11 stl.
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700227 weak_interns_.erase(it++);
228 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700229 it->second = down_cast<mirror::String*>(new_object);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700230 ++it;
231 }
232 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700233}
234
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700235} // namespace art