blob: fa3c0753a95695a081a42d27236b3ababdf2092b [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 Rogers2dd0e2c2013-01-24 12:42:14 -080041void InternTable::VisitRoots(RootVisitor* visitor, void* arg) {
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 }
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070047 is_dirty_ = false;
Ian Rogers5d76c432011-10-31 21:42:49 -070048 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070049}
50
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051mirror::String* InternTable::Lookup(Table& table, mirror::String* s, uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070052 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070053 typedef Table::const_iterator It; // TODO: C++0x auto
54 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 mirror::String* existing_string = it->second;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056 if (existing_string->Equals(s)) {
57 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070058 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070059 }
60 return NULL;
61}
62
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063mirror::String* InternTable::Insert(Table& table, mirror::String* s, uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070064 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065 table.insert(std::make_pair(hash_code, s));
66 return s;
67}
68
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069void InternTable::RegisterStrong(mirror::String* s) {
Ian Rogers50b35e22012-10-04 10:09:15 -070070 MutexLock mu(Thread::Current(), intern_table_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -070071 Insert(image_strong_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070072}
73
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074void InternTable::Remove(Table& table, const mirror::String* s, uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070075 intern_table_lock_.AssertHeld(Thread::Current());
Elliott Hughese5448b52012-01-18 16:44:06 -080076 typedef Table::iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
78 if (it->second == s) {
79 table.erase(it);
80 return;
81 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -070082 }
83}
84
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Ian Rogers50b35e22012-10-04 10:09:15 -070086 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070087
88 DCHECK(s != NULL);
89 uint32_t hash_code = s->GetHashCode();
90
91 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -070092 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070094 if (strong != NULL) {
95 return strong;
96 }
jeffhaob1c6f342012-03-20 17:57:26 -070097 // Check the image table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 mirror::String* image = Lookup(image_strong_interns_, s, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -070099 if (image != NULL) {
100 return image;
Ian Rogers5d76c432011-10-31 21:42:49 -0700101 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700102
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700103 // Mark as dirty so that we rescan the roots.
104 Dirty();
105
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700106 // There is no match in the strong table, check the weak table.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700108 if (weak != NULL) {
109 // A match was found in the weak table. Promote to the strong table.
110 Remove(weak_interns_, weak, hash_code);
111 return Insert(strong_interns_, weak, hash_code);
112 }
113
114 // No match in the strong table or the weak table. Insert into the strong table.
115 return Insert(strong_interns_, s, hash_code);
116 }
117
118 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700120 if (strong != NULL) {
121 return strong;
122 }
jeffhaob1c6f342012-03-20 17:57:26 -0700123 // Check the image table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 mirror::String* image = Lookup(image_strong_interns_, s, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700125 if (image != NULL) {
126 return image;
127 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700128 // Check the weak table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700130 if (weak != NULL) {
131 return weak;
132 }
133 // Insert into the weak table.
134 return Insert(weak_interns_, s, hash_code);
135}
136
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
138 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700139}
140
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141mirror::String* InternTable::InternStrong(const char* utf8_data) {
142 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700143}
144
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145mirror::String* InternTable::InternStrong(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700146 if (s == NULL) {
147 return NULL;
148 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700149 return Insert(s, true);
150}
151
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700153 if (s == NULL) {
154 return NULL;
155 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700156 return Insert(s, false);
157}
158
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159bool InternTable::ContainsWeak(mirror::String* s) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700160 MutexLock mu(Thread::Current(), intern_table_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700162 return found == s;
163}
164
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165void InternTable::SweepInternTableWeaks(IsMarkedTester is_marked, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700166 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughese5448b52012-01-18 16:44:06 -0800167 typedef Table::iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700168 for (It it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 mirror::Object* object = it->second;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700170 if (!is_marked(object, arg)) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700171 weak_interns_.erase(it++);
172 } else {
173 ++it;
174 }
175 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700176}
177
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700178} // namespace art