Home | History | Annotate | Download | only in runtime
      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_METHOD_BSS_MAPPING_H_
     18 #define ART_RUNTIME_METHOD_BSS_MAPPING_H_
     19 
     20 #include "base/bit_utils.h"
     21 #include "base/length_prefixed_array.h"
     22 
     23 namespace art {
     24 
     25 // MethodBssMappingEntry describes a mapping of up to 17 method indexes to their offsets
     26 // in the .bss. The highest index and its associated .bss offset are stored in plain form
     27 // as `method_index` and `bss_offset`, respectively, while the additional indexes can be
     28 // stored in compressed form if their associated .bss entries are consecutive and in the
     29 // method index order. Each of the 16 bits of the `index_mask` corresponds to one of the
     30 // previous 16 method indexes and indicates whether there is a .bss entry for that index.
     31 //
     32 struct MethodBssMappingEntry {
     33   bool CoversIndex(uint32_t method_idx) const {
     34     uint32_t diff = method_index - method_idx;
     35     return (diff == 0) || (diff <= 16 && ((index_mask >> (16u - diff)) & 1u) != 0);
     36   }
     37 
     38   uint32_t GetBssOffset(uint32_t method_idx, size_t entry_size) const {
     39     DCHECK(CoversIndex(method_idx));
     40     uint32_t diff = method_index - method_idx;
     41     if (diff == 0) {
     42       return bss_offset;
     43     } else {
     44       return bss_offset - POPCOUNT(index_mask >> (16u - diff)) * entry_size;
     45     }
     46   }
     47 
     48   uint16_t method_index;
     49   uint16_t index_mask;
     50   uint32_t bss_offset;
     51 };
     52 
     53 using MethodBssMapping = LengthPrefixedArray<MethodBssMappingEntry>;
     54 
     55 }  // namespace art
     56 
     57 #endif  // ART_RUNTIME_METHOD_BSS_MAPPING_H_
     58