blob: 6fc53e9f20b2f98d72f95221c8358e46845108b0 [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
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -070022// Required for ToModifiedUtf8 below.
23#include "mirror/string-inl.h"
24
Mathieu Chartier74ccee62018-10-10 10:30:29 -070025namespace art {
26
27template <typename Visitor>
28inline void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space,
29 const Visitor& visitor) {
30 DCHECK(image_space != nullptr);
31 // Only add if we have the interned strings section.
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070032 const ImageHeader& header = image_space->GetImageHeader();
33 const ImageSection& section = header.GetInternedStringsSection();
Mathieu Chartier74ccee62018-10-10 10:30:29 -070034 if (section.Size() > 0) {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070035 AddTableFromMemory(image_space->Begin() + section.Offset(), visitor, !header.IsAppImage());
Mathieu Chartier74ccee62018-10-10 10:30:29 -070036 }
37}
38
39template <typename Visitor>
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070040inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr,
41 const Visitor& visitor,
42 bool is_boot_image) {
Mathieu Chartier74ccee62018-10-10 10:30:29 -070043 size_t read_count = 0;
44 UnorderedSet set(ptr, /*make copy*/false, &read_count);
Mathieu Chartier41c08082018-10-31 11:50:26 -070045 {
46 // Hold the lock while calling the visitor to prevent possible race
47 // conditions with another thread adding intern strings.
Mathieu Chartier74ccee62018-10-10 10:30:29 -070048 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier41c08082018-10-31 11:50:26 -070049 // Visit the unordered set, may remove elements.
50 visitor(set);
51 if (!set.empty()) {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070052 strong_interns_.AddInternStrings(std::move(set), is_boot_image);
Mathieu Chartier41c08082018-10-31 11:50:26 -070053 }
Mathieu Chartier74ccee62018-10-10 10:30:29 -070054 }
55 return read_count;
56}
57
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070058inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings,
59 bool is_boot_image) {
Mathieu Chartier74ccee62018-10-10 10:30:29 -070060 static constexpr bool kCheckDuplicates = kIsDebugBuild;
61 if (kCheckDuplicates) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070062 // Avoid doing read barriers since the space might not yet be added to the heap.
63 // See b/117803941
Mathieu Chartier74ccee62018-10-10 10:30:29 -070064 for (GcRoot<mirror::String>& string : intern_strings) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070065 CHECK(Find(string.Read<kWithoutReadBarrier>()) == nullptr)
66 << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8()
67 << " in the intern table";
Mathieu Chartier74ccee62018-10-10 10:30:29 -070068 }
69 }
70 // Insert at the front since we add new interns into the back.
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070071 tables_.insert(tables_.begin(),
72 InternalTable(std::move(intern_strings), is_boot_image));
73}
74
75template <typename Visitor>
76inline void InternTable::VisitInterns(const Visitor& visitor,
77 bool visit_boot_images,
78 bool visit_non_boot_images) {
79 auto visit_tables = [&](std::vector<Table::InternalTable>& tables)
Mathieu Chartier8fc75582018-11-01 14:21:33 -070080 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070081 for (Table::InternalTable& table : tables) {
82 // Determine if we want to visit the table based on the flags..
83 const bool visit =
84 (visit_boot_images && table.IsBootImage()) ||
85 (visit_non_boot_images && !table.IsBootImage());
86 if (visit) {
87 for (auto& intern : table.set_) {
88 visitor(intern);
89 }
90 }
91 }
92 };
93 visit_tables(strong_interns_.tables_);
94 visit_tables(weak_interns_.tables_);
Mathieu Chartier74ccee62018-10-10 10:30:29 -070095}
96
Mathieu Chartier8fc75582018-11-01 14:21:33 -070097inline size_t InternTable::CountInterns(bool visit_boot_images,
98 bool visit_non_boot_images) const {
99 size_t ret = 0u;
100 auto visit_tables = [&](const std::vector<Table::InternalTable>& tables)
101 NO_THREAD_SAFETY_ANALYSIS {
102 for (const Table::InternalTable& table : tables) {
103 // Determine if we want to visit the table based on the flags..
104 const bool visit =
105 (visit_boot_images && table.IsBootImage()) ||
106 (visit_non_boot_images && !table.IsBootImage());
107 if (visit) {
108 ret += table.set_.size();
109 }
110 }
111 };
112 visit_tables(strong_interns_.tables_);
113 visit_tables(weak_interns_.tables_);
114 return ret;
115}
116
Mathieu Chartier74ccee62018-10-10 10:30:29 -0700117} // namespace art
118
119#endif // ART_RUNTIME_INTERN_TABLE_INL_H_