Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [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_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_ |
| 18 | #define ART_RUNTIME_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 "gc_root.h" |
| 25 | #include "globals.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 26 | #include "mirror/dex_cache.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 27 | #include "primitive.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 31 | inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 32 | const DexFile::Header& header) |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 33 | : pointer_size_(pointer_size), |
| 34 | /* types_offset_ is always 0u, so it's constexpr */ |
Vladimir Marko | 0d4909e | 2016-02-02 20:27:08 +0000 | [diff] [blame] | 35 | methods_offset_( |
| 36 | RoundUp(types_offset_ + TypesSize(header.type_ids_size_), MethodsAlignment())), |
| 37 | strings_offset_( |
| 38 | RoundUp(methods_offset_ + MethodsSize(header.method_ids_size_), StringsAlignment())), |
| 39 | fields_offset_( |
| 40 | RoundUp(strings_offset_ + StringsSize(header.string_ids_size_), FieldsAlignment())), |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 41 | method_types_offset_( |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 42 | RoundUp(fields_offset_ + FieldsSize(header.field_ids_size_), MethodTypesAlignment())), |
Vladimir Marko | 0d4909e | 2016-02-02 20:27:08 +0000 | [diff] [blame] | 43 | size_( |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 44 | RoundUp(method_types_offset_ + MethodTypesSize(header.proto_ids_size_), Alignment())) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 47 | inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size, const DexFile* dex_file) |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 48 | : DexCacheArraysLayout(pointer_size, dex_file->GetHeader()) { |
| 49 | } |
| 50 | |
Vladimir Marko | ec78622 | 2016-12-20 16:24:13 +0000 | [diff] [blame] | 51 | constexpr size_t DexCacheArraysLayout::Alignment() { |
| 52 | // mirror::Type/String/MethodTypeDexCacheType alignment is 8, |
| 53 | // i.e. higher than or equal to the pointer alignment. |
| 54 | static_assert(alignof(mirror::TypeDexCacheType) == 8, |
| 55 | "Expecting alignof(ClassDexCacheType) == 8"); |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 56 | static_assert(alignof(mirror::StringDexCacheType) == 8, |
| 57 | "Expecting alignof(StringDexCacheType) == 8"); |
| 58 | static_assert(alignof(mirror::MethodTypeDexCacheType) == 8, |
| 59 | "Expecting alignof(MethodTypeDexCacheType) == 8"); |
| 60 | // This is the same as alignof(MethodTypeDexCacheType). |
Christina Wadsworth | 9210ce9 | 2016-08-19 13:28:19 -0700 | [diff] [blame] | 61 | return alignof(mirror::StringDexCacheType); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | template <typename T> |
Vladimir Marko | ec78622 | 2016-12-20 16:24:13 +0000 | [diff] [blame] | 65 | constexpr PointerSize GcRootAsPointerSize() { |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 66 | static_assert(sizeof(GcRoot<T>) == 4U, "Unexpected GcRoot size"); |
| 67 | return PointerSize::k32; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 70 | inline size_t DexCacheArraysLayout::TypeOffset(dex::TypeIndex type_idx) const { |
Vladimir Marko | ec78622 | 2016-12-20 16:24:13 +0000 | [diff] [blame] | 71 | return types_offset_ + ElementOffset(PointerSize::k64, |
| 72 | type_idx.index_ % mirror::DexCache::kDexCacheTypeCacheSize); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const { |
Vladimir Marko | ec78622 | 2016-12-20 16:24:13 +0000 | [diff] [blame] | 76 | size_t cache_size = mirror::DexCache::kDexCacheTypeCacheSize; |
| 77 | if (num_elements < cache_size) { |
| 78 | cache_size = num_elements; |
| 79 | } |
| 80 | return ArraySize(PointerSize::k64, cache_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | inline size_t DexCacheArraysLayout::TypesAlignment() const { |
| 84 | return alignof(GcRoot<mirror::Class>); |
| 85 | } |
| 86 | |
| 87 | inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const { |
| 88 | return methods_offset_ + ElementOffset(pointer_size_, method_idx); |
| 89 | } |
| 90 | |
| 91 | inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const { |
Vladimir Marko | 6dd1488 | 2016-10-25 11:51:35 +0100 | [diff] [blame] | 92 | return ArraySize(pointer_size_, num_elements); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | inline size_t DexCacheArraysLayout::MethodsAlignment() const { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 96 | return static_cast<size_t>(pointer_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 100 | return strings_offset_ + ElementOffset(PointerSize::k64, |
| 101 | string_idx % mirror::DexCache::kDexCacheStringCacheSize); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 105 | size_t cache_size = mirror::DexCache::kDexCacheStringCacheSize; |
| 106 | if (num_elements < cache_size) { |
| 107 | cache_size = num_elements; |
| 108 | } |
| 109 | return ArraySize(PointerSize::k64, cache_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | inline size_t DexCacheArraysLayout::StringsAlignment() const { |
Christina Wadsworth | 6353a63 | 2016-08-19 15:58:05 -0700 | [diff] [blame] | 113 | static_assert(alignof(mirror::StringDexCacheType) == 8, |
| 114 | "Expecting alignof(StringDexCacheType) == 8"); |
Christina Wadsworth | 9210ce9 | 2016-08-19 13:28:19 -0700 | [diff] [blame] | 115 | return alignof(mirror::StringDexCacheType); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const { |
| 119 | return fields_offset_ + ElementOffset(pointer_size_, field_idx); |
| 120 | } |
| 121 | |
| 122 | inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const { |
| 123 | return ArraySize(pointer_size_, num_elements); |
| 124 | } |
| 125 | |
| 126 | inline size_t DexCacheArraysLayout::FieldsAlignment() const { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 127 | return static_cast<size_t>(pointer_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 130 | inline size_t DexCacheArraysLayout::MethodTypesSize(size_t num_elements) const { |
| 131 | size_t cache_size = mirror::DexCache::kDexCacheMethodTypeCacheSize; |
| 132 | if (num_elements < cache_size) { |
| 133 | cache_size = num_elements; |
| 134 | } |
| 135 | |
| 136 | return ArraySize(PointerSize::k64, cache_size); |
| 137 | } |
| 138 | |
| 139 | inline size_t DexCacheArraysLayout::MethodTypesAlignment() const { |
| 140 | static_assert(alignof(mirror::MethodTypeDexCacheType) == 8, |
| 141 | "alignof(MethodTypeDexCacheType) != 8"); |
| 142 | return alignof(mirror::MethodTypeDexCacheType); |
| 143 | } |
| 144 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 145 | inline size_t DexCacheArraysLayout::ElementOffset(PointerSize element_size, uint32_t idx) { |
| 146 | return static_cast<size_t>(element_size) * idx; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 149 | inline size_t DexCacheArraysLayout::ArraySize(PointerSize element_size, uint32_t num_elements) { |
| 150 | return static_cast<size_t>(element_size) * num_elements; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } // namespace art |
| 154 | |
| 155 | #endif // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_ |