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 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 20 | #include "base/mutex.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 21 | #include "locks.h" |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 22 | #include "object_callbacks.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | |
| 24 | #include <map> |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | namespace mirror { |
| 28 | class String; |
| 29 | } // namespace mirror |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 30 | class Transaction; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Used to intern strings. |
| 34 | * |
| 35 | * There are actually two tables: one that holds strong references to its strings, and one that |
| 36 | * holds weak references. The former is used for string literals, for which there is an effective |
| 37 | * reference from the constant pool. The latter is used for strings interned at runtime via |
| 38 | * String.intern. Some code (XML parsers being a prime example) relies on being able to intern |
| 39 | * arbitrarily many strings for the duration of a parse without permanently increasing the memory |
| 40 | * footprint. |
| 41 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 42 | class InternTable { |
| 43 | public: |
| 44 | InternTable(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 46 | // Interns a potentially new string in the 'strong' table. (See above.) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 48 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 49 | |
| 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(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(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 57 | // Interns a potentially new string in the 'weak' table. (See above.) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 59 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 60 | void SweepInternTableWeaks(IsMarkedCallback* callback, void* arg); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 61 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 63 | |
| 64 | size_t Size() const; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 65 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 66 | void VisitRoots(RootCallback* callback, void* arg, bool only_dirty, bool clean_dirty); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 68 | void DumpForSigQuit(std::ostream& os) const; |
| 69 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 70 | void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 71 | void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 72 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 73 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | typedef std::multimap<int32_t, mirror::String*> Table; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 75 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | mirror::String* Insert(mirror::String* s, bool is_strong) |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 77 | LOCKS_EXCLUDED(Locks::intern_table_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 78 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 79 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | mirror::String* Lookup(Table& table, mirror::String* s, uint32_t hash_code) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 81 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 82 | mirror::String* InsertStrong(mirror::String* s, uint32_t hash_code) |
| 83 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 84 | mirror::String* InsertWeak(mirror::String* s, uint32_t hash_code) |
| 85 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 86 | mirror::String* Insert(Table& table, mirror::String* s, uint32_t hash_code) |
| 87 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 88 | void RemoveWeak(mirror::String* s, uint32_t hash_code) |
| 89 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 90 | void Remove(Table& table, mirror::String* s, uint32_t hash_code) |
| 91 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 92 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 93 | // Transaction rollback access. |
| 94 | mirror::String* InsertStrongFromTransaction(mirror::String* s, uint32_t hash_code) |
| 95 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 96 | mirror::String* InsertWeakFromTransaction(mirror::String* s, uint32_t hash_code) |
| 97 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 98 | void RemoveStrongFromTransaction(mirror::String* s, uint32_t hash_code) |
| 99 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 100 | void RemoveWeakFromTransaction(mirror::String* s, uint32_t hash_code) |
| 101 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
| 102 | friend class Transaction; |
| 103 | |
| 104 | bool is_dirty_ GUARDED_BY(Locks::intern_table_lock_); |
| 105 | bool allow_new_interns_ GUARDED_BY(Locks::intern_table_lock_); |
| 106 | ConditionVariable new_intern_condition_ GUARDED_BY(Locks::intern_table_lock_); |
| 107 | Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_); |
| 108 | Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace art |
| 112 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 113 | #endif // ART_RUNTIME_INTERN_TABLE_H_ |