blob: 80f21e237ba077e084c308cffb2626e9976d303a [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
Elliott Hughescac6cc72011-11-03 20:31:21 -07006#include <iosfwd>
Brian Carlstrom7e93b502011-08-04 14:16:22 -07007
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"
Elliott Hughescac6cc72011-11-03 20:31:21 -070011#include "unordered_map.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070012
13namespace art {
14
Elliott Hughescf4c6c42011-09-01 15:16:42 -070015/**
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 Carlstrom7e93b502011-08-04 14:16:22 -070025class InternTable {
26 public:
27 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070028
Elliott Hughescf4c6c42011-09-01 15:16:42 -070029 // Interns a potentially new string in the 'strong' table. (See above.)
Brian Carlstromc74255f2011-09-11 22:47:39 -070030 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 Carlstroma663ea52011-08-19 23:33:41 -070037
Elliott Hughescf4c6c42011-09-01 15:16:42 -070038 // Interns a potentially new string in the 'weak' table. (See above.)
Brian Carlstromc74255f2011-09-11 22:47:39 -070039 String* InternWeak(String* s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070040
41 // Register a String trusting that it is safe to intern.
42 // Used when reinitializing InternTable from an image.
Brian Carlstromc74255f2011-09-11 22:47:39 -070043 void RegisterStrong(String* s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070044
Elliott Hughesc33a32b2011-10-11 18:18:07 -070045 void SweepInternTableWeaks(Heap::IsMarkedTester is_marked, void* arg);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070046
Brian Carlstromc74255f2011-09-11 22:47:39 -070047 bool ContainsWeak(String* s);
Brian Carlstroma663ea52011-08-19 23:33:41 -070048
49 size_t Size() const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070050
Elliott Hughes410c0c82011-09-01 17:58:25 -070051 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070052
Elliott Hughescac6cc72011-11-03 20:31:21 -070053 void DumpForSigQuit(std::ostream& os) const;
54
Brian Carlstrom7e93b502011-08-04 14:16:22 -070055 private:
Brian Carlstromc74255f2011-09-11 22:47:39 -070056 typedef std::tr1::unordered_multimap<int32_t, String*> Table;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070057
Brian Carlstromc74255f2011-09-11 22:47:39 -070058 String* Insert(String* s, bool is_strong);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070059
Brian Carlstromc74255f2011-09-11 22:47:39 -070060 String* Lookup(Table& table, String* s, uint32_t hash_code);
61 String* Insert(Table& table, String* s, uint32_t hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070062 void Remove(Table& table, const String* s, uint32_t hash_code);
63
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 mutable Mutex intern_table_lock_;
Ian Rogers5d76c432011-10-31 21:42:49 -070065 Table image_strong_interns_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070066 Table strong_interns_;
67 Table weak_interns_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070068};
69
70} // namespace art
71
72#endif // ART_SRC_CLASS_LINKER_H_