Pass copies of large structs on the stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
diff --git a/src/moxie/ffi.c b/src/moxie/ffi.c
index 1a65660..9d71673 100644
--- a/src/moxie/ffi.c
+++ b/src/moxie/ffi.c
@@ -1,5 +1,5 @@
/* -----------------------------------------------------------------------
- ffi.c - Copyright (C) 2012, 2013, 2018, 2021 Anthony Green
+ ffi.c - Copyright (C) 2012, 2013, 2018, 2021, 2022 Anthony Green
Moxie Foreign Function Interface
@@ -128,6 +128,8 @@ void ffi_call(ffi_cif *cif,
void **avalue)
{
extended_cif ecif;
+ ffi_type **arg_types = cif->arg_types;
+ int i, nargs = cif->nargs;
ecif.cif = cif;
ecif.avalue = avalue;
@@ -143,6 +145,20 @@ void ffi_call(ffi_cif *cif,
else
ecif.rvalue = rvalue;
+ /* If we have any large structure arguments, make a copy so we are passing
+ by value. */
+ for (i = 0; i < nargs; i++)
+ {
+ ffi_type *at = arg_types[i];
+ int size = at->size;
+ if (at->type == FFI_TYPE_STRUCT && size > 16)
+ {
+ char *argcopy = alloca (size);
+ memcpy (argcopy, avalue[i], size);
+ avalue[i] = argcopy;
+ }
+ }
+
switch (cif->abi)
{
case FFI_EABI: