blob: 89c15f8db1b028c090b5b0374418470306e94ed3 [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()
31 : intern_table_lock_("InternTable lock"), is_dirty_(false) {}
Elliott Hughesde69d7f2011-08-18 16:49:37 -070032
Brian Carlstroma663ea52011-08-19 23:33:41 -070033size_t InternTable::Size() const {
Ian Rogers50b35e22012-10-04 10:09:15 -070034 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070036}
37
Elliott Hughescac6cc72011-11-03 20:31:21 -070038void InternTable::DumpForSigQuit(std::ostream& os) const {
Ian Rogers50b35e22012-10-04 10:09:15 -070039 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -070040 os << "Intern table: " << strong_interns_.size() << " strong; "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070041 << weak_interns_.size() << " weak\n";
Elliott Hughescac6cc72011-11-03 20:31:21 -070042}
43
Ian Rogers7dfb28c2013-08-22 08:18:36 -070044void InternTable::VisitRoots(RootVisitor* visitor, void* arg,
Mathieu Chartierc4621982013-09-16 19:43:47 -070045 bool only_dirty, bool clean_dirty) {
Ian Rogers50b35e22012-10-04 10:09:15 -070046 MutexLock mu(Thread::Current(), intern_table_lock_);
Mathieu Chartierc4621982013-09-16 19:43:47 -070047 if (!only_dirty || is_dirty_) {
48 for (const auto& strong_intern : strong_interns_) {
49 visitor(strong_intern.second, arg);
50 }
51 if (clean_dirty) {
52 is_dirty_ = false;
53 }
Ian Rogers1d54e732013-05-02 21:10:01 -070054 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070055 // Note: we deliberately don't visit the weak_interns_ table and the immutable
56 // image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070057}
58
Ian Rogers7dfb28c2013-08-22 08:18:36 -070059mirror::String* InternTable::Lookup(Table& table, mirror::String* s,
60 uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070061 intern_table_lock_.AssertHeld(Thread::Current());
Mathieu Chartier02e25112013-08-14 16:14:24 -070062 for (auto it = table.find(hash_code), end = table.end(); it != end; ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064 if (existing_string->Equals(s)) {
65 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070066 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 }
68 return NULL;
69}
70
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071mirror::String* InternTable::Insert(Table& table, mirror::String* s,
72 uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070073 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074 table.insert(std::make_pair(hash_code, s));
75 return s;
76}
77
Ian Rogers7dfb28c2013-08-22 08:18:36 -070078void InternTable::Remove(Table& table, const mirror::String* s,
79 uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070080 intern_table_lock_.AssertHeld(Thread::Current());
Mathieu Chartier02e25112013-08-14 16:14:24 -070081 for (auto it = table.find(hash_code), end = table.end(); it != end; ++it) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070082 if (it->second == s) {
83 table.erase(it);
84 return;
85 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -070086 }
87}
88
Ian Rogers7dfb28c2013-08-22 08:18:36 -070089static mirror::String* LookupStringFromImage(mirror::String* s)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
91 gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace();
92 if (image == NULL) {
93 return NULL; // No image present.
94 }
95 mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
96 mirror::ObjectArray<mirror::DexCache>* dex_caches = root->AsObjectArray<mirror::DexCache>();
97 const std::string utf8 = s->ToModifiedUtf8();
98 for (int32_t i = 0; i < dex_caches->GetLength(); ++i) {
99 mirror::DexCache* dex_cache = dex_caches->Get(i);
100 const DexFile* dex_file = dex_cache->GetDexFile();
101 // Binary search the dex file for the string index.
102 const DexFile::StringId* string_id = dex_file->FindStringId(utf8.c_str());
103 if (string_id != NULL) {
104 uint32_t string_idx = dex_file->GetIndexForStringId(*string_id);
105 mirror::String* image = dex_cache->GetResolvedString(string_idx);
106 if (image != NULL) {
107 return image;
108 }
109 }
110 }
111 return NULL;
112}
113
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700115 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700116
117 DCHECK(s != NULL);
118 uint32_t hash_code = s->GetHashCode();
119
120 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -0700121 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700123 if (strong != NULL) {
124 return strong;
125 }
126
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700127 // Mark as dirty so that we rescan the roots.
Mathieu Chartierc4621982013-09-16 19:43:47 -0700128 is_dirty_ = true;
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700129
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700130 // Check the image for a match.
131 mirror::String* image = LookupStringFromImage(s);
132 if (image != NULL) {
133 return Insert(strong_interns_, image, hash_code);
134 }
135
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700136 // There is no match in the strong table, check the weak table.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700138 if (weak != NULL) {
139 // A match was found in the weak table. Promote to the strong table.
140 Remove(weak_interns_, weak, hash_code);
141 return Insert(strong_interns_, weak, hash_code);
142 }
143
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700144 // No match in the strong table or the weak table. Insert into the strong
145 // table.
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700146 return Insert(strong_interns_, s, hash_code);
147 }
148
149 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700151 if (strong != NULL) {
152 return strong;
153 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700154 // Check the image for a match.
155 mirror::String* image = LookupStringFromImage(s);
jeffhaob1c6f342012-03-20 17:57:26 -0700156 if (image != NULL) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700157 return Insert(weak_interns_, image, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700158 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700159 // Check the weak table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700161 if (weak != NULL) {
162 return weak;
163 }
164 // Insert into the weak table.
165 return Insert(weak_interns_, s, hash_code);
166}
167
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700168mirror::String* InternTable::InternStrong(int32_t utf16_length,
169 const char* utf8_data) {
170 return InternStrong(mirror::String::AllocFromModifiedUtf8(
171 Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700172}
173
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174mirror::String* InternTable::InternStrong(const char* utf8_data) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700175 return InternStrong(
176 mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700177}
178
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179mirror::String* InternTable::InternStrong(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700180 if (s == NULL) {
181 return NULL;
182 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700183 return Insert(s, true);
184}
185
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700187 if (s == NULL) {
188 return NULL;
189 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700190 return Insert(s, false);
191}
192
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193bool InternTable::ContainsWeak(mirror::String* s) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700194 MutexLock mu(Thread::Current(), intern_table_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700196 return found == s;
197}
198
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199void InternTable::SweepInternTableWeaks(IsMarkedTester is_marked, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700200 MutexLock mu(Thread::Current(), intern_table_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700201 // TODO: std::remove_if + lambda.
202 for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 mirror::Object* object = it->second;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700204 if (!is_marked(object, arg)) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700205 weak_interns_.erase(it++);
206 } else {
207 ++it;
208 }
209 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700210}
211
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700212} // namespace art