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 { |
| 29 | class ArtField; |
| 30 | class ArtMethod; |
| 31 | class Class; |
| 32 | class String; |
| 33 | } // namespace mirror |
| 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | inline DexCacheArraysLayout::DexCacheArraysLayout(const DexFile* dex_file) |
| 38 | : /* types_offset_ is always 0u */ |
| 39 | methods_offset_(types_offset_ + ArraySize<mirror::Class>(dex_file->NumTypeIds())), |
| 40 | strings_offset_(methods_offset_ + ArraySize<mirror::ArtMethod>(dex_file->NumMethodIds())), |
| 41 | fields_offset_(strings_offset_ + ArraySize<mirror::String>(dex_file->NumStringIds())), |
| 42 | size_(fields_offset_ + ArraySize<mirror::ArtField>(dex_file->NumFieldIds())) { |
| 43 | } |
| 44 | |
| 45 | inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const { |
| 46 | return types_offset_ + ElementOffset<mirror::Class>(type_idx); |
| 47 | } |
| 48 | |
| 49 | inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const { |
| 50 | return methods_offset_ + ElementOffset<mirror::ArtMethod>(method_idx); |
| 51 | } |
| 52 | |
| 53 | inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const { |
| 54 | return strings_offset_ + ElementOffset<mirror::String>(string_idx); |
| 55 | } |
| 56 | |
| 57 | inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const { |
| 58 | return fields_offset_ + ElementOffset<mirror::ArtField>(field_idx); |
| 59 | } |
| 60 | |
| 61 | template <typename MirrorType> |
| 62 | inline size_t DexCacheArraysLayout::ElementOffset(uint32_t idx) { |
| 63 | return mirror::Array::DataOffset(sizeof(mirror::HeapReference<MirrorType>)).Uint32Value() + |
| 64 | sizeof(mirror::HeapReference<MirrorType>) * idx; |
| 65 | } |
| 66 | |
| 67 | template <typename MirrorType> |
| 68 | inline size_t DexCacheArraysLayout::ArraySize(uint32_t num_elements) { |
| 69 | size_t array_size = mirror::ComputeArraySize( |
| 70 | num_elements, ComponentSizeShiftWidth<sizeof(mirror::HeapReference<MirrorType>)>()); |
| 71 | DCHECK_NE(array_size, 0u); // No overflow expected for dex cache arrays. |
| 72 | return RoundUp(array_size, kObjectAlignment); |
| 73 | } |
| 74 | |
| 75 | } // namespace art |
| 76 | |
| 77 | #endif // ART_COMPILER_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_ |