blob: d8e5da82093f574ca1bdbe26946a082c14360228 [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
22namespace art {
23
24template <typename Visitor>
25inline void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space,
26 const Visitor& visitor) {
27 DCHECK(image_space != nullptr);
28 // Only add if we have the interned strings section.
29 const ImageSection& section = image_space->GetImageHeader().GetInternedStringsSection();
30 if (section.Size() > 0) {
31 AddTableFromMemory(image_space->Begin() + section.Offset(), visitor);
32 }
33}
34
35template <typename Visitor>
36inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor) {
37 size_t read_count = 0;
38 UnorderedSet set(ptr, /*make copy*/false, &read_count);
39 // Visit the unordered set, may remove elements.
40 visitor(set);
41 if (!set.empty()) {
42 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
43 strong_interns_.AddInternStrings(std::move(set));
44 }
45 return read_count;
46}
47
48inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings) {
49 static constexpr bool kCheckDuplicates = kIsDebugBuild;
50 if (kCheckDuplicates) {
51 for (GcRoot<mirror::String>& string : intern_strings) {
52 CHECK(Find(string.Read()) == nullptr)
53 << "Already found " << string.Read()->ToModifiedUtf8() << " in the intern table";
54 }
55 }
56 // Insert at the front since we add new interns into the back.
57 tables_.insert(tables_.begin(), std::move(intern_strings));
58}
59
60} // namespace art
61
62#endif // ART_RUNTIME_INTERN_TABLE_INL_H_