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