blob: c0b79b2b6c8743a81caf76d6e434bf5e91a26a28 [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
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
Mathieu Chartierd8891782014-03-02 13:28:37 -080017#include "entrypoints/quick/quick_alloc_entrypoints.h"
18
Ian Rogers57b86d42012-03-27 16:05:41 -070019#include "callee_save_frame.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070020#include "entrypoints/entrypoint_utils-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "mirror/object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025
26namespace art {
27
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070028static constexpr bool kUseTlabFastPath = true;
29
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080030#define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, suffix2, instrumented_bool, allocator_type) \
31extern "C" mirror::Object* artAllocObjectFromCode ##suffix##suffix2( \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070032 uint32_t type_idx, mirror::ArtMethod* method, Thread* self) \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070034 ScopedQuickEntrypointChecks sqec(self); \
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070035 if (kUseTlabFastPath && !instrumented_bool && allocator_type == gc::kAllocatorTypeTLAB) { \
Andreas Gampe05d2ab22014-08-06 16:27:52 -070036 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx); \
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070037 if (LIKELY(klass != nullptr && klass->IsInitialized() && !klass->IsFinalizable())) { \
38 size_t byte_count = klass->GetObjectSize(); \
39 byte_count = RoundUp(byte_count, gc::space::BumpPointerSpace::kAlignment); \
40 mirror::Object* obj; \
41 if (LIKELY(byte_count < self->TlabSize())) { \
42 obj = self->AllocTlab(byte_count); \
43 DCHECK(obj != nullptr) << "AllocTlab can't fail"; \
44 obj->SetClass(klass); \
45 if (kUseBakerOrBrooksReadBarrier) { \
46 if (kUseBrooksReadBarrier) { \
47 obj->SetReadBarrierPointer(obj); \
48 } \
49 obj->AssertReadBarrierPointer(); \
50 } \
51 QuasiAtomic::ThreadFenceForConstructor(); \
52 return obj; \
53 } \
54 } \
55 } \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080056 return AllocObjectFromCode<false, instrumented_bool>(type_idx, method, self, allocator_type); \
57} \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080058extern "C" mirror::Object* artAllocObjectFromCodeResolved##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070059 mirror::Class* klass, mirror::ArtMethod* method, Thread* self) \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070061 UNUSED(method); \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070062 ScopedQuickEntrypointChecks sqec(self); \
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070063 if (kUseTlabFastPath && !instrumented_bool && allocator_type == gc::kAllocatorTypeTLAB) { \
64 if (LIKELY(klass->IsInitialized())) { \
65 size_t byte_count = klass->GetObjectSize(); \
66 byte_count = RoundUp(byte_count, gc::space::BumpPointerSpace::kAlignment); \
67 mirror::Object* obj; \
68 if (LIKELY(byte_count < self->TlabSize())) { \
69 obj = self->AllocTlab(byte_count); \
70 DCHECK(obj != nullptr) << "AllocTlab can't fail"; \
71 obj->SetClass(klass); \
72 if (kUseBakerOrBrooksReadBarrier) { \
73 if (kUseBrooksReadBarrier) { \
74 obj->SetReadBarrierPointer(obj); \
75 } \
76 obj->AssertReadBarrierPointer(); \
77 } \
78 QuasiAtomic::ThreadFenceForConstructor(); \
79 return obj; \
80 } \
81 } \
82 } \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070083 return AllocObjectFromCodeResolved<instrumented_bool>(klass, self, allocator_type); \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080084} \
85extern "C" mirror::Object* artAllocObjectFromCodeInitialized##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070086 mirror::Class* klass, mirror::ArtMethod* method, Thread* self) \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070088 UNUSED(method); \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070089 ScopedQuickEntrypointChecks sqec(self); \
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070090 if (kUseTlabFastPath && !instrumented_bool && allocator_type == gc::kAllocatorTypeTLAB) { \
91 size_t byte_count = klass->GetObjectSize(); \
92 byte_count = RoundUp(byte_count, gc::space::BumpPointerSpace::kAlignment); \
93 mirror::Object* obj; \
94 if (LIKELY(byte_count < self->TlabSize())) { \
95 obj = self->AllocTlab(byte_count); \
96 DCHECK(obj != nullptr) << "AllocTlab can't fail"; \
97 obj->SetClass(klass); \
98 if (kUseBakerOrBrooksReadBarrier) { \
99 if (kUseBrooksReadBarrier) { \
100 obj->SetReadBarrierPointer(obj); \
101 } \
102 obj->AssertReadBarrierPointer(); \
103 } \
104 QuasiAtomic::ThreadFenceForConstructor(); \
105 return obj; \
106 } \
107 } \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700108 return AllocObjectFromCodeInitialized<instrumented_bool>(klass, self, allocator_type); \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800109} \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800110extern "C" mirror::Object* artAllocObjectFromCodeWithAccessCheck##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700111 uint32_t type_idx, mirror::ArtMethod* method, Thread* self) \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700113 ScopedQuickEntrypointChecks sqec(self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800114 return AllocObjectFromCode<true, instrumented_bool>(type_idx, method, self, allocator_type); \
115} \
116extern "C" mirror::Array* artAllocArrayFromCode##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700117 uint32_t type_idx, mirror::ArtMethod* method, int32_t component_count, Thread* self) \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700119 ScopedQuickEntrypointChecks sqec(self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800120 return AllocArrayFromCode<false, instrumented_bool>(type_idx, method, component_count, self, \
121 allocator_type); \
122} \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800123extern "C" mirror::Array* artAllocArrayFromCodeResolved##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700124 mirror::Class* klass, mirror::ArtMethod* method, int32_t component_count, Thread* self) \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700126 ScopedQuickEntrypointChecks sqec(self); \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800127 return AllocArrayFromCodeResolved<false, instrumented_bool>(klass, method, component_count, self, \
128 allocator_type); \
129} \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800130extern "C" mirror::Array* artAllocArrayFromCodeWithAccessCheck##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700131 uint32_t type_idx, mirror::ArtMethod* method, int32_t component_count, Thread* self) \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700133 ScopedQuickEntrypointChecks sqec(self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800134 return AllocArrayFromCode<true, instrumented_bool>(type_idx, method, component_count, self, \
135 allocator_type); \
136} \
137extern "C" mirror::Array* artCheckAndAllocArrayFromCode##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700138 uint32_t type_idx, mirror::ArtMethod* method, int32_t component_count, Thread* self) \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700140 ScopedQuickEntrypointChecks sqec(self); \
Hiroshi Yamauchicbbb0802013-11-21 12:42:36 -0800141 if (!instrumented_bool) { \
142 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false, allocator_type); \
143 } else { \
144 return CheckAndAllocArrayFromCodeInstrumented(type_idx, method, component_count, self, false, allocator_type); \
145 } \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800146} \
147extern "C" mirror::Array* artCheckAndAllocArrayFromCodeWithAccessCheck##suffix##suffix2( \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700148 uint32_t type_idx, mirror::ArtMethod* method, int32_t component_count, Thread* self) \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700150 ScopedQuickEntrypointChecks sqec(self); \
Hiroshi Yamauchicbbb0802013-11-21 12:42:36 -0800151 if (!instrumented_bool) { \
152 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true, allocator_type); \
153 } else { \
154 return CheckAndAllocArrayFromCodeInstrumented(type_idx, method, component_count, self, true, allocator_type); \
155 } \
Ian Rogers57b86d42012-03-27 16:05:41 -0700156}
157
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800158#define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(suffix, allocator_type) \
159 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, Instrumented, true, allocator_type) \
160 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, , false, allocator_type)
Ian Rogers57b86d42012-03-27 16:05:41 -0700161
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800162GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(DlMalloc, gc::kAllocatorTypeDlMalloc)
163GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RosAlloc, gc::kAllocatorTypeRosAlloc)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800164GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(BumpPointer, gc::kAllocatorTypeBumpPointer)
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800165GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(TLAB, gc::kAllocatorTypeTLAB)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700166
Mathieu Chartierd8891782014-03-02 13:28:37 -0800167#define GENERATE_ENTRYPOINTS(suffix) \
168extern "C" void* art_quick_alloc_array##suffix(uint32_t, void*, int32_t); \
169extern "C" void* art_quick_alloc_array_resolved##suffix(void* klass, void*, int32_t); \
170extern "C" void* art_quick_alloc_array_with_access_check##suffix(uint32_t, void*, int32_t); \
171extern "C" void* art_quick_alloc_object##suffix(uint32_t type_idx, void* method); \
172extern "C" void* art_quick_alloc_object_resolved##suffix(void* klass, void* method); \
173extern "C" void* art_quick_alloc_object_initialized##suffix(void* klass, void* method); \
174extern "C" void* art_quick_alloc_object_with_access_check##suffix(uint32_t type_idx, void* method); \
175extern "C" void* art_quick_check_and_alloc_array##suffix(uint32_t, void*, int32_t); \
176extern "C" void* art_quick_check_and_alloc_array_with_access_check##suffix(uint32_t, void*, int32_t); \
177extern "C" void* art_quick_alloc_array##suffix##_instrumented(uint32_t, void*, int32_t); \
178extern "C" void* art_quick_alloc_array_resolved##suffix##_instrumented(void* klass, void*, int32_t); \
179extern "C" void* art_quick_alloc_array_with_access_check##suffix##_instrumented(uint32_t, void*, int32_t); \
180extern "C" void* art_quick_alloc_object##suffix##_instrumented(uint32_t type_idx, void* method); \
181extern "C" void* art_quick_alloc_object_resolved##suffix##_instrumented(void* klass, void* method); \
182extern "C" void* art_quick_alloc_object_initialized##suffix##_instrumented(void* klass, void* method); \
183extern "C" void* art_quick_alloc_object_with_access_check##suffix##_instrumented(uint32_t type_idx, void* method); \
184extern "C" void* art_quick_check_and_alloc_array##suffix##_instrumented(uint32_t, void*, int32_t); \
185extern "C" void* art_quick_check_and_alloc_array_with_access_check##suffix##_instrumented(uint32_t, void*, int32_t); \
186void SetQuickAllocEntryPoints##suffix(QuickEntryPoints* qpoints, bool instrumented) { \
187 if (instrumented) { \
188 qpoints->pAllocArray = art_quick_alloc_array##suffix##_instrumented; \
189 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix##_instrumented; \
190 qpoints->pAllocArrayWithAccessCheck = art_quick_alloc_array_with_access_check##suffix##_instrumented; \
191 qpoints->pAllocObject = art_quick_alloc_object##suffix##_instrumented; \
192 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix##_instrumented; \
193 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix##_instrumented; \
194 qpoints->pAllocObjectWithAccessCheck = art_quick_alloc_object_with_access_check##suffix##_instrumented; \
195 qpoints->pCheckAndAllocArray = art_quick_check_and_alloc_array##suffix##_instrumented; \
196 qpoints->pCheckAndAllocArrayWithAccessCheck = art_quick_check_and_alloc_array_with_access_check##suffix##_instrumented; \
197 } else { \
198 qpoints->pAllocArray = art_quick_alloc_array##suffix; \
199 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix; \
200 qpoints->pAllocArrayWithAccessCheck = art_quick_alloc_array_with_access_check##suffix; \
201 qpoints->pAllocObject = art_quick_alloc_object##suffix; \
202 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix; \
203 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix; \
204 qpoints->pAllocObjectWithAccessCheck = art_quick_alloc_object_with_access_check##suffix; \
205 qpoints->pCheckAndAllocArray = art_quick_check_and_alloc_array##suffix; \
206 qpoints->pCheckAndAllocArrayWithAccessCheck = art_quick_check_and_alloc_array_with_access_check##suffix; \
207 } \
208}
209
210// Generate the entrypoint functions.
Ian Rogersc3ccc102014-06-25 11:52:14 -0700211#if !defined(__APPLE__) || !defined(__LP64__)
Andreas Gampec8ccf682014-09-29 20:07:43 -0700212GENERATE_ENTRYPOINTS(_dlmalloc)
213GENERATE_ENTRYPOINTS(_rosalloc)
214GENERATE_ENTRYPOINTS(_bump_pointer)
215GENERATE_ENTRYPOINTS(_tlab)
Ian Rogersc3ccc102014-06-25 11:52:14 -0700216#endif
Mathieu Chartierd8891782014-03-02 13:28:37 -0800217
218static bool entry_points_instrumented = false;
219static gc::AllocatorType entry_points_allocator = gc::kAllocatorTypeDlMalloc;
220
221void SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator) {
222 entry_points_allocator = allocator;
223}
224
225void SetQuickAllocEntryPointsInstrumented(bool instrumented) {
226 entry_points_instrumented = instrumented;
227}
228
229void ResetQuickAllocEntryPoints(QuickEntryPoints* qpoints) {
Ian Rogersc3ccc102014-06-25 11:52:14 -0700230#if !defined(__APPLE__) || !defined(__LP64__)
Ian Rogersde2db522014-11-04 14:43:18 -0800231 switch (entry_points_allocator) {
Mathieu Chartierd8891782014-03-02 13:28:37 -0800232 case gc::kAllocatorTypeDlMalloc: {
233 SetQuickAllocEntryPoints_dlmalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800234 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800235 }
236 case gc::kAllocatorTypeRosAlloc: {
237 SetQuickAllocEntryPoints_rosalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800238 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800239 }
240 case gc::kAllocatorTypeBumpPointer: {
241 CHECK(kMovingCollector);
242 SetQuickAllocEntryPoints_bump_pointer(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800243 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800244 }
245 case gc::kAllocatorTypeTLAB: {
246 CHECK(kMovingCollector);
247 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800248 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800249 }
Ian Rogersde2db522014-11-04 14:43:18 -0800250 default:
251 break;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800252 }
Ian Rogersde2db522014-11-04 14:43:18 -0800253#else
254 UNUSED(qpoints);
255#endif
256 UNIMPLEMENTED(FATAL);
257 UNREACHABLE();
Mathieu Chartierd8891782014-03-02 13:28:37 -0800258}
259
Ian Rogers57b86d42012-03-27 16:05:41 -0700260} // namespace art