blob: dcbc05c195ff717488b7d88eb4c2629fa8e5b50f [file] [log] [blame]
Vladimir Markof3c52b42017-11-17 17:32:12 +00001/*
2 * Copyright (C) 2017 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_RUNTIME_INDEX_BSS_MAPPING_H_
18#define ART_RUNTIME_INDEX_BSS_MAPPING_H_
19
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21
Vladimir Markof3c52b42017-11-17 17:32:12 +000022#include "base/bit_utils.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000023
24namespace art {
25
26template<typename T> class LengthPrefixedArray;
27
28// IndexBssMappingEntry describes a mapping of one or more indexes to their offsets in the .bss.
29// A sorted array of IndexBssMappingEntry is used to describe the mapping of method indexes,
30// type indexes or string indexes to offsets of their assigned slots in the .bss.
31//
32// The highest index and a mask are stored in a single `uint32_t index_and_mask` and the split
33// between the index and the mask is provided externally. The "mask" bits specify whether some
34// of the previous indexes are mapped to immediately preceding slots. This is permissible only
35// if the slots are consecutive and in the same order as indexes.
36//
37// The .bss offset of the slot associated with the highest index is stored in plain form as
38// `bss_offset`. If the mask specifies any smaller indexes being mapped to immediately
39// preceding slots, their offsets are calculated using an externally supplied size of the slot.
40struct IndexBssMappingEntry {
41 static size_t IndexBits(uint32_t number_of_indexes) {
42 DCHECK_NE(number_of_indexes, 0u);
43 return MinimumBitsToStore(number_of_indexes - 1u);
44 }
45
46 static uint32_t IndexMask(size_t index_bits) {
47 DCHECK_LE(index_bits, 32u);
48 constexpr uint32_t kAllOnes = static_cast<uint32_t>(-1);
49 // Handle `index_bits == 32u` explicitly; shifting uint32_t left by 32 is undefined behavior.
50 return (index_bits == 32u) ? kAllOnes : ~(kAllOnes << index_bits);
51 }
52
53 uint32_t GetIndex(size_t index_bits) const {
54 return index_and_mask & IndexMask(index_bits);
55 }
56
57 uint32_t GetMask(size_t index_bits) const {
58 DCHECK_LT(index_bits, 32u); // GetMask() is valid only if there is at least 1 mask bit.
59 return index_and_mask >> index_bits;
60 }
61
62 size_t GetBssOffset(size_t index_bits, uint32_t index, size_t slot_size) const;
63
64 uint32_t index_and_mask;
65 uint32_t bss_offset;
66};
67
68using IndexBssMapping = LengthPrefixedArray<IndexBssMappingEntry>;
69
70class IndexBssMappingLookup {
71 public:
72 static constexpr size_t npos = static_cast<size_t>(-1);
73
74 static size_t GetBssOffset(const IndexBssMapping* mapping,
75 uint32_t index,
76 uint32_t number_of_indexes,
77 size_t slot_size);
78};
79
80} // namespace art
81
82#endif // ART_RUNTIME_INDEX_BSS_MAPPING_H_