Skip to content

Commit 7245f91

Browse files
committed
invocation_wrapper: support non-POD types using placement new
Each context maintains a pool of manually allocated invocation_wrapper objects, which so far contained only POD types. To support the use of non-POD types, e.g., std::vector, explicitly invoke the constructor using placement new after malloc, and the destructor before free. https://en.cppreference.com/w/cpp/language/new#Placement_new #103 (comment)
1 parent bd7f107 commit 7245f91

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/acl_context.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,11 +1014,15 @@ static void l_forcibly_release_allocations(cl_context context) {
10141014
}
10151015

10161016
if (context->invocation_wrapper) {
1017-
unsigned i;
1018-
for (i = 0; i < context->num_invocation_wrappers_allocated; i++) {
1019-
if (context->invocation_wrapper[i]->image->arg_value)
1020-
acl_delete_arr(context->invocation_wrapper[i]->image->arg_value);
1021-
acl_free(context->invocation_wrapper[i]);
1017+
for (unsigned int i = 0; i < context->num_invocation_wrappers_allocated;
1018+
i++) {
1019+
auto *const wrapper = context->invocation_wrapper[i];
1020+
if (wrapper->image->arg_value) {
1021+
acl_delete_arr(wrapper->image->arg_value);
1022+
}
1023+
// invoke non-default destructors of non-POD types, e.g., std::vector
1024+
wrapper->~acl_kernel_invocation_wrapper_t();
1025+
acl_free(wrapper);
10221026
}
10231027
acl_free(context->invocation_wrapper);
10241028
context->invocation_wrapper = 0;
@@ -1208,6 +1212,8 @@ l_init_kernel_invocation_wrapper(acl_kernel_invocation_wrapper_t *wrapper,
12081212
acl_assert_locked();
12091213

12101214
if (wrapper) {
1215+
// invoke non-default constructors of non-POD types, e.g., std::vector
1216+
new (wrapper) acl_kernel_invocation_wrapper_t{};
12111217
wrapper->id = i;
12121218
wrapper->image = &(wrapper->image_storage);
12131219
wrapper->image->arg_value = nullptr;

0 commit comments

Comments
 (0)