1 /*
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
22 #include "gc/space/image_space.h"
23 #include "image.h"
24 #include "mirror/string-inl.h" // Required for ToModifiedUtf8 below.
25
26 namespace art {
27
28 template <typename Visitor>
AddImageStringsToTable(gc::space::ImageSpace * image_space,const Visitor & visitor)29 inline 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.
33 const ImageHeader& header = image_space->GetImageHeader();
34 const ImageSection& section = header.GetInternedStringsSection();
35 if (section.Size() > 0) {
36 AddTableFromMemory(image_space->Begin() + section.Offset(), visitor, !header.IsAppImage());
37 }
38 }
39
40 template <typename Visitor>
AddTableFromMemory(const uint8_t * ptr,const Visitor & visitor,bool is_boot_image)41 inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr,
42 const Visitor& visitor,
43 bool is_boot_image) {
44 size_t read_count = 0;
45 UnorderedSet set(ptr, /*make copy*/false, &read_count);
46 {
47 // Hold the lock while calling the visitor to prevent possible race
48 // conditions with another thread adding intern strings.
49 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
50 // Visit the unordered set, may remove elements.
51 visitor(set);
52 if (!set.empty()) {
53 strong_interns_.AddInternStrings(std::move(set), is_boot_image);
54 }
55 }
56 return read_count;
57 }
58
AddInternStrings(UnorderedSet && intern_strings,bool is_boot_image)59 inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings,
60 bool is_boot_image) {
61 static constexpr bool kCheckDuplicates = kIsDebugBuild;
62 if (kCheckDuplicates) {
63 // Avoid doing read barriers since the space might not yet be added to the heap.
64 // See b/117803941
65 for (GcRoot<mirror::String>& string : intern_strings) {
66 CHECK(Find(string.Read<kWithoutReadBarrier>()) == nullptr)
67 << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8()
68 << " in the intern table";
69 }
70 }
71 // Insert at the front since we add new interns into the back.
72 tables_.insert(tables_.begin(),
73 InternalTable(std::move(intern_strings), is_boot_image));
74 }
75
76 template <typename Visitor>
VisitInterns(const Visitor & visitor,bool visit_boot_images,bool visit_non_boot_images)77 inline 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)
81 NO_THREAD_SAFETY_ANALYSIS {
82 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_);
96 }
97
CountInterns(bool visit_boot_images,bool visit_non_boot_images)98 inline 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
118 } // namespace art
119
120 #endif // ART_RUNTIME_INTERN_TABLE_INL_H_
121