* The "trivial example" of a closure in libffi info does not work
@ 2013-11-15 15:57 Andrew Haley
0 siblings, 0 replies; only message in thread
From: Andrew Haley @ 2013-11-15 15:57 UTC (permalink / raw)
To: libffi-discuss
The example in the libffi doc does not work. It uses the wrong types,
and will not compile:
trivial.c: In function 'main':
trivial.c:21:3: warning: passing argument 2 of 'ffi_closure_alloc' from incompatible pointer type [enabled by default]
closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
^
In file included from /usr/include/ffi.h:10:0,
from trivial.c:2:
/usr/include/ffi-ppc64.h:318:7: note: expected 'void **' but argument is of type 'int (**)(char *)'
void *ffi_closure_alloc (size_t size, void **code);
^
trivial.c:34:8: warning: passing argument 3 of 'ffi_prep_closure_loc' from incompatible pointer type [enabled by default]
stdout, bound_puts) == FFI_OK)
^
In file included from /usr/include/ffi.h:10:0,
from trivial.c:2:
/usr/include/ffi-ppc64.h:328:1: note: expected 'void (*)(struct ffi_cif *, void *, void **, void *)' but argument is of type 'void (*)(struct ffi_cif *, ffi_arg *, void **, struct FILE *)'
ffi_prep_closure_loc (ffi_closure*,
^
I have appended a corrected version.
Andrew.
#include <stdio.h>
#include <ffi.h>
/* 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);
}
typedef int (*puts_t)(char *);
int main()
{
ffi_cif cif;
ffi_type *args[1];
ffi_closure *closure;
void *bound_puts;
int rc;
/* Allocate closure and bound_puts */
closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
if (closure)
{
/* Initialize the argument info vectors */
args[0] = &ffi_type_pointer;
/* Initialize the cif */
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
&ffi_type_sint, args) == FFI_OK)
{
/* Initialize the closure, setting stream to stdout */
if (ffi_prep_closure_loc(closure, &cif, puts_binding,
stdout, bound_puts) == FFI_OK)
{
rc = ((puts_t)bound_puts)("Hello World!");
/* rc now holds the result of the call to fputs */
}
}
}
/* Deallocate both closure, and bound_puts */
ffi_closure_free(closure);
return 0;
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2013-11-15 15:57 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-15 15:57 The "trivial example" of a closure in libffi info does not work Andrew Haley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).