public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Haley <aph@redhat.com>
To: "libffi-discuss@sourceware.org" <libffi-discuss@sourceware.org>
Subject: The "trivial example" of a closure in libffi info does not work
Date: Fri, 15 Nov 2013 15:57:00 -0000	[thread overview]
Message-ID: <5286444E.1080201@redhat.com> (raw)

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;
}



                 reply	other threads:[~2013-11-15 15:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5286444E.1080201@redhat.com \
    --to=aph@redhat.com \
    --cc=libffi-discuss@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).