1 /*
2 * Copyright (C) 2012 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 #include "entrypoints/quick/quick_alloc_entrypoints.h"
18
19 #include "art_method-inl.h"
20 #include "base/enums.h"
21 #include "callee_save_frame.h"
22 #include "dex_file_types.h"
23 #include "entrypoints/entrypoint_utils-inl.h"
24 #include "mirror/class-inl.h"
25 #include "mirror/object_array-inl.h"
26 #include "mirror/object-inl.h"
27
28 namespace art {
29
30 static constexpr bool kUseTlabFastPath = true;
31
32 template <bool kInitialized,
33 bool kFinalize,
34 bool kInstrumented,
35 gc::AllocatorType allocator_type>
artAllocObjectFromCode(mirror::Class * klass,Thread * self)36 static ALWAYS_INLINE inline mirror::Object* artAllocObjectFromCode(
37 mirror::Class* klass,
38 Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
39 ScopedQuickEntrypointChecks sqec(self);
40 DCHECK(klass != nullptr);
41 if (kUseTlabFastPath && !kInstrumented && allocator_type == gc::kAllocatorTypeTLAB) {
42 if (kInitialized || klass->IsInitialized()) {
43 if (!kFinalize || !klass->IsFinalizable()) {
44 size_t byte_count = klass->GetObjectSize();
45 byte_count = RoundUp(byte_count, gc::space::BumpPointerSpace::kAlignment);
46 mirror::Object* obj;
47 if (LIKELY(byte_count < self->TlabSize())) {
48 obj = self->AllocTlab(byte_count);
49 DCHECK(obj != nullptr) << "AllocTlab can't fail";
50 obj->SetClass(klass);
51 if (kUseBakerReadBarrier) {
52 obj->AssertReadBarrierState();
53 }
54 QuasiAtomic::ThreadFenceForConstructor();
55 return obj;
56 }
57 }
58 }
59 }
60 if (kInitialized) {
61 return AllocObjectFromCodeInitialized<kInstrumented>(klass, self, allocator_type);
62 } else if (!kFinalize) {
63 return AllocObjectFromCodeResolved<kInstrumented>(klass, self, allocator_type);
64 } else {
65 return AllocObjectFromCode<kInstrumented>(klass, self, allocator_type);
66 }
67 }
68
69 #define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, suffix2, instrumented_bool, allocator_type) \
70 extern "C" mirror::Object* artAllocObjectFromCodeWithChecks##suffix##suffix2( \
71 mirror::Class* klass, Thread* self) \
72 REQUIRES_SHARED(Locks::mutator_lock_) { \
73 return artAllocObjectFromCode<false, true, instrumented_bool, allocator_type>(klass, self); \
74 } \
75 extern "C" mirror::Object* artAllocObjectFromCodeResolved##suffix##suffix2( \
76 mirror::Class* klass, Thread* self) \
77 REQUIRES_SHARED(Locks::mutator_lock_) { \
78 return artAllocObjectFromCode<false, false, instrumented_bool, allocator_type>(klass, self); \
79 } \
80 extern "C" mirror::Object* artAllocObjectFromCodeInitialized##suffix##suffix2( \
81 mirror::Class* klass, Thread* self) \
82 REQUIRES_SHARED(Locks::mutator_lock_) { \
83 return artAllocObjectFromCode<true, false, instrumented_bool, allocator_type>(klass, self); \
84 } \
85 extern "C" mirror::Array* artAllocArrayFromCodeResolved##suffix##suffix2( \
86 mirror::Class* klass, int32_t component_count, Thread* self) \
87 REQUIRES_SHARED(Locks::mutator_lock_) { \
88 ScopedQuickEntrypointChecks sqec(self); \
89 return AllocArrayFromCodeResolved<instrumented_bool>(klass, component_count, self, \
90 allocator_type); \
91 } \
92 extern "C" mirror::String* artAllocStringFromBytesFromCode##suffix##suffix2( \
93 mirror::ByteArray* byte_array, int32_t high, int32_t offset, int32_t byte_count, \
94 Thread* self) \
95 REQUIRES_SHARED(Locks::mutator_lock_) { \
96 ScopedQuickEntrypointChecks sqec(self); \
97 StackHandleScope<1> hs(self); \
98 Handle<mirror::ByteArray> handle_array(hs.NewHandle(byte_array)); \
99 return mirror::String::AllocFromByteArray<instrumented_bool>(self, byte_count, handle_array, \
100 offset, high, allocator_type); \
101 } \
102 extern "C" mirror::String* artAllocStringFromCharsFromCode##suffix##suffix2( \
103 int32_t offset, int32_t char_count, mirror::CharArray* char_array, Thread* self) \
104 REQUIRES_SHARED(Locks::mutator_lock_) { \
105 StackHandleScope<1> hs(self); \
106 Handle<mirror::CharArray> handle_array(hs.NewHandle(char_array)); \
107 return mirror::String::AllocFromCharArray<instrumented_bool>(self, char_count, handle_array, \
108 offset, allocator_type); \
109 } \
110 extern "C" mirror::String* artAllocStringFromStringFromCode##suffix##suffix2( /* NOLINT */ \
111 mirror::String* string, Thread* self) \
112 REQUIRES_SHARED(Locks::mutator_lock_) { \
113 StackHandleScope<1> hs(self); \
114 Handle<mirror::String> handle_string(hs.NewHandle(string)); \
115 return mirror::String::AllocFromString<instrumented_bool>(self, handle_string->GetLength(), \
116 handle_string, 0, allocator_type); \
117 }
118
119 #define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(suffix, allocator_type) \
120 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, Instrumented, true, allocator_type) \
121 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, , false, allocator_type)
122
123 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(DlMalloc, gc::kAllocatorTypeDlMalloc)
124 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RosAlloc, gc::kAllocatorTypeRosAlloc)
125 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(BumpPointer, gc::kAllocatorTypeBumpPointer)
126 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(TLAB, gc::kAllocatorTypeTLAB)
127 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(Region, gc::kAllocatorTypeRegion)
128 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RegionTLAB, gc::kAllocatorTypeRegionTLAB)
129
130 #define GENERATE_ENTRYPOINTS(suffix) \
131 extern "C" void* art_quick_alloc_array_resolved##suffix(mirror::Class* klass, int32_t); \
132 extern "C" void* art_quick_alloc_array_resolved8##suffix(mirror::Class* klass, int32_t); \
133 extern "C" void* art_quick_alloc_array_resolved16##suffix(mirror::Class* klass, int32_t); \
134 extern "C" void* art_quick_alloc_array_resolved32##suffix(mirror::Class* klass, int32_t); \
135 extern "C" void* art_quick_alloc_array_resolved64##suffix(mirror::Class* klass, int32_t); \
136 extern "C" void* art_quick_alloc_object_resolved##suffix(mirror::Class* klass); \
137 extern "C" void* art_quick_alloc_object_initialized##suffix(mirror::Class* klass); \
138 extern "C" void* art_quick_alloc_object_with_checks##suffix(mirror::Class* klass); \
139 extern "C" void* art_quick_alloc_string_from_bytes##suffix(void*, int32_t, int32_t, int32_t); \
140 extern "C" void* art_quick_alloc_string_from_chars##suffix(int32_t, int32_t, void*); \
141 extern "C" void* art_quick_alloc_string_from_string##suffix(void*); \
142 extern "C" void* art_quick_alloc_array_resolved##suffix##_instrumented(mirror::Class* klass, int32_t); \
143 extern "C" void* art_quick_alloc_array_resolved8##suffix##_instrumented(mirror::Class* klass, int32_t); \
144 extern "C" void* art_quick_alloc_array_resolved16##suffix##_instrumented(mirror::Class* klass, int32_t); \
145 extern "C" void* art_quick_alloc_array_resolved32##suffix##_instrumented(mirror::Class* klass, int32_t); \
146 extern "C" void* art_quick_alloc_array_resolved64##suffix##_instrumented(mirror::Class* klass, int32_t); \
147 extern "C" void* art_quick_alloc_object_resolved##suffix##_instrumented(mirror::Class* klass); \
148 extern "C" void* art_quick_alloc_object_initialized##suffix##_instrumented(mirror::Class* klass); \
149 extern "C" void* art_quick_alloc_object_with_checks##suffix##_instrumented(mirror::Class* klass); \
150 extern "C" void* art_quick_alloc_string_from_bytes##suffix##_instrumented(void*, int32_t, int32_t, int32_t); \
151 extern "C" void* art_quick_alloc_string_from_chars##suffix##_instrumented(int32_t, int32_t, void*); \
152 extern "C" void* art_quick_alloc_string_from_string##suffix##_instrumented(void*); \
153 void SetQuickAllocEntryPoints##suffix(QuickEntryPoints* qpoints, bool instrumented) { \
154 if (instrumented) { \
155 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix##_instrumented; \
156 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix##_instrumented; \
157 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix##_instrumented; \
158 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix##_instrumented; \
159 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix##_instrumented; \
160 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix##_instrumented; \
161 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix##_instrumented; \
162 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix##_instrumented; \
163 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix##_instrumented; \
164 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix##_instrumented; \
165 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix##_instrumented; \
166 } else { \
167 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix; \
168 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix; \
169 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix; \
170 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix; \
171 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix; \
172 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix; \
173 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix; \
174 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix; \
175 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix; \
176 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix; \
177 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix; \
178 } \
179 }
180
181 // Generate the entrypoint functions.
182 #if !defined(__APPLE__) || !defined(__LP64__)
183 GENERATE_ENTRYPOINTS(_dlmalloc)
184 GENERATE_ENTRYPOINTS(_rosalloc)
185 GENERATE_ENTRYPOINTS(_bump_pointer)
186 GENERATE_ENTRYPOINTS(_tlab)
187 GENERATE_ENTRYPOINTS(_region)
188 GENERATE_ENTRYPOINTS(_region_tlab)
189 #endif
190
191 static bool entry_points_instrumented = false;
192 static gc::AllocatorType entry_points_allocator = gc::kAllocatorTypeDlMalloc;
193
SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator)194 void SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator) {
195 entry_points_allocator = allocator;
196 }
197
SetQuickAllocEntryPointsInstrumented(bool instrumented)198 void SetQuickAllocEntryPointsInstrumented(bool instrumented) {
199 entry_points_instrumented = instrumented;
200 }
201
ResetQuickAllocEntryPoints(QuickEntryPoints * qpoints,bool is_marking)202 void ResetQuickAllocEntryPoints(QuickEntryPoints* qpoints, bool is_marking) {
203 #if !defined(__APPLE__) || !defined(__LP64__)
204 switch (entry_points_allocator) {
205 case gc::kAllocatorTypeDlMalloc: {
206 SetQuickAllocEntryPoints_dlmalloc(qpoints, entry_points_instrumented);
207 return;
208 }
209 case gc::kAllocatorTypeRosAlloc: {
210 SetQuickAllocEntryPoints_rosalloc(qpoints, entry_points_instrumented);
211 return;
212 }
213 case gc::kAllocatorTypeBumpPointer: {
214 CHECK(kMovingCollector);
215 SetQuickAllocEntryPoints_bump_pointer(qpoints, entry_points_instrumented);
216 return;
217 }
218 case gc::kAllocatorTypeTLAB: {
219 CHECK(kMovingCollector);
220 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
221 return;
222 }
223 case gc::kAllocatorTypeRegion: {
224 CHECK(kMovingCollector);
225 SetQuickAllocEntryPoints_region(qpoints, entry_points_instrumented);
226 return;
227 }
228 case gc::kAllocatorTypeRegionTLAB: {
229 CHECK(kMovingCollector);
230 if (is_marking) {
231 SetQuickAllocEntryPoints_region_tlab(qpoints, entry_points_instrumented);
232 } else {
233 // Not marking means we need no read barriers and can just use the normal TLAB case.
234 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
235 }
236 return;
237 }
238 default:
239 break;
240 }
241 #else
242 UNUSED(qpoints);
243 UNUSED(is_marking);
244 #endif
245 UNIMPLEMENTED(FATAL);
246 UNREACHABLE();
247 }
248
249 } // namespace art
250