blob: 5ccd446e0fbb93d625fe87e90282dca26e104f97 [file] [log] [blame]
Vladimir Marko05792b92015-08-03 11:56:49 +01001/*
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 Wadsworthbf44e0e2016-08-18 10:37:42 -070026#include "mirror/dex_cache.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010027#include "primitive.h"
28
29namespace art {
30
Andreas Gampe542451c2016-07-26 09:02:02 -070031inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size,
Vladimir Marko09d09432015-09-08 13:47:48 +010032 const DexFile::Header& header)
Vladimir Marko05792b92015-08-03 11:56:49 +010033 : pointer_size_(pointer_size),
34 /* types_offset_ is always 0u, so it's constexpr */
Vladimir Marko0d4909e2016-02-02 20:27:08 +000035 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 Kamath25352fc2016-08-03 12:46:58 +010041 method_types_offset_(
42 RoundUp(fields_offset_ + FieldsSize(header.field_ids_size_), Alignment())),
Vladimir Marko0d4909e2016-02-02 20:27:08 +000043 size_(
Narayan Kamath25352fc2016-08-03 12:46:58 +010044 RoundUp(method_types_offset_ + MethodTypesSize(header.proto_ids_size_), Alignment())) {
Vladimir Marko05792b92015-08-03 11:56:49 +010045}
46
Andreas Gampe542451c2016-07-26 09:02:02 -070047inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size, const DexFile* dex_file)
Vladimir Marko09d09432015-09-08 13:47:48 +010048 : DexCacheArraysLayout(pointer_size, dex_file->GetHeader()) {
49}
50
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070051inline constexpr size_t DexCacheArraysLayout::Alignment() {
Vladimir Marko05792b92015-08-03 11:56:49 +010052 // GcRoot<> alignment is 4, i.e. lower than or equal to the pointer alignment.
53 static_assert(alignof(GcRoot<mirror::Class>) == 4, "Expecting alignof(GcRoot<>) == 4");
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070054 static_assert(alignof(mirror::StringDexCacheType) == 8, "Expecting alignof(StringDexCacheType) == 8");
Christina Wadsworth9210ce92016-08-19 13:28:19 -070055 return alignof(mirror::StringDexCacheType);
Andreas Gampe542451c2016-07-26 09:02:02 -070056}
57
58template <typename T>
59static constexpr PointerSize GcRootAsPointerSize() {
Andreas Gampebda1d602016-08-29 17:43:45 -070060 static_assert(sizeof(GcRoot<T>) == 4U, "Unexpected GcRoot size");
61 return PointerSize::k32;
Vladimir Marko05792b92015-08-03 11:56:49 +010062}
63
64inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const {
Andreas Gampe542451c2016-07-26 09:02:02 -070065 return types_offset_ + ElementOffset(GcRootAsPointerSize<mirror::Class>(), type_idx);
Vladimir Marko05792b92015-08-03 11:56:49 +010066}
67
68inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080069 // App image patching relies on having enough room for a forwarding pointer in the types array.
70 // See FixupArtMethodArrayVisitor and ClassLinker::AddImageSpace.
Andreas Gampe542451c2016-07-26 09:02:02 -070071 return std::max(ArraySize(GcRootAsPointerSize<mirror::Class>(), num_elements),
72 static_cast<size_t>(pointer_size_));
Vladimir Marko05792b92015-08-03 11:56:49 +010073}
74
75inline size_t DexCacheArraysLayout::TypesAlignment() const {
76 return alignof(GcRoot<mirror::Class>);
77}
78
79inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const {
80 return methods_offset_ + ElementOffset(pointer_size_, method_idx);
81}
82
83inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080084 // App image patching relies on having enough room for a forwarding pointer in the methods array.
Andreas Gampe542451c2016-07-26 09:02:02 -070085 return std::max(ArraySize(pointer_size_, num_elements), static_cast<size_t>(pointer_size_));
Vladimir Marko05792b92015-08-03 11:56:49 +010086}
87
88inline size_t DexCacheArraysLayout::MethodsAlignment() const {
Andreas Gampe542451c2016-07-26 09:02:02 -070089 return static_cast<size_t>(pointer_size_);
Vladimir Marko05792b92015-08-03 11:56:49 +010090}
91
92inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070093 return strings_offset_ + ElementOffset(PointerSize::k64,
94 string_idx % mirror::DexCache::kDexCacheStringCacheSize);
Vladimir Marko05792b92015-08-03 11:56:49 +010095}
96
97inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070098 size_t cache_size = mirror::DexCache::kDexCacheStringCacheSize;
99 if (num_elements < cache_size) {
100 cache_size = num_elements;
101 }
102 return ArraySize(PointerSize::k64, cache_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100103}
104
105inline size_t DexCacheArraysLayout::StringsAlignment() const {
Christina Wadsworth6353a632016-08-19 15:58:05 -0700106 static_assert(alignof(mirror::StringDexCacheType) == 8,
107 "Expecting alignof(StringDexCacheType) == 8");
Christina Wadsworth9210ce92016-08-19 13:28:19 -0700108 return alignof(mirror::StringDexCacheType);
Vladimir Marko05792b92015-08-03 11:56:49 +0100109}
110
111inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const {
112 return fields_offset_ + ElementOffset(pointer_size_, field_idx);
113}
114
115inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const {
116 return ArraySize(pointer_size_, num_elements);
117}
118
119inline size_t DexCacheArraysLayout::FieldsAlignment() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700120 return static_cast<size_t>(pointer_size_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100121}
122
Narayan Kamath25352fc2016-08-03 12:46:58 +0100123inline size_t DexCacheArraysLayout::MethodTypeOffset(uint32_t proto_idx) const {
124 return strings_offset_
125 + ElementOffset(PointerSize::k64,
126 proto_idx % mirror::DexCache::kDexCacheMethodTypeCacheSize);
127}
128
129inline size_t DexCacheArraysLayout::MethodTypesSize(size_t num_elements) const {
130 size_t cache_size = mirror::DexCache::kDexCacheMethodTypeCacheSize;
131 if (num_elements < cache_size) {
132 cache_size = num_elements;
133 }
134
135 return ArraySize(PointerSize::k64, cache_size);
136}
137
138inline size_t DexCacheArraysLayout::MethodTypesAlignment() const {
139 static_assert(alignof(mirror::MethodTypeDexCacheType) == 8,
140 "alignof(MethodTypeDexCacheType) != 8");
141 return alignof(mirror::MethodTypeDexCacheType);
142}
143
Andreas Gampe542451c2016-07-26 09:02:02 -0700144inline size_t DexCacheArraysLayout::ElementOffset(PointerSize element_size, uint32_t idx) {
145 return static_cast<size_t>(element_size) * idx;
Vladimir Marko05792b92015-08-03 11:56:49 +0100146}
147
Andreas Gampe542451c2016-07-26 09:02:02 -0700148inline size_t DexCacheArraysLayout::ArraySize(PointerSize element_size, uint32_t num_elements) {
149 return static_cast<size_t>(element_size) * num_elements;
Vladimir Marko05792b92015-08-03 11:56:49 +0100150}
151
152} // namespace art
153
154#endif // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_