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