dynload.texi 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. @node Dynamic loading of C modules
  2. @section Dynamic loading of C modules
  3. @cindex C dynamic loading
  4. @cindex C shared objects
  5. @stindex load-dynamic-externals
  6. External code can be loaded into a running Scheme48 on most Unices and
  7. on Windows. Such external code must be stored in shared objects; see
  8. below on details of the C side. The relevant Scheme procedures are
  9. available in the @code{load-dynamic-externals} structure:
  10. @deffn procedure load-dynamic-externals filename add-file-type? reload-on-repeat? reload-on-resume? @returns{} dynamic-externals
  11. @deffnx procedure import-dynamic-externals filename @returns dynamic-externals
  12. @deffnx procedure unload-dynamic-externals dynamic-externals @returns{} unspecified
  13. @code{Load-dynamic-external} loads a shared object from
  14. @var{filename}, with an appropriate file type appended if
  15. @var{add-file-type?} is true (@code{.so} on Unix and @code{.dll} on
  16. Windows), and returns a @dfn{dynamic externals} object representing
  17. the loaded shared object. If the shared object was already loaded,
  18. then if @var{reload-on-repeat?} is true, it is reloaded; otherwise,
  19. the @code{load-dynamic-externals} call has no effect. If the dynamic
  20. externals descriptor is stored in a dumped heap image, when that heap
  21. image is resumed, if @code{reload-on-resume?} is true, the shared
  22. object corresponding with that dynamic external descriptor is
  23. reloaded. @code{Unload-dynamic-externals} unloads the given dynamic
  24. externals object.
  25. @code{Import-dynamic-externals} is a convenient wrapper for the common
  26. case of @code{load-dynamic-externals}; it is equivalent to
  27. @code{(load-dynamic-externals #t #f #t)}, @ie{} it will append a file
  28. type, it will not reload the shared object if it was already loaded,
  29. and the shared object will be loaded if part of a resumed heap image.
  30. @end deffn
  31. @deffn procedure reload-dynamic-externals filename @returns{} unspecified
  32. Reloads the shared object named by @var{filename}. This is intended
  33. as an interactive utility, which is why it accepts the filename of the
  34. shared object and not a dynamic externals descriptor.
  35. @end deffn
  36. Shared objects intended to be loaded into Scheme48 must define two
  37. functions:
  38. @deftypefn {C function} void s48_on_load (void)
  39. @deftypefnx {C function} void s48_on_reload (void)
  40. @code{s48_on_load} is called when the shared object is initially
  41. loaded by Scheme48. It typically consists of a number of invocations
  42. of @code{S48_EXPORT_FUNCTION} to make C functions available to
  43. Scheme48 code. @code{s48_on_reload} is called when the shared object
  44. is reloaded after it has been initially loaded once; it typically just
  45. calls @code{s48_on_load}, but it may perform other reinitializations.
  46. @end deftypefn
  47. On Linux, the following commands compile the C source file
  48. @file{foo.c} into a shared object @file{foo.so} that can be loaded
  49. dynamically by Scheme48:
  50. @example
  51. % gcc -c -o foo.o foo.c
  52. % ld -shared -o foo.so foo.o@end example
  53. @subsection Old dynamic loading interface
  54. The old @code{dynamic-externals} structures, which exported
  55. @code{dynamic-load}, @code{get-external}, @code{lookup-external},
  56. @code{lookup-all-externals}, @code{external?}, @code{external-name},
  57. @code{external-value}, and @code{call-external}, is still supported,
  58. but it will not work on Windows, its use is deprecated, and it is
  59. likely to vanish in a future release. The old documentation is
  60. preserved to aid updating of old code:
  61. @stindex dynamic-externals
  62. On architectures that support it, external code can be loaded into a
  63. running Scheme48 process, and C object file bindings can be accessed
  64. at runtime & their values called. These Scheme procedures are exported
  65. by the structure @code{dynamic-externals}.
  66. In some Unices, retrieving a value from the current process may require
  67. a non-trivial amount of computation. We recommend that a dynamically
  68. loaded file contain a single initialization function that creates
  69. shared bindings for the values exported by the file.
  70. @deffn {Scheme procedure} dynamic-load string @returns{} unspecified
  71. Loads the filename named by @var{string} into the current process.
  72. An exception is raised if the file cannot be found or if dynamic
  73. loading is not supported by the host operating system. The file must
  74. have been compiled & linked appropriately. For Linux, for example,
  75. the following commands compile @file{foo.c} into a file @file{foo.so}
  76. that can be loaded dynamically:
  77. @example
  78. % gcc -c -o foo.o foo.c
  79. % ld -shared -o foo.so foo.o@end example
  80. @end deffn
  81. @deffn {Scheme procedure} get-external string @returns{} external
  82. @deffnx {Scheme procedure} external? object @returns{} boolean
  83. @deffnx {Scheme procedure} external-name external @returns{} string
  84. @deffnx {Scheme procedure} external-value external @returns{} byte-vector
  85. These procedures access external values bound in the current process.
  86. @code{Get-external} returns a @dfn{external} object that contains the
  87. value of the C binding with the name @var{string}. It signals a
  88. warning if there is no such binding in the current process.
  89. @code{External?} is the disjoint type predicate for externals, and
  90. @code{external-name} & @code{external-value} return the name & value of
  91. an external. The value is represented as a @embedref{Bitwise
  92. manipulation,byte vector} of length four on 32-bit architectures. The
  93. value is that of the C binding from when @code{get-external} (or
  94. @code{lookup-external}, as described below) was called.
  95. @end deffn
  96. @deffn {Scheme procedure} lookup-external external @returns{} boolean
  97. @deffnx {Scheme procedure} lookup-all-externals @returns{} boolean
  98. @code{Lookup-external} updates the value of @var{external} by looking
  99. up its binding in the current process. It returns @code{#t} if the
  100. external is bound and @code{#f} if not. @code{Lookup-all-externals}
  101. calls @code{lookup-external} on all externals in the current Scheme48
  102. image. It returns @code{#t} if all were bound and @code{#f} if there
  103. was at least one unbound external.
  104. @end deffn
  105. @deffn {Scheme procedure} call-external external argument @dots{} @returns{} value
  106. Calls the C function pointed to by @var{external} with the given
  107. arguments, and returns the value that the C function returned. This
  108. is like @code{call-imported-binding} and @code{call-external-value}
  109. except that the function argument is represented as an external, not as
  110. an imported binding or byte vector containing a pointer. For more
  111. details, @pxref{Calling C functions from Scheme}.
  112. @end deffn