blob: 84b730e2b690604180a65c9db54c895963dd40fd [file] [log] [blame]
Mathieu Chartier74ccee62018-10-10 10:30:29 -07001/*
2 * Copyright (C) 2018 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_INL_H_
18#define ART_RUNTIME_INTERN_TABLE_INL_H_
19
20#include "intern_table.h"
21
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000022#include "dex/utf.h"
Vladimir Markofdd46842020-03-25 14:57:17 +000023#include "gc/space/image_space.h"
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000024#include "gc_root-inl.h"
Vladimir Markofdd46842020-03-25 14:57:17 +000025#include "image.h"
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000026#include "mirror/string-inl.h"
27#include "thread-current-inl.h"
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -070028
Mathieu Chartier74ccee62018-10-10 10:30:29 -070029namespace art {
30
Vladimir Markocdc3ab02022-04-05 13:33:12 +000031ALWAYS_INLINE
Vladimir Marko365c0202022-03-22 09:53:31 +000032inline uint32_t InternTable::Utf8String::Hash(uint32_t utf16_length, const char* utf8_data) {
Vladimir Marko4625f252022-02-23 11:20:26 +000033 DCHECK_EQ(utf16_length, CountModifiedUtf8Chars(utf8_data));
34 if (LIKELY(utf8_data[utf16_length] == 0)) {
35 int32_t hash = ComputeUtf16Hash(utf8_data, utf16_length);
36 DCHECK_EQ(hash, ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length));
37 return hash;
38 } else {
39 return ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length);
40 }
41}
42
Vladimir Markocdc3ab02022-04-05 13:33:12 +000043ALWAYS_INLINE
Vladimir Marko365c0202022-03-22 09:53:31 +000044inline size_t InternTable::StringHash::operator()(const GcRoot<mirror::String>& root) const {
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000045 if (kIsDebugBuild) {
46 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
47 }
Vladimir Marko365c0202022-03-22 09:53:31 +000048 ObjPtr<mirror::String> s = root.Read<kWithoutReadBarrier>();
49 int32_t hash = s->GetStoredHashCode();
50 DCHECK_EQ(hash, s->ComputeHashCode());
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000051 // An additional cast to prevent undesired sign extension.
Vladimir Marko365c0202022-03-22 09:53:31 +000052 return static_cast<uint32_t>(hash);
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000053}
54
Vladimir Markocdc3ab02022-04-05 13:33:12 +000055ALWAYS_INLINE
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000056inline bool InternTable::StringEquals::operator()(const GcRoot<mirror::String>& a,
57 const GcRoot<mirror::String>& b) const {
58 if (kIsDebugBuild) {
59 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
60 }
61 return a.Read<kWithoutReadBarrier>()->Equals(b.Read<kWithoutReadBarrier>());
62}
63
Vladimir Markocdc3ab02022-04-05 13:33:12 +000064ALWAYS_INLINE
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000065inline bool InternTable::StringEquals::operator()(const GcRoot<mirror::String>& a,
66 const Utf8String& b) const {
67 if (kIsDebugBuild) {
68 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
69 }
70 ObjPtr<mirror::String> a_string = a.Read<kWithoutReadBarrier>();
71 uint32_t a_length = static_cast<uint32_t>(a_string->GetLength());
72 if (a_length != b.GetUtf16Length()) {
73 return false;
74 }
Vladimir Marko4625f252022-02-23 11:20:26 +000075 DCHECK_GE(strlen(b.GetUtf8Data()), a_length);
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000076 if (a_string->IsCompressed()) {
Vladimir Marko4625f252022-02-23 11:20:26 +000077 // Modified UTF-8 single byte character range is 0x01 .. 0x7f.
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000078 // The string compression occurs on regular ASCII with same exact range,
Vladimir Marko4625f252022-02-23 11:20:26 +000079 // not on extended ASCII which is up to 0xff.
80 return b.GetUtf8Data()[a_length] == 0 &&
81 memcmp(b.GetUtf8Data(), a_string->GetValueCompressed(), a_length * sizeof(uint8_t)) == 0;
82 } else if (mirror::kUseStringCompression && b.GetUtf8Data()[a_length] == 0) {
83 // ASCII string `b` cannot equal non-ASCII `a_string`.
84 return false;
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000085 } else {
86 const uint16_t* a_value = a_string->GetValue();
87 return CompareModifiedUtf8ToUtf16AsCodePointValues(b.GetUtf8Data(), a_value, a_length) == 0;
88 }
89}
90
Mathieu Chartier74ccee62018-10-10 10:30:29 -070091template <typename Visitor>
92inline void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space,
93 const Visitor& visitor) {
94 DCHECK(image_space != nullptr);
95 // Only add if we have the interned strings section.
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070096 const ImageHeader& header = image_space->GetImageHeader();
97 const ImageSection& section = header.GetInternedStringsSection();
Mathieu Chartier74ccee62018-10-10 10:30:29 -070098 if (section.Size() > 0) {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070099 AddTableFromMemory(image_space->Begin() + section.Offset(), visitor, !header.IsAppImage());
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700100 }
101}
102
103template <typename Visitor>
Mathieu Chartier8cc418e2018-10-31 10:54:30 -0700104inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr,
105 const Visitor& visitor,
106 bool is_boot_image) {
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700107 size_t read_count = 0;
108 UnorderedSet set(ptr, /*make copy*/false, &read_count);
Mathieu Chartier41c08082018-10-31 11:50:26 -0700109 {
110 // Hold the lock while calling the visitor to prevent possible race
111 // conditions with another thread adding intern strings.
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700112 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier41c08082018-10-31 11:50:26 -0700113 // Visit the unordered set, may remove elements.
114 visitor(set);
115 if (!set.empty()) {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -0700116 strong_interns_.AddInternStrings(std::move(set), is_boot_image);
Mathieu Chartier41c08082018-10-31 11:50:26 -0700117 }
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700118 }
119 return read_count;
120}
121
Mathieu Chartier8cc418e2018-10-31 10:54:30 -0700122inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings,
123 bool is_boot_image) {
Vladimir Marko365c0202022-03-22 09:53:31 +0000124 if (kIsDebugBuild) {
Mathieu Chartier2547af32018-10-16 17:48:20 -0700125 // Avoid doing read barriers since the space might not yet be added to the heap.
126 // See b/117803941
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700127 for (GcRoot<mirror::String>& string : intern_strings) {
Vladimir Marko365c0202022-03-22 09:53:31 +0000128 ObjPtr<mirror::String> s = string.Read<kWithoutReadBarrier>();
129 uint32_t hash = static_cast<uint32_t>(s->GetStoredHashCode());
130 CHECK_EQ(hash, static_cast<uint32_t>(s->ComputeHashCode()));
131 CHECK(Find(s, hash) == nullptr)
Mathieu Chartier2547af32018-10-16 17:48:20 -0700132 << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8()
133 << " in the intern table";
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700134 }
135 }
Vladimir Marko365c0202022-03-22 09:53:31 +0000136
Vladimir Marko47eea602022-04-04 10:27:25 +0000137 // Insert before the last (unfrozen) table since we add new interns into the back.
138 // Keep the order of previous frozen tables unchanged, so that we can can remember
139 // the number of searched frozen tables and not search them again.
140 DCHECK(!tables_.empty());
141 tables_.insert(tables_.end() - 1, InternalTable(std::move(intern_strings), is_boot_image));
Mathieu Chartier8cc418e2018-10-31 10:54:30 -0700142}
143
144template <typename Visitor>
145inline void InternTable::VisitInterns(const Visitor& visitor,
146 bool visit_boot_images,
147 bool visit_non_boot_images) {
Vladimir Marko365c0202022-03-22 09:53:31 +0000148 auto visit_tables = [&](dchecked_vector<Table::InternalTable>& tables)
Mathieu Chartier8fc75582018-11-01 14:21:33 -0700149 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -0700150 for (Table::InternalTable& table : tables) {
Vladimir Marko365c0202022-03-22 09:53:31 +0000151 // Determine if we want to visit the table based on the flags.
152 const bool visit = table.IsBootImage() ? visit_boot_images : visit_non_boot_images;
Mathieu Chartier8cc418e2018-10-31 10:54:30 -0700153 if (visit) {
154 for (auto& intern : table.set_) {
155 visitor(intern);
156 }
157 }
158 }
159 };
160 visit_tables(strong_interns_.tables_);
161 visit_tables(weak_interns_.tables_);
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700162}
163
Vladimir Marko365c0202022-03-22 09:53:31 +0000164inline size_t InternTable::CountInterns(bool visit_boot_images, bool visit_non_boot_images) const {
Mathieu Chartier8fc75582018-11-01 14:21:33 -0700165 size_t ret = 0u;
Vladimir Marko365c0202022-03-22 09:53:31 +0000166 auto visit_tables = [&](const dchecked_vector<Table::InternalTable>& tables)
Mathieu Chartier8fc75582018-11-01 14:21:33 -0700167 NO_THREAD_SAFETY_ANALYSIS {
168 for (const Table::InternalTable& table : tables) {
Vladimir Marko365c0202022-03-22 09:53:31 +0000169 // Determine if we want to visit the table based on the flags.
170 const bool visit = table.IsBootImage() ? visit_boot_images : visit_non_boot_images;
Mathieu Chartier8fc75582018-11-01 14:21:33 -0700171 if (visit) {
172 ret += table.set_.size();
173 }
174 }
175 };
176 visit_tables(strong_interns_.tables_);
177 visit_tables(weak_interns_.tables_);
178 return ret;
179}
180
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700181} // namespace art
182
183#endif // ART_RUNTIME_INTERN_TABLE_INL_H_