blob: d7398caa57510b1e251121c0862471aad358ce54 [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_);
Mathieu Chartier02e25112013-08-14 16:14:24 -070043 for (const auto& strong_intern : strong_interns_) {
44 visitor(strong_intern.second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070045 }
Ian Rogers1d54e732013-05-02 21:10:01 -070046 if (clean_dirty) {
47 is_dirty_ = false;
48 }
Ian Rogers5d76c432011-10-31 21:42:49 -070049 // Note: we deliberately don't visit the weak_interns_ table and the immutable image roots.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070050}
51
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052mirror::String* InternTable::Lookup(Table& table, mirror::String* s, uint32_t hash_code) {
Ian Rogers50b35e22012-10-04 10:09:15 -070053 intern_table_lock_.AssertHeld(Thread::Current());
Mathieu Chartier02e25112013-08-14 16:14:24 -070054 for (auto 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());
Mathieu Chartier02e25112013-08-14 16:14:24 -070076 for (auto it = table.find(hash_code), end = table.end(); it != end; ++it) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077 if (it->second == s) {
78 table.erase(it);
79 return;
80 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -070081 }
82}
83
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084mirror::String* InternTable::Insert(mirror::String* s, bool is_strong) {
Ian Rogers50b35e22012-10-04 10:09:15 -070085 MutexLock mu(Thread::Current(), intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070086
87 DCHECK(s != NULL);
88 uint32_t hash_code = s->GetHashCode();
89
90 if (is_strong) {
jeffhaob1c6f342012-03-20 17:57:26 -070091 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070093 if (strong != NULL) {
94 return strong;
95 }
jeffhaob1c6f342012-03-20 17:57:26 -070096 // Check the image table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 mirror::String* image = Lookup(image_strong_interns_, s, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -070098 if (image != NULL) {
99 return image;
Ian Rogers5d76c432011-10-31 21:42:49 -0700100 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700101
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700102 // Mark as dirty so that we rescan the roots.
103 Dirty();
104
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700105 // There is no match in the strong table, check the weak table.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700107 if (weak != NULL) {
108 // A match was found in the weak table. Promote to the strong table.
109 Remove(weak_interns_, weak, hash_code);
110 return Insert(strong_interns_, weak, hash_code);
111 }
112
113 // No match in the strong table or the weak table. Insert into the strong table.
114 return Insert(strong_interns_, s, hash_code);
115 }
116
117 // Check the strong table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 mirror::String* strong = Lookup(strong_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700119 if (strong != NULL) {
120 return strong;
121 }
jeffhaob1c6f342012-03-20 17:57:26 -0700122 // Check the image table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 mirror::String* image = Lookup(image_strong_interns_, s, hash_code);
jeffhaob1c6f342012-03-20 17:57:26 -0700124 if (image != NULL) {
125 return image;
126 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700127 // Check the weak table for a match.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 mirror::String* weak = Lookup(weak_interns_, s, hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700129 if (weak != NULL) {
130 return weak;
131 }
132 // Insert into the weak table.
133 return Insert(weak_interns_, s, hash_code);
134}
135
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136mirror::String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
137 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf16_length, utf8_data));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700138}
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140mirror::String* InternTable::InternStrong(const char* utf8_data) {
141 return InternStrong(mirror::String::AllocFromModifiedUtf8(Thread::Current(), utf8_data));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700142}
143
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144mirror::String* InternTable::InternStrong(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700145 if (s == NULL) {
146 return NULL;
147 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700148 return Insert(s, true);
149}
150
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151mirror::String* InternTable::InternWeak(mirror::String* s) {
Elliott Hughes37d4e6b2011-10-13 12:05:20 -0700152 if (s == NULL) {
153 return NULL;
154 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700155 return Insert(s, false);
156}
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158bool InternTable::ContainsWeak(mirror::String* s) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700159 MutexLock mu(Thread::Current(), intern_table_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 const mirror::String* found = Lookup(weak_interns_, s, s->GetHashCode());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700161 return found == s;
162}
163
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164void InternTable::SweepInternTableWeaks(IsMarkedTester is_marked, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700165 MutexLock mu(Thread::Current(), intern_table_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700166 // TODO: std::remove_if + lambda.
167 for (auto it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 mirror::Object* object = it->second;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700169 if (!is_marked(object, arg)) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700170 weak_interns_.erase(it++);
171 } else {
172 ++it;
173 }
174 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700175}
176
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700177} // namespace art