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