1 /*
2  * Copyright (C) 2015 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_COMPILER_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_
18 #define ART_COMPILER_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_
19 
20 #include "dex_cache_arrays_layout.h"
21 
22 #include "base/bit_utils.h"
23 #include "base/logging.h"
24 #include "globals.h"
25 #include "mirror/array-inl.h"
26 #include "primitive.h"
27 
28 namespace art {
29 
DexCacheArraysLayout(size_t pointer_size,const DexFile * dex_file)30 inline DexCacheArraysLayout::DexCacheArraysLayout(size_t pointer_size, const DexFile* dex_file)
31     : /* types_offset_ is always 0u */
32       pointer_size_(pointer_size),
33       methods_offset_(types_offset_ + TypesSize(dex_file->NumTypeIds())),
34       strings_offset_(methods_offset_ + MethodsSize(dex_file->NumMethodIds())),
35       fields_offset_(strings_offset_ + StringsSize(dex_file->NumStringIds())),
36       size_(fields_offset_ + FieldsSize(dex_file->NumFieldIds())) {
37   DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
38 }
39 
TypeOffset(uint32_t type_idx)40 inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const {
41   return types_offset_ + ElementOffset(sizeof(mirror::HeapReference<mirror::Class>), type_idx);
42 }
43 
TypesSize(size_t num_elements)44 inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const {
45   return ArraySize(sizeof(mirror::HeapReference<mirror::Class>), num_elements);
46 }
47 
MethodOffset(uint32_t method_idx)48 inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const {
49   return methods_offset_ + ElementOffset(pointer_size_, method_idx);
50 }
51 
MethodsSize(size_t num_elements)52 inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const {
53   return ArraySize(pointer_size_, num_elements);
54 }
55 
StringOffset(uint32_t string_idx)56 inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const {
57   return strings_offset_ + ElementOffset(sizeof(mirror::HeapReference<mirror::String>), string_idx);
58 }
59 
StringsSize(size_t num_elements)60 inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const {
61   return ArraySize(sizeof(mirror::HeapReference<mirror::String>), num_elements);
62 }
63 
FieldOffset(uint32_t field_idx)64 inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const {
65   return fields_offset_ + ElementOffset(pointer_size_, field_idx);
66 }
67 
FieldsSize(size_t num_elements)68 inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const {
69   return ArraySize(pointer_size_, num_elements);
70 }
71 
ElementOffset(size_t element_size,uint32_t idx)72 inline size_t DexCacheArraysLayout::ElementOffset(size_t element_size, uint32_t idx) {
73   return mirror::Array::DataOffset(element_size).Uint32Value() + element_size * idx;
74 }
75 
ArraySize(size_t element_size,uint32_t num_elements)76 inline size_t DexCacheArraysLayout::ArraySize(size_t element_size, uint32_t num_elements) {
77   size_t array_size = mirror::ComputeArraySize(num_elements, ComponentSizeShiftWidth(element_size));
78   DCHECK_NE(array_size, 0u);  // No overflow expected for dex cache arrays.
79   return RoundUp(array_size, kObjectAlignment);
80 }
81 
82 }  // namespace art
83 
84 #endif  // ART_COMPILER_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_
85