• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "mirror/dex_cache.h"
27 #include "primitive.h"
28 
29 namespace art {
30 
DexCacheArraysLayout(PointerSize pointer_size,const DexFile::Header & header,uint32_t num_call_sites)31 inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size,
32                                                   const DexFile::Header& header,
33                                                   uint32_t num_call_sites)
34     : pointer_size_(pointer_size),
35       /* types_offset_ is always 0u, so it's constexpr */
36       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())),
42       method_types_offset_(
43           RoundUp(fields_offset_ + FieldsSize(header.field_ids_size_), MethodTypesAlignment())),
44     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())) {
48 }
49 
DexCacheArraysLayout(PointerSize pointer_size,const DexFile * dex_file)50 inline DexCacheArraysLayout::DexCacheArraysLayout(PointerSize pointer_size, const DexFile* dex_file)
51     : DexCacheArraysLayout(pointer_size, dex_file->GetHeader(), dex_file->NumCallSiteIds()) {
52 }
53 
Alignment()54 inline size_t DexCacheArraysLayout::Alignment() const {
55   return Alignment(pointer_size_);
56 }
57 
Alignment(PointerSize pointer_size)58 inline constexpr size_t DexCacheArraysLayout::Alignment(PointerSize pointer_size) {
59   // mirror::Type/String/MethodTypeDexCacheType alignment is 8,
60   // i.e. higher than or equal to the pointer alignment.
61   static_assert(alignof(mirror::TypeDexCacheType) == 8,
62                 "Expecting alignof(ClassDexCacheType) == 8");
63   static_assert(alignof(mirror::StringDexCacheType) == 8,
64                 "Expecting alignof(StringDexCacheType) == 8");
65   static_assert(alignof(mirror::MethodTypeDexCacheType) == 8,
66                 "Expecting alignof(MethodTypeDexCacheType) == 8");
67   // This is the same as alignof({Field,Method}DexCacheType) for the given pointer size.
68   return 2u * static_cast<size_t>(pointer_size);
69 }
70 
71 template <typename T>
GcRootAsPointerSize()72 constexpr PointerSize GcRootAsPointerSize() {
73   static_assert(sizeof(GcRoot<T>) == 4U, "Unexpected GcRoot size");
74   return PointerSize::k32;
75 }
76 
TypeOffset(dex::TypeIndex type_idx)77 inline size_t DexCacheArraysLayout::TypeOffset(dex::TypeIndex type_idx) const {
78   return types_offset_ + ElementOffset(PointerSize::k64,
79                                        type_idx.index_ % mirror::DexCache::kDexCacheTypeCacheSize);
80 }
81 
TypesSize(size_t num_elements)82 inline size_t DexCacheArraysLayout::TypesSize(size_t num_elements) const {
83   size_t cache_size = mirror::DexCache::kDexCacheTypeCacheSize;
84   if (num_elements < cache_size) {
85     cache_size = num_elements;
86   }
87   return PairArraySize(GcRootAsPointerSize<mirror::Class>(), cache_size);
88 }
89 
TypesAlignment()90 inline size_t DexCacheArraysLayout::TypesAlignment() const {
91   return alignof(GcRoot<mirror::Class>);
92 }
93 
MethodOffset(uint32_t method_idx)94 inline size_t DexCacheArraysLayout::MethodOffset(uint32_t method_idx) const {
95   return methods_offset_ + ElementOffset(pointer_size_, method_idx);
96 }
97 
MethodsSize(size_t num_elements)98 inline size_t DexCacheArraysLayout::MethodsSize(size_t num_elements) const {
99   size_t cache_size = mirror::DexCache::kDexCacheMethodCacheSize;
100   if (num_elements < cache_size) {
101     cache_size = num_elements;
102   }
103   return PairArraySize(pointer_size_, cache_size);
104 }
105 
MethodsAlignment()106 inline size_t DexCacheArraysLayout::MethodsAlignment() const {
107   return 2u * static_cast<size_t>(pointer_size_);
108 }
109 
StringOffset(uint32_t string_idx)110 inline size_t DexCacheArraysLayout::StringOffset(uint32_t string_idx) const {
111   uint32_t string_hash = string_idx % mirror::DexCache::kDexCacheStringCacheSize;
112   return strings_offset_ + ElementOffset(PointerSize::k64, string_hash);
113 }
114 
StringsSize(size_t num_elements)115 inline size_t DexCacheArraysLayout::StringsSize(size_t num_elements) const {
116   size_t cache_size = mirror::DexCache::kDexCacheStringCacheSize;
117   if (num_elements < cache_size) {
118     cache_size = num_elements;
119   }
120   return PairArraySize(GcRootAsPointerSize<mirror::String>(), cache_size);
121 }
122 
StringsAlignment()123 inline size_t DexCacheArraysLayout::StringsAlignment() const {
124   static_assert(alignof(mirror::StringDexCacheType) == 8,
125                 "Expecting alignof(StringDexCacheType) == 8");
126   return alignof(mirror::StringDexCacheType);
127 }
128 
FieldOffset(uint32_t field_idx)129 inline size_t DexCacheArraysLayout::FieldOffset(uint32_t field_idx) const {
130   uint32_t field_hash = field_idx % mirror::DexCache::kDexCacheFieldCacheSize;
131   return fields_offset_ + 2u * static_cast<size_t>(pointer_size_) * field_hash;
132 }
133 
FieldsSize(size_t num_elements)134 inline size_t DexCacheArraysLayout::FieldsSize(size_t num_elements) const {
135   size_t cache_size = mirror::DexCache::kDexCacheFieldCacheSize;
136   if (num_elements < cache_size) {
137     cache_size = num_elements;
138   }
139   return PairArraySize(pointer_size_, cache_size);
140 }
141 
FieldsAlignment()142 inline size_t DexCacheArraysLayout::FieldsAlignment() const {
143   return 2u * static_cast<size_t>(pointer_size_);
144 }
145 
MethodTypesSize(size_t num_elements)146 inline size_t DexCacheArraysLayout::MethodTypesSize(size_t num_elements) const {
147   size_t cache_size = mirror::DexCache::kDexCacheMethodTypeCacheSize;
148   if (num_elements < cache_size) {
149     cache_size = num_elements;
150   }
151 
152   return ArraySize(PointerSize::k64, cache_size);
153 }
154 
MethodTypesAlignment()155 inline size_t DexCacheArraysLayout::MethodTypesAlignment() const {
156   static_assert(alignof(mirror::MethodTypeDexCacheType) == 8,
157                 "Expecting alignof(MethodTypeDexCacheType) == 8");
158   return alignof(mirror::MethodTypeDexCacheType);
159 }
160 
CallSitesSize(size_t num_elements)161 inline size_t DexCacheArraysLayout::CallSitesSize(size_t num_elements) const {
162   return ArraySize(GcRootAsPointerSize<mirror::CallSite>(), num_elements);
163 }
164 
CallSitesAlignment()165 inline size_t DexCacheArraysLayout::CallSitesAlignment() const {
166   return alignof(GcRoot<mirror::CallSite>);
167 }
168 
ElementOffset(PointerSize element_size,uint32_t idx)169 inline size_t DexCacheArraysLayout::ElementOffset(PointerSize element_size, uint32_t idx) {
170   return static_cast<size_t>(element_size) * idx;
171 }
172 
ArraySize(PointerSize element_size,uint32_t num_elements)173 inline size_t DexCacheArraysLayout::ArraySize(PointerSize element_size, uint32_t num_elements) {
174   return static_cast<size_t>(element_size) * num_elements;
175 }
176 
PairArraySize(PointerSize element_size,uint32_t num_elements)177 inline size_t DexCacheArraysLayout::PairArraySize(PointerSize element_size, uint32_t num_elements) {
178   return 2u * static_cast<size_t>(element_size) * num_elements;
179 }
180 
181 }  // namespace art
182 
183 #endif  // ART_RUNTIME_UTILS_DEX_CACHE_ARRAYS_LAYOUT_INL_H_
184