2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Verification-time map of data section items
21 #include "DexDataMap.h"
26 * Allocate and initialize a DexDataMap. Returns NULL on failure.
28 DexDataMap* dexDataMapAlloc(u4 maxCount) {
30 * Allocate a single chunk for the DexDataMap per se as well as the
34 DexDataMap* map = NULL;
37 * Avoiding pulling in safe_iop for safe_iopf.
39 if (!safe_mul(&size, maxCount, sizeof(u4) + sizeof(u2)) ||
40 !safe_add(&size, size, sizeof(DexDataMap))) {
52 map->offsets = (u4*) (map + 1);
53 map->types = (u2*) (map->offsets + maxCount);
61 void dexDataMapFree(DexDataMap* map) {
63 * Since everything got allocated together, everything can be freed
64 * in one fell swoop. Also, free(NULL) is a nop (per spec), so we
65 * don't have to worry about an explicit test for that.
71 * Add a new element to the map. The offset must be greater than the
72 * all previously added offsets.
74 void dexDataMapAdd(DexDataMap* map, u4 offset, u2 type) {
76 assert(map->count < map->max);
78 if ((map->count != 0) &&
79 (map->offsets[map->count - 1] >= offset)) {
80 LOGE("Out-of-order data map offset: 0x%x then 0x%x\n",
81 map->offsets[map->count - 1], offset);
85 map->offsets[map->count] = offset;
86 map->types[map->count] = type;
91 * Get the type associated with the given offset. This returns -1 if
92 * there is no entry for the given offset.
94 int dexDataMapGet(DexDataMap* map, u4 offset) {
97 // Note: Signed type is important for max and min.
99 int max = map->count - 1;
100 u4* offsets = map->offsets;
103 int guessIdx = (min + max) >> 1;
104 u4 guess = offsets[guessIdx];
106 if (offset < guess) {
108 } else if (offset > guess) {
112 return map->types[guessIdx];
121 * Verify that there is an entry in the map, mapping the given offset to
122 * the given type. This will return true if such an entry exists and
123 * return false as well as log an error if not.
125 bool dexDataMapVerify(DexDataMap* map, u4 offset, u2 type) {
126 int found = dexDataMapGet(map, offset);
133 LOGE("No data map entry found @ 0x%x; expected %x\n",
136 LOGE("Unexpected data map entry @ 0x%x: expected %x, found %x\n",
137 offset, type, found);