blob: 435cc430b18ec6586be9130b9611fa236ea22523 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include <map>
Brian Carlstrom7e93b502011-08-04 14:16:22 -070021
Ian Rogers719d1a32014-03-06 12:13:39 -080022#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070023#include "gc_root.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080024#include "object_callbacks.h"
25
Brian Carlstrom7e93b502011-08-04 14:16:22 -070026namespace art {
Mathieu Chartier893263b2014-03-04 11:07:42 -080027
28enum VisitRootFlags : uint8_t;
29
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
31class String;
32} // namespace mirror
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010033class Transaction;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070034
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035/**
36 * Used to intern strings.
37 *
38 * There are actually two tables: one that holds strong references to its strings, and one that
39 * holds weak references. The former is used for string literals, for which there is an effective
40 * reference from the constant pool. The latter is used for strings interned at runtime via
41 * String.intern. Some code (XML parsers being a prime example) relies on being able to intern
42 * arbitrarily many strings for the duration of a parse without permanently increasing the memory
43 * footprint.
44 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070045class InternTable {
46 public:
47 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070048
Elliott Hughescf4c6c42011-09-01 15:16:42 -070049 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070051 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070052
53 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 mirror::String* InternStrong(const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070055 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070056
57 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070059
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060 // Interns a potentially new string in the 'weak' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070062
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070063 void SweepInternTableWeaks(IsMarkedCallback* callback, void* arg)
64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070067
68 size_t Size() const;
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070069 size_t StrongSize() const;
70 size_t WeakSize() const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070072 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070074
Elliott Hughescac6cc72011-11-03 20:31:21 -070075 void DumpForSigQuit(std::ostream& os) const;
76
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070077 void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
78 void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
79
Brian Carlstrom7e93b502011-08-04 14:16:22 -070080 private:
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070081 typedef std::multimap<int32_t, GcRoot<mirror::String>> Table;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070082
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 mirror::String* Insert(mirror::String* s, bool is_strong)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010084 LOCKS_EXCLUDED(Locks::intern_table_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -070085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070086
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070087 mirror::String* LookupStrong(mirror::String* s, int32_t hash_code)
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89 mirror::String* LookupWeak(mirror::String* s, int32_t hash_code)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070091 mirror::String* Lookup(Table* table, mirror::String* s, int32_t hash_code)
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080093 mirror::String* InsertStrong(mirror::String* s, int32_t hash_code)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010094 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -080095 mirror::String* InsertWeak(mirror::String* s, int32_t hash_code)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010096 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070097 void RemoveStrong(mirror::String* s, int32_t hash_code)
98 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010099 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700100 void RemoveWeak(mirror::String* s, int32_t hash_code)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
102 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700103 void Remove(Table* table, mirror::String* s, int32_t hash_code)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100105 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700106
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100107 // Transaction rollback access.
Mathieu Chartier893263b2014-03-04 11:07:42 -0800108 mirror::String* InsertStrongFromTransaction(mirror::String* s, int32_t hash_code)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100109 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800110 mirror::String* InsertWeakFromTransaction(mirror::String* s, int32_t hash_code)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100111 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800112 void RemoveStrongFromTransaction(mirror::String* s, int32_t hash_code)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800115 void RemoveWeakFromTransaction(mirror::String* s, int32_t hash_code)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
118 friend class Transaction;
119
Mathieu Chartier893263b2014-03-04 11:07:42 -0800120 bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100121 bool allow_new_interns_ GUARDED_BY(Locks::intern_table_lock_);
122 ConditionVariable new_intern_condition_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700123 // Since this contains (strong) roots, they need a read barrier to
124 // enable concurrent intern table (strong) root scan. Do not
125 // directly access the strings in it. Use functions that contain
126 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100127 Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700128 std::vector<std::pair<int32_t, GcRoot<mirror::String>>> new_strong_intern_roots_
Mathieu Chartier893263b2014-03-04 11:07:42 -0800129 GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700130 // Since this contains (weak) roots, they need a read barrier. Do
131 // not directly access the strings in it. Use functions that contain
132 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100133 Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700134};
135
136} // namespace art
137
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700138#endif // ART_RUNTIME_INTERN_TABLE_H_