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