Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_COMPILER_UTILS_STRING_REFERENCE_H_ |
| 18 | #define ART_COMPILER_UTILS_STRING_REFERENCE_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include "base/logging.h" |
| 23 | #include "utf-inl.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class DexFile; |
| 28 | |
| 29 | // A string is uniquely located by its DexFile and the string_ids_ table index into that DexFile. |
| 30 | struct StringReference { |
| 31 | StringReference(const DexFile* file, uint32_t index) : dex_file(file), string_index(index) { } |
| 32 | |
| 33 | const DexFile* dex_file; |
| 34 | uint32_t string_index; |
| 35 | }; |
| 36 | |
| 37 | // Compare the actual referenced string values. Used for string reference deduplication. |
| 38 | struct StringReferenceValueComparator { |
| 39 | bool operator()(StringReference sr1, StringReference sr2) const { |
| 40 | // Note that we want to deduplicate identical strings even if they are referenced |
| 41 | // by different dex files, so we need some (any) total ordering of strings, rather |
| 42 | // than references. However, the references should usually be from the same dex file, |
| 43 | // so we choose the dex file string ordering so that we can simply compare indexes |
| 44 | // and avoid the costly string comparison in the most common case. |
| 45 | if (sr1.dex_file == sr2.dex_file) { |
| 46 | // Use the string order enforced by the dex file verifier. |
| 47 | DCHECK_EQ( |
| 48 | sr1.string_index < sr2.string_index, |
| 49 | CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues( |
| 50 | sr1.dex_file->GetStringData(sr1.dex_file->GetStringId(sr1.string_index)), |
| 51 | sr1.dex_file->GetStringData(sr2.dex_file->GetStringId(sr2.string_index))) < 0); |
| 52 | return sr1.string_index < sr2.string_index; |
| 53 | } else { |
| 54 | // Cannot compare indexes, so do the string comparison. |
| 55 | return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues( |
| 56 | sr1.dex_file->GetStringData(sr1.dex_file->GetStringId(sr1.string_index)), |
| 57 | sr1.dex_file->GetStringData(sr2.dex_file->GetStringId(sr2.string_index))) < 0; |
| 58 | } |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | } // namespace art |
| 63 | |
| 64 | #endif // ART_COMPILER_UTILS_STRING_REFERENCE_H_ |