blob: 41c1b2af58148d6d0b96977091f733643b949c05 [file] [log] [blame]
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "intern_table.h"
4
Elliott Hughes90a33692011-08-30 13:27:07 -07005#include "UniquePtr.h"
Elliott Hughes814e4032011-08-23 12:07:56 -07006#include "utf.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -07007
8namespace art {
9
Elliott Hughes8daa0922011-09-11 13:46:25 -070010InternTable::InternTable() : intern_table_lock_("InternTable lock") {
Elliott Hughesde69d7f2011-08-18 16:49:37 -070011}
12
Brian Carlstroma663ea52011-08-19 23:33:41 -070013size_t InternTable::Size() const {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070014 MutexLock mu(intern_table_lock_);
15 return strong_interns_.size() + weak_interns_.size();
Brian Carlstroma663ea52011-08-19 23:33:41 -070016}
17
Elliott Hughes410c0c82011-09-01 17:58:25 -070018void InternTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -070019 MutexLock mu(intern_table_lock_);
20 typedef Table::const_iterator It; // TODO: C++0x auto
Elliott Hughescf4c6c42011-09-01 15:16:42 -070021 for (It it = strong_interns_.begin(), end = strong_interns_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070022 visitor(it->second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070023 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070024 // Note: we deliberately don't visit the weak_interns_ table.
Brian Carlstrom7e93b502011-08-04 14:16:22 -070025}
26
Elliott Hughescf4c6c42011-09-01 15:16:42 -070027const String* InternTable::Lookup(Table& table, const String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070028 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070029 typedef Table::const_iterator It; // TODO: C++0x auto
30 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
31 const String* existing_string = it->second;
32 if (existing_string->Equals(s)) {
33 return existing_string;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070034 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035 }
36 return NULL;
37}
38
39const String* InternTable::Insert(Table& table, const String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070040 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070041 table.insert(std::make_pair(hash_code, s));
42 return s;
43}
44
45void InternTable::RegisterStrong(const String* s) {
46 MutexLock mu(intern_table_lock_);
47 Insert(strong_interns_, s, s->GetHashCode());
48}
49
50void InternTable::Remove(Table& table, const String* s, uint32_t hash_code) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070051 intern_table_lock_.AssertHeld();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052 typedef Table::const_iterator It; // TODO: C++0x auto
53 for (It it = table.find(hash_code), end = table.end(); it != end; ++it) {
54 if (it->second == s) {
55 table.erase(it);
56 return;
57 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -070058 }
59}
60
Elliott Hughescf4c6c42011-09-01 15:16:42 -070061const String* InternTable::Insert(const String* s, bool is_strong) {
62 MutexLock mu(intern_table_lock_);
63
64 DCHECK(s != NULL);
65 uint32_t hash_code = s->GetHashCode();
66
67 if (is_strong) {
68 // Check the strong table for a match.
69 const String* strong = Lookup(strong_interns_, s, hash_code);
70 if (strong != NULL) {
71 return strong;
72 }
73
74 // There is no match in the strong table, check the weak table.
75 const String* weak = Lookup(weak_interns_, s, hash_code);
76 if (weak != NULL) {
77 // A match was found in the weak table. Promote to the strong table.
78 Remove(weak_interns_, weak, hash_code);
79 return Insert(strong_interns_, weak, hash_code);
80 }
81
82 // No match in the strong table or the weak table. Insert into the strong table.
83 return Insert(strong_interns_, s, hash_code);
84 }
85
86 // Check the strong table for a match.
87 const String* strong = Lookup(strong_interns_, s, hash_code);
88 if (strong != NULL) {
89 return strong;
90 }
91 // Check the weak table for a match.
92 const String* weak = Lookup(weak_interns_, s, hash_code);
93 if (weak != NULL) {
94 return weak;
95 }
96 // Insert into the weak table.
97 return Insert(weak_interns_, s, hash_code);
98}
99
100const String* InternTable::InternStrong(int32_t utf16_length, const char* utf8_data) {
101 return Insert(String::AllocFromModifiedUtf8(utf16_length, utf8_data), true);
102}
103
104const String* InternTable::InternWeak(const String* s) {
105 return Insert(s, false);
106}
107
108bool InternTable::ContainsWeak(const String* s) {
109 MutexLock mu(intern_table_lock_);
110 const String* found = Lookup(weak_interns_, s, s->GetHashCode());
111 return found == s;
112}
113
Elliott Hughes410c0c82011-09-01 17:58:25 -0700114void InternTable::RemoveWeakIf(const Predicate& predicate) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700115 MutexLock mu(intern_table_lock_);
116 typedef Table::const_iterator It; // TODO: C++0x auto
117 for (It it = weak_interns_.begin(), end = weak_interns_.end(); it != end;) {
118 if (predicate(it->second)) {
119 weak_interns_.erase(it++);
120 } else {
121 ++it;
122 }
123 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124}
125
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700126} // namespace art