blob: 84754fafb89d52ad048c87243ce62f0d9e55fc38 [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);
Mathieu Chartier41c08082018-10-31 11:50:26 -070042 {
43 // Hold the lock while calling the visitor to prevent possible race
44 // conditions with another thread adding intern strings.
Mathieu Chartier74ccee62018-10-10 10:30:29 -070045 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier41c08082018-10-31 11:50:26 -070046 // Visit the unordered set, may remove elements.
47 visitor(set);
48 if (!set.empty()) {
49 strong_interns_.AddInternStrings(std::move(set));
50 }
Mathieu Chartier74ccee62018-10-10 10:30:29 -070051 }
52 return read_count;
53}
54
55inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings) {
56 static constexpr bool kCheckDuplicates = kIsDebugBuild;
57 if (kCheckDuplicates) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070058 // Avoid doing read barriers since the space might not yet be added to the heap.
59 // See b/117803941
Mathieu Chartier74ccee62018-10-10 10:30:29 -070060 for (GcRoot<mirror::String>& string : intern_strings) {
Mathieu Chartier2547af32018-10-16 17:48:20 -070061 CHECK(Find(string.Read<kWithoutReadBarrier>()) == nullptr)
62 << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8()
63 << " in the intern table";
Mathieu Chartier74ccee62018-10-10 10:30:29 -070064 }
65 }
66 // Insert at the front since we add new interns into the back.
67 tables_.insert(tables_.begin(), std::move(intern_strings));
68}
69
70} // namespace art
71
72#endif // ART_RUNTIME_INTERN_TABLE_INL_H_