Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_INTERN_TABLE_H_ |
| 18 | #define ART_RUNTIME_INTERN_TABLE_H_ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 19 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 20 | #include <unordered_set> |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 21 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 22 | #include "atomic.h" |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 23 | #include "base/allocator.h" |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 24 | #include "base/hash_set.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 25 | #include "base/mutex.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 26 | #include "gc_root.h" |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 27 | #include "gc/weak_root_state.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 28 | #include "object_callbacks.h" |
| 29 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 30 | namespace art { |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 31 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 32 | namespace gc { |
| 33 | namespace space { |
| 34 | class ImageSpace; |
| 35 | } // namespace space |
| 36 | } // namespace gc |
| 37 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 38 | enum VisitRootFlags : uint8_t; |
| 39 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | namespace mirror { |
| 41 | class String; |
| 42 | } // namespace mirror |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 43 | class Transaction; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Used to intern strings. |
| 47 | * |
| 48 | * There are actually two tables: one that holds strong references to its strings, and one that |
| 49 | * holds weak references. The former is used for string literals, for which there is an effective |
| 50 | * reference from the constant pool. The latter is used for strings interned at runtime via |
| 51 | * String.intern. Some code (XML parsers being a prime example) relies on being able to intern |
| 52 | * arbitrarily many strings for the duration of a parse without permanently increasing the memory |
| 53 | * footprint. |
| 54 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 55 | class InternTable { |
| 56 | public: |
| 57 | InternTable(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 58 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 59 | // Interns a potentially new string in the 'strong' table. May cause thread suspension. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 61 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 62 | |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 63 | // Only used by image writer. Special version that may not cause thread suspension since the GC |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 64 | // cannot be running while we are doing image writing. Maybe be called while while holding a |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 65 | // lock since there will not be thread suspension. |
| 66 | mirror::String* InternStrongImageString(mirror::String* s) |
| 67 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 68 | |
| 69 | // Interns a potentially new string in the 'strong' table. May cause thread suspension. |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 70 | mirror::String* InternStrong(const char* utf8_data) SHARED_REQUIRES(Locks::mutator_lock_) |
| 71 | REQUIRES(!Roles::uninterruptible_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 72 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 73 | // Interns a potentially new string in the 'strong' table. May cause thread suspension. |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 74 | mirror::String* InternStrong(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_) |
| 75 | REQUIRES(!Roles::uninterruptible_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 76 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 77 | // Interns a potentially new string in the 'weak' table. May cause thread suspension. |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 78 | mirror::String* InternWeak(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_) |
| 79 | REQUIRES(!Roles::uninterruptible_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 80 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 81 | void SweepInternTableWeaks(IsMarkedVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_) |
| 82 | REQUIRES(!Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 83 | |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 84 | bool ContainsWeak(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_) |
| 85 | REQUIRES(!Locks::intern_table_lock_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 86 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 87 | // Lookup a strong intern, returns null if not found. |
| 88 | mirror::String* LookupStrong(Thread* self, mirror::String* s) |
| 89 | REQUIRES(!Locks::intern_table_lock_) |
| 90 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 91 | mirror::String* LookupStrong(Thread* self, uint32_t utf16_length, const char* utf8_data) |
| 92 | REQUIRES(!Locks::intern_table_lock_) |
| 93 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 94 | |
| 95 | // Lookup a weak intern, returns null if not found. |
| 96 | mirror::String* LookupWeak(Thread* self, mirror::String* s) |
| 97 | REQUIRES(!Locks::intern_table_lock_) |
| 98 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 99 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 100 | // Total number of interned strings. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 101 | size_t Size() const REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 102 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 103 | // Total number of weakly live interned strings. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | size_t StrongSize() const REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 105 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 106 | // Total number of strongly live interned strings. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 107 | size_t WeakSize() const REQUIRES(!Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 108 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 109 | void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 110 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 111 | |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 112 | void DumpForSigQuit(std::ostream& os) const REQUIRES(!Locks::intern_table_lock_); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 113 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 114 | void BroadcastForNewInterns() SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 115 | |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 116 | // Adds all of the resolved image strings from the image spaces into the intern table. The |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 117 | // advantage of doing this is preventing expensive DexFile::FindStringId calls. Sets |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 118 | // images_added_to_intern_table_ to true. |
| 119 | void AddImagesStringsToTable(const std::vector<gc::space::ImageSpace*>& image_spaces) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 120 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 121 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 122 | // Add a new intern table for inserting to, previous intern tables are still there but no |
| 123 | // longer inserted into and ideally unmodified. This is done to prevent dirty pages. |
| 124 | void AddNewTable() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 125 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 126 | |
| 127 | // Read the intern table from memory. The elements aren't copied, the intern hash set data will |
| 128 | // point to somewhere within ptr. Only reads the strong interns. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 129 | size_t AddTableFromMemory(const uint8_t* ptr) REQUIRES(!Locks::intern_table_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 130 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 131 | |
| 132 | // Write the post zygote intern table to a pointer. Only writes the strong interns since it is |
| 133 | // expected that there is no weak interns since this is called from the image writer. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 134 | size_t WriteToMemory(uint8_t* ptr) SHARED_REQUIRES(Locks::mutator_lock_) |
| 135 | REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 136 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 137 | // Change the weak root state. May broadcast to waiters. |
| 138 | void ChangeWeakRootState(gc::WeakRootState new_state) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 139 | REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 140 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 141 | private: |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 142 | // Modified UTF-8-encoded string treated as UTF16. |
| 143 | class Utf8String { |
| 144 | public: |
| 145 | Utf8String(uint32_t utf16_length, const char* utf8_data, int32_t hash) |
| 146 | : hash_(hash), utf16_length_(utf16_length), utf8_data_(utf8_data) { } |
| 147 | |
| 148 | int32_t GetHash() const { return hash_; } |
| 149 | uint32_t GetUtf16Length() const { return utf16_length_; } |
| 150 | const char* GetUtf8Data() const { return utf8_data_; } |
| 151 | |
| 152 | private: |
| 153 | int32_t hash_; |
| 154 | uint32_t utf16_length_; |
| 155 | const char* utf8_data_; |
| 156 | }; |
| 157 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 158 | class StringHashEquals { |
| 159 | public: |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 160 | std::size_t operator()(const GcRoot<mirror::String>& root) const NO_THREAD_SAFETY_ANALYSIS; |
| 161 | bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b) const |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 162 | NO_THREAD_SAFETY_ANALYSIS; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 163 | |
| 164 | // Utf8String can be used for lookup. |
| 165 | std::size_t operator()(const Utf8String& key) const { return key.GetHash(); } |
| 166 | bool operator()(const GcRoot<mirror::String>& a, const Utf8String& b) const |
| 167 | NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 168 | }; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 169 | class GcRootEmptyFn { |
| 170 | public: |
| 171 | void MakeEmpty(GcRoot<mirror::String>& item) const { |
| 172 | item = GcRoot<mirror::String>(); |
| 173 | } |
| 174 | bool IsEmpty(const GcRoot<mirror::String>& item) const { |
| 175 | return item.IsNull(); |
| 176 | } |
| 177 | }; |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 178 | |
| 179 | // Table which holds pre zygote and post zygote interned strings. There is one instance for |
| 180 | // weak interns and strong interns. |
| 181 | class Table { |
| 182 | public: |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 183 | Table(); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 184 | mirror::String* Find(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_) |
| 185 | REQUIRES(Locks::intern_table_lock_); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 186 | mirror::String* Find(const Utf8String& string) SHARED_REQUIRES(Locks::mutator_lock_) |
| 187 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 188 | void Insert(mirror::String* s) SHARED_REQUIRES(Locks::mutator_lock_) |
| 189 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 190 | void Remove(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 191 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 192 | void VisitRoots(RootVisitor* visitor) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 193 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 194 | void SweepWeaks(IsMarkedVisitor* visitor) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 195 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 196 | // Add a new intern table that will only be inserted into from now on. |
| 197 | void AddNewTable() REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 198 | size_t Size() const REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 199 | // Read and add an intern table from ptr. |
| 200 | // Tables read are inserted at the front of the table array. Only checks for conflicts in |
| 201 | // debug builds. Returns how many bytes were read. |
| 202 | size_t AddTableFromMemory(const uint8_t* ptr) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 203 | REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 204 | // Write the intern tables to ptr, if there are multiple tables they are combined into a single |
| 205 | // one. Returns how many bytes were written. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 206 | size_t WriteToMemory(uint8_t* ptr) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 207 | REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 208 | |
| 209 | private: |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 210 | typedef HashSet<GcRoot<mirror::String>, GcRootEmptyFn, StringHashEquals, StringHashEquals, |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 211 | TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>> UnorderedSet; |
| 212 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 213 | void SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 214 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 215 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 216 | // We call AddNewTable when we create the zygote to reduce private dirty pages caused by |
| 217 | // modifying the zygote intern table. The back of table is modified when strings are interned. |
| 218 | std::vector<UnorderedSet> tables_; |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 219 | }; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 220 | |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 221 | // Insert if non null, otherwise return null. Must be called holding the mutator lock. |
| 222 | // If holding_locks is true, then we may also hold other locks. If holding_locks is true, then we |
| 223 | // require GC is not running since it is not safe to wait while holding locks. |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 224 | mirror::String* Insert(mirror::String* s, bool is_strong, bool holding_locks) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 225 | REQUIRES(!Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 226 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 227 | mirror::String* LookupStrongLocked(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 228 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 229 | mirror::String* LookupWeakLocked(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 230 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 231 | mirror::String* InsertStrong(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 232 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 233 | mirror::String* InsertWeak(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 234 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 235 | void RemoveStrong(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 236 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 237 | void RemoveWeak(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 238 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 239 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 240 | // Transaction rollback access. |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 241 | mirror::String* LookupStringFromImage(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 242 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 243 | mirror::String* InsertStrongFromTransaction(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 244 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 245 | mirror::String* InsertWeakFromTransaction(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 246 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 247 | void RemoveStrongFromTransaction(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 248 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 249 | void RemoveWeakFromTransaction(mirror::String* s) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 250 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 251 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 252 | size_t AddTableFromMemoryLocked(const uint8_t* ptr) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 253 | REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 254 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 255 | // Change the weak root state. May broadcast to waiters. |
| 256 | void ChangeWeakRootStateLocked(gc::WeakRootState new_state) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 257 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 258 | |
| 259 | // Wait until we can read weak roots. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 260 | void WaitUntilAccessible(Thread* self) |
| 261 | REQUIRES(Locks::intern_table_lock_) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 262 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 263 | bool images_added_to_intern_table_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 264 | bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 265 | ConditionVariable weak_intern_condition_ GUARDED_BY(Locks::intern_table_lock_); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 266 | // Since this contains (strong) roots, they need a read barrier to |
| 267 | // enable concurrent intern table (strong) root scan. Do not |
| 268 | // directly access the strings in it. Use functions that contain |
| 269 | // read barriers. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 270 | Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 271 | std::vector<GcRoot<mirror::String>> new_strong_intern_roots_ |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 272 | GUARDED_BY(Locks::intern_table_lock_); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 273 | // Since this contains (weak) roots, they need a read barrier. Do |
| 274 | // not directly access the strings in it. Use functions that contain |
| 275 | // read barriers. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 276 | Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 277 | // Weak root state, used for concurrent system weak processing and more. |
| 278 | gc::WeakRootState weak_root_state_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 279 | |
| 280 | friend class Transaction; |
| 281 | DISALLOW_COPY_AND_ASSIGN(InternTable); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | } // namespace art |
| 285 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 286 | #endif // ART_RUNTIME_INTERN_TABLE_H_ |