blob: bb8e0315da188c9c72e4a37f259f0d42134daa70 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom7e93b502011-08-04 14:16:22 -070016
17#ifndef ART_SRC_INTERN_TABLE_H_
18#define ART_SRC_INTERN_TABLE_H_
19
Elliott Hughescac6cc72011-11-03 20:31:21 -070020#include <iosfwd>
Elliott Hughese5448b52012-01-18 16:44:06 -080021#include <map>
Brian Carlstrom7e93b502011-08-04 14:16:22 -070022
23#include "heap.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070024#include "mutex.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070025#include "object.h"
26
27namespace art {
28
Elliott Hughescf4c6c42011-09-01 15:16:42 -070029/**
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 Carlstrom7e93b502011-08-04 14:16:22 -070039class InternTable {
40 public:
41 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070042
Elliott Hughescf4c6c42011-09-01 15:16:42 -070043 // Interns a potentially new string in the 'strong' table. (See above.)
Brian Carlstromc74255f2011-09-11 22:47:39 -070044 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 Carlstroma663ea52011-08-19 23:33:41 -070051
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052 // Interns a potentially new string in the 'weak' table. (See above.)
Brian Carlstromc74255f2011-09-11 22:47:39 -070053 String* InternWeak(String* s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070054
55 // Register a String trusting that it is safe to intern.
56 // Used when reinitializing InternTable from an image.
Brian Carlstromc74255f2011-09-11 22:47:39 -070057 void RegisterStrong(String* s);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070058
Elliott Hughesc33a32b2011-10-11 18:18:07 -070059 void SweepInternTableWeaks(Heap::IsMarkedTester is_marked, void* arg);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060
Brian Carlstromc74255f2011-09-11 22:47:39 -070061 bool ContainsWeak(String* s);
Brian Carlstroma663ea52011-08-19 23:33:41 -070062
63 size_t Size() const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064
Elliott Hughes410c0c82011-09-01 17:58:25 -070065 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070066
Elliott Hughescac6cc72011-11-03 20:31:21 -070067 void DumpForSigQuit(std::ostream& os) const;
68
Brian Carlstrom7e93b502011-08-04 14:16:22 -070069 private:
Elliott Hughese5448b52012-01-18 16:44:06 -080070 typedef std::multimap<int32_t, String*> Table;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071
Brian Carlstromc74255f2011-09-11 22:47:39 -070072 String* Insert(String* s, bool is_strong);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070073
Brian Carlstromc74255f2011-09-11 22:47:39 -070074 String* Lookup(Table& table, String* s, uint32_t hash_code);
75 String* Insert(Table& table, String* s, uint32_t hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070076 void Remove(Table& table, const String* s, uint32_t hash_code);
77
Elliott Hughes8daa0922011-09-11 13:46:25 -070078 mutable Mutex intern_table_lock_;
Ian Rogers5d76c432011-10-31 21:42:49 -070079 Table image_strong_interns_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070080 Table strong_interns_;
81 Table weak_interns_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070082};
83
84} // namespace art
85
86#endif // ART_SRC_CLASS_LINKER_H_