blob: 7733a51aa3ab2001faa7061e746675964a162179 [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"
26#include "primitive.h"
27
28namespace art {
29
Andreas Gampe542451c2016-07-26 09:02:02 -070030inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size,
Vladimir Marko09d09432015-09-08 13:47:48 +010031 const DexFile::Header& header)
Vladimir Marko05792b92015-08-03 11:56:49 +010032 : pointer_size_(pointer_size),
33 /* types_offset_ is always 0u, so it's constexpr */
Vladimir Marko0d4909e2016-02-02 20:27:08 +000034 methods_offset_(
35 RoundUp(types_offset_ + TypesSize(header.type_ids_size_), MethodsAlignment())),
36 strings_offset_(
37 RoundUp(methods_offset_ + MethodsSize(header.method_ids_size_), StringsAlignment())),
38 fields_offset_(
39 RoundUp(strings_offset_ + StringsSize(header.string_ids_size_), FieldsAlignment())),
40 size_(
41 RoundUp(fields_offset_ + FieldsSize(header.field_ids_size_), Alignment())) {
Vladimir Marko05792b92015-08-03 11:56:49 +010042}
43
Andreas Gampe542451c2016-07-26 09:02:02 -070044inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size, const DexFile* dex_file)
Vladimir Marko09d09432015-09-08 13:47:48 +010045 : DexCacheArraysLayout(pointer_size, dex_file->GetHeader()) {
46}
47
Vladimir Marko05792b92015-08-03 11:56:49 +010048inline size_t DexCacheArraysLayout::Alignment() const {
49 // GcRoot<> alignment is 4, i.e. lower than or equal to the pointer alignment.
50 static_assert(alignof(GcRoot<mirror::Class>) == 4, "Expecting alignof(GcRoot<>) == 4");
51 static_assert(alignof(GcRoot<mirror::String>) == 4, "Expecting alignof(GcRoot<>) == 4");
Vladimir Marko05792b92015-08-03 11:56:49 +010052 // Pointer alignment is the same as pointer size.
Andreas Gampe542451c2016-07-26 09:02:02 -070053 return static_cast<size_t>(pointer_size_);
54}
55
56template <typename T>
57static constexpr PointerSize GcRootAsPointerSize() {
58 return ConvertToPointerSize(sizeof(GcRoot<T>));
Vladimir Marko05792b92015-08-03 11:56:49 +010059}
60
61inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const {
Andreas Gampe542451c2016-07-26 09:02:02 -070062 return types_offset_ + ElementOffset(GcRootAsPointerSize<mirror::Class>(), type_idx);
Vladimir Marko05792b92015-08-03 11:56:49 +010063}
64
65inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080066 // App image patching relies on having enough room for a forwarding pointer in the types array.
67 // See FixupArtMethodArrayVisitor and ClassLinker::AddImageSpace.
Andreas Gampe542451c2016-07-26 09:02:02 -070068 return std::max(ArraySize(GcRootAsPointerSize<mirror::Class>(), num_elements),
69 static_cast<size_t>(pointer_size_));
Vladimir Marko05792b92015-08-03 11:56:49 +010070}
71
72inline size_t DexCacheArraysLayout::TypesAlignment() const {
73 return alignof(GcRoot<mirror::Class>);
74}
75
76inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const {
77 return methods_offset_ + ElementOffset(pointer_size_, method_idx);
78}
79
80inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const {
Mathieu Chartierfbc31082016-01-24 11:59:56 -080081 // App image patching relies on having enough room for a forwarding pointer in the methods array.
Andreas Gampe542451c2016-07-26 09:02:02 -070082 return std::max(ArraySize(pointer_size_, num_elements), static_cast<size_t>(pointer_size_));
Vladimir Marko05792b92015-08-03 11:56:49 +010083}
84
85inline size_t DexCacheArraysLayout::MethodsAlignment() const {
Andreas Gampe542451c2016-07-26 09:02:02 -070086 return static_cast<size_t>(pointer_size_);
Vladimir Marko05792b92015-08-03 11:56:49 +010087}
88
89inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const {
Andreas Gampe542451c2016-07-26 09:02:02 -070090 return strings_offset_ + ElementOffset(GcRootAsPointerSize<mirror::String>(), string_idx);
Vladimir Marko05792b92015-08-03 11:56:49 +010091}
92
93inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const {
Andreas Gampe542451c2016-07-26 09:02:02 -070094 return ArraySize(GcRootAsPointerSize<mirror::String>(), num_elements);
Vladimir Marko05792b92015-08-03 11:56:49 +010095}
96
97inline size_t DexCacheArraysLayout::StringsAlignment() const {
Vladimir Marko69a04052016-02-02 14:43:28 +000098 return alignof(GcRoot<mirror::String>);
Vladimir Marko05792b92015-08-03 11:56:49 +010099}
100
101inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const {
102 return fields_offset_ + ElementOffset(pointer_size_, field_idx);
103}
104
105inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const {
106 return ArraySize(pointer_size_, num_elements);
107}
108
109inline size_t DexCacheArraysLayout::FieldsAlignment() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700110 return static_cast<size_t>(pointer_size_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100111}
112
Andreas Gampe542451c2016-07-26 09:02:02 -0700113inline size_t DexCacheArraysLayout::ElementOffset(PointerSize element_size, uint32_t idx) {
114 return static_cast<size_t>(element_size) * idx;
Vladimir Marko05792b92015-08-03 11:56:49 +0100115}
116
Andreas Gampe542451c2016-07-26 09:02:02 -0700117inline size_t DexCacheArraysLayout::ArraySize(PointerSize element_size, uint32_t num_elements) {
118 return static_cast<size_t>(element_size) * num_elements;
Vladimir Marko05792b92015-08-03 11:56:49 +0100119}
120
121} // namespace art
122
123#endif // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_