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