blob: e3223c86162452ff51fd1a85764f55074e0f9696 [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
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070020#include <unordered_set>
Brian Carlstrom7e93b502011-08-04 14:16:22 -070021
Mathieu Chartierbad02672014-08-25 13:08:22 -070022#include "base/allocator.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080023#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070024#include "gc_root.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080025#include "object_callbacks.h"
26
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027namespace art {
Mathieu Chartier893263b2014-03-04 11:07:42 -080028
29enum VisitRootFlags : uint8_t;
30
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
32class String;
33} // namespace mirror
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010034class Transaction;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070035
Elliott Hughescf4c6c42011-09-01 15:16:42 -070036/**
37 * Used to intern strings.
38 *
39 * There are actually two tables: one that holds strong references to its strings, and one that
40 * holds weak references. The former is used for string literals, for which there is an effective
41 * reference from the constant pool. The latter is used for strings interned at runtime via
42 * String.intern. Some code (XML parsers being a prime example) relies on being able to intern
43 * arbitrarily many strings for the duration of a parse without permanently increasing the memory
44 * footprint.
45 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070046class InternTable {
47 public:
48 InternTable();
Brian Carlstroma663ea52011-08-19 23:33:41 -070049
Elliott Hughescf4c6c42011-09-01 15:16:42 -070050 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070052 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070053
54 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 mirror::String* InternStrong(const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070056 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070057
58 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070060
Elliott Hughescf4c6c42011-09-01 15:16:42 -070061 // Interns a potentially new string in the 'weak' table. (See above.)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070064 void SweepInternTableWeaks(IsMarkedCallback* callback, void* arg)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070066
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070068
69 size_t Size() const;
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070070 size_t StrongSize() const;
71 size_t WeakSize() const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070072
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070073 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070075
Elliott Hughescac6cc72011-11-03 20:31:21 -070076 void DumpForSigQuit(std::ostream& os) const;
77
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070078 void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
79 void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80
Brian Carlstrom7e93b502011-08-04 14:16:22 -070081 private:
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070082 class StringHashEquals {
83 public:
84 std::size_t operator()(const GcRoot<mirror::String>& root) NO_THREAD_SAFETY_ANALYSIS;
85 bool operator()(const GcRoot<mirror::String>& a, const GcRoot<mirror::String>& b)
86 NO_THREAD_SAFETY_ANALYSIS;
87 };
88 typedef std::unordered_set<GcRoot<mirror::String>, StringHashEquals, StringHashEquals,
89 TrackingAllocator<GcRoot<mirror::String>, kAllocatorTagInternTable>> Table;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070090
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 mirror::String* Insert(mirror::String* s, bool is_strong)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010092 LOCKS_EXCLUDED(Locks::intern_table_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -070093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070094
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070095 mirror::String* LookupStrong(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070097 mirror::String* LookupWeak(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070099 mirror::String* Lookup(Table* table, mirror::String* s)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700101 mirror::String* InsertStrong(mirror::String* s)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100102 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700103 mirror::String* InsertWeak(mirror::String* s)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700105 void RemoveStrong(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100107 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700108 void RemoveWeak(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
110 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700111 void Remove(Table* table, mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700114
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 // Transaction rollback access.
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700116 mirror::String* InsertStrongFromTransaction(mirror::String* s)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700118 mirror::String* InsertWeakFromTransaction(mirror::String* s)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100119 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700120 void RemoveStrongFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100122 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700123 void RemoveWeakFromTransaction(mirror::String* s)
Hiroshi Yamauchi1bd48722014-05-23 19:58:15 -0700124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100125 EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_);
126 friend class Transaction;
127
Mathieu Chartier893263b2014-03-04 11:07:42 -0800128 bool log_new_roots_ GUARDED_BY(Locks::intern_table_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100129 bool allow_new_interns_ GUARDED_BY(Locks::intern_table_lock_);
130 ConditionVariable new_intern_condition_ GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700131 // Since this contains (strong) roots, they need a read barrier to
132 // enable concurrent intern table (strong) root scan. Do not
133 // directly access the strings in it. Use functions that contain
134 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100135 Table strong_interns_ GUARDED_BY(Locks::intern_table_lock_);
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700136 std::vector<GcRoot<mirror::String>> new_strong_intern_roots_
Mathieu Chartier893263b2014-03-04 11:07:42 -0800137 GUARDED_BY(Locks::intern_table_lock_);
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700138 // Since this contains (weak) roots, they need a read barrier. Do
139 // not directly access the strings in it. Use functions that contain
140 // read barriers.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100141 Table weak_interns_ GUARDED_BY(Locks::intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700142};
143
144} // namespace art
145
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700146#endif // ART_RUNTIME_INTERN_TABLE_H_