blob: c814a1b85c117a2f1bb90ee90aa033c3c1e0f034 [file] [log] [blame]
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001// 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 Hughes8daa0922011-09-11 13:46:25 -07009#include "mutex.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070010#include "object.h"
11
12namespace art {
13
Elliott Hughescf4c6c42011-09-01 15:16:42 -070014/**
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 Carlstrom7e93b502011-08-04 14:16:22 -070024class InternTable {
25 public:
26 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070027
Elliott Hughescf4c6c42011-09-01 15:16:42 -070028 // Interns a potentially new string in the 'strong' table. (See above.)
Brian Carlstromc74255f2011-09-11 22:47:39 -070029 String* InternStrong(int32_t utf16_length, const char* utf8_data);
30
31 // Interns a potentially new string in the 'strong' table. (See above.)
32 String* InternStrong(const char* utf8_data);
33
34 // Interns a potentially new string in the 'strong' table. (See above.)
35 String* InternStrong(String* s);
Brian Carlstroma663ea52011-08-19 23:33:41 -070036
Elliott Hughescf4c6c42011-09-01 15:16:42 -070037 // Interns a potentially new string in the 'weak' table. (See above.)
Brian Carlstromc74255f2011-09-11 22:47:39 -070038 String* InternWeak(String* s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070039
40 // Register a String trusting that it is safe to intern.
41 // Used when reinitializing InternTable from an image.
Brian Carlstromc74255f2011-09-11 22:47:39 -070042 void RegisterStrong(String* s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070043
Elliott Hughes410c0c82011-09-01 17:58:25 -070044 // Removes all weak interns for which the predicate functor 'p' returns true.
45 // (We use an explicit Predicate type rather than a template to keep implementation
46 // out of the header file.)
47 struct Predicate {
48 virtual ~Predicate() {}
49 virtual bool operator()(const String*) const = 0;
50 };
51 void RemoveWeakIf(const Predicate& p);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052
Brian Carlstromc74255f2011-09-11 22:47:39 -070053 bool ContainsWeak(String* s);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054
55 size_t Size() const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056
Elliott Hughes410c0c82011-09-01 17:58:25 -070057 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070058
59 private:
Brian Carlstromc74255f2011-09-11 22:47:39 -070060 typedef std::tr1::unordered_multimap<int32_t, String*> Table;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070061
Brian Carlstromc74255f2011-09-11 22:47:39 -070062 String* Insert(String* s, bool is_strong);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063
Brian Carlstromc74255f2011-09-11 22:47:39 -070064 String* Lookup(Table& table, String* s, uint32_t hash_code);
65 String* Insert(Table& table, String* s, uint32_t hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070066 void Remove(Table& table, const String* s, uint32_t hash_code);
67
Elliott Hughes8daa0922011-09-11 13:46:25 -070068 mutable Mutex intern_table_lock_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070069 Table strong_interns_;
70 Table weak_interns_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070071};
72
73} // namespace art
74
75#endif // ART_SRC_CLASS_LINKER_H_