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 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 22 | #include "base/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" |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 26 | #include "gc/weak_root_state.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 27 | #include "gc_root.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 28 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 29 | namespace art { |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 30 | |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 31 | class IsMarkedVisitor; |
| 32 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 33 | namespace gc { |
| 34 | namespace space { |
| 35 | class ImageSpace; |
| 36 | } // namespace space |
| 37 | } // namespace gc |
| 38 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 39 | enum VisitRootFlags : uint8_t; |
| 40 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 41 | namespace linker { |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 42 | class ImageWriter; |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 43 | } // namespace linker |
| 44 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | namespace mirror { |
| 46 | class String; |
| 47 | } // namespace mirror |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 48 | class Transaction; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Used to intern strings. |
| 52 | * |
| 53 | * There are actually two tables: one that holds strong references to its strings, and one that |
| 54 | * holds weak references. The former is used for string literals, for which there is an effective |
| 55 | * reference from the constant pool. The latter is used for strings interned at runtime via |
| 56 | * String.intern. Some code (XML parsers being a prime example) relies on being able to intern |
| 57 | * arbitrarily many strings for the duration of a parse without permanently increasing the memory |
| 58 | * footprint. |
| 59 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 60 | class InternTable { |
| 61 | public: |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 62 | // Modified UTF-8-encoded string treated as UTF16. |
| 63 | class Utf8String { |
| 64 | public: |
| 65 | Utf8String(uint32_t utf16_length, const char* utf8_data, int32_t hash) |
| 66 | : hash_(hash), utf16_length_(utf16_length), utf8_data_(utf8_data) { } |
| 67 | |
| 68 | int32_t GetHash() const { return hash_; } |
| 69 | uint32_t GetUtf16Length() const { return utf16_length_; } |
| 70 | const char* GetUtf8Data() const { return utf8_data_; } |
| 71 | |
| 72 | private: |
| 73 | int32_t hash_; |
| 74 | uint32_t utf16_length_; |
| 75 | const char* utf8_data_; |
| 76 | }; |
| 77 | |
| 78 | class StringHashEquals { |
| 79 | public: |
| 80 | std::size_t operator()(const GcRoot<mirror::String>& root) const NO_THREAD_SAFETY_ANALYSIS; |
| 81 | bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b) const |
| 82 | NO_THREAD_SAFETY_ANALYSIS; |
| 83 | |
| 84 | // Utf8String can be used for lookup. |
| 85 | std::size_t operator()(const Utf8String& key) const { |
| 86 | // A cast to prevent undesired sign extension. |
| 87 | return static_cast<uint32_t>(key.GetHash()); |
| 88 | } |
| 89 | |
| 90 | bool operator()(const GcRoot<mirror::String>& a, const Utf8String& b) const |
| 91 | NO_THREAD_SAFETY_ANALYSIS; |
| 92 | }; |
| 93 | |
| 94 | class GcRootEmptyFn { |
| 95 | public: |
| 96 | void MakeEmpty(GcRoot<mirror::String>& item) const { |
| 97 | item = GcRoot<mirror::String>(); |
| 98 | } |
| 99 | bool IsEmpty(const GcRoot<mirror::String>& item) const { |
| 100 | return item.IsNull(); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | using UnorderedSet = HashSet<GcRoot<mirror::String>, |
| 105 | GcRootEmptyFn, |
| 106 | StringHashEquals, |
| 107 | StringHashEquals, |
| 108 | TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>>; |
| 109 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 110 | InternTable(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 111 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 112 | // Interns a potentially new string in the 'strong' table. May cause thread suspension. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 113 | ObjPtr<mirror::String> InternStrong(int32_t utf16_length, const char* utf8_data) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 114 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 115 | |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 116 | // 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] | 117 | // 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] | 118 | // lock since there will not be thread suspension. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 119 | ObjPtr<mirror::String> InternStrongImageString(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 120 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 121 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 122 | // Only used by image writer. Promote all weak interns to strong interns. |
| 123 | void PromoteWeakToStrong() REQUIRES_SHARED(Locks::mutator_lock_); |
| 124 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 125 | // Interns a potentially new string in the 'strong' table. May cause thread suspension. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 126 | ObjPtr<mirror::String> InternStrong(const char* utf8_data) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 127 | REQUIRES(!Roles::uninterruptible_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 128 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 129 | // Interns a potentially new string in the 'strong' table. May cause thread suspension. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 130 | ObjPtr<mirror::String> InternStrong(ObjPtr<mirror::String> s) |
| 131 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 132 | REQUIRES(!Roles::uninterruptible_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 133 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 134 | // Interns a potentially new string in the 'weak' table. May cause thread suspension. |
Vladimir Marko | 31c3daa | 2019-06-13 12:18:37 +0100 | [diff] [blame] | 135 | ObjPtr<mirror::String> InternWeak(const char* utf8_data) REQUIRES_SHARED(Locks::mutator_lock_) |
| 136 | REQUIRES(!Roles::uninterruptible_); |
| 137 | |
| 138 | // Interns a potentially new string in the 'weak' table. May cause thread suspension. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 139 | ObjPtr<mirror::String> InternWeak(ObjPtr<mirror::String> s) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 140 | REQUIRES(!Roles::uninterruptible_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 141 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 142 | void SweepInternTableWeaks(IsMarkedVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 143 | REQUIRES(!Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 144 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 145 | bool ContainsWeak(ObjPtr<mirror::String> s) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 146 | REQUIRES(!Locks::intern_table_lock_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 147 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 148 | // Lookup a strong intern, returns null if not found. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 149 | ObjPtr<mirror::String> LookupStrong(Thread* self, ObjPtr<mirror::String> s) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 150 | REQUIRES(!Locks::intern_table_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 151 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 152 | ObjPtr<mirror::String> LookupStrong(Thread* self, uint32_t utf16_length, const char* utf8_data) |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 153 | REQUIRES(!Locks::intern_table_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 154 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 155 | ObjPtr<mirror::String> LookupStrongLocked(ObjPtr<mirror::String> s) |
| 156 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 157 | |
| 158 | // Lookup a weak intern, returns null if not found. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 159 | ObjPtr<mirror::String> LookupWeak(Thread* self, ObjPtr<mirror::String> s) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 160 | REQUIRES(!Locks::intern_table_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 161 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 162 | ObjPtr<mirror::String> LookupWeakLocked(ObjPtr<mirror::String> s) |
| 163 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 164 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 165 | // Total number of interned strings. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 166 | size_t Size() const REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 167 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 168 | // Total number of weakly live interned strings. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 169 | size_t StrongSize() const REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 170 | |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 171 | // Total number of strongly live interned strings. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 172 | size_t WeakSize() const REQUIRES(!Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 173 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 174 | void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 175 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 176 | |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 177 | // Visit all of the interns in the table. |
| 178 | template <typename Visitor> |
| 179 | void VisitInterns(const Visitor& visitor, |
| 180 | bool visit_boot_images, |
| 181 | bool visit_non_boot_images) |
| 182 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
| 183 | |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 184 | // Count the number of intern strings in the table. |
| 185 | size_t CountInterns(bool visit_boot_images, bool visit_non_boot_images) const |
| 186 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
| 187 | |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 188 | void DumpForSigQuit(std::ostream& os) const REQUIRES(!Locks::intern_table_lock_); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 189 | |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 190 | void BroadcastForNewInterns(); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 191 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 192 | // Add all of the strings in the image's intern table into this intern table. This is required so |
| 193 | // the intern table is correct. |
| 194 | // The visitor arg type is UnorderedSet |
| 195 | template <typename Visitor> |
| 196 | void AddImageStringsToTable(gc::space::ImageSpace* image_space, |
| 197 | const Visitor& visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 198 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 199 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 200 | // Add a new intern table for inserting to, previous intern tables are still there but no |
| 201 | // longer inserted into and ideally unmodified. This is done to prevent dirty pages. |
| 202 | void AddNewTable() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 203 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 204 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 205 | // Write the post zygote intern table to a pointer. Only writes the strong interns since it is |
| 206 | // expected that there is no weak interns since this is called from the image writer. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 207 | size_t WriteToMemory(uint8_t* ptr) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 208 | REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 209 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 210 | // Change the weak root state. May broadcast to waiters. |
| 211 | void ChangeWeakRootState(gc::WeakRootState new_state) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 212 | REQUIRES(!Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 213 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 214 | private: |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 215 | // Table which holds pre zygote and post zygote interned strings. There is one instance for |
| 216 | // weak interns and strong interns. |
| 217 | class Table { |
| 218 | public: |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 219 | class InternalTable { |
| 220 | public: |
| 221 | InternalTable() = default; |
| 222 | InternalTable(UnorderedSet&& set, bool is_boot_image) |
| 223 | : set_(std::move(set)), is_boot_image_(is_boot_image) {} |
| 224 | |
| 225 | bool Empty() const { |
| 226 | return set_.empty(); |
| 227 | } |
| 228 | |
| 229 | size_t Size() const { |
| 230 | return set_.size(); |
| 231 | } |
| 232 | |
| 233 | bool IsBootImage() const { |
| 234 | return is_boot_image_; |
| 235 | } |
| 236 | |
| 237 | private: |
| 238 | UnorderedSet set_; |
| 239 | bool is_boot_image_ = false; |
| 240 | |
| 241 | friend class InternTable; |
| 242 | friend class Table; |
| 243 | ART_FRIEND_TEST(InternTableTest, CrossHash); |
| 244 | }; |
| 245 | |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 246 | Table(); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 247 | ObjPtr<mirror::String> Find(ObjPtr<mirror::String> s) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 248 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 249 | ObjPtr<mirror::String> Find(const Utf8String& string) REQUIRES_SHARED(Locks::mutator_lock_) |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 250 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 251 | void Insert(ObjPtr<mirror::String> s) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 252 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 253 | void Remove(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 254 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 255 | void VisitRoots(RootVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 256 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 257 | void SweepWeaks(IsMarkedVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 258 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 259 | // Add a new intern table that will only be inserted into from now on. |
| 260 | void AddNewTable() REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 261 | size_t Size() const REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 262 | // Read and add an intern table from ptr. |
| 263 | // Tables read are inserted at the front of the table array. Only checks for conflicts in |
| 264 | // debug builds. Returns how many bytes were read. |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 265 | // NO_THREAD_SAFETY_ANALYSIS for the visitor that may require locks. |
| 266 | template <typename Visitor> |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 267 | size_t AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor, bool is_boot_image) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 268 | REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 205b762 | 2016-01-06 15:47:09 -0800 | [diff] [blame] | 269 | // Write the intern tables to ptr, if there are multiple tables they are combined into a single |
| 270 | // one. Returns how many bytes were written. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 271 | size_t WriteToMemory(uint8_t* ptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 272 | REQUIRES(Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 273 | |
| 274 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 275 | void SweepWeaks(UnorderedSet* set, IsMarkedVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 276 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 277 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 278 | // Add a table to the front of the tables vector. |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 279 | void AddInternStrings(UnorderedSet&& intern_strings, bool is_boot_image) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 280 | REQUIRES(Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
| 281 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 282 | // We call AddNewTable when we create the zygote to reduce private dirty pages caused by |
| 283 | // modifying the zygote intern table. The back of table is modified when strings are interned. |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 284 | std::vector<InternalTable> tables_; |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 285 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 286 | friend class InternTable; |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 287 | friend class linker::ImageWriter; |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 288 | ART_FRIEND_TEST(InternTableTest, CrossHash); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 289 | }; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 290 | |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 291 | // Insert if non null, otherwise return null. Must be called holding the mutator lock. |
| 292 | // If holding_locks is true, then we may also hold other locks. If holding_locks is true, then we |
| 293 | // require GC is not running since it is not safe to wait while holding locks. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 294 | ObjPtr<mirror::String> Insert(ObjPtr<mirror::String> s, bool is_strong, bool holding_locks) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 295 | REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 296 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 297 | // Add a table from memory to the strong interns. |
| 298 | template <typename Visitor> |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 299 | size_t AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor, bool is_boot_image) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 300 | REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
| 301 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 302 | ObjPtr<mirror::String> InsertStrong(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 303 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 304 | ObjPtr<mirror::String> InsertWeak(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 305 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 306 | void RemoveStrong(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 307 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 308 | void RemoveWeak(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 309 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 310 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 311 | // Transaction rollback access. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 312 | ObjPtr<mirror::String> InsertStrongFromTransaction(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 313 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 314 | ObjPtr<mirror::String> InsertWeakFromTransaction(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 315 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 316 | void RemoveStrongFromTransaction(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 317 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 318 | void RemoveWeakFromTransaction(ObjPtr<mirror::String> s) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 319 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 320 | |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 321 | // Change the weak root state. May broadcast to waiters. |
| 322 | void ChangeWeakRootStateLocked(gc::WeakRootState new_state) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 323 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 324 | |
| 325 | // Wait until we can read weak roots. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 326 | void WaitUntilAccessible(Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 327 | REQUIRES(Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 328 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 329 | bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 330 | ConditionVariable weak_intern_condition_ GUARDED_BY(Locks::intern_table_lock_); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 331 | // Since this contains (strong) roots, they need a read barrier to |
| 332 | // enable concurrent intern table (strong) root scan. Do not |
| 333 | // directly access the strings in it. Use functions that contain |
| 334 | // read barriers. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 335 | Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 336 | std::vector<GcRoot<mirror::String>> new_strong_intern_roots_ |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 337 | GUARDED_BY(Locks::intern_table_lock_); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 338 | // Since this contains (weak) roots, they need a read barrier. Do |
| 339 | // not directly access the strings in it. Use functions that contain |
| 340 | // read barriers. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 341 | Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 342 | // Weak root state, used for concurrent system weak processing and more. |
| 343 | gc::WeakRootState weak_root_state_ GUARDED_BY(Locks::intern_table_lock_); |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 344 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 345 | friend class gc::space::ImageSpace; |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 346 | friend class linker::ImageWriter; |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 347 | friend class Transaction; |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 348 | ART_FRIEND_TEST(InternTableTest, CrossHash); |
Mathieu Chartier | ed8990a | 2015-07-23 14:11:16 -0700 | [diff] [blame] | 349 | DISALLOW_COPY_AND_ASSIGN(InternTable); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | } // namespace art |
| 353 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 354 | #endif // ART_RUNTIME_INTERN_TABLE_H_ |