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