Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 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 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 23 | #include "base/logging.h" |
| 24 | #include "globals.h" |
| 25 | #include "mirror/array-inl.h" |
| 26 | #include "primitive.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 27 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 30 | inline DexCacheArraysLayout::DexCacheArraysLayout(size_t pointer_size, const DexFile* dex_file) |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 31 | : /* types_offset_ is always 0u */ |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 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())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 41 | return types_offset_ + ElementOffset(sizeof(mirror::HeapReference<mirror::Class>), type_idx); |
| 42 | } |
| 43 | |
| 44 | inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const { |
| 45 | return ArraySize(sizeof(mirror::HeapReference<mirror::Class>), num_elements); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 49 | return methods_offset_ + ElementOffset(pointer_size_, method_idx); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 53 | return ArraySize(pointer_size_, num_elements); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 57 | return strings_offset_ + ElementOffset(sizeof(mirror::HeapReference<mirror::String>), string_idx); |
| 58 | } |
| 59 | |
| 60 | inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const { |
| 61 | return ArraySize(sizeof(mirror::HeapReference<mirror::String>), num_elements); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 65 | return fields_offset_ + ElementOffset(pointer_size_, field_idx); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 68 | inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const { |
| 69 | return ArraySize(pointer_size_, num_elements); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 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 | |
| 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)); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 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_ |