blob: d0ab4e40d0558c7ea6145e49f15bd2f8af5237c0 [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
Mathieu Chartierdc00f182016-07-14 10:10:44 -070017#ifndef ART_RUNTIME_STRING_REFERENCE_H_
18#define ART_RUNTIME_STRING_REFERENCE_H_
Vladimir Markocac5a7e2016-02-22 10:39:50 +000019
20#include <stdint.h>
21
22#include "base/logging.h"
Vladimir Marko4d2bb1b2016-06-24 11:56:59 +010023#include "dex_file-inl.h"
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070024#include "dex_file_reference.h"
Andreas Gampe8a0128a2016-11-28 07:38:35 -080025#include "dex_file_types.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000026#include "utf-inl.h"
27
28namespace art {
29
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010030// A string is located by its DexFile and the string_ids_ table index into that DexFile.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070031class StringReference : public DexFileReference {
32 public:
Andreas Gampe8a0128a2016-11-28 07:38:35 -080033 StringReference(const DexFile* file, dex::StringIndex index)
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070034 : DexFileReference(file, index.index_) {}
Vladimir Markocac5a7e2016-02-22 10:39:50 +000035
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070036 dex::StringIndex StringIndex() const {
37 return dex::StringIndex(index);
Vladimir Marko5c6a5872016-06-27 13:50:16 +010038 }
39
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070040 const char* GetStringData() const {
41 return dex_file->GetStringData(dex_file->GetStringId(StringIndex()));
Mathieu Chartierdc00f182016-07-14 10:10:44 -070042 }
43};
44
Vladimir Markocac5a7e2016-02-22 10:39:50 +000045// Compare the actual referenced string values. Used for string reference deduplication.
46struct StringReferenceValueComparator {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070047 bool operator()(const StringReference& sr1, const StringReference& sr2) const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +000048 // Note that we want to deduplicate identical strings even if they are referenced
49 // by different dex files, so we need some (any) total ordering of strings, rather
50 // than references. However, the references should usually be from the same dex file,
51 // so we choose the dex file string ordering so that we can simply compare indexes
52 // and avoid the costly string comparison in the most common case.
53 if (sr1.dex_file == sr2.dex_file) {
54 // Use the string order enforced by the dex file verifier.
55 DCHECK_EQ(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070056 sr1.index < sr2.index,
Vladimir Marko5c6a5872016-06-27 13:50:16 +010057 CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(),
58 sr2.GetStringData()) < 0);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070059 return sr1.index < sr2.index;
Vladimir Markocac5a7e2016-02-22 10:39:50 +000060 } else {
61 // Cannot compare indexes, so do the string comparison.
Vladimir Marko5c6a5872016-06-27 13:50:16 +010062 return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(),
63 sr2.GetStringData()) < 0;
Vladimir Markocac5a7e2016-02-22 10:39:50 +000064 }
65 }
66};
67
68} // namespace art
69
Mathieu Chartierdc00f182016-07-14 10:10:44 -070070#endif // ART_RUNTIME_STRING_REFERENCE_H_