Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | cd0f38f | 2018-10-15 09:44:35 -0700 | [diff] [blame^] | 22 | // Required for ToModifiedUtf8 below. |
| 23 | #include "mirror/string-inl.h" |
| 24 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
| 27 | template <typename Visitor> |
| 28 | inline 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 | |
| 38 | template <typename Visitor> |
| 39 | inline 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 | |
| 51 | inline 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_ |