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 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_STRING_REFERENCE_H_ |
| 18 | #define ART_RUNTIME_STRING_REFERENCE_H_ |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include "base/logging.h" |
Vladimir Marko | 4d2bb1b | 2016-06-24 11:56:59 +0100 | [diff] [blame] | 23 | #include "dex_file-inl.h" |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 24 | #include "dex_file_types.h" |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 25 | #include "utf-inl.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 29 | // A string is located by its DexFile and the string_ids_ table index into that DexFile. |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 30 | struct StringReference { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 31 | StringReference(const DexFile* file, dex::StringIndex index) |
| 32 | : dex_file(file), string_index(index) { } |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 33 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 34 | const char* GetStringData() const { |
| 35 | return dex_file->GetStringData(dex_file->GetStringId(string_index)); |
| 36 | } |
| 37 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 38 | const DexFile* dex_file; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 39 | dex::StringIndex string_index; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 42 | // Compare only the reference and not the string contents. |
| 43 | struct StringReferenceComparator { |
| 44 | bool operator()(const StringReference& a, const StringReference& b) { |
| 45 | if (a.dex_file != b.dex_file) { |
| 46 | return a.dex_file < b.dex_file; |
| 47 | } |
| 48 | return a.string_index < b.string_index; |
| 49 | } |
| 50 | }; |
| 51 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 52 | // Compare the actual referenced string values. Used for string reference deduplication. |
| 53 | struct StringReferenceValueComparator { |
| 54 | bool operator()(StringReference sr1, StringReference sr2) const { |
| 55 | // Note that we want to deduplicate identical strings even if they are referenced |
| 56 | // by different dex files, so we need some (any) total ordering of strings, rather |
| 57 | // than references. However, the references should usually be from the same dex file, |
| 58 | // so we choose the dex file string ordering so that we can simply compare indexes |
| 59 | // and avoid the costly string comparison in the most common case. |
| 60 | if (sr1.dex_file == sr2.dex_file) { |
| 61 | // Use the string order enforced by the dex file verifier. |
| 62 | DCHECK_EQ( |
| 63 | sr1.string_index < sr2.string_index, |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 64 | CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(), |
| 65 | sr2.GetStringData()) < 0); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 66 | return sr1.string_index < sr2.string_index; |
| 67 | } else { |
| 68 | // Cannot compare indexes, so do the string comparison. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 69 | return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(), |
| 70 | sr2.GetStringData()) < 0; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | } // namespace art |
| 76 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 77 | #endif // ART_RUNTIME_STRING_REFERENCE_H_ |