1/*
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 */
16
17#ifndef ART_RUNTIME_INTERN_TABLE_H_
18#define ART_RUNTIME_INTERN_TABLE_H_
19
20#include "base/mutex.h"
21#include "root_visitor.h"
22
23#include <map>
24
25namespace art {
26namespace mirror {
27class String;
28}  // namespace mirror
29
30/**
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 */
40class InternTable {
41 public:
42  InternTable();
43
44  // Interns a potentially new string in the 'strong' table. (See above.)
45  mirror::String* InternStrong(int32_t utf16_length, const char* utf8_data)
46      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
47
48  // Interns a potentially new string in the 'strong' table. (See above.)
49  mirror::String* InternStrong(const char* utf8_data)
50      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
51
52  // Interns a potentially new string in the 'strong' table. (See above.)
53  mirror::String* InternStrong(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
54
55  // Interns a potentially new string in the 'weak' table. (See above.)
56  mirror::String* InternWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
57
58  void SweepInternTableWeaks(IsMarkedTester is_marked, void* arg)
59      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
60
61  bool ContainsWeak(mirror::String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
62
63  size_t Size() const;
64
65  void VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty);
66
67  void DumpForSigQuit(std::ostream& os) const;
68
69  void DisallowNewInterns() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
70  void AllowNewInterns() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
71
72 private:
73  typedef std::multimap<int32_t, mirror::String*> Table;
74
75  mirror::String* Insert(mirror::String* s, bool is_strong)
76      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
77
78  mirror::String* Lookup(Table& table, mirror::String* s, uint32_t hash_code)
79      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80  mirror::String* Insert(Table& table, mirror::String* s, uint32_t hash_code);
81  void Remove(Table& table, const mirror::String* s, uint32_t hash_code);
82
83  mutable Mutex intern_table_lock_;
84  bool is_dirty_ GUARDED_BY(intern_table_lock_);
85  bool allow_new_interns_ GUARDED_BY(intern_table_lock_);
86  ConditionVariable new_intern_condition_ GUARDED_BY(intern_table_lock_);
87  Table strong_interns_ GUARDED_BY(intern_table_lock_);
88  Table weak_interns_ GUARDED_BY(intern_table_lock_);
89};
90
91}  // namespace art
92
93#endif  // ART_RUNTIME_INTERN_TABLE_H_
94