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())), |
| 41 | size_( |
| 42 | RoundUp(fields_offset_ + FieldsSize(header.field_ids_size_), Alignment())) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 45 | inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size, const DexFile* dex_file) |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 46 | : DexCacheArraysLayout(pointer_size, dex_file->GetHeader()) { |
| 47 | } |
| 48 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame^] | 49 | inline constexpr size_t DexCacheArraysLayout::Alignment() { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 50 | // GcRoot<> alignment is 4, i.e. lower than or equal to the pointer alignment. |
| 51 | static_assert(alignof(GcRoot<mirror::Class>) == 4, "Expecting alignof(GcRoot<>) == 4"); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame^] | 52 | static_assert(alignof(mirror::StringDexCacheType) == 8, "Expecting alignof(StringDexCacheType) == 8"); |
| 53 | return sizeof(mirror::StringDexCacheType); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | template <typename T> |
| 57 | static constexpr PointerSize GcRootAsPointerSize() { |
| 58 | return ConvertToPointerSize(sizeof(GcRoot<T>)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 62 | return types_offset_ + ElementOffset(GcRootAsPointerSize<mirror::Class>(), type_idx); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 66 | // App image patching relies on having enough room for a forwarding pointer in the types array. |
| 67 | // See FixupArtMethodArrayVisitor and ClassLinker::AddImageSpace. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 68 | return std::max(ArraySize(GcRootAsPointerSize<mirror::Class>(), num_elements), |
| 69 | static_cast<size_t>(pointer_size_)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | inline size_t DexCacheArraysLayout::TypesAlignment() const { |
| 73 | return alignof(GcRoot<mirror::Class>); |
| 74 | } |
| 75 | |
| 76 | inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const { |
| 77 | return methods_offset_ + ElementOffset(pointer_size_, method_idx); |
| 78 | } |
| 79 | |
| 80 | inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 81 | // App image patching relies on having enough room for a forwarding pointer in the methods array. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 82 | return std::max(ArraySize(pointer_size_, num_elements), static_cast<size_t>(pointer_size_)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | inline size_t DexCacheArraysLayout::MethodsAlignment() const { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 86 | return static_cast<size_t>(pointer_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame^] | 90 | return strings_offset_ + ElementOffset(PointerSize::k64, |
| 91 | string_idx % mirror::DexCache::kDexCacheStringCacheSize); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame^] | 95 | size_t cache_size = mirror::DexCache::kDexCacheStringCacheSize; |
| 96 | if (num_elements < cache_size) { |
| 97 | cache_size = num_elements; |
| 98 | } |
| 99 | return ArraySize(PointerSize::k64, cache_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | inline size_t DexCacheArraysLayout::StringsAlignment() const { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame^] | 103 | return alignof(uint64_t); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const { |
| 107 | return fields_offset_ + ElementOffset(pointer_size_, field_idx); |
| 108 | } |
| 109 | |
| 110 | inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const { |
| 111 | return ArraySize(pointer_size_, num_elements); |
| 112 | } |
| 113 | |
| 114 | inline size_t DexCacheArraysLayout::FieldsAlignment() const { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 115 | return static_cast<size_t>(pointer_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 118 | inline size_t DexCacheArraysLayout::ElementOffset(PointerSize element_size, uint32_t idx) { |
| 119 | return static_cast<size_t>(element_size) * idx; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 122 | inline size_t DexCacheArraysLayout::ArraySize(PointerSize element_size, uint32_t num_elements) { |
| 123 | return static_cast<size_t>(element_size) * num_elements; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace art |
| 127 | |
| 128 | #endif // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_ |