blob: ec759e200d1bc45df564b97483f20e4328b7249f [file] [log] [blame]
Mathieu Chartier8892c6b2018-01-09 15:10:17 -08001/*
2 * Copyright (C) 2018 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 Chartier5e3cfa22018-02-20 16:53:37 -080017#ifndef ART_LIBDEXFILE_DEX_COMPACT_OFFSET_TABLE_H_
18#define ART_LIBDEXFILE_DEX_COMPACT_OFFSET_TABLE_H_
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080019
20#include <cstdint>
21#include <vector>
22
23namespace art {
24
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080025// Compact offset table that aims to minimize size while still providing reasonable speed (10-20ns
26// average time per lookup on host).
27class CompactOffsetTable {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080028 public:
29 // This value is coupled with the leb chunk bitmask. That logic must also be adjusted when the
30 // integer is modified.
31 static constexpr size_t kElementsPerIndex = 16;
32
33 // Leb block format:
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080034 // [uint16_t] 16 bit mask for what indexes actually have a non zero offset for the chunk.
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080035 // [lebs] Up to 16 lebs encoded using leb128, one leb bit. The leb specifies how the offset
36 // changes compared to the previous index.
37
38 class Accessor {
39 public:
Mathieu Chartier2daa1342018-02-20 16:19:28 -080040 // Read the minimum and table offsets from the data pointer.
41 explicit Accessor(const uint8_t* data_begin);
42
43 Accessor(const uint8_t* data_begin, uint32_t minimum_offset, uint32_t table_offset);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080044
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080045 // Return the offset for the index.
46 uint32_t GetOffset(uint32_t index) const;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080047
48 private:
49 const uint32_t* const table_;
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080050 const uint32_t minimum_offset_;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080051 const uint8_t* const data_begin_;
52 };
53
Mathieu Chartier2daa1342018-02-20 16:19:28 -080054 // Version that also serializes the min offset and table offset.
55 static void Build(const std::vector<uint32_t>& offsets, std::vector<uint8_t>* out_data);
56
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080057 // Returned offsets are all relative to out_min_offset.
58 static void Build(const std::vector<uint32_t>& offsets,
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080059 std::vector<uint8_t>* out_data,
60 uint32_t* out_min_offset,
61 uint32_t* out_table_offset);
62
63 // 32 bit aligned for the offset table.
64 static constexpr size_t kAlignment = sizeof(uint32_t);
65};
66
67} // namespace art
68
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080069#endif // ART_LIBDEXFILE_DEX_COMPACT_OFFSET_TABLE_H_