blob: eec63c874f860236bbee13facf8ab8f5a78be682 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INTERN_TABLE_H_
18#define ART_RUNTIME_INTERN_TABLE_H_
Brian Carlstrom7e93b502011-08-04 14:16:22 -070019
Elliott Hughes76b61672012-12-12 17:47:30 -080020#include "base/mutex.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "root_visitor.h"
22
23#include <map>
Brian Carlstrom7e93b502011-08-04 14:16:22 -070024
25namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026namespace mirror {
27class String;
28} // namespace mirror
Brian Carlstrom7e93b502011-08-04 14:16:22 -070029
Elliott Hughescf4c6c42011-09-01 15:16:42 -070030/**
31 * Used to intern strings.
32 *
33 * There are actually two tables: one that holds strong references to its strings, and one that
34 * holds weak references. The former is used for string literals, for which there is an effective
35 * reference from the constant pool. The latter is used for strings interned at runtime via
36 * String.intern. Some code (XML parsers being a prime example) relies on being able to intern
37 * arbitrarily many strings for the duration of a parse without permanently increasing the memory
38 * footprint.
39 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070040class InternTable {
41 public:
42 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070043
Elliott Hughescf4c6c42011-09-01 15:16:42 -070044 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070046 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070047
48 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 mirror::String* InternStrong(const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070050 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070051
52 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054
Elliott Hughescf4c6c42011-09-01 15:16:42 -070055 // Interns a potentially new string in the 'weak' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070057
Mathieu Chartier6aa3df92013-09-17 15:17:28 -070058 void SweepInternTableWeaks(RootVisitor visitor, void* arg);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070059
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070061
62 size_t Size() const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063
Mathieu Chartierc4621982013-09-16 19:43:47 -070064 void VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070065
Elliott Hughescac6cc72011-11-03 20:31:21 -070066 void DumpForSigQuit(std::ostream& os) const;
67
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070068 void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
69 void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
70
Brian Carlstrom7e93b502011-08-04 14:16:22 -070071 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 typedef std::multimap<int32_t, mirror::String*> Table;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070073
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 mirror::String* Insert(mirror::String* s, bool is_strong)
Ian Rogersb726dcb2012-09-05 08:57:23 -070075 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070076
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 mirror::String* Lookup(Table& table, mirror::String* s, uint32_t hash_code)
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 mirror::String* Insert(Table& table, mirror::String* s, uint32_t hash_code);
80 void Remove(Table& table, const mirror::String* s, uint32_t hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070081
Elliott Hughes8daa0922011-09-11 13:46:25 -070082 mutable Mutex intern_table_lock_;
Mathieu Chartierc4621982013-09-16 19:43:47 -070083 bool is_dirty_ GUARDED_BY(intern_table_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070084 bool allow_new_interns_ GUARDED_BY(intern_table_lock_);
85 ConditionVariable new_intern_condition_ GUARDED_BY(intern_table_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -070086 Table strong_interns_ GUARDED_BY(intern_table_lock_);
87 Table weak_interns_ GUARDED_BY(intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070088};
89
90} // namespace art
91
Brian Carlstromfc0e3212013-07-17 14:40:12 -070092#endif // ART_RUNTIME_INTERN_TABLE_H_