blob: f9a14053547f20fbbc86f07a7f18649fe1f993a5 [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,
Orion Hodsonc069a302017-01-18 09:23:12 +000032 const DexFile::Header& header,
33 uint32_t num_call_sites)
Vladimir Marko05792b92015-08-03 11:56:49 +010034 : pointer_size_(pointer_size),
35 /* types_offset_ is always 0u, so it's constexpr */
Vladimir Marko0d4909e2016-02-02 20:27:08 +000036 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 Kamath25352fc2016-08-03 12:46:58 +010042 method_types_offset_(
Narayan Kamath7fe56582016-10-14 18:49:12 +010043 RoundUp(fields_offset_ + FieldsSize(header.field_ids_size_), MethodTypesAlignment())),
Orion Hodsonc069a302017-01-18 09:23:12 +000044 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 Marko05792b92015-08-03 11:56:49 +010048}
49
Andreas Gampe542451c2016-07-26 09:02:02 -070050inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size, const DexFile* dex_file)
Orion Hodsonc069a302017-01-18 09:23:12 +000051 : DexCacheArraysLayout(pointer_size, dex_file->GetHeader(), dex_file->NumCallSiteIds()) {
Vladimir Marko09d09432015-09-08 13:47:48 +010052}
53
Vladimir Marko8d6768d2017-03-14 10:13:21 +000054constexpr size_t DexCacheArraysLayout::Alignment() {
55 // mirror::Type/String/MethodTypeDexCacheType alignment is 8,
56 // i.e. higher than or equal to the pointer alignment.
57 static_assert(alignof(mirror::TypeDexCacheType) == 8,
58 "Expecting alignof(ClassDexCacheType) == 8");
Narayan Kamath7fe56582016-10-14 18:49:12 +010059 static_assert(alignof(mirror::StringDexCacheType) == 8,
60 "Expecting alignof(StringDexCacheType) == 8");
61 static_assert(alignof(mirror::MethodTypeDexCacheType) == 8,
62 "Expecting alignof(MethodTypeDexCacheType) == 8");
Vladimir Marko6374c582017-03-13 14:51:19 +000063 // This is the same as alignof(MethodTypeDexCacheType).
64 return alignof(mirror::StringDexCacheType);
Andreas Gampe542451c2016-07-26 09:02:02 -070065}
66
67template <typename T>
Vladimir Marko8d6768d2017-03-14 10:13:21 +000068constexpr PointerSize GcRootAsPointerSize() {
Andreas Gampebda1d602016-08-29 17:43:45 -070069 static_assert(sizeof(GcRoot<T>) == 4U, "Unexpected GcRoot size");
70 return PointerSize::k32;
Vladimir Marko05792b92015-08-03 11:56:49 +010071}
72
Andreas Gampea5b09a62016-11-17 15:21:22 -080073inline size_t DexCacheArraysLayout::TypeOffset(dex::TypeIndex type_idx) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +000074 return types_offset_ + ElementOffset(PointerSize::k64,
75 type_idx.index_ % mirror::DexCache::kDexCacheTypeCacheSize);
Vladimir Marko05792b92015-08-03 11:56:49 +010076}
77
78inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +000079 size_t cache_size = mirror::DexCache::kDexCacheTypeCacheSize;
80 if (num_elements < cache_size) {
81 cache_size = num_elements;
82 }
83 return ArraySize(PointerSize::k64, cache_size);
Vladimir Marko05792b92015-08-03 11:56:49 +010084}
85
86inline size_t DexCacheArraysLayout::TypesAlignment() const {
87 return alignof(GcRoot<mirror::Class>);
88}
89
90inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const {
91 return methods_offset_ + ElementOffset(pointer_size_, method_idx);
92}
93
94inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const {
Vladimir Marko6dd14882016-10-25 11:51:35 +010095 return ArraySize(pointer_size_, num_elements);
Vladimir Marko05792b92015-08-03 11:56:49 +010096}
97
98inline size_t DexCacheArraysLayout::MethodsAlignment() const {
Andreas Gampe542451c2016-07-26 09:02:02 -070099 return static_cast<size_t>(pointer_size_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100100}
101
102inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const {
Vladimir Marko6374c582017-03-13 14:51:19 +0000103 return strings_offset_ + ElementOffset(PointerSize::k64,
104 string_idx % mirror::DexCache::kDexCacheStringCacheSize);
Vladimir Marko05792b92015-08-03 11:56:49 +0100105}
106
107inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700108 size_t cache_size = mirror::DexCache::kDexCacheStringCacheSize;
109 if (num_elements < cache_size) {
110 cache_size = num_elements;
111 }
112 return ArraySize(PointerSize::k64, cache_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100113}
114
115inline size_t DexCacheArraysLayout::StringsAlignment() const {
Christina Wadsworth6353a632016-08-19 15:58:05 -0700116 static_assert(alignof(mirror::StringDexCacheType) == 8,
117 "Expecting alignof(StringDexCacheType) == 8");
Christina Wadsworth9210ce92016-08-19 13:28:19 -0700118 return alignof(mirror::StringDexCacheType);
Vladimir Marko05792b92015-08-03 11:56:49 +0100119}
120
121inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const {
Vladimir Marko6374c582017-03-13 14:51:19 +0000122 return fields_offset_ + ElementOffset(pointer_size_, field_idx);
Vladimir Marko05792b92015-08-03 11:56:49 +0100123}
124
125inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const {
Vladimir Marko6374c582017-03-13 14:51:19 +0000126 return ArraySize(pointer_size_, num_elements);
Vladimir Marko05792b92015-08-03 11:56:49 +0100127}
128
129inline size_t DexCacheArraysLayout::FieldsAlignment() const {
Vladimir Marko6374c582017-03-13 14:51:19 +0000130 return static_cast<size_t>(pointer_size_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100131}
132
Narayan Kamath25352fc2016-08-03 12:46:58 +0100133inline size_t DexCacheArraysLayout::MethodTypesSize(size_t num_elements) const {
134 size_t cache_size = mirror::DexCache::kDexCacheMethodTypeCacheSize;
135 if (num_elements < cache_size) {
136 cache_size = num_elements;
137 }
138
139 return ArraySize(PointerSize::k64, cache_size);
140}
141
142inline size_t DexCacheArraysLayout::MethodTypesAlignment() const {
143 static_assert(alignof(mirror::MethodTypeDexCacheType) == 8,
Orion Hodsonc069a302017-01-18 09:23:12 +0000144 "Expecting alignof(MethodTypeDexCacheType) == 8");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100145 return alignof(mirror::MethodTypeDexCacheType);
146}
147
Orion Hodsonc069a302017-01-18 09:23:12 +0000148inline size_t DexCacheArraysLayout::CallSitesSize(size_t num_elements) const {
149 return ArraySize(GcRootAsPointerSize<mirror::CallSite>(), num_elements);
150}
151
152inline size_t DexCacheArraysLayout::CallSitesAlignment() const {
153 return alignof(GcRoot<mirror::CallSite>);
154}
155
Andreas Gampe542451c2016-07-26 09:02:02 -0700156inline size_t DexCacheArraysLayout::ElementOffset(PointerSize element_size, uint32_t idx) {
157 return static_cast<size_t>(element_size) * idx;
Vladimir Marko05792b92015-08-03 11:56:49 +0100158}
159
Andreas Gampe542451c2016-07-26 09:02:02 -0700160inline size_t DexCacheArraysLayout::ArraySize(PointerSize element_size, uint32_t num_elements) {
161 return static_cast<size_t>(element_size) * num_elements;
Vladimir Marko05792b92015-08-03 11:56:49 +0100162}
163
164} // namespace art
165
166#endif // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_