From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11388 invoked by alias); 15 Nov 2013 15:57:13 -0000 Mailing-List: contact libffi-discuss-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libffi-discuss-owner@sourceware.org Received: (qmail 11378 invoked by uid 89); 15 Nov 2013 15:57:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.9 required=5.0 tests=AWL,BAYES_20,RDNS_NONE,SPF_HELO_PASS,SPF_PASS autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from Unknown (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 15 Nov 2013 15:57:11 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rAFFv32f023534 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 15 Nov 2013 10:57:03 -0500 Received: from zebedee.pink (ovpn-113-156.phx2.redhat.com [10.3.113.156]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id rAFFv2TW016845; Fri, 15 Nov 2013 10:57:03 -0500 Message-ID: <5286444E.1080201@redhat.com> Date: Fri, 15 Nov 2013 15:57:00 -0000 From: Andrew Haley User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131028 Thunderbird/17.0.10 MIME-Version: 1.0 To: "libffi-discuss@sourceware.org" Subject: The "trivial example" of a closure in libffi info does not work Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2013/txt/msg00210.txt.bz2 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 #include /* 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; }