This is the mail archive of the
libffi-discuss@sourceware.org
mailing list for the libffi project.
The "trivial example" of a closure in libffi info does not work
- From: Andrew Haley <aph at redhat dot com>
- To: "libffi-discuss at sourceware dot org" <libffi-discuss at sourceware dot org>
- Date: Fri, 15 Nov 2013 15:57:02 +0000
- Subject: The "trivial example" of a closure in libffi info does not work
- Authentication-results: sourceware.org; auth=none
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;
}