blob: 4f662d5a8f87844447650608bb5031d2a84d964b [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
30inline DexCacheArraysLayout::DexCacheArraysLayout(size_t pointer_size, const DexFile* dex_file)
31 : pointer_size_(pointer_size),
32 /* types_offset_ is always 0u, so it's constexpr */
33 methods_offset_(types_offset_ +
34 RoundUp(TypesSize(dex_file->NumTypeIds()), MethodsAlignment())),
35 strings_offset_(methods_offset_ +
36 RoundUp(MethodsSize(dex_file->NumMethodIds()), StringsAlignment())),
37 fields_offset_(strings_offset_ +
38 RoundUp(StringsSize(dex_file->NumStringIds()), FieldsAlignment())),
39 size_(fields_offset_ +
40 RoundUp(FieldsSize(dex_file->NumFieldIds()), Alignment())) {
41 DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
42}
43
44inline size_t DexCacheArraysLayout::Alignment() const {
45 // GcRoot<> alignment is 4, i.e. lower than or equal to the pointer alignment.
46 static_assert(alignof(GcRoot<mirror::Class>) == 4, "Expecting alignof(GcRoot<>) == 4");
47 static_assert(alignof(GcRoot<mirror::String>) == 4, "Expecting alignof(GcRoot<>) == 4");
48 DCHECK(pointer_size_ == 4u || pointer_size_ == 8u);
49 // Pointer alignment is the same as pointer size.
50 return pointer_size_;
51}
52
53inline size_t DexCacheArraysLayout::TypeOffset(uint32_t type_idx) const {
54 return types_offset_ + ElementOffset(sizeof(GcRoot<mirror::Class>), type_idx);
55}
56
57inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const {
58 return ArraySize(sizeof(GcRoot<mirror::Class>), num_elements);
59}
60
61inline size_t DexCacheArraysLayout::TypesAlignment() const {
62 return alignof(GcRoot<mirror::Class>);
63}
64
65inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const {
66 return methods_offset_ + ElementOffset(pointer_size_, method_idx);
67}
68
69inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const {
70 return ArraySize(pointer_size_, num_elements);
71}
72
73inline size_t DexCacheArraysLayout::MethodsAlignment() const {
74 return pointer_size_;
75}
76
77inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const {
78 return strings_offset_ + ElementOffset(sizeof(GcRoot<mirror::String>), string_idx);
79}
80
81inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const {
82 return ArraySize(sizeof(GcRoot<mirror::String>), num_elements);
83}
84
85inline size_t DexCacheArraysLayout::StringsAlignment() const {
86 return alignof(GcRoot<mirror::String>);
87}
88
89inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const {
90 return fields_offset_ + ElementOffset(pointer_size_, field_idx);
91}
92
93inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const {
94 return ArraySize(pointer_size_, num_elements);
95}
96
97inline size_t DexCacheArraysLayout::FieldsAlignment() const {
98 return pointer_size_;
99}
100
101inline size_t DexCacheArraysLayout::ElementOffset(size_t element_size, uint32_t idx) {
102 return element_size * idx;
103}
104
105inline size_t DexCacheArraysLayout::ArraySize(size_t element_size, uint32_t num_elements) {
106 return element_size * num_elements;
107}
108
109} // namespace art
110
111#endif // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_