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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include <map> |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 21 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame^] | 22 | #include "base/allocator.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 23 | #include "base/mutex.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 24 | #include "gc_root.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 25 | #include "object_callbacks.h" |
| 26 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 27 | namespace art { |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 28 | |
| 29 | enum VisitRootFlags : uint8_t; |
| 30 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | namespace mirror { |
| 32 | class String; |
| 33 | } // namespace mirror |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 34 | class Transaction; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Used to intern strings. |
| 38 | * |
| 39 | * There are actually two tables: one that holds strong references to its strings, and one that |
| 40 | * holds weak references. The former is used for string literals, for which there is an effective |
| 41 | * reference from the constant pool. The latter is used for strings interned at runtime via |
| 42 | * String.intern. Some code (XML parsers being a prime example) relies on being able to intern |
| 43 | * arbitrarily many strings for the duration of a parse without permanently increasing the memory |
| 44 | * footprint. |
| 45 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 46 | class InternTable { |
| 47 | public: |
| 48 | InternTable(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 50 | // Interns a potentially new string in the 'strong' table. (See above.) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 52 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 53 | |
| 54 | // Interns a potentially new string in the 'strong' table. (See above.) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | mirror::String* InternStrong(const char* utf8_data) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 56 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 57 | |
| 58 | // Interns a potentially new string in the 'strong' table. (See above.) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 61 | // Interns a potentially new string in the 'weak' table. (See above.) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 63 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 64 | void SweepInternTableWeaks(IsMarkedCallback* callback, void* arg) |
| 65 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 66 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 67 | bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 68 | |
| 69 | size_t Size() const; |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 70 | size_t StrongSize() const; |
| 71 | size_t WeakSize() const; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 72 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 73 | void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) |
| 74 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 75 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 76 | void DumpForSigQuit(std::ostream& os) const; |
| 77 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 78 | void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 79 | void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 80 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 81 | private: |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame^] | 82 | typedef AllocationTrackingMultiMap<int32_t, GcRoot<mirror::String>, |
| 83 | kAllocatorTagInternTable> Table; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 84 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | mirror::String* Insert(mirror::String* s, bool is_strong) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 86 | LOCKS_EXCLUDED(Locks::intern_table_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 87 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 88 | |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 89 | mirror::String* LookupStrong(mirror::String* s, int32_t hash_code) |
| 90 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 91 | mirror::String* LookupWeak(mirror::String* s, int32_t hash_code) |
| 92 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 93 | mirror::String* Lookup(Table* table, mirror::String* s, int32_t hash_code) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 94 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 95 | mirror::String* InsertStrong(mirror::String* s, int32_t hash_code) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 96 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 97 | mirror::String* InsertWeak(mirror::String* s, int32_t hash_code) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 98 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 99 | void RemoveStrong(mirror::String* s, int32_t hash_code) |
| 100 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 101 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 102 | void RemoveWeak(mirror::String* s, int32_t hash_code) |
| 103 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 104 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 105 | void Remove(Table* table, mirror::String* s, int32_t hash_code) |
| 106 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 107 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 108 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 109 | // Transaction rollback access. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 110 | mirror::String* InsertStrongFromTransaction(mirror::String* s, int32_t hash_code) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 111 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 112 | mirror::String* InsertWeakFromTransaction(mirror::String* s, int32_t hash_code) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 113 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 114 | void RemoveStrongFromTransaction(mirror::String* s, int32_t hash_code) |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 115 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 116 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 117 | void RemoveWeakFromTransaction(mirror::String* s, int32_t hash_code) |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 118 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 119 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 120 | friend class Transaction; |
| 121 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 122 | bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 123 | bool allow_new_interns_ GUARDED_BY(Locks::intern_table_lock_); |
| 124 | ConditionVariable new_intern_condition_ GUARDED_BY(Locks::intern_table_lock_); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 125 | // Since this contains (strong) roots, they need a read barrier to |
| 126 | // enable concurrent intern table (strong) root scan. Do not |
| 127 | // directly access the strings in it. Use functions that contain |
| 128 | // read barriers. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 129 | Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 130 | std::vector<std::pair<int32_t, GcRoot<mirror::String>>> new_strong_intern_roots_ |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 131 | GUARDED_BY(Locks::intern_table_lock_); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 132 | // Since this contains (weak) roots, they need a read barrier. Do |
| 133 | // not directly access the strings in it. Use functions that contain |
| 134 | // read barriers. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 135 | Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | } // namespace art |
| 139 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 140 | #endif // ART_RUNTIME_INTERN_TABLE_H_ |