blob: 56e926257310114250c9ed9298dc535edcec5ea4 [file] [log] [blame]
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001/*
2 * Copyright (C) 2015 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#include "type_lookup_table.h"
18
Vladimir Marko9bdf1082016-01-21 12:15:52 +000019#include "base/bit_utils.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030020#include "dex_file-inl.h"
21#include "utf-inl.h"
22#include "utils.h"
23
24#include <memory>
25#include <cstring>
26
27namespace art {
28
29static uint16_t MakeData(uint16_t class_def_idx, uint32_t hash, uint32_t mask) {
30 uint16_t hash_mask = static_cast<uint16_t>(~mask);
31 return (static_cast<uint16_t>(hash) & hash_mask) | class_def_idx;
32}
33
34TypeLookupTable::~TypeLookupTable() {
35 if (!owns_entries_) {
36 // We don't actually own the entries, don't let the unique_ptr release them.
37 entries_.release();
38 }
39}
40
Vladimir Marko9bdf1082016-01-21 12:15:52 +000041uint32_t TypeLookupTable::RawDataLength(uint32_t num_class_defs) {
42 return SupportedSize(num_class_defs) ? RoundUpToPowerOfTwo(num_class_defs) * sizeof(Entry) : 0u;
43}
44
45uint32_t TypeLookupTable::CalculateMask(uint32_t num_class_defs) {
46 return SupportedSize(num_class_defs) ? RoundUpToPowerOfTwo(num_class_defs) - 1u : 0u;
47}
48
49bool TypeLookupTable::SupportedSize(uint32_t num_class_defs) {
50 return num_class_defs != 0u && num_class_defs <= std::numeric_limits<uint16_t>::max();
51}
52
53TypeLookupTable* TypeLookupTable::Create(const DexFile& dex_file, uint8_t* storage) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +030054 const uint32_t num_class_defs = dex_file.NumClassDefs();
Vladimir Marko9bdf1082016-01-21 12:15:52 +000055 return SupportedSize(num_class_defs)
56 ? new TypeLookupTable(dex_file, storage)
57 : nullptr;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030058}
59
David Sehr9aa352e2016-09-15 18:13:52 -070060TypeLookupTable* TypeLookupTable::Open(const uint8_t* dex_file_pointer,
61 const uint8_t* raw_data,
62 uint32_t num_class_defs) {
63 return new TypeLookupTable(dex_file_pointer, raw_data, num_class_defs);
Artem Udovichenkod9786b02015-10-14 16:36:55 +030064}
65
Vladimir Marko9bdf1082016-01-21 12:15:52 +000066TypeLookupTable::TypeLookupTable(const DexFile& dex_file, uint8_t* storage)
David Sehr9aa352e2016-09-15 18:13:52 -070067 : dex_file_begin_(dex_file.Begin()),
68 raw_data_length_(RawDataLength(dex_file.NumClassDefs())),
Vladimir Marko9bdf1082016-01-21 12:15:52 +000069 mask_(CalculateMask(dex_file.NumClassDefs())),
70 entries_(storage != nullptr ? reinterpret_cast<Entry*>(storage) : new Entry[mask_ + 1]),
71 owns_entries_(storage == nullptr) {
72 static_assert(alignof(Entry) == 4u, "Expecting Entry to be 4-byte aligned.");
73 DCHECK_ALIGNED(storage, alignof(Entry));
Artem Udovichenkod9786b02015-10-14 16:36:55 +030074 std::vector<uint16_t> conflict_class_defs;
75 // The first stage. Put elements on their initial positions. If an initial position is already
76 // occupied then delay the insertion of the element to the second stage to reduce probing
77 // distance.
78 for (size_t i = 0; i < dex_file.NumClassDefs(); ++i) {
79 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
80 const DexFile::TypeId& type_id = dex_file.GetTypeId(class_def.class_idx_);
81 const DexFile::StringId& str_id = dex_file.GetStringId(type_id.descriptor_idx_);
82 const uint32_t hash = ComputeModifiedUtf8Hash(dex_file.GetStringData(str_id));
83 Entry entry;
84 entry.str_offset = str_id.string_data_off_;
85 entry.data = MakeData(i, hash, GetSizeMask());
86 if (!SetOnInitialPos(entry, hash)) {
87 conflict_class_defs.push_back(i);
88 }
89 }
90 // The second stage. The initial position of these elements had a collision. Put these elements
91 // into the nearest free cells and link them together by updating next_pos_delta.
92 for (uint16_t class_def_idx : conflict_class_defs) {
93 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx);
94 const DexFile::TypeId& type_id = dex_file.GetTypeId(class_def.class_idx_);
95 const DexFile::StringId& str_id = dex_file.GetStringId(type_id.descriptor_idx_);
96 const uint32_t hash = ComputeModifiedUtf8Hash(dex_file.GetStringData(str_id));
97 Entry entry;
98 entry.str_offset = str_id.string_data_off_;
99 entry.data = MakeData(class_def_idx, hash, GetSizeMask());
100 Insert(entry, hash);
101 }
102}
103
David Sehr9aa352e2016-09-15 18:13:52 -0700104TypeLookupTable::TypeLookupTable(const uint8_t* dex_file_pointer,
105 const uint8_t* raw_data,
106 uint32_t num_class_defs)
107 : dex_file_begin_(dex_file_pointer),
108 raw_data_length_(RawDataLength(num_class_defs)),
109 mask_(CalculateMask(num_class_defs)),
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300110 entries_(reinterpret_cast<Entry*>(const_cast<uint8_t*>(raw_data))),
111 owns_entries_(false) {}
112
113bool TypeLookupTable::SetOnInitialPos(const Entry& entry, uint32_t hash) {
114 const uint32_t pos = hash & GetSizeMask();
115 if (!entries_[pos].IsEmpty()) {
116 return false;
117 }
118 entries_[pos] = entry;
119 entries_[pos].next_pos_delta = 0;
120 return true;
121}
122
123void TypeLookupTable::Insert(const Entry& entry, uint32_t hash) {
124 uint32_t pos = FindLastEntryInBucket(hash & GetSizeMask());
125 uint32_t next_pos = (pos + 1) & GetSizeMask();
126 while (!entries_[next_pos].IsEmpty()) {
127 next_pos = (next_pos + 1) & GetSizeMask();
128 }
129 const uint32_t delta = (next_pos >= pos) ? (next_pos - pos) : (next_pos + Size() - pos);
130 entries_[pos].next_pos_delta = delta;
131 entries_[next_pos] = entry;
132 entries_[next_pos].next_pos_delta = 0;
133}
134
135uint32_t TypeLookupTable::FindLastEntryInBucket(uint32_t pos) const {
136 const Entry* entry = &entries_[pos];
137 while (!entry->IsLast()) {
138 pos = (pos + entry->next_pos_delta) & GetSizeMask();
139 entry = &entries_[pos];
140 }
141 return pos;
142}
143
144} // namespace art