api-foreign.texi 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2008,
  4. @c 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Foreign Function Interface
  7. @section Foreign Function Interface
  8. @cindex foreign function interface
  9. @cindex ffi
  10. The more one hacks in Scheme, the more one realizes that there are
  11. actually two computational worlds: one which is warm and alive, that
  12. land of parentheses, and one cold and dead, the land of C and its ilk.
  13. But yet we as programmers live in both worlds, and Guile itself is half
  14. implemented in C. So it is that Guile's living half pays respect to its
  15. dead counterpart, via a spectrum of interfaces to C ranging from dynamic
  16. loading of Scheme primitives to dynamic binding of stock C library
  17. procedures.
  18. @menu
  19. * Foreign Libraries:: Dynamically linking to libraries.
  20. * Foreign Functions:: Simple calls to C procedures.
  21. * C Extensions:: Extending Guile in C with loadable modules.
  22. * Modules and Extensions:: Loading C extensions into modules.
  23. * Foreign Pointers:: Accessing global variables.
  24. * Dynamic FFI:: Calling arbitrary C functions.
  25. @end menu
  26. @node Foreign Libraries
  27. @subsection Foreign Libraries
  28. Most modern Unices have something called @dfn{shared libraries}. This
  29. ordinarily means that they have the capability to share the executable
  30. image of a library between several running programs to save memory and
  31. disk space. But generally, shared libraries give a lot of additional
  32. flexibility compared to the traditional static libraries. In fact,
  33. calling them `dynamic' libraries is as correct as calling them `shared'.
  34. Shared libraries really give you a lot of flexibility in addition to the
  35. memory and disk space savings. When you link a program against a shared
  36. library, that library is not closely incorporated into the final
  37. executable. Instead, the executable of your program only contains
  38. enough information to find the needed shared libraries when the program
  39. is actually run. Only then, when the program is starting, is the final
  40. step of the linking process performed. This means that you need not
  41. recompile all programs when you install a new, only slightly modified
  42. version of a shared library. The programs will pick up the changes
  43. automatically the next time they are run.
  44. Now, when all the necessary machinery is there to perform part of the
  45. linking at run-time, why not take the next step and allow the programmer
  46. to explicitly take advantage of it from within his program? Of course,
  47. many operating systems that support shared libraries do just that, and
  48. chances are that Guile will allow you to access this feature from within
  49. your Scheme programs. As you might have guessed already, this feature
  50. is called @dfn{dynamic linking}.@footnote{Some people also refer to the
  51. final linking stage at program startup as `dynamic linking', so if you
  52. want to make yourself perfectly clear, it is probably best to use the
  53. more technical term @dfn{dlopening}, as suggested by Gordon Matzigkeit
  54. in his libtool documentation.}
  55. We titled this section ``foreign libraries'' because although the name
  56. ``foreign'' doesn't leak into the API, the world of C really is foreign
  57. to Scheme -- and that estrangement extends to components of foreign
  58. libraries as well, as we see in future sections.
  59. @deffn {Scheme Procedure} dynamic-link [library]
  60. @deffnx {C Function} scm_dynamic_link (library)
  61. Find the shared library denoted by @var{library} (a string) and link it
  62. into the running Guile application. When everything works out, return a
  63. Scheme object suitable for representing the linked object file.
  64. Otherwise an error is thrown. How object files are searched is system
  65. dependent.
  66. Normally, @var{library} is just the name of some shared library file
  67. that will be searched for in the places where shared libraries usually
  68. reside, such as in @file{/usr/lib} and @file{/usr/local/lib}.
  69. @var{library} should not contain an extension such as @code{.so}. The
  70. correct file name extension for the host operating system is provided
  71. automatically, according to libltdl's rules (@pxref{Libltdl interface,
  72. lt_dlopenext, @code{lt_dlopenext}, libtool, Shared Library Support for
  73. GNU}).
  74. When @var{library} is omitted, a @dfn{global symbol handle} is returned. This
  75. handle provides access to the symbols available to the program at run-time,
  76. including those exported by the program itself and the shared libraries already
  77. loaded.
  78. @end deffn
  79. @deffn {Scheme Procedure} dynamic-object? obj
  80. @deffnx {C Function} scm_dynamic_object_p (obj)
  81. Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}
  82. otherwise.
  83. @end deffn
  84. @deffn {Scheme Procedure} dynamic-unlink dobj
  85. @deffnx {C Function} scm_dynamic_unlink (dobj)
  86. Unlink the indicated object file from the application. The
  87. argument @var{dobj} must have been obtained by a call to
  88. @code{dynamic-link}. After @code{dynamic-unlink} has been
  89. called on @var{dobj}, its content is no longer accessible.
  90. @end deffn
  91. @smallexample
  92. (define libgl-obj (dynamic-link "libGL"))
  93. libgl-obj
  94. @result{} #<dynamic-object "libGL">
  95. (dynamic-unlink libGL-obj)
  96. libGL-obj
  97. @result{} #<dynamic-object "libGL" (unlinked)>
  98. @end smallexample
  99. As you can see, after calling @code{dynamic-unlink} on a dynamically
  100. linked library, it is marked as @samp{(unlinked)} and you are no longer
  101. able to use it with @code{dynamic-call}, etc. Whether the library is
  102. really removed from you program is system-dependent and will generally
  103. not happen when some other parts of your program still use it.
  104. When dynamic linking is disabled or not supported on your system,
  105. the above functions throw errors, but they are still available.
  106. @node Foreign Functions
  107. @subsection Foreign Functions
  108. The most natural thing to do with a dynamic library is to grovel around
  109. in it for a function pointer: a @dfn{foreign function}.
  110. @code{dynamic-func} exists for that purpose.
  111. @deffn {Scheme Procedure} dynamic-func name dobj
  112. @deffnx {C Function} scm_dynamic_func (name, dobj)
  113. Return a ``handle'' for the func @var{name} in the shared object referred to
  114. by @var{dobj}. The handle can be passed to @code{dynamic-call} to
  115. actually call the function.
  116. Regardless whether your C compiler prepends an underscore @samp{_} to the global
  117. names in a program, you should @strong{not} include this underscore in
  118. @var{name} since it will be added automatically when necessary.
  119. @end deffn
  120. Guile has static support for calling functions with no arguments,
  121. @code{dynamic-call}.
  122. @deffn {Scheme Procedure} dynamic-call func dobj
  123. @deffnx {C Function} scm_dynamic_call (func, dobj)
  124. Call the C function indicated by @var{func} and @var{dobj}.
  125. The function is passed no arguments and its return value is
  126. ignored. When @var{function} is something returned by
  127. @code{dynamic-func}, call that function and ignore @var{dobj}.
  128. When @var{func} is a string , look it up in @var{dynobj}; this
  129. is equivalent to
  130. @smallexample
  131. (dynamic-call (dynamic-func @var{func} @var{dobj}) #f)
  132. @end smallexample
  133. @end deffn
  134. @code{dynamic-call} is not very powerful. It is mostly intended to be
  135. used for calling specially written initialization functions that will
  136. then add new primitives to Guile. For example, we do not expect that you
  137. will dynamically link @file{libX11} with @code{dynamic-link} and then
  138. construct a beautiful graphical user interface just by using
  139. @code{dynamic-call}. Instead, the usual way would be to write a special
  140. Guile-to-X11 glue library that has intimate knowledge about both Guile
  141. and X11 and does whatever is necessary to make them inter-operate
  142. smoothly. This glue library could then be dynamically linked into a
  143. vanilla Guile interpreter and activated by calling its initialization
  144. function. That function would add all the new types and primitives to
  145. the Guile interpreter that it has to offer.
  146. (There is actually another, better option: simply to create a
  147. @file{libX11} wrapper in Scheme via the dynamic FFI. @xref{Dynamic FFI},
  148. for more information.)
  149. Given some set of C extensions to Guile, the next logical step is to
  150. integrate these glue libraries into the module system of Guile so that
  151. you can load new primitives into a running system just as you can load
  152. new Scheme code.
  153. @deffn {Scheme Procedure} load-extension lib init
  154. @deffnx {C Function} scm_load_extension (lib, init)
  155. Load and initialize the extension designated by LIB and INIT.
  156. When there is no pre-registered function for LIB/INIT, this is
  157. equivalent to
  158. @lisp
  159. (dynamic-call INIT (dynamic-link LIB))
  160. @end lisp
  161. When there is a pre-registered function, that function is called
  162. instead.
  163. Normally, there is no pre-registered function. This option exists
  164. only for situations where dynamic linking is unavailable or unwanted.
  165. In that case, you would statically link your program with the desired
  166. library, and register its init function right after Guile has been
  167. initialized.
  168. As for @code{dynamic-link}, @var{lib} should not contain any suffix such
  169. as @code{.so} (@pxref{Foreign Libraries, dynamic-link}). It
  170. should also not contain any directory components. Libraries that
  171. implement Guile Extensions should be put into the normal locations for
  172. shared libraries. We recommend to use the naming convention
  173. @file{libguile-bla-blum} for a extension related to a module @code{(bla
  174. blum)}.
  175. The normal way for a extension to be used is to write a small Scheme
  176. file that defines a module, and to load the extension into this
  177. module. When the module is auto-loaded, the extension is loaded as
  178. well. For example,
  179. @lisp
  180. (define-module (bla blum))
  181. (load-extension "libguile-bla-blum" "bla_init_blum")
  182. @end lisp
  183. @end deffn
  184. @node C Extensions
  185. @subsection C Extensions
  186. The most interesting application of dynamically linked libraries is
  187. probably to use them for providing @emph{compiled code modules} to
  188. Scheme programs. As much fun as programming in Scheme is, every now and
  189. then comes the need to write some low-level C stuff to make Scheme even
  190. more fun.
  191. Not only can you put these new primitives into their own module (see the
  192. previous section), you can even put them into a shared library that is
  193. only then linked to your running Guile image when it is actually
  194. needed.
  195. An example will hopefully make everything clear. Suppose we want to
  196. make the Bessel functions of the C library available to Scheme in the
  197. module @samp{(math bessel)}. First we need to write the appropriate
  198. glue code to convert the arguments and return values of the functions
  199. from Scheme to C and back. Additionally, we need a function that will
  200. add them to the set of Guile primitives. Because this is just an
  201. example, we will only implement this for the @code{j0} function.
  202. @smallexample
  203. #include <math.h>
  204. #include <libguile.h>
  205. SCM
  206. j0_wrapper (SCM x)
  207. @{
  208. return scm_from_double (j0 (scm_to_double (x, "j0")));
  209. @}
  210. void
  211. init_math_bessel ()
  212. @{
  213. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  214. @}
  215. @end smallexample
  216. We can already try to bring this into action by manually calling the low
  217. level functions for performing dynamic linking. The C source file needs
  218. to be compiled into a shared library. Here is how to do it on
  219. GNU/Linux, please refer to the @code{libtool} documentation for how to
  220. create dynamically linkable libraries portably.
  221. @smallexample
  222. gcc -shared -o libbessel.so -fPIC bessel.c
  223. @end smallexample
  224. Now fire up Guile:
  225. @lisp
  226. (define bessel-lib (dynamic-link "./libbessel.so"))
  227. (dynamic-call "init_math_bessel" bessel-lib)
  228. (j0 2)
  229. @result{} 0.223890779141236
  230. @end lisp
  231. The filename @file{./libbessel.so} should be pointing to the shared
  232. library produced with the @code{gcc} command above, of course. The
  233. second line of the Guile interaction will call the
  234. @code{init_math_bessel} function which in turn will register the C
  235. function @code{j0_wrapper} with the Guile interpreter under the name
  236. @code{j0}. This function becomes immediately available and we can call
  237. it from Scheme.
  238. Fun, isn't it? But we are only half way there. This is what
  239. @code{apropos} has to say about @code{j0}:
  240. @smallexample
  241. (apropos "j0")
  242. @print{} (guile-user): j0 #<primitive-procedure j0>
  243. @end smallexample
  244. As you can see, @code{j0} is contained in the root module, where all
  245. the other Guile primitives like @code{display}, etc live. In general,
  246. a primitive is put into whatever module is the @dfn{current module} at
  247. the time @code{scm_c_define_gsubr} is called.
  248. A compiled module should have a specially named @dfn{module init
  249. function}. Guile knows about this special name and will call that
  250. function automatically after having linked in the shared library. For
  251. our example, we replace @code{init_math_bessel} with the following code in
  252. @file{bessel.c}:
  253. @smallexample
  254. void
  255. init_math_bessel (void *unused)
  256. @{
  257. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  258. scm_c_export ("j0", NULL);
  259. @}
  260. void
  261. scm_init_math_bessel_module ()
  262. @{
  263. scm_c_define_module ("math bessel", init_math_bessel, NULL);
  264. @}
  265. @end smallexample
  266. The general pattern for the name of a module init function is:
  267. @samp{scm_init_}, followed by the name of the module where the
  268. individual hierarchical components are concatenated with underscores,
  269. followed by @samp{_module}.
  270. After @file{libbessel.so} has been rebuilt, we need to place the shared
  271. library into the right place.
  272. Once the module has been correctly installed, it should be possible to
  273. use it like this:
  274. @smallexample
  275. guile> (load-extension "./libbessel.so" "scm_init_math_bessel_module")
  276. guile> (use-modules (math bessel))
  277. guile> (j0 2)
  278. 0.223890779141236
  279. guile> (apropos "j0")
  280. @print{} (math bessel): j0 #<primitive-procedure j0>
  281. @end smallexample
  282. That's it!
  283. @node Modules and Extensions
  284. @subsection Modules and Extensions
  285. The new primitives that you add to Guile with @code{scm_c_define_gsubr}
  286. (@pxref{Primitive Procedures}) or with any of the other mechanisms are
  287. placed into the module that is current when the
  288. @code{scm_c_define_gsubr} is executed. Extensions loaded from the REPL,
  289. for example, will be placed into the @code{(guile-user)} module, if the
  290. REPL module was not changed.
  291. To define C primitives within a specific module, the simplest way is:
  292. @example
  293. (define-module (foo bar))
  294. (load-extension "foobar-c-code" "foo_bar_init")
  295. @end example
  296. @cindex extensiondir
  297. When loaded with @code{(use-modules (foo bar))}, the
  298. @code{load-extension} call looks for the @file{foobar-c-code.so} (etc)
  299. object file in Guile's @code{extensiondir}, which is usually a
  300. subdirectory of the @code{libdir}. For example, if your libdir is
  301. @file{/usr/lib}, the @code{extensiondir} for the Guile @value{EFFECTIVE-VERSION}.@var{x}
  302. series will be @file{/usr/lib/guile/@value{EFFECTIVE-VERSION}/}.
  303. The extension path includes the major and minor version of Guile (the
  304. ``effective version''), because Guile guarantees compatibility within a
  305. given effective version. This allows you to install different versions
  306. of the same extension for different versions of Guile.
  307. If the extension is not found in the @code{extensiondir}, Guile will
  308. also search the standard system locations, such as @file{/usr/lib} or
  309. @file{/usr/local/lib}. It is preferable, however, to keep your extension
  310. out of the system library path, to prevent unintended interference with
  311. other dynamically-linked C libraries.
  312. If someone installs your module to a non-standard location then the
  313. object file won't be found. You can address this by inserting the
  314. install location in the @file{foo/bar.scm} file. This is convenient
  315. for the user and also guarantees the intended object is read, even if
  316. stray older or newer versions are in the loader's path.
  317. The usual way to specify an install location is with a @code{prefix}
  318. at the configure stage, for instance @samp{./configure prefix=/opt}
  319. results in library files as say @file{/opt/lib/foobar-c-code.so}.
  320. When using Autoconf (@pxref{Top, , Introduction, autoconf, The GNU
  321. Autoconf Manual}), the library location is in a @code{libdir}
  322. variable. Its value is intended to be expanded by @command{make}, and
  323. can by substituted into a source file like @file{foo.scm.in}
  324. @example
  325. (define-module (foo bar))
  326. (load-extension "XXextensiondirXX/foobar-c-code" "foo_bar_init")
  327. @end example
  328. @noindent
  329. with the following in a @file{Makefile}, using @command{sed}
  330. (@pxref{Top, , Introduction, sed, SED, A Stream Editor}),
  331. @example
  332. foo.scm: foo.scm.in
  333. sed 's|XXextensiondirXX|$(libdir)/guile/@value{EFFECTIVE-VERSION}|' <foo.scm.in >foo.scm
  334. @end example
  335. The actual pattern @code{XXextensiondirXX} is arbitrary, it's only something
  336. which doesn't otherwise occur. If several modules need the value, it
  337. can be easier to create one @file{foo/config.scm} with a define of the
  338. @code{extensiondir} location, and use that as required.
  339. @example
  340. (define-module (foo config))
  341. (define-public foo-config-extensiondir "XXextensiondirXX"")
  342. @end example
  343. Such a file might have other locations too, for instance a data
  344. directory for auxiliary files, or @code{localedir} if the module has
  345. its own @code{gettext} message catalogue
  346. (@pxref{Internationalization}).
  347. It will be noted all of the above requires that the Scheme code to be
  348. found in @code{%load-path} (@pxref{Load Paths}). Presently it's left up
  349. to the system administrator or each user to augment that path when
  350. installing Guile modules in non-default locations. But having reached
  351. the Scheme code, that code should take care of hitting any of its own
  352. private files etc.
  353. @node Foreign Pointers
  354. @subsection Foreign Pointers
  355. The previous sections have shown how Guile can be extended at runtime by
  356. loading compiled C extensions. This approach is all well and good, but
  357. wouldn't it be nice if we didn't have to write any C at all? This
  358. section takes up the problem of accessing C values from Scheme, and the
  359. next discusses C functions.
  360. @menu
  361. * Foreign Types:: Expressing C types in Scheme.
  362. * Foreign Variables:: Pointers to C symbols.
  363. * Void Pointers and Byte Access:: Pointers into the ether.
  364. * Foreign Structs:: Packing and unpacking structs.
  365. @end menu
  366. @node Foreign Types
  367. @subsubsection Foreign Types
  368. The first impedance mismatch that one sees between C and Scheme is that
  369. in C, the storage locations (variables) are typed, but in Scheme types
  370. are associated with values, not variables. @xref{Values and Variables}.
  371. So when describing a C function or a C structure so that it can be
  372. accessed from Scheme, the data types of the parameters or fields must be
  373. passed explicitly.
  374. These ``C type values'' may be constructed using the constants and
  375. procedures from the @code{(system foreign)} module, which may be loaded
  376. like this:
  377. @example
  378. (use-modules (system foreign))
  379. @end example
  380. @code{(system foreign)} exports a number of values expressing the basic
  381. C types:
  382. @defvr {Scheme Variable} int8
  383. @defvrx {Scheme Variable} uint8
  384. @defvrx {Scheme Variable} uint16
  385. @defvrx {Scheme Variable} int16
  386. @defvrx {Scheme Variable} uint32
  387. @defvrx {Scheme Variable} int32
  388. @defvrx {Scheme Variable} uint64
  389. @defvrx {Scheme Variable} int64
  390. @defvrx {Scheme Variable} float
  391. @defvrx {Scheme Variable} double
  392. These values represent the C numeric types of the specified sizes and
  393. signednesses.
  394. @end defvr
  395. In addition there are some convenience bindings for indicating types of
  396. platform-dependent size:
  397. @defvr {Scheme Variable} int
  398. @defvrx {Scheme Variable} unsigned-int
  399. @defvrx {Scheme Variable} long
  400. @defvrx {Scheme Variable} unsigned-long
  401. @defvrx {Scheme Variable} size_t
  402. @defvrx {Scheme Variable} ssize_t
  403. @defvrx {Scheme Variable} ptrdiff_t
  404. Values exported by the @code{(system foreign)} module, representing C
  405. numeric types. For example, @code{long} may be @code{equal?} to
  406. @code{int64} on a 64-bit platform.
  407. @end defvr
  408. @defvr {Scheme Variable} void
  409. The @code{void} type. It can be used as the first argument to
  410. @code{pointer->procedure} to wrap a C function that returns nothing.
  411. @end defvr
  412. In addition, the symbol @code{*} is used by convention to denote pointer
  413. types. Procedures detailed in the following sections, such as
  414. @code{pointer->procedure}, accept it as a type descriptor.
  415. @node Foreign Variables
  416. @subsubsection Foreign Variables
  417. Pointers to variables in the current address space may be looked up
  418. dynamically using @code{dynamic-pointer}.
  419. @deffn {Scheme Procedure} dynamic-pointer name dobj
  420. @deffnx {C Function} scm_dynamic_pointer (name, dobj)
  421. Return a ``wrapped pointer'' for the symbol @var{name} in the shared
  422. object referred to by @var{dobj}. The returned pointer points to a C
  423. object.
  424. Regardless whether your C compiler prepends an underscore @samp{_} to the global
  425. names in a program, you should @strong{not} include this underscore in
  426. @var{name} since it will be added automatically when necessary.
  427. @end deffn
  428. For example, currently Guile has a variable, @code{scm_numptob}, as part
  429. of its API. It is declared as a C @code{long}. So, to create a handle
  430. pointing to that foreign value, we do:
  431. @example
  432. (use-modules (system foreign))
  433. (define numptob (dynamic-pointer "scm_numptob" (dynamic-link)))
  434. numptob
  435. @result{} #<pointer 0x7fb35b1b4688>
  436. @end example
  437. (The next section discusses ways to dereference pointers.)
  438. A value returned by @code{dynamic-pointer} is a Scheme wrapper for a C
  439. pointer.
  440. @deffn {Scheme Procedure} pointer-address pointer
  441. @deffnx {C Function} scm_pointer_address (pointer)
  442. Return the numerical value of @var{pointer}.
  443. @example
  444. (pointer-address numptob)
  445. @result{} 139984413364296 ; YMMV
  446. @end example
  447. @end deffn
  448. @deffn {Scheme Procedure} make-pointer address [finalizer]
  449. Return a foreign pointer object pointing to @var{address}. If
  450. @var{finalizer} is passed, it should be a pointer to a one-argument C
  451. function that will be called when the pointer object becomes
  452. unreachable.
  453. @end deffn
  454. @deffn {Scheme Procedure} pointer? obj
  455. Return @code{#t} if @var{obj} is a pointer object, @code{#f} otherwise.
  456. @end deffn
  457. @defvr {Scheme Variable} %null-pointer
  458. A foreign pointer whose value is 0.
  459. @end defvr
  460. @deffn {Scheme Procedure} null-pointer? pointer
  461. Return @code{#t} if @var{pointer} is the null pointer, @code{#f} otherwise.
  462. @end deffn
  463. For the purpose of passing SCM values directly to foreign functions, and
  464. allowing them to return SCM values, Guile also supports some unsafe
  465. casting operators.
  466. @deffn {Scheme Procedure} scm->pointer scm
  467. Return a foreign pointer object with the @code{object-address}
  468. of @var{scm}.
  469. @end deffn
  470. @deffn {Scheme Procedure} pointer->scm pointer
  471. Unsafely cast @var{pointer} to a Scheme object.
  472. Cross your fingers!
  473. @end deffn
  474. Sometimes you want to give C extensions access to the dynamic FFI. At
  475. that point, the names get confusing, because ``pointer'' can refer to a
  476. @code{SCM} object that wraps a pointer, or to a @code{void*} value. We
  477. will try to use ``pointer object'' to refer to Scheme objects, and
  478. ``pointer value'' to refer to @code{void *} values.
  479. @deftypefn {C Function} SCM scm_from_pointer (void *ptr, void (*finalizer) (void*))
  480. Create a pointer object from a pointer value.
  481. If @var{finalizer} is non-null, Guile arranges to call it on the pointer
  482. value at some point after the pointer object becomes collectable.
  483. @end deftypefn
  484. @deftypefn {C Function} void* scm_to_pointer (SCM obj)
  485. Unpack the pointer value from a pointer object.
  486. @end deftypefn
  487. @node Void Pointers and Byte Access
  488. @subsubsection Void Pointers and Byte Access
  489. Wrapped pointers are untyped, so they are essentially equivalent to C
  490. @code{void} pointers. As in C, the memory region pointed to by a
  491. pointer can be accessed at the byte level. This is achieved using
  492. @emph{bytevectors} (@pxref{Bytevectors}). The @code{(rnrs bytevector)}
  493. module contains procedures that can be used to convert byte sequences to
  494. Scheme objects such as strings, floating point numbers, or integers.
  495. @deffn {Scheme Procedure} pointer->bytevector pointer len [offset [uvec_type]]
  496. @deffnx {C Function} scm_pointer_to_bytevector (pointer, len, offset, uvec_type)
  497. Return a bytevector aliasing the @var{len} bytes pointed to by
  498. @var{pointer}.
  499. The user may specify an alternate default interpretation for
  500. the memory by passing the @var{uvec_type} argument, to indicate
  501. that the memory is an array of elements of that type.
  502. @var{uvec_type} should be something that
  503. @code{uniform-vector-element-type} would return, like @code{f32}
  504. or @code{s16}.
  505. When @var{offset} is passed, it specifies the offset in bytes relative
  506. to @var{pointer} of the memory region aliased by the returned
  507. bytevector.
  508. Mutating the returned bytevector mutates the memory pointed to by
  509. @var{pointer}, so buckle your seatbelts.
  510. @end deffn
  511. @deffn {Scheme Procedure} bytevector->pointer bv [offset]
  512. @deffnx {C Function} scm_bytevector_to_pointer (bv, offset)
  513. Return a pointer pointer aliasing the memory pointed to by @var{bv} or
  514. @var{offset} bytes after @var{bv} when @var{offset} is passed.
  515. @end deffn
  516. In addition to these primitives, convenience procedures are available:
  517. @deffn {Scheme Procedure} dereference-pointer pointer
  518. Assuming @var{pointer} points to a memory region that holds a pointer,
  519. return this pointer.
  520. @end deffn
  521. @deffn {Scheme Procedure} string->pointer string [encoding]
  522. Return a foreign pointer to a nul-terminated copy of @var{string} in the
  523. given @var{encoding}, defaulting to the current locale encoding. The C
  524. string is freed when the returned foreign pointer becomes unreachable.
  525. This is the Scheme equivalent of @code{scm_to_stringn}.
  526. @end deffn
  527. @deffn {Scheme Procedure} pointer->string pointer [length] [encoding]
  528. Return the string representing the C string pointed to by @var{pointer}.
  529. If @var{length} is omitted or @code{-1}, the string is assumed to be
  530. nul-terminated. Otherwise @var{length} is the number of bytes in memory
  531. pointed to by @var{pointer}. The C string is assumed to be in the given
  532. @var{encoding}, defaulting to the current locale encoding.
  533. This is the Scheme equivalent of @code{scm_from_stringn}.
  534. @end deffn
  535. @cindex wrapped pointer types
  536. Most object-oriented C libraries use pointers to specific data
  537. structures to identify objects. It is useful in such cases to reify the
  538. different pointer types as disjoint Scheme types. The
  539. @code{define-wrapped-pointer-type} macro simplifies this.
  540. @deffn {Scheme Syntax} define-wrapped-pointer-type type-name pred wrap unwrap print
  541. Define helper procedures to wrap pointer objects into Scheme objects
  542. with a disjoint type. Specifically, this macro defines:
  543. @itemize
  544. @item @var{pred}, a predicate for the new Scheme type;
  545. @item @var{wrap}, a procedure that takes a pointer object and returns an
  546. object that satisfies @var{pred};
  547. @item @var{unwrap}, which does the reverse.
  548. @end itemize
  549. @var{wrap} preserves pointer identity, for two pointer objects @var{p1}
  550. and @var{p2} that are @code{equal?}, @code{(eq? (@var{wrap} @var{p1})
  551. (@var{wrap} @var{p2})) @result{} #t}.
  552. Finally, @var{print} should name a user-defined procedure to print such
  553. objects. The procedure is passed the wrapped object and a port to write
  554. to.
  555. For example, assume we are wrapping a C library that defines a type,
  556. @code{bottle_t}, and functions that can be passed @code{bottle_t *}
  557. pointers to manipulate them. We could write:
  558. @example
  559. (define-wrapped-pointer-type bottle
  560. bottle?
  561. wrap-bottle unwrap-bottle
  562. (lambda (b p)
  563. (format p "#<bottle of ~a ~x>"
  564. (bottle-contents b)
  565. (pointer-address (unwrap-bottle b)))))
  566. (define grab-bottle
  567. ;; Wrapper for `bottle_t *grab (void)'.
  568. (let ((grab (pointer->procedure '*
  569. (dynamic-func "grab_bottle" libbottle)
  570. '())))
  571. (lambda ()
  572. "Return a new bottle."
  573. (wrap-bottle (grab)))))
  574. (define bottle-contents
  575. ;; Wrapper for `const char *bottle_contents (bottle_t *)'.
  576. (let ((contents (pointer->procedure '*
  577. (dynamic-func "bottle_contents"
  578. libbottle)
  579. '(*))))
  580. (lambda (b)
  581. "Return the contents of B."
  582. (pointer->string (contents (unwrap-bottle b))))))
  583. (write (grab-bottle))
  584. @result{} #<bottle of Ch@^ateau Haut-Brion 803d36>
  585. @end example
  586. In this example, @code{grab-bottle} is guaranteed to return a genuine
  587. @code{bottle} object satisfying @code{bottle?}. Likewise,
  588. @code{bottle-contents} errors out when its argument is not a genuine
  589. @code{bottle} object.
  590. @end deffn
  591. Going back to the @code{scm_numptob} example above, here is how we can
  592. read its value as a C @code{long} integer:
  593. @example
  594. (use-modules (rnrs bytevectors))
  595. (bytevector-uint-ref (pointer->bytevector numptob (sizeof long))
  596. 0 (native-endianness)
  597. (sizeof long))
  598. @result{} 8
  599. @end example
  600. If we wanted to corrupt Guile's internal state, we could set
  601. @code{scm_numptob} to another value; but we shouldn't, because that
  602. variable is not meant to be set. Indeed this point applies more widely:
  603. the C API is a dangerous place to be. Not only might setting a value
  604. crash your program, simply accessing the data pointed to by a dangling
  605. pointer or similar can prove equally disastrous.
  606. @node Foreign Structs
  607. @subsubsection Foreign Structs
  608. Finally, one last note on foreign values before moving on to actually
  609. calling foreign functions. Sometimes you need to deal with C structs,
  610. which requires interpreting each element of the struct according to the
  611. its type, offset, and alignment. Guile has some primitives to support
  612. this.
  613. @deffn {Scheme Procedure} sizeof type
  614. @deffnx {C Function} scm_sizeof (type)
  615. Return the size of @var{type}, in bytes.
  616. @var{type} should be a valid C type, like @code{int}.
  617. Alternately @var{type} may be the symbol @code{*}, in which
  618. case the size of a pointer is returned. @var{type} may
  619. also be a list of types, in which case the size of a
  620. @code{struct} with ABI-conventional packing is returned.
  621. @end deffn
  622. @deffn {Scheme Procedure} alignof type
  623. @deffnx {C Function} scm_alignof (type)
  624. Return the alignment of @var{type}, in bytes.
  625. @var{type} should be a valid C type, like @code{int}.
  626. Alternately @var{type} may be the symbol @code{*}, in which
  627. case the alignment of a pointer is returned. @var{type} may
  628. also be a list of types, in which case the alignment of a
  629. @code{struct} with ABI-conventional packing is returned.
  630. @end deffn
  631. Guile also provides some convenience methods to pack and unpack foreign
  632. pointers wrapping C structs.
  633. @deffn {Scheme Procedure} make-c-struct types vals
  634. Create a foreign pointer to a C struct containing @var{vals} with types
  635. @code{types}.
  636. @var{vals} and @code{types} should be lists of the same length.
  637. @end deffn
  638. @deffn {Scheme Procedure} parse-c-struct foreign types
  639. Parse a foreign pointer to a C struct, returning a list of values.
  640. @code{types} should be a list of C types.
  641. @end deffn
  642. For example, to create and parse the equivalent of a @code{struct @{
  643. int64_t a; uint8_t b; @}}:
  644. @example
  645. (parse-c-struct (make-c-struct (list int64 uint8)
  646. (list 300 43))
  647. (list int64 uint8))
  648. @result{} (300 43)
  649. @end example
  650. As yet, Guile only has convenience routines to support
  651. conventionally-packed structs. But given the @code{bytevector->pointer}
  652. and @code{pointer->bytevector} routines, one can create and parse
  653. tightly packed structs and unions by hand. See the code for
  654. @code{(system foreign)} for details.
  655. @node Dynamic FFI
  656. @subsection Dynamic FFI
  657. Of course, the land of C is not all nouns and no verbs: there are
  658. functions too, and Guile allows you to call them.
  659. @deffn {Scheme Procedure} pointer->procedure return_type func_ptr arg_types
  660. @deffnx {C Procedure} scm_pointer_to_procedure (return_type, func_ptr, arg_types)
  661. Make a foreign function.
  662. Given the foreign void pointer @var{func_ptr}, its argument and
  663. return types @var{arg_types} and @var{return_type}, return a
  664. procedure that will pass arguments to the foreign function
  665. and return appropriate values.
  666. @var{arg_types} should be a list of foreign types.
  667. @code{return_type} should be a foreign type. @xref{Foreign Types}, for
  668. more information on foreign types.
  669. @end deffn
  670. Here is a better definition of @code{(math bessel)}:
  671. @example
  672. (define-module (math bessel)
  673. #:use-module (system foreign)
  674. #:export (j0))
  675. (define libm (dynamic-link "libm"))
  676. (define j0
  677. (pointer->procedure double
  678. (dynamic-func "j0" libm)
  679. (list double)))
  680. @end example
  681. That's it! No C at all.
  682. Numeric arguments and return values from foreign functions are
  683. represented as Scheme values. For example, @code{j0} in the above
  684. example takes a Scheme number as its argument, and returns a Scheme
  685. number.
  686. Pointers may be passed to and returned from foreign functions as well.
  687. In that case the type of the argument or return value should be the
  688. symbol @code{*}, indicating a pointer. For example, the following
  689. code makes @code{memcpy} available to Scheme:
  690. @example
  691. (define memcpy
  692. (let ((this (dynamic-link)))
  693. (pointer->procedure '*
  694. (dynamic-func "memcpy" this)
  695. (list '* '* size_t))))
  696. @end example
  697. To invoke @code{memcpy}, one must pass it foreign pointers:
  698. @example
  699. (use-modules (rnrs bytevectors))
  700. (define src-bits
  701. (u8-list->bytevector '(0 1 2 3 4 5 6 7)))
  702. (define src
  703. (bytevector->pointer src-bits))
  704. (define dest
  705. (bytevector->pointer (make-bytevector 16 0)))
  706. (memcpy dest src (bytevector-length src-bits))
  707. (bytevector->u8-list (pointer->bytevector dest 16))
  708. @result{} (0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0)
  709. @end example
  710. One may also pass structs as values, passing structs as foreign
  711. pointers. @xref{Foreign Structs}, for more information on how to express
  712. struct types and struct values.
  713. ``Out'' arguments are passed as foreign pointers. The memory pointed to
  714. by the foreign pointer is mutated in place.
  715. @example
  716. ;; struct timeval @{
  717. ;; time_t tv_sec; /* seconds */
  718. ;; suseconds_t tv_usec; /* microseconds */
  719. ;; @};
  720. ;; assuming fields are of type "long"
  721. (define gettimeofday
  722. (let ((f (pointer->procedure
  723. int
  724. (dynamic-func "gettimeofday" (dynamic-link))
  725. (list '* '*)))
  726. (tv-type (list long long)))
  727. (lambda ()
  728. (let* ((timeval (make-c-struct tv-type (list 0 0)))
  729. (ret (f timeval %null-pointer)))
  730. (if (zero? ret)
  731. (apply values (parse-c-struct timeval tv-type))
  732. (error "gettimeofday returned an error" ret))))))
  733. (gettimeofday)
  734. @result{} 1270587589
  735. @result{} 499553
  736. @end example
  737. As you can see, this interface to foreign functions is at a very low,
  738. somewhat dangerous level@footnote{A contribution to Guile in the form of
  739. a high-level FFI would be most welcome.}.
  740. @cindex callbacks
  741. The FFI can also work in the opposite direction: making Scheme
  742. procedures callable from C. This makes it possible to use Scheme
  743. procedures as ``callbacks'' expected by C function.
  744. @deffn {Scheme Procedure} procedure->pointer return-type proc arg-types
  745. @deffnx {C Function} scm_procedure_to_pointer (return_type, proc, arg_types)
  746. Return a pointer to a C function of type @var{return-type}
  747. taking arguments of types @var{arg-types} (a list) and
  748. behaving as a proxy to procedure @var{proc}. Thus
  749. @var{proc}'s arity, supported argument types, and return
  750. type should match @var{return-type} and @var{arg-types}.
  751. @end deffn
  752. As an example, here's how the C library's @code{qsort} array sorting
  753. function can be made accessible to Scheme (@pxref{Array Sort Function,
  754. @code{qsort},, libc, The GNU C Library Reference Manual}):
  755. @example
  756. (define qsort!
  757. (let ((qsort (pointer->procedure void
  758. (dynamic-func "qsort"
  759. (dynamic-link))
  760. (list '* size_t size_t '*))))
  761. (lambda (bv compare)
  762. ;; Sort bytevector BV in-place according to comparison
  763. ;; procedure COMPARE.
  764. (let ((ptr (procedure->pointer int
  765. (lambda (x y)
  766. ;; X and Y are pointers so,
  767. ;; for convenience, dereference
  768. ;; them before calling COMPARE.
  769. (compare (dereference-uint8* x)
  770. (dereference-uint8* y)))
  771. (list '* '*))))
  772. (qsort (bytevector->pointer bv)
  773. (bytevector-length bv) 1 ;; we're sorting bytes
  774. ptr)))))
  775. (define (dereference-uint8* ptr)
  776. ;; Helper function: dereference the byte pointed to by PTR.
  777. (let ((b (pointer->bytevector ptr 1)))
  778. (bytevector-u8-ref b 0)))
  779. (define bv
  780. ;; An unsorted array of bytes.
  781. (u8-list->bytevector '(7 1 127 3 5 4 77 2 9 0)))
  782. ;; Sort BV.
  783. (qsort! bv (lambda (x y) (- x y)))
  784. ;; Let's see what the sorted array looks like:
  785. (bytevector->u8-list bv)
  786. @result{} (0 1 2 3 4 5 7 9 77 127)
  787. @end example
  788. And voil@`a!
  789. Note that @code{procedure->pointer} is not supported (and not defined)
  790. on a few exotic architectures. Thus, user code may need to check
  791. @code{(defined? 'procedure->pointer)}. Nevertheless, it is available on
  792. many architectures, including (as of libffi 3.0.9) x86, ia64, SPARC,
  793. PowerPC, ARM, and MIPS, to name a few.
  794. @c Local Variables:
  795. @c TeX-master: "guile.texi"
  796. @c End: