blob: 6f5677385f0fe3c89148ddea4dbe98d2759d98bd [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>
Brian Carlstrom7e93b502011-08-04 14:16:22 -070021
22#include "heap.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023#include "mutex.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070024#include "object.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025#include "safe_map.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070026
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.)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 String* InternStrong(int32_t utf16_length, const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070045 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070046
47 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 String* InternStrong(const char* utf8_data)
Ian Rogersb726dcb2012-09-05 08:57:23 -070049 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070050
51 // Interns a potentially new string in the 'strong' table. (See above.)
Ian Rogersb726dcb2012-09-05 08:57:23 -070052 String* InternStrong(String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070053
Elliott Hughescf4c6c42011-09-01 15:16:42 -070054 // Interns a potentially new string in the 'weak' table. (See above.)
Ian Rogersb726dcb2012-09-05 08:57:23 -070055 String* InternWeak(String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056
57 // Register a String trusting that it is safe to intern.
58 // Used when reinitializing InternTable from an image.
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 void RegisterStrong(String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061 void SweepInternTableWeaks(Heap::IsMarkedTester is_marked, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -070062 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063
Ian Rogersb726dcb2012-09-05 08:57:23 -070064 bool ContainsWeak(String* s) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -070065
66 size_t Size() const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067
Elliott Hughes410c0c82011-09-01 17:58:25 -070068 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom7e93b502011-08-04 14:16:22 -070069
Elliott Hughescac6cc72011-11-03 20:31:21 -070070 void DumpForSigQuit(std::ostream& os) const;
71
Brian Carlstrom7e93b502011-08-04 14:16:22 -070072 private:
Elliott Hughese5448b52012-01-18 16:44:06 -080073 typedef std::multimap<int32_t, String*> Table;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 String* Insert(String* s, bool is_strong)
Ian Rogersb726dcb2012-09-05 08:57:23 -070076 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 String* Lookup(Table& table, String* s, uint32_t hash_code)
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromc74255f2011-09-11 22:47:39 -070080 String* Insert(Table& table, String* s, uint32_t hash_code);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070081 void Remove(Table& table, const String* s, uint32_t hash_code);
82
Elliott Hughes8daa0922011-09-11 13:46:25 -070083 mutable Mutex intern_table_lock_;
Elliott Hughesf8349362012-06-18 15:00:06 -070084 Table image_strong_interns_ GUARDED_BY(intern_table_lock_);
85 Table strong_interns_ GUARDED_BY(intern_table_lock_);
86 Table weak_interns_ GUARDED_BY(intern_table_lock_);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070087};
88
89} // namespace art
90
91#endif // ART_SRC_CLASS_LINKER_H_