node.texi 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. @node C interface
  2. @chapter C interface
  3. @i{(This chapter was derived from work copyrighted @copyright{}
  4. 1993--2005 by Richard Kelsey, Jonathan Rees, and Mike Sperber.)}
  5. @texonlyindent
  6. This chapter describes an interface for calling C functions from
  7. Scheme, calling Scheme procedures from C, and working with the Scheme
  8. heap in C. Scheme48 manages stub functions in C that negotiate between
  9. the calling conventions of Scheme & C and the memory allocation
  10. policies of both worlds. No stub generator is available yet, but
  11. writing stubs is a straightforward task.
  12. @menu
  13. @include c-ffi/menu.texi
  14. @end menu
  15. @section Overview of the C interface
  16. The following facilities are available for interfacing between Scheme48
  17. & C:
  18. @itemize @bullet
  19. @item Scheme code can call C functions.
  20. @item The external interface provides full introspection for all Scheme
  21. objects. External code may inspect, modify, and allocate Scheme
  22. objects arbitrarily.
  23. @item External code may raise exceptions back to Scheme48 to signal
  24. errors.
  25. @item External code may call back into Scheme. Scheme48 correctly
  26. unrolls the process stack on non-local exits.
  27. @item External modules may register bindings of names to values with a
  28. central registry accessible from Scheme. Conversely, Scheme code can
  29. register shared bindings for access by C code.
  30. @end itemize
  31. @subsection Scheme structures
  32. @stindex external-calls
  33. @stindex load-dynamic-externals
  34. @stindex shared-bindings
  35. @stindex dynamic-externals
  36. On the Scheme side of the C interface, there are three pertinent
  37. structures: @embedref{Shared bindings between Scheme and C,
  38. @code{shared-bindings}}, which provides the Scheme side of the
  39. facility for sharing data between Scheme and C; @embedref{Calling C
  40. functions from Scheme, @code{external-calls}}, which exports several
  41. ways to call C functions from Scheme, along with some useful
  42. facilities, such as object finalizers, which are also available from
  43. elsewhere; and @embedref{Dynamic loading of C modules,
  44. @code{load-dynamic-externals}}, which provides a dynamic external
  45. object loading facility. Also, the old dynamic loading facility is
  46. still available from the @code{dynamic-externals} structure, but its
  47. use is deprecated, and it will most likely vanish in a later release.
  48. @subsection C naming conventions
  49. @cindex C naming conventions
  50. Scheme48's C bindings all have strict naming conventions. Variables
  51. & procedures have @code{s48_} prefixed to them; macros, @code{S48_}.
  52. Whenever a C name is derived from a Scheme identifier, hyphens are
  53. replaced with underscores. Also, procedures or variables are converted
  54. to lowercase, while macros are converted to uppercase. The @code{?}
  55. suffix, generally appended to predicates, is converted to @code{_p} (or
  56. @code{_P} in macro names). Trailing @code{!} is dropped. For example,
  57. the C macro that corresponds with Scheme's @code{pair?} predicate is
  58. named @code{S48_PAIR_P}, and the C macro to assign the car of a pair is
  59. named @code{S48_SET_CAR}. Procedures and macros that do not verify the
  60. types of their arguments have `unsafe' in their names.
  61. All of the C functions and macros described have prototypes or
  62. definitions in the file @file{c/scheme48.h} of Scheme48's standard
  63. distribution. The C type for Scheme values is defined there to be
  64. @code{s48_value}.
  65. @subsection Garbage collection
  66. Scheme48 uses a copying garbage collector. The collector must be able
  67. to locate all references to objects allocated in the Scheme48 heap in
  68. order to ensure that storage is not reclaimed prematurely and to
  69. update references to objects moved by the collector. The garbage
  70. collector may run whenever an object is allocated in the heap. C
  71. variables whose values are Scheme48 objects and which are live across
  72. heap allocation calls need to be registered with the garbage
  73. collector. For more information, @pxref{Interacting with the Scheme
  74. heap in C}.
  75. @include c-ffi/shared.texi
  76. @include c-ffi/c-from-scm.texi
  77. @include c-ffi/dynload.texi
  78. @include c-ffi/scm-from-c.texi
  79. @include c-ffi/heap.texi
  80. @include c-ffi/record.texi
  81. @include c-ffi/extern-exn.texi
  82. @include c-ffi/unsafe.texi