api-foreign.texi 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000-2004, 2007-2014, 2016-2017, 2021
  4. @c 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. Sometimes you need to use libraries written in C or Rust or some other
  11. non-Scheme language. More rarely, you might need to write some C to
  12. extend Guile. This section describes how to load these ``foreign
  13. libraries'', look up data and functions inside them, and so on.
  14. @menu
  15. * Foreign Libraries:: Dynamically linking to libraries.
  16. * Foreign Extensions:: Extending Guile in C with loadable modules.
  17. * Foreign Pointers:: Pointers to C data or functions.
  18. * Foreign Types:: Expressing C types in Scheme.
  19. * Foreign Functions:: Simple calls to C procedures.
  20. * Void Pointers and Byte Access:: Pointers into the ether.
  21. * Foreign Structs:: Packing and unpacking structs.
  22. * More Foreign Functions:: Advanced examples.
  23. @end menu
  24. @node Foreign Libraries
  25. @subsection Foreign Libraries
  26. Just as Guile can load up Scheme libraries at run-time, Guile can also
  27. load some system libraries written in C or other low-level languages.
  28. We refer to these as dynamically-loadable modules as @dfn{foreign
  29. libraries}, to distinguish them from native libraries written in Scheme
  30. or other languages implemented by Guile.
  31. @cindex foreign libraries
  32. @cindex libraries, foreign
  33. Foreign libraries usually come in two forms. Some foreign libraries are
  34. part of the operating system, such as the compression library
  35. @code{libz}. These shared libraries are built in such a way that many
  36. programs can use their functionality without duplicating their code.
  37. When a program written in C is built, it can declare that it uses a
  38. specific set of shared libraries.
  39. @cindex shared libraries
  40. @cindex libraries, shared
  41. When the program is run, the operating system takes care of locating and
  42. loading the shared libraries.
  43. The operating system components that can dynamically load and link
  44. shared libraries when a program is run are also available
  45. programmatically during a program's execution. This is the interface
  46. that's most useful for Guile, and this is what we mean in Guile when we
  47. refer to @dfn{dynamic linking}. Dynamic linking at run-time is
  48. sometimes called @dfn{dlopening}, to distinguish it from the dynamic
  49. linking that happens at program start-up.
  50. @cindex dynamic linking
  51. @cindex dlopening
  52. The other kind of foreign library is sometimes known as a module,
  53. plug-in, bundle, or an extension. These foreign libraries aren't meant
  54. to be linked to by C programs, but rather only to be dynamically loaded
  55. at run-time -- they extend some main program with functionality, but
  56. don't stand on their own. Sometimes a Guile library will implement some
  57. of its functionality in a loadable module.
  58. In either case, the interface on the Guile side is the same. You load
  59. the interface using @code{load-foreign-library}. The resulting foreign
  60. library object implements a simple lookup interface whereby the user can
  61. get addresses of data or code exported by the library. There is no
  62. facility to inspect foreign libraries; you have to know what's in there
  63. already before you look.
  64. Routines for loading foreign libraries and accessing their contents are
  65. implemented in the @code{(system foreign-library)} module.
  66. @example
  67. (use-modules (system foreign-library))
  68. @end example
  69. @deffn {Scheme Procedure} load-foreign-library [library] @
  70. [#:extensions=system-library-extensions] @
  71. [#:search-ltdl-library-path?=#t] @
  72. [#:search-path=search-path] @
  73. [#:search-system-paths?=#t] [#:lazy?=#t] [#:global=#f]
  74. Find the shared library denoted by @var{library} (a string or @code{#f})
  75. and link it into the running Guile application. When everything works
  76. out, return a Scheme object suitable for representing the linked object
  77. file. Otherwise an error is thrown.
  78. If @var{library} argument is omitted, it defaults to @code{#f}. If
  79. @code{library} is false, the resulting foreign library gives access to
  80. all symbols available for dynamic linking in the main binary.
  81. It is not necessary to include any extension such as @code{.so} in
  82. @var{library}. For each system, Guile has a default set of extensions
  83. that it will try. On GNU systems, the default extension set is just
  84. @code{.so}; on Windows, just @code{.dll}; and on Darwin (Mac OS), it is
  85. @code{.bundle}, @code{.so}, and @code{.dylib}. Pass @code{#:extensions
  86. @var{extensions}} to override the default extensions list. If
  87. @var{library} contains one of the extensions, no extensions are tried,
  88. so it is possible to specify the extension if you know exactly what file
  89. to load.
  90. Unless @var{library} denotes an absolute file name or otherwise contains
  91. a directory separator (@code{/}, and also @code{\} on Windows), Guile
  92. will search for the library in the directories listed in
  93. @var{search-paths}. The default search path has three components, which
  94. can all be overriden by colon-delimited (semicolon on Windows)
  95. environment variables:
  96. @table @env
  97. @item GUILE_EXTENSIONS_PATH
  98. This is the main environment variable for users to add directories
  99. containing Guile extensions. The default value has no entries. This
  100. environment variable was added in Guile 3.0.6.
  101. @item LTDL_LIBRARY_PATH
  102. Before Guile 3.0.6, Guile loaded foreign libraries using @code{libltdl},
  103. the dynamic library loader provided by libtool. This loader used
  104. @env{LTDL_LIBRARY_PATH}, and for backwards compatibility we still
  105. support that path.
  106. However, @code{libltdl} would not only open @code{.so} (or @code{.dll}
  107. and so on) files, but also the @code{.la} files created by libtool. In
  108. installed libraries -- libraries that are in the target directories of
  109. @code{make install} -- @code{.la} files are never needed, to the extent
  110. that most GNU/Linux distributions remove them entirely. It is
  111. sufficient to just load the @code{.so} (or @code{.dll} and so on) files,
  112. which are always located in the same directory as the @code{.la} files.
  113. But for uninstalled dynamic libraries, like those in a build tree, the
  114. situation is a bit of a mess. If you have a project that uses libtool
  115. to build libraries -- which is the case for Guile, and for most projects
  116. using autotools -- and you build @file{foo.so} in directory @file{D},
  117. libtool will put @file{foo.la} in @file{D}, but @file{foo.so} gets put
  118. into @file{D/.libs}.
  119. Users were mostly oblivious to this situation, as @code{libltdl} had
  120. special logic to be able to read the @code{.la} file to know where to
  121. find the @code{.so}, even from an uninstalled build tree, preventing the
  122. existence of @file{.libs} from leaking out to the user.
  123. We don't use libltdl now, essentially for flexibility and
  124. error-reporting reasons. But, to keep this old use-case working, if
  125. @var{search-ltdl-library-path?} is true, we add each entry of
  126. @code{LTDL_LIBRARY_PATH} to the default extensions load path,
  127. additionally adding the @file{.libs} subdirextories for each entry, in
  128. case there are @file{.so} files there instead of alongside the
  129. @file{.la} files.
  130. @item GUILE_SYSTEM_EXTENSIONS_PATH
  131. The last path in Guile's search path belongs to Guile itself, and
  132. defaults to the libdir and the extensiondir, in that order. For
  133. example, if you install to @file{/opt/guile}, these would probably be
  134. @file{/opt/guile/lib} and
  135. @code{/opt/guile/lib/guile/@value{EFFECTIVE-VERSION}/extensions},
  136. respectively. @xref{Parallel Installations}, for more details on
  137. @code{extensionsdir}.
  138. @end table
  139. Finally, if no library is found in the search path, and if @var{library}
  140. is not absolute and does not include directory separators, and if
  141. @var{search-system-paths?} is true, the operating system may have its
  142. own logic for where to locate @var{library}. For example, on GNU, there
  143. will be a default set of paths (often @file{/usr/lib} and @file{/lib},
  144. though it depends on the system), and the @code{LD_LIBRARY_PATH}
  145. environment variable can add additional paths. Other operating systems
  146. have other conventions.
  147. Falling back to the operating system for search is usually not a great
  148. thing; it is a recipe for making programs that work on one machine but
  149. not on others. Still, when wrapping system libraries, it can be the
  150. only way to get things working at all.
  151. If @var{lazy?} is true (the default), Guile will request the operating
  152. system to resolve symbols used by the loaded library as they are first
  153. used. If @var{global?} is true, symbols defined by the loaded library
  154. will be available when other modules need to resolve symbols; the
  155. default is @code{#f}, which keeps symbols local.
  156. @end deffn
  157. The environment variables mentioned above are parsed when the
  158. foreign-library module is first loaded and bound to parameters. Null
  159. path components, for example the three components of
  160. @env{GUILE_SYSTEM_EXTENSIONS_PATH="::"}, are ignored.
  161. @deffn {Scheme Parameter} guile-extensions-path
  162. @deffnx {Scheme Parameter} ltdl-library-path
  163. @deffnx {Scheme Parameter} guile-system-extensions-path
  164. Parameters whose initial values are taken from
  165. @env{GUILE_EXTENSIONS_PATH}, @env{LTDL_LIBRARY_PATH}, and
  166. @env{GUILE_SYSTEM_EXTENSIONS_PATH}, respectively. @xref{Parameters}.
  167. The current values of these parameters are used when building the search
  168. path when @code{load-foreign-library} is called, unless the caller
  169. explicitly passes a @code{#:search-path} argument.
  170. @end deffn
  171. @deffn {Scheme Procedure} foreign-library? obj
  172. Return @code{#t} if @var{obj} is a foreign library, or @code{#f}
  173. otherwise.
  174. @end deffn
  175. @node Foreign Extensions
  176. @subsection Foreign Extensions
  177. One way to use shared libraries is to extend Guile. Such loadable
  178. modules generally define one distinguished initialization function that,
  179. when called, will use the @code{libguile} API to define procedures in
  180. the current module.
  181. Concretely, you might extend Guile with an implementation of the Bessel
  182. function, @code{j0}:
  183. @smallexample
  184. #include <math.h>
  185. #include <libguile.h>
  186. SCM
  187. j0_wrapper (SCM x)
  188. @{
  189. return scm_from_double (j0 (scm_to_double (x, "j0")));
  190. @}
  191. void
  192. init_math_bessel (void)
  193. @{
  194. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  195. @}
  196. @end smallexample
  197. The C source file would then need to be compiled into a shared library.
  198. On GNU/Linux, the compiler invocation might look like this:
  199. @smallexample
  200. gcc -shared -o bessel.so -fPIC bessel.c
  201. @end smallexample
  202. A good default place to put shared libraries that extend Guile is into
  203. the extensions dir. From the command line or a build script, invoke
  204. @code{pkg-config --variable=extensionsdir
  205. guile-@value{EFFECTIVE-VERSION}} to print the extensions dir.
  206. @xref{Parallel Installations}, for more details.
  207. Guile can load up @code{bessel.so} via @code{load-extension}.
  208. @deffn {Scheme Procedure} load-extension lib init
  209. @deffnx {C Function} scm_load_extension (lib, init)
  210. Load and initialize the extension designated by LIB and INIT.
  211. @end deffn
  212. The normal way for a extension to be used is to write a small Scheme
  213. file that defines a module, and to load the extension into this
  214. module. When the module is auto-loaded, the extension is loaded as
  215. well. For example:
  216. @lisp
  217. (define-module (math bessel)
  218. #:export (j0))
  219. (load-extension "bessel" "init_math_bessel")
  220. @end lisp
  221. This @code{load-extension} invocation loads the @code{bessel} library
  222. via @code{(load-foreign-library "bessel")}, then looks up the
  223. @code{init_math_bessel} symbol in the library, treating it as a function
  224. of no arguments, and calls that function.
  225. If you decide to put your extension outside the default search path for
  226. @code{load-foreign-library}, probably you should adapt the Scheme module
  227. to specify its absolute path. For example, if you use @code{automake}
  228. to build your extension and place it in @code{$(pkglibdir)}, you might
  229. define a build-parameters module that gets created by the build system:
  230. @example
  231. (define-module (math config)
  232. #:export (extensiondir))
  233. (define extensiondir "PKGLIBDIR")
  234. @end example
  235. This file would be @code{config.scm.in}. You would define a @code{make}
  236. rule to substitute in the absolute installed file name:
  237. @example
  238. config.scm: config.scm.in
  239. sed 's|PKGLIBDIR|$(pkglibdir)|' <$< >$@
  240. @end example
  241. Then your @code{(math bessel)} would import @code{(math config)}, then
  242. @code{(load-extension (in-vicinity extensiondir "bessel")
  243. "init_math_bessel")}.
  244. An alternate approach would be to rebind the
  245. @code{guile-extensions-path} parameter, or its corresponding environment
  246. variable, but note that changing those parameters applies to other users
  247. of @code{load-foreign-library} as well.
  248. Note that the new primitives that the extension adds to Guile with
  249. @code{scm_c_define_gsubr} (@pxref{Primitive Procedures}) or with any of
  250. the other mechanisms are placed into the module that is current when the
  251. @code{scm_c_define_gsubr} is executed, so to be clear about what goes
  252. vwhere it's best to include the @code{load-extension} in a module, as
  253. above. Alternately, the C code can use @code{scm_c_define_module} to
  254. specify which module is being created:
  255. @smallexample
  256. static void
  257. do_init (void *unused)
  258. @{
  259. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  260. scm_c_export ("j0", NULL);
  261. @}
  262. void
  263. init_math_bessel ()
  264. @{
  265. scm_c_define_module ("math bessel", do_init, NULL);
  266. @}
  267. @end smallexample
  268. And yet... if what we want is just the @code{j0} function, it seems like
  269. a lot of ceremony to have to compile a Guile-specific wrapper library
  270. complete with an initialization function and wraper module to allow
  271. Guile users to call it. There is another way, but to get there, we have
  272. to talk about function pointers and function types first. @xref{Foreign
  273. Functions}, to skip to the good parts.
  274. @node Foreign Pointers
  275. @subsection Foreign Pointers
  276. Foreign libraries are essentially key-value mappings, where the keys are
  277. names of definitions and the values are the addresses of those
  278. definitions. To look up the address of a definition, use
  279. @code{foreign-library-pointer} from the @code{(system foreign-library)}
  280. module.
  281. @deffn {Scheme Procedure} foreign-library-pointer lib name
  282. Return a ``wrapped pointer'' for the symbol @var{name} in the shared
  283. object referred to by @var{lib}. The returned pointer points to a C
  284. object.
  285. As a convenience, if @var{lib} is not a foreign library, it will be
  286. passed to @code{load-foreign-library}.
  287. @end deffn
  288. If we continue with the @code{bessel.so} example from before, we can get
  289. the address of the @code{init_math_bessel} function via:
  290. @example
  291. (use-modules (system foreign-library))
  292. (define init (foreign-library-pointer "bessel" "init_math_bessel"))
  293. init
  294. @result{} #<pointer 0x7fb35b1b4688>
  295. @end example
  296. A value returned by @code{foreign-library-pointer} is a Scheme wrapper
  297. for a C pointer. Pointers are a data type in Guile that is disjoint
  298. from all other types. The next section discusses ways to dereference
  299. pointers, but before then we describe the usual type predicates and so
  300. on.
  301. Note that the rest of the interfaces in this section are part of the
  302. @code{(system foreign)} library:
  303. @example
  304. (use-modules (system foreign))
  305. @end example
  306. @deffn {Scheme Procedure} pointer-address pointer
  307. @deffnx {C Function} scm_pointer_address (pointer)
  308. Return the numerical value of @var{pointer}.
  309. @example
  310. (pointer-address init)
  311. @result{} 139984413364296 ; YMMV
  312. @end example
  313. @end deffn
  314. @deffn {Scheme Procedure} make-pointer address [finalizer]
  315. Return a foreign pointer object pointing to @var{address}. If
  316. @var{finalizer} is passed, it should be a pointer to a one-argument C
  317. function that will be called when the pointer object becomes
  318. unreachable.
  319. @end deffn
  320. @deffn {Scheme Procedure} pointer? obj
  321. Return @code{#t} if @var{obj} is a pointer object, or @code{#f}
  322. otherwise.
  323. @end deffn
  324. @defvr {Scheme Variable} %null-pointer
  325. A foreign pointer whose value is 0.
  326. @end defvr
  327. @deffn {Scheme Procedure} null-pointer? pointer
  328. Return @code{#t} if @var{pointer} is the null pointer, @code{#f} otherwise.
  329. @end deffn
  330. For the purpose of passing SCM values directly to foreign functions, and
  331. allowing them to return SCM values, Guile also supports some unsafe
  332. casting operators.
  333. @deffn {Scheme Procedure} scm->pointer scm
  334. Return a foreign pointer object with the @code{object-address}
  335. of @var{scm}.
  336. @end deffn
  337. @deffn {Scheme Procedure} pointer->scm pointer
  338. Unsafely cast @var{pointer} to a Scheme object.
  339. Cross your fingers!
  340. @end deffn
  341. Sometimes you want to give C extensions access to the dynamic FFI. At
  342. that point, the names get confusing, because ``pointer'' can refer to a
  343. @code{SCM} object that wraps a pointer, or to a @code{void*} value. We
  344. will try to use ``pointer object'' to refer to Scheme objects, and
  345. ``pointer value'' to refer to @code{void *} values.
  346. @deftypefn {C Function} SCM scm_from_pointer (void *ptr, void (*finalizer) (void*))
  347. Create a pointer object from a pointer value.
  348. If @var{finalizer} is non-null, Guile arranges to call it on the pointer
  349. value at some point after the pointer object becomes collectable.
  350. @end deftypefn
  351. @deftypefn {C Function} void* scm_to_pointer (SCM obj)
  352. Unpack the pointer value from a pointer object.
  353. @end deftypefn
  354. @node Foreign Types
  355. @subsection Foreign Types
  356. From Scheme's perspective, foreign pointers are shards of chaos. The
  357. user can create a foreign pointer for any address, and do with it what
  358. they will. The only thing that lends a sense of order to the whole is a
  359. shared hallucination that certain storage locations have certain types.
  360. When making Scheme wrappers for foreign interfaces, we hide the madness
  361. by explicitly representing the the data types of parameters and fields.
  362. These ``foreign type values'' may be constructed using the constants and
  363. procedures from the @code{(system foreign)} module, which may be loaded
  364. like this:
  365. @example
  366. (use-modules (system foreign))
  367. @end example
  368. @code{(system foreign)} exports a number of values expressing the basic
  369. C types.
  370. @defvr {Scheme Variable} int8
  371. @defvrx {Scheme Variable} uint8
  372. @defvrx {Scheme Variable} uint16
  373. @defvrx {Scheme Variable} int16
  374. @defvrx {Scheme Variable} uint32
  375. @defvrx {Scheme Variable} int32
  376. @defvrx {Scheme Variable} uint64
  377. @defvrx {Scheme Variable} int64
  378. @defvrx {Scheme Variable} float
  379. @defvrx {Scheme Variable} double
  380. These values represent the C numeric types of the specified sizes and
  381. signednesses.
  382. @end defvr
  383. In addition there are some convenience bindings for indicating types of
  384. platform-dependent size.
  385. @defvr {Scheme Variable} int
  386. @defvrx {Scheme Variable} unsigned-int
  387. @defvrx {Scheme Variable} long
  388. @defvrx {Scheme Variable} unsigned-long
  389. @defvrx {Scheme Variable} short
  390. @defvrx {Scheme Variable} unsigned-short
  391. @defvrx {Scheme Variable} size_t
  392. @defvrx {Scheme Variable} ssize_t
  393. @defvrx {Scheme Variable} ptrdiff_t
  394. @defvrx {Scheme Variable} intptr_t
  395. @defvrx {Scheme Variable} uintptr_t
  396. Values exported by the @code{(system foreign)} module, representing C
  397. numeric types. For example, @code{long} may be @code{equal?} to
  398. @code{int64} on a 64-bit platform.
  399. @end defvr
  400. @defvr {Scheme Variable} void
  401. The @code{void} type. It can be used as the first argument to
  402. @code{pointer->procedure} to wrap a C function that returns nothing.
  403. @end defvr
  404. In addition, the symbol @code{*} is used by convention to denote pointer
  405. types. Procedures detailed in the following sections, such as
  406. @code{pointer->procedure}, accept it as a type descriptor.
  407. @node Foreign Functions
  408. @subsection Foreign Functions
  409. The most natural thing to do with a dynamic library is to grovel around
  410. in it for a function pointer: a @dfn{foreign function}. Load the
  411. @code{(system foreign)} module to use these Scheme interfaces.
  412. @example
  413. (use-modules (system foreign))
  414. @end example
  415. @deffn {Scheme Procedure} pointer->procedure return_type func_ptr arg_types @
  416. [#:return-errno?=#f]
  417. @deffnx {C Function} scm_pointer_to_procedure (return_type, func_ptr, arg_types)
  418. @deffnx {C Function} scm_pointer_to_procedure_with_errno (return_type, func_ptr, arg_types)
  419. Make a foreign function.
  420. Given the foreign void pointer @var{func_ptr}, its argument and
  421. return types @var{arg_types} and @var{return_type}, return a
  422. procedure that will pass arguments to the foreign function
  423. and return appropriate values.
  424. @var{arg_types} should be a list of foreign types.
  425. @code{return_type} should be a foreign type. @xref{Foreign Types}, for
  426. more information on foreign types.
  427. If @var{return-errno?} is true, or when calling
  428. @code{scm_pointer_to_procedure_with_errno}, the returned procedure will
  429. return two values, with @code{errno} as the second value.
  430. @end deffn
  431. Finally, in @code{(system foreign-library)} there is a convenient
  432. wrapper function, joining together @code{foreign-libary-pointer} and
  433. @code{procedure->pointer}:
  434. @deffn {Scheme Procedure} foreign-library-function lib name @
  435. [#:return-type=void] [#:arg-types='()] [#:return-errno?=#f]
  436. Load the address of @var{name} from @var{lib}, and treat it as a
  437. function taking arguments @var{arg-types} and returning
  438. @var{return-type}, optionally also with errno.
  439. An invocation of @code{foreign-library-function} is entirely equivalent
  440. to:
  441. @example
  442. (pointer->procedure @var{return-type}
  443. (foreign-library-pointer @var{lib} @var{name})
  444. @var{arg-types}
  445. #:return-errno? @var{return-errno?}).
  446. @end example
  447. @end deffn
  448. Pulling all this together, here is a better definition of @code{(math
  449. bessel)}:
  450. @example
  451. (define-module (math bessel)
  452. #:use-module (system foreign)
  453. #:use-module (system foreign-library)
  454. #:export (j0))
  455. (define j0
  456. (foreign-library-function "libm" "j0"
  457. #:return-type double
  458. #:arg-types (list double)))
  459. @end example
  460. That's it! No C at all.
  461. Before going on to more detailed examples, the next two sections discuss
  462. how to deal with data that is more complex than, say, @code{int8}.
  463. @xref{More Foreign Functions}, to continue with foreign function examples.
  464. @node Void Pointers and Byte Access
  465. @subsection Void Pointers and Byte Access
  466. Wrapped pointers are untyped, so they are essentially equivalent to C
  467. @code{void} pointers. As in C, the memory region pointed to by a
  468. pointer can be accessed at the byte level. This is achieved using
  469. @emph{bytevectors} (@pxref{Bytevectors}). The @code{(rnrs bytevectors)}
  470. module contains procedures that can be used to convert byte sequences to
  471. Scheme objects such as strings, floating point numbers, or integers.
  472. Load the @code{(system foreign)} module to use these Scheme interfaces.
  473. @example
  474. (use-modules (system foreign))
  475. @end example
  476. @deffn {Scheme Procedure} pointer->bytevector pointer len [offset [uvec_type]]
  477. @deffnx {C Function} scm_pointer_to_bytevector (pointer, len, offset, uvec_type)
  478. Return a bytevector aliasing the @var{len} bytes pointed to by
  479. @var{pointer}.
  480. The user may specify an alternate default interpretation for the memory
  481. by passing the @var{uvec_type} argument, to indicate that the memory is
  482. an array of elements of that type. @var{uvec_type} should be something
  483. that @code{array-type} would return, like @code{f32} or @code{s16}.
  484. When @var{offset} is passed, it specifies the offset in bytes relative
  485. to @var{pointer} of the memory region aliased by the returned
  486. bytevector.
  487. Mutating the returned bytevector mutates the memory pointed to by
  488. @var{pointer}, so buckle your seatbelts.
  489. @end deffn
  490. @deffn {Scheme Procedure} bytevector->pointer bv [offset]
  491. @deffnx {C Function} scm_bytevector_to_pointer (bv, offset)
  492. Return a pointer pointer aliasing the memory pointed to by @var{bv} or
  493. @var{offset} bytes after @var{bv} when @var{offset} is passed.
  494. @end deffn
  495. In addition to these primitives, convenience procedures are available:
  496. @deffn {Scheme Procedure} dereference-pointer pointer
  497. Assuming @var{pointer} points to a memory region that holds a pointer,
  498. return this pointer.
  499. @end deffn
  500. @deffn {Scheme Procedure} string->pointer string [encoding]
  501. Return a foreign pointer to a nul-terminated copy of @var{string} in the
  502. given @var{encoding}, defaulting to the current locale encoding. The C
  503. string is freed when the returned foreign pointer becomes unreachable.
  504. This is the Scheme equivalent of @code{scm_to_stringn}.
  505. @end deffn
  506. @deffn {Scheme Procedure} pointer->string pointer [length] [encoding]
  507. Return the string representing the C string pointed to by @var{pointer}.
  508. If @var{length} is omitted or @code{-1}, the string is assumed to be
  509. nul-terminated. Otherwise @var{length} is the number of bytes in memory
  510. pointed to by @var{pointer}. The C string is assumed to be in the given
  511. @var{encoding}, defaulting to the current locale encoding.
  512. This is the Scheme equivalent of @code{scm_from_stringn}.
  513. @end deffn
  514. @cindex wrapped pointer types
  515. Most object-oriented C libraries use pointers to specific data
  516. structures to identify objects. It is useful in such cases to reify the
  517. different pointer types as disjoint Scheme types. The
  518. @code{define-wrapped-pointer-type} macro simplifies this.
  519. @deffn {Scheme Syntax} define-wrapped-pointer-type type-name pred wrap unwrap print
  520. Define helper procedures to wrap pointer objects into Scheme objects
  521. with a disjoint type. Specifically, this macro defines:
  522. @itemize
  523. @item @var{pred}, a predicate for the new Scheme type;
  524. @item @var{wrap}, a procedure that takes a pointer object and returns an
  525. object that satisfies @var{pred};
  526. @item @var{unwrap}, which does the reverse.
  527. @end itemize
  528. @var{wrap} preserves pointer identity, for two pointer objects @var{p1}
  529. and @var{p2} that are @code{equal?}, @code{(eq? (@var{wrap} @var{p1})
  530. (@var{wrap} @var{p2})) @result{} #t}.
  531. Finally, @var{print} should name a user-defined procedure to print such
  532. objects. The procedure is passed the wrapped object and a port to write
  533. to.
  534. For example, assume we are wrapping a C library that defines a type,
  535. @code{bottle_t}, and functions that can be passed @code{bottle_t *}
  536. pointers to manipulate them. We could write:
  537. @example
  538. (define-wrapped-pointer-type bottle
  539. bottle?
  540. wrap-bottle unwrap-bottle
  541. (lambda (b p)
  542. (format p "#<bottle of ~a ~x>"
  543. (bottle-contents b)
  544. (pointer-address (unwrap-bottle b)))))
  545. (define grab-bottle
  546. ;; Wrapper for `bottle_t *grab (void)'.
  547. (let ((grab (foreign-library-function libbottle "grab_bottle"
  548. #:return-type '*)))
  549. (lambda ()
  550. "Return a new bottle."
  551. (wrap-bottle (grab)))))
  552. (define bottle-contents
  553. ;; Wrapper for `const char *bottle_contents (bottle_t *)'.
  554. (let ((contents (foreign-library-function libbottle "bottle_contents"
  555. #:return-type '*
  556. #:arg-types '(*))))
  557. (lambda (b)
  558. "Return the contents of B."
  559. (pointer->string (contents (unwrap-bottle b))))))
  560. (write (grab-bottle))
  561. @result{} #<bottle of Ch@^ateau Haut-Brion 803d36>
  562. @end example
  563. In this example, @code{grab-bottle} is guaranteed to return a genuine
  564. @code{bottle} object satisfying @code{bottle?}. Likewise,
  565. @code{bottle-contents} errors out when its argument is not a genuine
  566. @code{bottle} object.
  567. @end deffn
  568. As another example, currently Guile has a variable, @code{scm_numptob},
  569. as part of its API. It is declared as a C @code{long}. So, to read its
  570. value, we can do:
  571. @example
  572. (use-modules (system foreign))
  573. (use-modules (rnrs bytevectors))
  574. (define numptob
  575. (foreign-library-pointer #f "scm_numptob"))
  576. numptob
  577. (bytevector-uint-ref (pointer->bytevector numptob (sizeof long))
  578. 0 (native-endianness)
  579. (sizeof long))
  580. @result{} 8
  581. @end example
  582. If we wanted to corrupt Guile's internal state, we could set
  583. @code{scm_numptob} to another value; but we shouldn't, because that
  584. variable is not meant to be set. Indeed this point applies more widely:
  585. the C API is a dangerous place to be. Not only might setting a value
  586. crash your program, simply accessing the data pointed to by a dangling
  587. pointer or similar can prove equally disastrous.
  588. @node Foreign Structs
  589. @subsection Foreign Structs
  590. Finally, one last note on foreign values before moving on to actually
  591. calling foreign functions. Sometimes you need to deal with C structs,
  592. which requires interpreting each element of the struct according to the
  593. its type, offset, and alignment. The @code{(system foreign)} module has
  594. some primitives to support this.
  595. @example
  596. (use-modules (system foreign))
  597. @end example
  598. @deffn {Scheme Procedure} sizeof type
  599. @deffnx {C Function} scm_sizeof (type)
  600. Return the size of @var{type}, in bytes.
  601. @var{type} should be a valid C type, like @code{int}.
  602. Alternately @var{type} may be the symbol @code{*}, in which
  603. case the size of a pointer is returned. @var{type} may
  604. also be a list of types, in which case the size of a
  605. @code{struct} with ABI-conventional packing is returned.
  606. @end deffn
  607. @deffn {Scheme Procedure} alignof type
  608. @deffnx {C Function} scm_alignof (type)
  609. Return the alignment of @var{type}, in bytes.
  610. @var{type} should be a valid C type, like @code{int}.
  611. Alternately @var{type} may be the symbol @code{*}, in which
  612. case the alignment of a pointer is returned. @var{type} may
  613. also be a list of types, in which case the alignment of a
  614. @code{struct} with ABI-conventional packing is returned.
  615. @end deffn
  616. Guile also provides some convenience methods to pack and unpack foreign
  617. pointers wrapping C structs.
  618. @deffn {Scheme Procedure} make-c-struct types vals
  619. Create a foreign pointer to a C struct containing @var{vals} with types
  620. @code{types}.
  621. @var{vals} and @code{types} should be lists of the same length.
  622. @end deffn
  623. @deffn {Scheme Procedure} parse-c-struct foreign types
  624. Parse a foreign pointer to a C struct, returning a list of values.
  625. @code{types} should be a list of C types.
  626. @end deffn
  627. For example, to create and parse the equivalent of a @code{struct @{
  628. int64_t a; uint8_t b; @}}:
  629. @example
  630. (parse-c-struct (make-c-struct (list int64 uint8)
  631. (list 300 43))
  632. (list int64 uint8))
  633. @result{} (300 43)
  634. @end example
  635. As yet, Guile only has convenience routines to support
  636. conventionally-packed structs. But given the @code{bytevector->pointer}
  637. and @code{pointer->bytevector} routines, one can create and parse
  638. tightly packed structs and unions by hand. See the code for
  639. @code{(system foreign)} for details.
  640. @node More Foreign Functions
  641. @subsection More Foreign Functions
  642. It is possible to pass pointers to foreign functions, and to return them
  643. as well. In that case the type of the argument or return value should
  644. be the symbol @code{*}, indicating a pointer. For example, the following
  645. code makes @code{memcpy} available to Scheme:
  646. @example
  647. (use-modules (system foreign))
  648. (define memcpy
  649. (foreign-library-function #f "memcpy"
  650. #:return-type '*
  651. #:arg-types (list '* '* size_t)))
  652. @end example
  653. To invoke @code{memcpy}, one must pass it foreign pointers:
  654. @example
  655. (use-modules (rnrs bytevectors))
  656. (define src-bits
  657. (u8-list->bytevector '(0 1 2 3 4 5 6 7)))
  658. (define src
  659. (bytevector->pointer src-bits))
  660. (define dest
  661. (bytevector->pointer (make-bytevector 16 0)))
  662. (memcpy dest src (bytevector-length src-bits))
  663. (bytevector->u8-list (pointer->bytevector dest 16))
  664. @result{} (0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0)
  665. @end example
  666. One may also pass structs as values, passing structs as foreign
  667. pointers. @xref{Foreign Structs}, for more information on how to express
  668. struct types and struct values.
  669. ``Out'' arguments are passed as foreign pointers. The memory pointed to
  670. by the foreign pointer is mutated in place.
  671. @example
  672. ;; struct timeval @{
  673. ;; time_t tv_sec; /* seconds */
  674. ;; suseconds_t tv_usec; /* microseconds */
  675. ;; @};
  676. ;; assuming fields are of type "long"
  677. (define gettimeofday
  678. (let ((f (foreign-library-function #f "gettimeofday"
  679. #:return-type int
  680. #:arg-types (list '* '*)))
  681. (tv-type (list long long)))
  682. (lambda ()
  683. (let* ((timeval (make-c-struct tv-type (list 0 0)))
  684. (ret (f timeval %null-pointer)))
  685. (if (zero? ret)
  686. (apply values (parse-c-struct timeval tv-type))
  687. (error "gettimeofday returned an error" ret))))))
  688. (gettimeofday)
  689. @result{} 1270587589
  690. @result{} 499553
  691. @end example
  692. As you can see, this interface to foreign functions is at a very low,
  693. somewhat dangerous level@footnote{A contribution to Guile in the form of
  694. a high-level FFI would be most welcome.}.
  695. @cindex callbacks
  696. The FFI can also work in the opposite direction: making Scheme
  697. procedures callable from C. This makes it possible to use Scheme
  698. procedures as ``callbacks'' expected by C function.
  699. @deffn {Scheme Procedure} procedure->pointer return-type proc arg-types
  700. @deffnx {C Function} scm_procedure_to_pointer (return_type, proc, arg_types)
  701. Return a pointer to a C function of type @var{return-type}
  702. taking arguments of types @var{arg-types} (a list) and
  703. behaving as a proxy to procedure @var{proc}. Thus
  704. @var{proc}'s arity, supported argument types, and return
  705. type should match @var{return-type} and @var{arg-types}.
  706. @end deffn
  707. As an example, here's how the C library's @code{qsort} array sorting
  708. function can be made accessible to Scheme (@pxref{Array Sort Function,
  709. @code{qsort},, libc, The GNU C Library Reference Manual}):
  710. @example
  711. (define qsort!
  712. (let ((qsort (foreign-library-function
  713. #f "qsort" #:arg-types (list '* size_t size_t '*))))
  714. (lambda (bv compare)
  715. ;; Sort bytevector BV in-place according to comparison
  716. ;; procedure COMPARE.
  717. (let ((ptr (procedure->pointer int
  718. (lambda (x y)
  719. ;; X and Y are pointers so,
  720. ;; for convenience, dereference
  721. ;; them before calling COMPARE.
  722. (compare (dereference-uint8* x)
  723. (dereference-uint8* y)))
  724. (list '* '*))))
  725. (qsort (bytevector->pointer bv)
  726. (bytevector-length bv) 1 ;; we're sorting bytes
  727. ptr)))))
  728. (define (dereference-uint8* ptr)
  729. ;; Helper function: dereference the byte pointed to by PTR.
  730. (let ((b (pointer->bytevector ptr 1)))
  731. (bytevector-u8-ref b 0)))
  732. (define bv
  733. ;; An unsorted array of bytes.
  734. (u8-list->bytevector '(7 1 127 3 5 4 77 2 9 0)))
  735. ;; Sort BV.
  736. (qsort! bv (lambda (x y) (- x y)))
  737. ;; Let's see what the sorted array looks like:
  738. (bytevector->u8-list bv)
  739. @result{} (0 1 2 3 4 5 7 9 77 127)
  740. @end example
  741. And voil@`a!
  742. Note that @code{procedure->pointer} is not supported (and not defined)
  743. on a few exotic architectures. Thus, user code may need to check
  744. @code{(defined? 'procedure->pointer)}. Nevertheless, it is available on
  745. many architectures, including (as of libffi 3.0.9) x86, ia64, SPARC,
  746. PowerPC, ARM, and MIPS, to name a few.
  747. @c Local Variables:
  748. @c TeX-master: "guile.texi"
  749. @c End: