blob: 90e24b96329fd6c4f7859cacd2a5ee8bf36879be [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
Vladimir Marko09d09432015-09-08 13:47:48 +010030inline DexCacheArraysLayout::DexCacheArraysLayout(size_t pointer_size,
31 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 */
34 methods_offset_(types_offset_ +
Vladimir Marko09d09432015-09-08 13:47:48 +010035 RoundUp(TypesSize(header.type_ids_size_), MethodsAlignment())),
Vladimir Marko05792b92015-08-03 11:56:49 +010036 strings_offset_(methods_offset_ +
Vladimir Marko09d09432015-09-08 13:47:48 +010037 RoundUp(MethodsSize(header.method_ids_size_), StringsAlignment())),
Vladimir Marko05792b92015-08-03 11:56:49 +010038 fields_offset_(strings_offset_ +
Vladimir Marko09d09432015-09-08 13:47:48 +010039 RoundUp(StringsSize(header.string_ids_size_), FieldsAlignment())),
Vladimir Marko05792b92015-08-03 11:56:49 +010040 size_(fields_offset_ +
Vladimir Marko09d09432015-09-08 13:47:48 +010041 RoundUp(FieldsSize(header.field_ids_size_), Alignment())) {
Vladimir Marko05792b92015-08-03 11:56:49 +010042 DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
43}
44
Vladimir Marko09d09432015-09-08 13:47:48 +010045inline DexCacheArraysLayout::DexCacheArraysLayout(size_t pointer_size, const DexFile* dex_file)
46 : DexCacheArraysLayout(pointer_size, dex_file->GetHeader()) {
47}
48
Vladimir Marko05792b92015-08-03 11:56:49 +010049inline size_t DexCacheArraysLayout::Alignment() const {
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");
52 static_assert(alignof(GcRoot<mirror::String>) == 4, "Expecting alignof(GcRoot<>) == 4");
53 DCHECK(pointer_size_ == 4u || pointer_size_ == 8u);
54 // Pointer alignment is the same as pointer size.
55 return pointer_size_;
56}
57
58inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const {
59 return types_offset_ + ElementOffset(sizeof(GcRoot<mirror::Class>), type_idx);
60}
61
62inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const {
63 return ArraySize(sizeof(GcRoot<mirror::Class>), num_elements);
64}
65
66inline size_t DexCacheArraysLayout::TypesAlignment() const {
67 return alignof(GcRoot<mirror::Class>);
68}
69
70inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const {
71 return methods_offset_ + ElementOffset(pointer_size_, method_idx);
72}
73
74inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const {
75 return ArraySize(pointer_size_, num_elements);
76}
77
78inline size_t DexCacheArraysLayout::MethodsAlignment() const {
79 return pointer_size_;
80}
81
82inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const {
83 return strings_offset_ + ElementOffset(sizeof(GcRoot<mirror::String>), string_idx);
84}
85
86inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const {
87 return ArraySize(sizeof(GcRoot<mirror::String>), num_elements);
88}
89
90inline size_t DexCacheArraysLayout::StringsAlignment() const {
91 return alignof(GcRoot<mirror::String>);
92}
93
94inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const {
95 return fields_offset_ + ElementOffset(pointer_size_, field_idx);
96}
97
98inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const {
99 return ArraySize(pointer_size_, num_elements);
100}
101
102inline size_t DexCacheArraysLayout::FieldsAlignment() const {
103 return pointer_size_;
104}
105
106inline size_t DexCacheArraysLayout::ElementOffset(size_t element_size, uint32_t idx) {
107 return element_size * idx;
108}
109
110inline size_t DexCacheArraysLayout::ArraySize(size_t element_size, uint32_t num_elements) {
111 return element_size * num_elements;
112}
113
114} // namespace art
115
116#endif // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_