blob: e4c34ca6051dbba0152aad9d121ba6d8466b2584 [file] [log] [blame]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001/*
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"
Vladimir Marko4d2bb1b2016-06-24 11:56:59 +010023#include "dex_file-inl.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000024#include "utf-inl.h"
25
26namespace art {
27
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010028// A string is located by its DexFile and the string_ids_ table index into that DexFile.
Vladimir Markocac5a7e2016-02-22 10:39:50 +000029struct StringReference {
30 StringReference(const DexFile* file, uint32_t index) : dex_file(file), string_index(index) { }
31
Vladimir Marko5c6a5872016-06-27 13:50:16 +010032 const char* GetStringData() const {
33 return dex_file->GetStringData(dex_file->GetStringId(string_index));
34 }
35
Vladimir Markocac5a7e2016-02-22 10:39:50 +000036 const DexFile* dex_file;
37 uint32_t string_index;
38};
39
40// Compare the actual referenced string values. Used for string reference deduplication.
41struct StringReferenceValueComparator {
42 bool operator()(StringReference sr1, StringReference sr2) const {
43 // Note that we want to deduplicate identical strings even if they are referenced
44 // by different dex files, so we need some (any) total ordering of strings, rather
45 // than references. However, the references should usually be from the same dex file,
46 // so we choose the dex file string ordering so that we can simply compare indexes
47 // and avoid the costly string comparison in the most common case.
48 if (sr1.dex_file == sr2.dex_file) {
49 // Use the string order enforced by the dex file verifier.
50 DCHECK_EQ(
51 sr1.string_index < sr2.string_index,
Vladimir Marko5c6a5872016-06-27 13:50:16 +010052 CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(),
53 sr2.GetStringData()) < 0);
Vladimir Markocac5a7e2016-02-22 10:39:50 +000054 return sr1.string_index < sr2.string_index;
55 } else {
56 // Cannot compare indexes, so do the string comparison.
Vladimir Marko5c6a5872016-06-27 13:50:16 +010057 return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(),
58 sr2.GetStringData()) < 0;
Vladimir Markocac5a7e2016-02-22 10:39:50 +000059 }
60 }
61};
62
63} // namespace art
64
65#endif // ART_COMPILER_UTILS_STRING_REFERENCE_H_