api-foreign.texi 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 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. Interrupts are deferred while the C function is executing (with
  134. @code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).
  135. @end deffn
  136. @code{dynamic-call} is not very powerful. It is mostly intended to be
  137. used for calling specially written initialization functions that will
  138. then add new primitives to Guile. For example, we do not expect that you
  139. will dynamically link @file{libX11} with @code{dynamic-link} and then
  140. construct a beautiful graphical user interface just by using
  141. @code{dynamic-call}. Instead, the usual way would be to write a special
  142. Guile-to-X11 glue library that has intimate knowledge about both Guile
  143. and X11 and does whatever is necessary to make them inter-operate
  144. smoothly. This glue library could then be dynamically linked into a
  145. vanilla Guile interpreter and activated by calling its initialization
  146. function. That function would add all the new types and primitives to
  147. the Guile interpreter that it has to offer.
  148. (There is actually another, better option: simply to create a
  149. @file{libX11} wrapper in Scheme via the dynamic FFI. @xref{Dynamic FFI},
  150. for more information.)
  151. Given some set of C extensions to Guile, the next logical step is to
  152. integrate these glue libraries into the module system of Guile so that
  153. you can load new primitives into a running system just as you can load
  154. new Scheme code.
  155. @deffn {Scheme Procedure} load-extension lib init
  156. @deffnx {C Function} scm_load_extension (lib, init)
  157. Load and initialize the extension designated by LIB and INIT.
  158. When there is no pre-registered function for LIB/INIT, this is
  159. equivalent to
  160. @lisp
  161. (dynamic-call INIT (dynamic-link LIB))
  162. @end lisp
  163. When there is a pre-registered function, that function is called
  164. instead.
  165. Normally, there is no pre-registered function. This option exists
  166. only for situations where dynamic linking is unavailable or unwanted.
  167. In that case, you would statically link your program with the desired
  168. library, and register its init function right after Guile has been
  169. initialized.
  170. As for @code{dynamic-link}, @var{lib} should not contain any suffix such
  171. as @code{.so} (@pxref{Foreign Libraries, dynamic-link}). It
  172. should also not contain any directory components. Libraries that
  173. implement Guile Extensions should be put into the normal locations for
  174. shared libraries. We recommend to use the naming convention
  175. @file{libguile-bla-blum} for a extension related to a module @code{(bla
  176. blum)}.
  177. The normal way for a extension to be used is to write a small Scheme
  178. file that defines a module, and to load the extension into this
  179. module. When the module is auto-loaded, the extension is loaded as
  180. well. For example,
  181. @lisp
  182. (define-module (bla blum))
  183. (load-extension "libguile-bla-blum" "bla_init_blum")
  184. @end lisp
  185. @end deffn
  186. @node C Extensions
  187. @subsection C Extensions
  188. The most interesting application of dynamically linked libraries is
  189. probably to use them for providing @emph{compiled code modules} to
  190. Scheme programs. As much fun as programming in Scheme is, every now and
  191. then comes the need to write some low-level C stuff to make Scheme even
  192. more fun.
  193. Not only can you put these new primitives into their own module (see the
  194. previous section), you can even put them into a shared library that is
  195. only then linked to your running Guile image when it is actually
  196. needed.
  197. An example will hopefully make everything clear. Suppose we want to
  198. make the Bessel functions of the C library available to Scheme in the
  199. module @samp{(math bessel)}. First we need to write the appropriate
  200. glue code to convert the arguments and return values of the functions
  201. from Scheme to C and back. Additionally, we need a function that will
  202. add them to the set of Guile primitives. Because this is just an
  203. example, we will only implement this for the @code{j0} function.
  204. @smallexample
  205. #include <math.h>
  206. #include <libguile.h>
  207. SCM
  208. j0_wrapper (SCM x)
  209. @{
  210. return scm_from_double (j0 (scm_to_double (x, "j0")));
  211. @}
  212. void
  213. init_math_bessel ()
  214. @{
  215. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  216. @}
  217. @end smallexample
  218. We can already try to bring this into action by manually calling the low
  219. level functions for performing dynamic linking. The C source file needs
  220. to be compiled into a shared library. Here is how to do it on
  221. GNU/Linux, please refer to the @code{libtool} documentation for how to
  222. create dynamically linkable libraries portably.
  223. @smallexample
  224. gcc -shared -o libbessel.so -fPIC bessel.c
  225. @end smallexample
  226. Now fire up Guile:
  227. @lisp
  228. (define bessel-lib (dynamic-link "./libbessel.so"))
  229. (dynamic-call "init_math_bessel" bessel-lib)
  230. (j0 2)
  231. @result{} 0.223890779141236
  232. @end lisp
  233. The filename @file{./libbessel.so} should be pointing to the shared
  234. library produced with the @code{gcc} command above, of course. The
  235. second line of the Guile interaction will call the
  236. @code{init_math_bessel} function which in turn will register the C
  237. function @code{j0_wrapper} with the Guile interpreter under the name
  238. @code{j0}. This function becomes immediately available and we can call
  239. it from Scheme.
  240. Fun, isn't it? But we are only half way there. This is what
  241. @code{apropos} has to say about @code{j0}:
  242. @smallexample
  243. (apropos "j0")
  244. @print{} (guile-user): j0 #<primitive-procedure j0>
  245. @end smallexample
  246. As you can see, @code{j0} is contained in the root module, where all
  247. the other Guile primitives like @code{display}, etc live. In general,
  248. a primitive is put into whatever module is the @dfn{current module} at
  249. the time @code{scm_c_define_gsubr} is called.
  250. A compiled module should have a specially named @dfn{module init
  251. function}. Guile knows about this special name and will call that
  252. function automatically after having linked in the shared library. For
  253. our example, we replace @code{init_math_bessel} with the following code in
  254. @file{bessel.c}:
  255. @smallexample
  256. void
  257. init_math_bessel (void *unused)
  258. @{
  259. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  260. scm_c_export ("j0", NULL);
  261. @}
  262. void
  263. scm_init_math_bessel_module ()
  264. @{
  265. scm_c_define_module ("math bessel", init_math_bessel, NULL);
  266. @}
  267. @end smallexample
  268. The general pattern for the name of a module init function is:
  269. @samp{scm_init_}, followed by the name of the module where the
  270. individual hierarchical components are concatenated with underscores,
  271. followed by @samp{_module}.
  272. After @file{libbessel.so} has been rebuilt, we need to place the shared
  273. library into the right place.
  274. Once the module has been correctly installed, it should be possible to
  275. use it like this:
  276. @smallexample
  277. guile> (load-extension "./libbessel.so" "scm_init_math_bessel_module")
  278. guile> (use-modules (math bessel))
  279. guile> (j0 2)
  280. 0.223890779141236
  281. guile> (apropos "j0")
  282. @print{} (math bessel): j0 #<primitive-procedure j0>
  283. @end smallexample
  284. That's it!
  285. @node Modules and Extensions
  286. @subsection Modules and Extensions
  287. The new primitives that you add to Guile with @code{scm_c_define_gsubr}
  288. (@pxref{Primitive Procedures}) or with any of the other mechanisms are
  289. placed into the module that is current when the
  290. @code{scm_c_define_gsubr} is executed. Extensions loaded from the REPL,
  291. for example, will be placed into the @code{(guile-user)} module, if the
  292. REPL module was not changed.
  293. To define C primitives within a specific module, the simplest way is:
  294. @example
  295. (define-module (foo bar))
  296. (load-extension "foobar-c-code" "foo_bar_init")
  297. @end example
  298. @cindex extensiondir
  299. When loaded with @code{(use-modules (foo bar))}, the
  300. @code{load-extension} call looks for the @file{foobar-c-code.so} (etc)
  301. object file in Guile's @code{extensiondir}, which is usually a
  302. subdirectory of the @code{libdir}. For example, if your libdir is
  303. @file{/usr/lib}, the @code{extensiondir} for the Guile @value{EFFECTIVE-VERSION}.@var{x}
  304. series will be @file{/usr/lib/guile/@value{EFFECTIVE-VERSION}/}.
  305. The extension path includes the major and minor version of Guile (the
  306. ``effective version''), because Guile guarantees compatibility within a
  307. given effective version. This allows you to install different versions
  308. of the same extension for different versions of Guile.
  309. If the extension is not found in the @code{extensiondir}, Guile will
  310. also search the standard system locations, such as @file{/usr/lib} or
  311. @file{/usr/local/lib}. It is preferable, however, to keep your extension
  312. out of the system library path, to prevent unintended interference with
  313. other dynamically-linked C libraries.
  314. If someone installs your module to a non-standard location then the
  315. object file won't be found. You can address this by inserting the
  316. install location in the @file{foo/bar.scm} file. This is convenient
  317. for the user and also guarantees the intended object is read, even if
  318. stray older or newer versions are in the loader's path.
  319. The usual way to specify an install location is with a @code{prefix}
  320. at the configure stage, for instance @samp{./configure prefix=/opt}
  321. results in library files as say @file{/opt/lib/foobar-c-code.so}.
  322. When using Autoconf (@pxref{Top, , Introduction, autoconf, The GNU
  323. Autoconf Manual}), the library location is in a @code{libdir}
  324. variable. Its value is intended to be expanded by @command{make}, and
  325. can by substituted into a source file like @file{foo.scm.in}
  326. @example
  327. (define-module (foo bar))
  328. (load-extension "XXextensiondirXX/foobar-c-code" "foo_bar_init")
  329. @end example
  330. @noindent
  331. with the following in a @file{Makefile}, using @command{sed}
  332. (@pxref{Top, , Introduction, sed, SED, A Stream Editor}),
  333. @example
  334. foo.scm: foo.scm.in
  335. sed 's|XXextensiondirXX|$(libdir)/guile/@value{EFFECTIVE-VERSION}|' <foo.scm.in >foo.scm
  336. @end example
  337. The actual pattern @code{XXextensiondirXX} is arbitrary, it's only something
  338. which doesn't otherwise occur. If several modules need the value, it
  339. can be easier to create one @file{foo/config.scm} with a define of the
  340. @code{extensiondir} location, and use that as required.
  341. @example
  342. (define-module (foo config))
  343. (define-public foo-config-extensiondir "XXextensiondirXX"")
  344. @end example
  345. Such a file might have other locations too, for instance a data
  346. directory for auxiliary files, or @code{localedir} if the module has
  347. its own @code{gettext} message catalogue
  348. (@pxref{Internationalization}).
  349. It will be noted all of the above requires that the Scheme code to be
  350. found in @code{%load-path} (@pxref{Build Config}). Presently it's
  351. left up to the system administrator or each user to augment that path
  352. when installing Guile modules in non-default locations. But having
  353. reached the Scheme code, that code should take care of hitting any of
  354. its own private files etc.
  355. @node Foreign Pointers
  356. @subsection Foreign Pointers
  357. The previous sections have shown how Guile can be extended at runtime by
  358. loading compiled C extensions. This approach is all well and good, but
  359. wouldn't it be nice if we didn't have to write any C at all? This
  360. section takes up the problem of accessing C values from Scheme, and the
  361. next discusses C functions.
  362. @menu
  363. * Foreign Types:: Expressing C types in Scheme.
  364. * Foreign Variables:: Pointers to C symbols.
  365. * Void Pointers and Byte Access:: Pointers into the ether.
  366. * Foreign Structs:: Packing and unpacking structs.
  367. @end menu
  368. @node Foreign Types
  369. @subsubsection Foreign Types
  370. The first impedance mismatch that one sees between C and Scheme is that
  371. in C, the storage locations (variables) are typed, but in Scheme types
  372. are associated with values, not variables. @xref{Values and Variables}.
  373. So when describing a C function or a C structure so that it can be
  374. accessed from Scheme, the data types of the parameters or fields must be
  375. passed explicitly.
  376. These ``C type values'' may be constructed using the constants and
  377. procedures from the @code{(system foreign)} module, which may be loaded
  378. like this:
  379. @example
  380. (use-modules (system foreign))
  381. @end example
  382. @code{(system foreign)} exports a number of values expressing the basic
  383. C types:
  384. @defvr {Scheme Variable} int8
  385. @defvrx {Scheme Variable} uint8
  386. @defvrx {Scheme Variable} uint16
  387. @defvrx {Scheme Variable} int16
  388. @defvrx {Scheme Variable} uint32
  389. @defvrx {Scheme Variable} int32
  390. @defvrx {Scheme Variable} uint64
  391. @defvrx {Scheme Variable} int64
  392. @defvrx {Scheme Variable} float
  393. @defvrx {Scheme Variable} double
  394. These values represent the C numeric types of the specified sizes and
  395. signednesses.
  396. @end defvr
  397. In addition there are some convenience bindings for indicating types of
  398. platform-dependent size:
  399. @defvr {Scheme Variable} int
  400. @defvrx {Scheme Variable} unsigned-int
  401. @defvrx {Scheme Variable} long
  402. @defvrx {Scheme Variable} unsigned-long
  403. @defvrx {Scheme Variable} size_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. @node Void Pointers and Byte Access
  475. @subsubsection Void Pointers and Byte Access
  476. Wrapped pointers are untyped, so they are essentially equivalent to C
  477. @code{void} pointers. As in C, the memory region pointed to by a
  478. pointer can be accessed at the byte level. This is achieved using
  479. @emph{bytevectors} (@pxref{Bytevectors}). The @code{(rnrs bytevector)}
  480. module contains procedures that can be used to convert byte sequences to
  481. Scheme objects such as strings, floating point numbers, or integers.
  482. @deffn {Scheme Procedure} pointer->bytevector pointer len [offset [uvec_type]]
  483. @deffnx {C Function} scm_foreign_to_bytevector pointer len offset uvec_type
  484. Return a bytevector aliasing the @var{len} bytes pointed to by
  485. @var{pointer}.
  486. The user may specify an alternate default interpretation for
  487. the memory by passing the @var{uvec_type} argument, to indicate
  488. that the memory is an array of elements of that type.
  489. @var{uvec_type} should be something that
  490. @code{uniform-vector-element-type} would return, like @code{f32}
  491. or @code{s16}.
  492. When @var{offset} is passed, it specifies the offset in bytes relative
  493. to @var{pointer} of the memory region aliased by the returned
  494. bytevector.
  495. Mutating the returned bytevector mutates the memory pointed to by
  496. @var{pointer}, so buckle your seatbelts.
  497. @end deffn
  498. @deffn {Scheme Procedure} bytevector->pointer bv [offset]
  499. @deffnx {C Function} scm_bytevector_to_pointer bv offset
  500. Return a pointer pointer aliasing the memory pointed to by @var{bv} or
  501. @var{offset} bytes after @var{bv} when @var{offset} is passed.
  502. @end deffn
  503. In addition to these primitives, convenience procedures are available:
  504. @deffn {Scheme Procedure} dereference-pointer pointer
  505. Assuming @var{pointer} points to a memory region that holds a pointer,
  506. return this pointer.
  507. @end deffn
  508. @deffn {Scheme Procedure} string->pointer string [encoding]
  509. Return a foreign pointer to a nul-terminated copy of @var{string} in the
  510. given @var{encoding}, defaulting to the current locale encoding. The C
  511. string is freed when the returned foreign pointer becomes unreachable.
  512. This is the Scheme equivalent of @code{scm_to_stringn}.
  513. @end deffn
  514. @deffn {Scheme Procedure} pointer->string pointer [length] [encoding]
  515. Return the string representing the C string pointed to by @var{pointer}.
  516. If @var{length} is omitted or @code{-1}, the string is assumed to be
  517. nul-terminated. Otherwise @var{length} is the number of bytes in memory
  518. pointed to by @var{pointer}. The C string is assumed to be in the given
  519. @var{encoding}, defaulting to the current locale encoding.
  520. This is the Scheme equivalent of @code{scm_from_stringn}.
  521. @end deffn
  522. @cindex wrapped pointer types
  523. Most object-oriented C libraries use pointers to specific data
  524. structures to identify objects. It is useful in such cases to reify the
  525. different pointer types as disjoint Scheme types. The
  526. @code{define-wrapped-pointer-type} macro simplifies this.
  527. @deffn {Scheme Syntax} define-wrapped-pointer-type type-name pred wrap unwrap print
  528. Define helper procedures to wrap pointer objects into Scheme objects
  529. with a disjoint type. Specifically, this macro defines:
  530. @itemize
  531. @item @var{pred}, a predicate for the new Scheme type;
  532. @item @var{wrap}, a procedure that takes a pointer object and returns an
  533. object that satisfies @var{pred};
  534. @item @var{unwrap}, which does the reverse.
  535. @end itemize
  536. @var{wrap} preserves pointer identity, for two pointer objects @var{p1}
  537. and @var{p2} that are @code{equal?}, @code{(eq? (@var{wrap} @var{p1})
  538. (@var{wrap} @var{p2})) @result{} #t}.
  539. Finally, @var{print} should name a user-defined procedure to print such
  540. objects. The procedure is passed the wrapped object and a port to write
  541. to.
  542. For example, assume we are wrapping a C library that defines a type,
  543. @code{bottle_t}, and functions that can be passed @code{bottle_t *}
  544. pointers to manipulate them. We could write:
  545. @example
  546. (define-wrapped-pointer-type bottle
  547. bottle?
  548. wrap-bottle unwrap-bottle
  549. (lambda (b p)
  550. (format p "#<bottle of ~a ~x>"
  551. (bottle-contents b)
  552. (pointer-address (unwrap-foo b)))))
  553. (define grab-bottle
  554. ;; Wrapper for `bottle_t *grab (void)'.
  555. (let ((grab (pointer->procedure '*
  556. (dynamic-func "grab_bottle" libbottle)
  557. '())))
  558. (lambda ()
  559. "Return a new bottle."
  560. (wrap-bottle (grab)))))
  561. (define bottle-contents
  562. ;; Wrapper for `const char *bottle_contents (bottle_t *)'.
  563. (let ((contents (pointer->procedure '*
  564. (dynamic-func "bottle_contents"
  565. libbottle)
  566. '(*))))
  567. (lambda (b)
  568. "Return the contents of B."
  569. (pointer->string (contents (unwrap-bottle b))))))
  570. (write (grab-bottle))
  571. @result{} #<bottle of Ch@^ateau Haut-Brion 803d36>
  572. @end example
  573. In this example, @code{grab-bottle} is guaranteed to return a genuine
  574. @code{bottle} object satisfying @code{bottle?}. Likewise,
  575. @code{bottle-contents} errors out when its argument is not a genuine
  576. @code{bottle} object.
  577. @end deffn
  578. Going back to the @code{scm_numptob} example above, here is how we can
  579. read its value as a C @code{long} integer:
  580. @example
  581. (use-modules (rnrs bytevectors))
  582. (bytevector-uint-ref (pointer->bytevector numptob (sizeof long))
  583. 0 (native-endianness)
  584. (sizeof long))
  585. @result{} 8
  586. @end example
  587. If we wanted to corrupt Guile's internal state, we could set
  588. @code{scm_numptob} to another value; but we shouldn't, because that
  589. variable is not meant to be set. Indeed this point applies more widely:
  590. the C API is a dangerous place to be. Not only might setting a value
  591. crash your program, simply accessing the data pointed to by a dangling
  592. pointer or similar can prove equally disastrous.
  593. @node Foreign Structs
  594. @subsubsection Foreign Structs
  595. Finally, one last note on foreign values before moving on to actually
  596. calling foreign functions. Sometimes you need to deal with C structs,
  597. which requires interpreting each element of the struct according to the
  598. its type, offset, and alignment. Guile has some primitives to support
  599. this.
  600. @deffn {Scheme Procedure} sizeof type
  601. @deffnx {C Function} scm_sizeof type
  602. Return the size of @var{type}, in bytes.
  603. @var{type} should be a valid C type, like @code{int}.
  604. Alternately @var{type} may be the symbol @code{*}, in which
  605. case the size of a pointer is returned. @var{type} may
  606. also be a list of types, in which case the size of a
  607. @code{struct} with ABI-conventional packing is returned.
  608. @end deffn
  609. @deffn {Scheme Procedure} alignof type
  610. @deffnx {C Function} scm_alignof type
  611. Return the alignment of @var{type}, in bytes.
  612. @var{type} should be a valid C type, like @code{int}.
  613. Alternately @var{type} may be the symbol @code{*}, in which
  614. case the alignment of a pointer is returned. @var{type} may
  615. also be a list of types, in which case the alignment of a
  616. @code{struct} with ABI-conventional packing is returned.
  617. @end deffn
  618. Guile also provides some convenience methods to pack and unpack foreign
  619. pointers wrapping C structs.
  620. @deffn {Scheme Procedure} make-c-struct types vals
  621. Create a foreign pointer to a C struct containing @var{vals} with types
  622. @code{types}.
  623. @var{vals} and @code{types} should be lists of the same length.
  624. @end deffn
  625. @deffn {Scheme Procedure} parse-c-struct foreign types
  626. Parse a foreign pointer to a C struct, returning a list of values.
  627. @code{types} should be a list of C types.
  628. @end deffn
  629. For example, to create and parse the equivalent of a @code{struct @{
  630. int64_t a; uint8_t b; @}}:
  631. @example
  632. (parse-c-struct (make-c-struct (list int64 uint8)
  633. (list 300 43))
  634. (list int64 uint8))
  635. @result{} (300 43)
  636. @end example
  637. As yet, Guile only has convenience routines to support
  638. conventionally-packed structs. But given the @code{bytevector->foreign}
  639. and @code{foreign->bytevector} routines, one can create and parse
  640. tightly packed structs and unions by hand. See the code for
  641. @code{(system foreign)} for details.
  642. @node Dynamic FFI
  643. @subsection Dynamic FFI
  644. Of course, the land of C is not all nouns and no verbs: there are
  645. functions too, and Guile allows you to call them.
  646. @deffn {Scheme Procedure} pointer->procedure return_type func_ptr arg_types
  647. @deffnx {C Procedure} scm_pointer_to_procedure return_type func_ptr arg_types
  648. Make a foreign function.
  649. Given the foreign void pointer @var{func_ptr}, its argument and
  650. return types @var{arg_types} and @var{return_type}, return a
  651. procedure that will pass arguments to the foreign function
  652. and return appropriate values.
  653. @var{arg_types} should be a list of foreign types.
  654. @code{return_type} should be a foreign type. @xref{Foreign Types}, for
  655. more information on foreign types.
  656. @end deffn
  657. Here is a better definition of @code{(math bessel)}:
  658. @example
  659. (define-module (math bessel)
  660. #:use-module (system foreign)
  661. #:export (j0))
  662. (define libm (dynamic-link "libm"))
  663. (define j0
  664. (pointer->procedure double
  665. (dynamic-func "j0" libm)
  666. (list double)))
  667. @end example
  668. That's it! No C at all.
  669. Numeric arguments and return values from foreign functions are
  670. represented as Scheme values. For example, @code{j0} in the above
  671. example takes a Scheme number as its argument, and returns a Scheme
  672. number.
  673. Pointers may be passed to and returned from foreign functions as well.
  674. In that case the type of the argument or return value should be the
  675. symbol @code{*}, indicating a pointer. For example, the following
  676. code makes @code{memcpy} available to Scheme:
  677. @example
  678. (define memcpy
  679. (let ((this (dynamic-link)))
  680. (pointer->procedure '*
  681. (dynamic-func "memcpy" this)
  682. (list '* '* size_t))))
  683. @end example
  684. To invoke @code{memcpy}, one must pass it foreign pointers:
  685. @example
  686. (use-modules (rnrs bytevectors))
  687. (define src-bits
  688. (u8-list->bytevector '(0 1 2 3 4 5 6 7)))
  689. (define src
  690. (bytevector->pointer src-bits))
  691. (define dest
  692. (bytevector->pointer (make-bytevector 16 0)))
  693. (memcpy dest src (bytevector-length src-bits))
  694. (bytevector->u8-list (pointer->bytevector dest 16))
  695. @result{} (0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0)
  696. @end example
  697. One may also pass structs as values, passing structs as foreign
  698. pointers. @xref{Foreign Structs}, for more information on how to express
  699. struct types and struct values.
  700. ``Out'' arguments are passed as foreign pointers. The memory pointed to
  701. by the foreign pointer is mutated in place.
  702. @example
  703. ;; struct timeval @{
  704. ;; time_t tv_sec; /* seconds */
  705. ;; suseconds_t tv_usec; /* microseconds */
  706. ;; @};
  707. ;; assuming fields are of type "long"
  708. (define gettimeofday
  709. (let ((f (pointer->procedure
  710. int
  711. (dynamic-func "gettimeofday" (dynamic-link))
  712. (list '* '*)))
  713. (tv-type (list long long)))
  714. (lambda ()
  715. (let* ((timeval (make-c-struct tv-type (list 0 0)))
  716. (ret (f timeval %null-pointer)))
  717. (if (zero? ret)
  718. (apply values (parse-c-struct timeval tv-type))
  719. (error "gettimeofday returned an error" ret))))))
  720. (gettimeofday)
  721. @result{} 1270587589
  722. @result{} 499553
  723. @end example
  724. As you can see, this interface to foreign functions is at a very low,
  725. somewhat dangerous level@footnote{A contribution to Guile in the form of
  726. a high-level FFI would be most welcome.}.
  727. @cindex callbacks
  728. The FFI can also work in the opposite direction: making Scheme
  729. procedures callable from C. This makes it possible to use Scheme
  730. procedures as ``callbacks'' expected by C function.
  731. @deffn {Scheme Procedure} procedure->pointer return-type proc arg-types
  732. @deffnx {C Function} scm_procedure_to_pointer (return_type, proc, arg_types)
  733. Return a pointer to a C function of type @var{return-type}
  734. taking arguments of types @var{arg-types} (a list) and
  735. behaving as a proxy to procedure @var{proc}. Thus
  736. @var{proc}'s arity, supported argument types, and return
  737. type should match @var{return-type} and @var{arg-types}.
  738. @end deffn
  739. As an example, here's how the C library's @code{qsort} array sorting
  740. function can be made accessible to Scheme (@pxref{Array Sort Function,
  741. @code{qsort},, libc, The GNU C Library Reference Manual}):
  742. @example
  743. (define qsort!
  744. (let ((qsort (pointer->procedure void
  745. (dynamic-func "qsort"
  746. (dynamic-link))
  747. (list '* size_t size_t '*))))
  748. (lambda (bv compare)
  749. ;; Sort bytevector BV in-place according to comparison
  750. ;; procedure COMPARE.
  751. (let ((ptr (procedure->pointer int
  752. (lambda (x y)
  753. ;; X and Y are pointers so,
  754. ;; for convenience, dereference
  755. ;; them before calling COMPARE.
  756. (compare (dereference-uint8* x)
  757. (dereference-uint8* y)))
  758. (list '* '*))))
  759. (qsort (bytevector->pointer bv)
  760. (bytevector-length bv) 1 ;; we're sorting bytes
  761. ptr)))))
  762. (define (dereference-uint8* ptr)
  763. ;; Helper function: dereference the byte pointed to by PTR.
  764. (let ((b (pointer->bytevector ptr 1)))
  765. (bytevector-u8-ref b 0)))
  766. (define bv
  767. ;; An unsorted array of bytes.
  768. (u8-list->bytevector '(7 1 127 3 5 4 77 2 9 0)))
  769. ;; Sort BV.
  770. (qsort! bv (lambda (x y) (- x y)))
  771. ;; Let's see what the sorted array looks like:
  772. (bytevector->u8-list bv)
  773. @result{} (0 1 2 3 4 5 7 9 77 127)
  774. @end example
  775. And voil@`a!
  776. Note that @code{procedure->pointer} is not supported (and not defined)
  777. on a few exotic architectures. Thus, user code may need to check
  778. @code{(defined? 'procedure->pointer)}. Nevertheless, it is available on
  779. many architectures, including (as of libffi 3.0.9) x86, ia64, SPARC,
  780. PowerPC, ARM, and MIPS, to name a few.
  781. @c Local Variables:
  782. @c TeX-master: "guile.texi"
  783. @c End: