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" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame^] | 9 | #include "mutex.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 10 | #include "object.h" |
| 11 | |
| 12 | namespace art { |
| 13 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 14 | /** |
| 15 | * Used to intern strings. |
| 16 | * |
| 17 | * There are actually two tables: one that holds strong references to its strings, and one that |
| 18 | * holds weak references. The former is used for string literals, for which there is an effective |
| 19 | * reference from the constant pool. The latter is used for strings interned at runtime via |
| 20 | * String.intern. Some code (XML parsers being a prime example) relies on being able to intern |
| 21 | * arbitrarily many strings for the duration of a parse without permanently increasing the memory |
| 22 | * footprint. |
| 23 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 24 | class InternTable { |
| 25 | public: |
| 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 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 38 | // Removes all weak interns for which the predicate functor 'p' returns true. |
| 39 | // (We use an explicit Predicate type rather than a template to keep implementation |
| 40 | // out of the header file.) |
| 41 | struct Predicate { |
| 42 | virtual ~Predicate() {} |
| 43 | virtual bool operator()(const String*) const = 0; |
| 44 | }; |
| 45 | void RemoveWeakIf(const Predicate& p); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 46 | |
| 47 | bool ContainsWeak(const String* s); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 48 | |
| 49 | size_t Size() const; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 51 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 54 | typedef std::tr1::unordered_multimap<int32_t, const String*> Table; |
| 55 | |
| 56 | const String* Insert(const String* s, bool is_strong); |
| 57 | |
| 58 | const String* Lookup(Table& table, const String* s, uint32_t hash_code); |
| 59 | const String* Insert(Table& table, const String* s, uint32_t hash_code); |
| 60 | void Remove(Table& table, const String* s, uint32_t hash_code); |
| 61 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame^] | 62 | mutable Mutex intern_table_lock_; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 63 | Table strong_interns_; |
| 64 | Table weak_interns_; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | } // namespace art |
| 68 | |
| 69 | #endif // ART_SRC_CLASS_LINKER_H_ |