Skip to content

Commit

Permalink
get_module_functions: separate gc pushes
Browse files Browse the repository at this point in the history
creating a new Array<...> calls jl_alloc_array_1d which can trigger garbage collection
thus we need to protect each array before creating the next one instead of one JL_GC_PUSH6 call
to do this we need some extra scope blocks
  • Loading branch information
benlorenz committed Dec 1, 2024
1 parent d4192fe commit dd36d43
Showing 1 changed file with 44 additions and 37 deletions.
81 changes: 44 additions & 37 deletions src/c_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,45 +140,52 @@ JLCXX_API jl_array_t* get_module_functions(jl_module_t* jlmod)
Array<jl_datatype_t*> arg_types_array;
jl_value_t* boxed_f = nullptr;
jl_value_t* boxed_thunk = nullptr;
Array<jl_value_t*> arg_names_array;
Array<jl_value_t*> arg_default_values_array;
jl_value_t* boxed_n_kwargs;
JL_GC_PUSH6(arg_types_array.gc_pointer(), &boxed_f, &boxed_thunk, arg_names_array.gc_pointer(), arg_default_values_array.gc_pointer(), &boxed_n_kwargs);

fill_types_vec(arg_types_array, f.argument_types());

boxed_f = jlcxx::box<void*>(f.pointer());
boxed_thunk = jlcxx::box<void*>(f.thunk());

fill_values_vec(arg_names_array, f.argument_names());
fill_values_vec(arg_default_values_array, f.argument_default_values());

boxed_n_kwargs = jlcxx::box<int>(f.number_of_keyword_arguments());

auto returntypes = f.return_type();

jl_datatype_t* ccall_return_type = returntypes.first;
jl_datatype_t* julia_return_type = returntypes.second;
if(ccall_return_type == nullptr)
JL_GC_PUSH3(arg_types_array.gc_pointer(), &boxed_f, &boxed_thunk);
{
ccall_return_type = julia_type<void>();
julia_return_type = ccall_return_type;
Array<jl_value_t*> arg_names_array;
JL_GC_PUSH1(arg_names_array.gc_pointer());
{
Array<jl_value_t*> arg_default_values_array;
jl_value_t* boxed_n_kwargs = nullptr;
JL_GC_PUSH2(arg_default_values_array.gc_pointer(), &boxed_n_kwargs);

fill_types_vec(arg_types_array, f.argument_types());

boxed_f = jlcxx::box<void*>(f.pointer());
boxed_thunk = jlcxx::box<void*>(f.thunk());

fill_values_vec(arg_names_array, f.argument_names());
fill_values_vec(arg_default_values_array, f.argument_default_values());

boxed_n_kwargs = jlcxx::box<int>(f.number_of_keyword_arguments());

auto returntypes = f.return_type();

jl_datatype_t* ccall_return_type = returntypes.first;
jl_datatype_t* julia_return_type = returntypes.second;
if(ccall_return_type == nullptr)
{
ccall_return_type = julia_type<void>();
julia_return_type = ccall_return_type;
}

function_array.push_back(jl_new_struct(g_cppfunctioninfo_type,
f.name(),
arg_types_array.wrapped(),
ccall_return_type,
julia_return_type,
boxed_f,
boxed_thunk,
f.override_module(),
f.doc(),
arg_names_array.wrapped(),
arg_default_values_array.wrapped(),
boxed_n_kwargs
));
JL_GC_POP();
}
JL_GC_POP();
}

function_array.push_back(jl_new_struct(g_cppfunctioninfo_type,
f.name(),
arg_types_array.wrapped(),
ccall_return_type,
julia_return_type,
boxed_f,
boxed_thunk,
f.override_module(),
f.doc(),
arg_names_array.wrapped(),
arg_default_values_array.wrapped(),
boxed_n_kwargs
));

JL_GC_POP();
});
JL_GC_POP();
Expand Down

0 comments on commit dd36d43

Please sign in to comment.