Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_INTERN_TABLE_H_ |
| 4 | #define ART_SRC_INTERN_TABLE_H_ |
| 5 | |
| 6 | #include "unordered_map.h" |
| 7 | |
| 8 | #include "heap.h" |
| 9 | #include "object.h" |
| 10 | |
| 11 | namespace art { |
| 12 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame^] | 13 | /** |
| 14 | * Used to intern strings. |
| 15 | * |
| 16 | * There are actually two tables: one that holds strong references to its strings, and one that |
| 17 | * holds weak references. The former is used for string literals, for which there is an effective |
| 18 | * reference from the constant pool. The latter is used for strings interned at runtime via |
| 19 | * String.intern. Some code (XML parsers being a prime example) relies on being able to intern |
| 20 | * arbitrarily many strings for the duration of a parse without permanently increasing the memory |
| 21 | * footprint. |
| 22 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 23 | class InternTable { |
| 24 | public: |
| 25 | InternTable(); |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 26 | ~InternTable(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame^] | 28 | // Interns a potentially new string in the 'strong' table. (See above.) |
| 29 | const String* InternStrong(int32_t utf16_length, const char* utf8_data); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame^] | 31 | // Interns a potentially new string in the 'weak' table. (See above.) |
| 32 | const String* InternWeak(const String* s); |
| 33 | |
| 34 | // Register a String trusting that it is safe to intern. |
| 35 | // Used when reinitializing InternTable from an image. |
| 36 | void RegisterStrong(const String* s); |
| 37 | |
| 38 | // Removes all weak interns for which 'predicate' is true. |
| 39 | void RemoveWeakIf(bool (*predicate)(const String*)); |
| 40 | |
| 41 | bool ContainsWeak(const String* s); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 42 | |
| 43 | size_t Size() const; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame^] | 44 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 45 | void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 46 | |
| 47 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame^] | 48 | typedef std::tr1::unordered_multimap<int32_t, const String*> Table; |
| 49 | |
| 50 | const String* Insert(const String* s, bool is_strong); |
| 51 | |
| 52 | const String* Lookup(Table& table, const String* s, uint32_t hash_code); |
| 53 | const String* Insert(Table& table, const String* s, uint32_t hash_code); |
| 54 | void Remove(Table& table, const String* s, uint32_t hash_code); |
| 55 | |
| 56 | Table strong_interns_; |
| 57 | Table weak_interns_; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 58 | Mutex* intern_table_lock_; |
| 59 | }; |
| 60 | |
| 61 | } // namespace art |
| 62 | |
| 63 | #endif // ART_SRC_CLASS_LINKER_H_ |