From patchwork Fri Aug 6 04:46:55 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Green X-Patchwork-Id: 61062 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 8D61CB70A4 for ; Fri, 6 Aug 2010 14:47:06 +1000 (EST) Received: (qmail 14477 invoked by alias); 6 Aug 2010 04:47:04 -0000 Received: (qmail 14468 invoked by uid 22791); 6 Aug 2010 04:47:03 -0000 X-SWARE-Spam-Status: No, hits=-4.1 required=5.0 tests=AWL, BAYES_20, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_BF, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 06 Aug 2010 04:46:59 +0000 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o764kvRO010779 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 6 Aug 2010 00:46:57 -0400 Received: from gmachine.redhat.com (vpn-10-81.rdu.redhat.com [10.11.10.81]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o764kttG017687; Fri, 6 Aug 2010 00:46:56 -0400 From: Anthony Green To: gcc-patches@gcc.gnu.org Subject: [patch, libffi] Add closure example documentation User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Date: Fri, 06 Aug 2010 00:46:55 -0400 Message-ID: MIME-Version: 1.0 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org I'm checking in the following libffi patch. This patch from Conrad Irwin adds an example to the documentation. 2010-01-12 Conrad Irwin * doc/libffi.texi: Add closure example. Index: libffi/doc/libffi.texi =================================================================== --- libffi.orig/doc/libffi.texi +++ libffi/doc/libffi.texi @@ -19,7 +19,7 @@ This manual is for Libffi, a portable foreign-function interface library. -Copyright @copyright{} 2008 Red Hat, Inc. +Copyright @copyright{} 2008, 2010 Red Hat, Inc. @quotation Permission is granted to copy, distribute and/or modify this document @@ -106,6 +106,7 @@ values passed between the two languages. * Types:: libffi type descriptions. * Multiple ABIs:: Different passing styles on one platform. * The Closure API:: Writing a generic function. +* Closure Example:: A closure example. @end menu @@ -500,12 +501,66 @@ After calling @code{ffi_prep_closure_loc to the appropriate pointer-to-function type. @end defun -@c FIXME: example - You may see old code referring to @code{ffi_prep_closure}. This function is deprecated, as it cannot handle the need for separate writable and executable addresses. +@node Closure Example +@section Closure Example + +A trivial example that creates a new @code{puts} by binding +@code{fputs} with @code{stdin}. + +@example +#include +#include + +/* Acts like puts with the file given at time of enclosure. */ +void puts_binding(ffi_cif *cif, unsigned int *ret, void* args[], + FILE *stream) +@{ + *ret = fputs(*(char **)args[0], stream); +@} + +int main() +@{ + ffi_cif cif; + ffi_type *args[1]; + ffi_closure *closure; + + int (*bound_puts)(char *); + 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_uint, 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 = 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; +@} + +@end example + @node Missing Features @chapter Missing Features @@ -525,6 +580,8 @@ There is no support for bit fields in st @item The closure API is +@c FIXME: ... + @item The ``raw'' API is undocumented. @c argument promotion?