1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
Index: libffi/doc/libffi.info
===================================================================
--- libffi.orig/doc/libffi.info
+++ libffi/doc/libffi.info
@@ -4,7 +4,7 @@ This is doc/libffi.info, produced by mak
This manual is for Libffi, a portable foreign-function interface
library.
- Copyright (C) 2008 Red Hat, Inc.
+ Copyright (C) 2008, 2010 Red Hat, Inc.
Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU General Public License as
@@ -27,7 +27,7 @@ libffi
This manual is for Libffi, a portable foreign-function interface
library.
- Copyright (C) 2008 Red Hat, Inc.
+ Copyright (C) 2008, 2010 Red Hat, Inc.
Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU General Public License as
@@ -89,6 +89,7 @@ File: libffi.info, Node: Using libffi,
* Types:: libffi type descriptions.
* Multiple ABIs:: Different passing styles on one platform.
* The Closure API:: Writing a generic function.
+* Closure Example:: A closure example.
File: libffi.info, Node: The Basics, Next: Simple Example, Up: Using libffi
@@ -368,7 +369,7 @@ instance, the x86 platform has both `std
necessarily platform-specific.
-File: libffi.info, Node: The Closure API, Prev: Multiple ABIs, Up: Using libffi
+File: libffi.info, Node: The Closure API, Next: Closure Example, Prev: Multiple ABIs, Up: Using libffi
2.5 The Closure API
===================
@@ -444,6 +445,62 @@ is deprecated, as it cannot handle the n
executable addresses.
+File: libffi.info, Node: Closure Example, Prev: The Closure API, Up: Using libffi
+
+2.6 Closure Example
+===================
+
+A trivial example that creates a new `puts' by binding `fputs' with
+`stdin'.
+
+ #include <stdio.h>
+ #include <ffi.h>
+
+ /* 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;
+ }
+
+
File: libffi.info, Node: Missing Features, Next: Index, Prev: Using libffi, Up: Top
3 Missing Features
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 <stdio.h>
+#include <ffi.h>
+
+/* 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?
Index: libffi/ChangeLog.libffi
===================================================================
--- libffi.orig/ChangeLog.libffi
+++ libffi/ChangeLog.libffi
@@ -1,3 +1,8 @@
+2010-01-12 Conrad Irwin <conrad.irwin@gmail.com>
+
+ * doc/libffi.texi: Add closure example.
+ * doc/libffi.info: Rebuilt.
+
2009-12-25 Samuli Suominen <ssuominen@gentoo.org>
* configure.ac: Undefine _AC_ARG_VAR_PRECIOUS for autoconf 2.64.