This is the mail archive of the
libffi-discuss@sourceware.org
mailing list for the libffi project.
Help: correct way to handle struct return values.
- From: Kaz Kylheku <kaz at kylheku dot com>
- To: libffi-discuss at sourceware dot org
- Date: Sat, 27 May 2017 20:14:22 -0700
- Subject: Help: correct way to handle struct return values.
- Authentication-results: sourceware.org; auth=none
Hi all,
In the current texinfo doc, the callback stub does this to
return a value of type int:
/* Acts like puts with the file given at time of enclosure. */
void puts_binding(ffi_cif *cif, void *ret, void* args[],
void *stream)
{
*(ffi_arg *)ret = fputs(*(char **)args[0], (FILE *)stream);
}
Can someone show what the code would look like for a function
that returns the following type, and work everywhere: all
supported platforms, big or little endian:
struct little { char a, b };
Obvously, this can't be used:
*(ffi_arg *)ret = fun_returning_struct_little(...);
After everything, I'm not confident that this is correct, either:
*(struct little *)ret = fun_returning_struct_little(...);
Also, can someone show how to extract "struct little" in after
a ffi down call? If the type is int, we can do this:
ffi_arg rc_buf;
int rc;
ffi_call(&cif, puts, &rc_buf, values);
rc = rc_buf;
But what if rc is of type "little struct"? Again, we can't just use a
cast.
Lastly, how does any of this change for struct bigger_struct { long x,
y, z; }?
Thanks.