blob: 687f5ee6baa77b1c7a9499e84072bf7ff52e34d3 [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 Markofdd46842020-03-25 14:57:17 +000022#include "gc/space/image_space.h"
23#include "image.h"
24#include "mirror/string-inl.h" // Required for ToModifiedUtf8 below.
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -070025
Mathieu Chartier74ccee62018-10-10 10:30:29 -070026namespace art {
27
28template <typename Visitor>
29inline void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space,
30 const Visitor& visitor) {
31 DCHECK(image_space != nullptr);
32 // Only add if we have the interned strings section.
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070033 const ImageHeader& header = image_space->GetImageHeader();
34 const ImageSection& section = header.GetInternedStringsSection();
Mathieu Chartier74ccee62018-10-10 10:30:29 -070035 if (section.Size() > 0) {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070036 AddTableFromMemory(image_space->Begin() + section.Offset(), visitor, !header.IsAppImage());
Mathieu Chartier74ccee62018-10-10 10:30:29 -070037 }
38}
39
40template <typename Visitor>
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070041inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr,
42 const Visitor& visitor,
43 bool is_boot_image) {
Mathieu Chartier74ccee62018-10-10 10:30:29 -070044 size_t read_count = 0;
45 UnorderedSet set(ptr, /*make copy*/false, &read_count);
Mathieu Chartier41c08082018-10-31 11:50:26 -070046 {
47 // Hold the lock while calling the visitor to prevent possible race
48 // conditions with another thread adding intern strings.
Mathieu Chartier74ccee62018-10-10 10:30:29 -070049 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier41c08082018-10-31 11:50:26 -070050 // Visit the unordered set, may remove elements.
51 visitor(set);
52 if (!set.empty()) {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070053 strong_interns_.AddInternStrings(std::move(set), is_boot_image);
Mathieu Chartier41c08082018-10-31 11:50:26 -070054 }
Mathieu Chartier74ccee62018-10-10 10:30:29 -070055 }
56 return read_count;
57}
58
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070059inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings,
60 bool is_boot_image) {
Mathieu Chartier74ccee62018-10-10 10:30:29 -070061 static constexpr bool kCheckDuplicates = kIsDebugBuild;
62 if (kCheckDuplicates) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070063 // Avoid doing read barriers since the space might not yet be added to the heap.
64 // See b/117803941
Mathieu Chartier74ccee62018-10-10 10:30:29 -070065 for (GcRoot<mirror::String>& string : intern_strings) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070066 CHECK(Find(string.Read<kWithoutReadBarrier>()) == nullptr)
67 << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8()
68 << " in the intern table";
Mathieu Chartier74ccee62018-10-10 10:30:29 -070069 }
70 }
71 // Insert at the front since we add new interns into the back.
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070072 tables_.insert(tables_.begin(),
73 InternalTable(std::move(intern_strings), is_boot_image));
74}
75
76template <typename Visitor>
77inline void InternTable::VisitInterns(const Visitor& visitor,
78 bool visit_boot_images,
79 bool visit_non_boot_images) {
80 auto visit_tables = [&](std::vector<Table::InternalTable>& tables)
Mathieu Chartier8fc75582018-11-01 14:21:33 -070081 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070082 for (Table::InternalTable& table : tables) {
83 // Determine if we want to visit the table based on the flags..
84 const bool visit =
85 (visit_boot_images && table.IsBootImage()) ||
86 (visit_non_boot_images && !table.IsBootImage());
87 if (visit) {
88 for (auto& intern : table.set_) {
89 visitor(intern);
90 }
91 }
92 }
93 };
94 visit_tables(strong_interns_.tables_);
95 visit_tables(weak_interns_.tables_);
Mathieu Chartier74ccee62018-10-10 10:30:29 -070096}
97
Mathieu Chartier8fc75582018-11-01 14:21:33 -070098inline size_t InternTable::CountInterns(bool visit_boot_images,
99 bool visit_non_boot_images) const {
100 size_t ret = 0u;
101 auto visit_tables = [&](const std::vector<Table::InternalTable>& tables)
102 NO_THREAD_SAFETY_ANALYSIS {
103 for (const Table::InternalTable& table : tables) {
104 // Determine if we want to visit the table based on the flags..
105 const bool visit =
106 (visit_boot_images && table.IsBootImage()) ||
107 (visit_non_boot_images && !table.IsBootImage());
108 if (visit) {
109 ret += table.set_.size();
110 }
111 }
112 };
113 visit_tables(strong_interns_.tables_);
114 visit_tables(weak_interns_.tables_);
115 return ret;
116}
117
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700118} // namespace art
119
120#endif // ART_RUNTIME_INTERN_TABLE_INL_H_