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 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame^] | 6 | #include <iosfwd> |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 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" |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame^] | 11 | #include "unordered_map.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 12 | |
| 13 | namespace art { |
| 14 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 15 | /** |
| 16 | * Used to intern strings. |
| 17 | * |
| 18 | * There are actually two tables: one that holds strong references to its strings, and one that |
| 19 | * holds weak references. The former is used for string literals, for which there is an effective |
| 20 | * reference from the constant pool. The latter is used for strings interned at runtime via |
| 21 | * String.intern. Some code (XML parsers being a prime example) relies on being able to intern |
| 22 | * arbitrarily many strings for the duration of a parse without permanently increasing the memory |
| 23 | * footprint. |
| 24 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 25 | class InternTable { |
| 26 | public: |
| 27 | InternTable(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 29 | // Interns a potentially new string in the 'strong' table. (See above.) |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 30 | String* InternStrong(int32_t utf16_length, const char* utf8_data); |
| 31 | |
| 32 | // Interns a potentially new string in the 'strong' table. (See above.) |
| 33 | String* InternStrong(const char* utf8_data); |
| 34 | |
| 35 | // Interns a potentially new string in the 'strong' table. (See above.) |
| 36 | String* InternStrong(String* s); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 38 | // Interns a potentially new string in the 'weak' table. (See above.) |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 39 | String* InternWeak(String* s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 40 | |
| 41 | // Register a String trusting that it is safe to intern. |
| 42 | // Used when reinitializing InternTable from an image. |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 43 | void RegisterStrong(String* s); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 45 | void SweepInternTableWeaks(Heap::IsMarkedTester is_marked, void* arg); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 46 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 47 | bool ContainsWeak(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 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame^] | 53 | void DumpForSigQuit(std::ostream& os) const; |
| 54 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 55 | private: |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 56 | typedef std::tr1::unordered_multimap<int32_t, String*> Table; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 57 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 58 | String* Insert(String* s, bool is_strong); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 59 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 60 | String* Lookup(Table& table, String* s, uint32_t hash_code); |
| 61 | String* Insert(Table& table, String* s, uint32_t hash_code); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 62 | void Remove(Table& table, const String* s, uint32_t hash_code); |
| 63 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 64 | mutable Mutex intern_table_lock_; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 65 | Table image_strong_interns_; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 66 | Table strong_interns_; |
| 67 | Table weak_interns_; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | } // namespace art |
| 71 | |
| 72 | #endif // ART_SRC_CLASS_LINKER_H_ |