blob: 8c7fb429521bb41753a731c1d02b08a104230218 [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) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070054 // Avoid doing read barriers since the space might not yet be added to the heap.
55 // See b/117803941
Mathieu Chartier74ccee62018-10-10 10:30:29 -070056 for (GcRoot<mirror::String>& string : intern_strings) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070057 CHECK(Find(string.Read<kWithoutReadBarrier>()) == nullptr)
58 << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8()
59 << " in the intern table";
Mathieu Chartier74ccee62018-10-10 10:30:29 -070060 }
61 }
62 // Insert at the front since we add new interns into the back.
63 tables_.insert(tables_.begin(), std::move(intern_strings));
64}
65
66} // namespace art
67
68#endif // ART_RUNTIME_INTERN_TABLE_INL_H_