blob: d1ad2dbadc49a2e39e9ae41c1fe5488c3649a5b6 [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 Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/string.h"
20#include "thread.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070021#include "UniquePtr.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070022#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070023
24namespace art {
25
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070026InternTable::InternTable() : intern_table_lock_("InternTable lock"), is_dirty_(false) {
Elliott Hughesde69d7f2011-08-18 16:49:37 -070027}
28
Brian Carlstroma663ea52011-08-19 23:33:41 -070029size_t InternTable::Size() const {
Ian Rogers50b35e22012-10-04 10:09:15 -070030 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070031 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070032}
33
Elliott Hughescac6cc72011-11-03 20:31:21 -070034void InternTable::DumpForSigQuit(std::ostream& os) const {
Ian Rogers50b35e22012-10-04 10:09:15 -070035 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -070036 os << "Intern table: " << strong_interns_.size() << " strong; "
37 << weak_interns_.size() << " weak; "
38 << image_strong_interns_.size() << " image strong\n";
39}
40
Ian Rogers1d54e732013-05-02 21:10:01 -070041void InternTable::VisitRoots(RootVisitor* visitor, void* arg, bool clean_dirty) {
Ian Rogers50b35e22012-10-04 10:09:15 -070042 MutexLock mu(Thread::Current(), intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070043 typedef Table::const_iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -070044 for (It it = strong_interns_.begin(), end = strong_interns_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070045 visitor(it->second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070046 }
Ian Rogers1d54e732013-05-02 21:10:01 -070047 if (clean_dirty) {
48 is_dirty_ = false;
49 }
Ian Rogers5d76c432011-10-31 21:42:49 -070050 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070051}
52
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053mirror::String* InternTable::Lookup(Table& table, mirror::String* s, uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070054 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070055 typedef Table::const_iterator It; // TODO: C++0x auto
56 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 mirror::String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070058 if (existing_string->Equals(s)) {
59 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070060 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070061 }
62 return NULL;
63}
64
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065mirror::String* InternTable::Insert(Table& table, mirror::String* s, uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070066 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 table.insert(std::make_pair(hash_code, s));
68 return s;
69}
70
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071void InternTable::RegisterStrong(mirror::String* s) {
Ian Rogers50b35e22012-10-04 10:09:15 -070072 MutexLock mu(Thread::Current(), intern_table_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -070073 Insert(image_strong_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076void InternTable::Remove(Table& table, const mirror::String* s, uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070077 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughese5448b52012-01-18 16:44:06 -080078 typedef Table::iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
80 if (it->second == s) {
81 table.erase(it);
82 return;
83 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -070084 }
85}
86
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Ian Rogers50b35e22012-10-04 10:09:15 -070088 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070089
90 DCHECK(s != NULL);
91 uint32_t hash_code = s->GetHashCode();
92
93 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -070094 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070096 if (strong != NULL) {
97 return strong;
98 }
jeffhaob1c6f342012-03-20 17:57:26 -070099 // Check the image table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 mirror::String* image = Lookup(image_strong_interns_, s, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700101 if (image != NULL) {
102 return image;
Ian Rogers5d76c432011-10-31 21:42:49 -0700103 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700104
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700105 // Mark as dirty so that we rescan the roots.
106 Dirty();
107
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700108 // There is no match in the strong table, check the weak table.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700110 if (weak != NULL) {
111 // A match was found in the weak table. Promote to the strong table.
112 Remove(weak_interns_, weak, hash_code);
113 return Insert(strong_interns_, weak, hash_code);
114 }
115
116 // No match in the strong table or the weak table. Insert into the strong table.
117 return Insert(strong_interns_, s, hash_code);
118 }
119
120 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700122 if (strong != NULL) {
123 return strong;
124 }
jeffhaob1c6f342012-03-20 17:57:26 -0700125 // Check the image table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 mirror::String* image = Lookup(image_strong_interns_, s, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700127 if (image != NULL) {
128 return image;
129 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700130 // Check the weak table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700132 if (weak != NULL) {
133 return weak;
134 }
135 // Insert into the weak table.
136 return Insert(weak_interns_, s, hash_code);
137}
138
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
140 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700141}
142
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143mirror::String* InternTable::InternStrong(const char* utf8_data) {
144 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700145}
146
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147mirror::String* InternTable::InternStrong(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700148 if (s == NULL) {
149 return NULL;
150 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700151 return Insert(s, true);
152}
153
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700155 if (s == NULL) {
156 return NULL;
157 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700158 return Insert(s, false);
159}
160
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161bool InternTable::ContainsWeak(mirror::String* s) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700162 MutexLock mu(Thread::Current(), intern_table_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700164 return found == s;
165}
166
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167void InternTable::SweepInternTableWeaks(IsMarkedTester is_marked, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700168 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughese5448b52012-01-18 16:44:06 -0800169 typedef Table::iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700170 for (It it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 mirror::Object* object = it->second;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700172 if (!is_marked(object, arg)) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700173 weak_interns_.erase(it++);
174 } else {
175 ++it;
176 }
177 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700178}
179
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700180} // namespace art