heap.texi 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. @node Interacting with the Scheme heap in C
  2. @section Interacting with the Scheme heap in C
  3. @cindex GC protection in C
  4. Scheme48 uses a precise copying garbage collector. Any code that
  5. allocates objects within the Scheme48 heap may trigger a garbage
  6. collection. Variables bound to values in the Scheme48 heap need to be
  7. registered with the garbage collector so that the value will be safely
  8. held and so that the variables will be updated if the garbage collector
  9. moves the object. The garbage collector has no facility for updating
  10. pointers to the interiors of objects, so such pointers, for example the
  11. ones returned by @code{S48_EXTRACT_STRING}, will likely become invalid
  12. when a garbage collection occurs.
  13. @deftypefn {C macro} {} S48_DECLARE_GC_PROTECT (@var{n})
  14. @deftypefnx {C macro} void S48_GC_PROTECT_@var{n} (s48_value @var{var}@sub{1}, @dots{}, s48_value @var{var}@sub{@var{n}})
  15. @deftypefnx {C macro} void S48_GC_UNPROTECT ()
  16. @code{S48_DECLARE_GC_PROTECT}, where 1 <= @var{n} <= 9, allocates
  17. storage for registering @var{n} variables. At most one use of
  18. @code{S48_DECLARE_GC_PROTECT} may occur in a block. After declaring a
  19. GC protection, @code{S48_GC_PROTECT_@var{n}} registers the @var{n}
  20. variables with the garbage collector. It must be within the scope that
  21. the @code{S48_DECLARE_GC_PROTECT} occurred in and before any code that
  22. can cause a garbage collection. @code{S48_GC_UNPROTECT} removes the
  23. current block's protected variables from the garbage collector's list.
  24. It must be called at the end of the block after any code that may cause
  25. a garbage collection. Omitting any of the three may cause serious and
  26. hard-to-debug problems, because the garbage collector may relocate an
  27. object and invalidate unprotected @code{s48_value} pointers. If not
  28. @code{S48_DECLARE_GC_PROTECT} is matched with a @code{S48_GC_UNPROTECT}
  29. or vice versa, a @code{gc-protection-mismatch} exception is raised when
  30. a C procedure returns to Scheme.
  31. @end deftypefn
  32. @deftypefn {C macro} {void *} S48_GC_PROTECT_GLOBAL (@var{global})
  33. @deftypefnx {C macro} void S48_GC_UNPROTECT_GLOBAL (void *@var{handle})
  34. @code{S48_GC_PROTECT_GLOBAL} permanently registers the l-value
  35. @var{global} with the system as a garbage collection root. It returns
  36. a pointer which may then be supplied to @code{S48_GC_UNPROTECT_GLOBAL}
  37. to unregister the l-value as a root.
  38. @end deftypefn
  39. @subsection Keeping C data structures in the Scheme heap
  40. @cindex storing C data in the Scheme heap
  41. C data structures can be stored within the Scheme heap by embedding
  42. them inside @embedref{Bitwise manipulation, byte vectors}. The
  43. following macros can be used to create and access embedded C objects.
  44. @deftypefn {C macro} s48_value S48_MAKE_VALUE (@var{type})
  45. @deftypefnx {C macro} @var{type} S48_EXTRACT_VALUE (s48_value @var{bytev}, @var{type})
  46. @deftypefnx {C macro} {@var{type} *} S48_EXTRACT_VALUE_POINTER (s48_value @var{bytev}, @var{type})
  47. @deftypefnx {C macro} void S48_SET_VALUE (s48_value @var{bytev}, @var{type}, @var{type} @var{value})
  48. @code{S48_MAKE_VALUE} allocates a byte vector large enough to hold a C
  49. value whose type is @var{type}. @code{S48_EXTRACT_VALUE} returns the
  50. contents of the byte vector @var{bytev} cast to @var{type}, and
  51. @code{S48_EXTRACT_VALUE_POINTER} returns a pointer to the contents of
  52. the byte vector, which is valid only until the next garbage collection.
  53. @code{S48_SET_VALUE} stores a value into the byte vector.
  54. @end deftypefn
  55. @subsection C code and heap images
  56. @cindex restoring C data after resuming images
  57. @cindex dumping Scheme heap images with C data
  58. @cindex record resumers
  59. Scheme48 uses dumped heap images to restore a previous system state.
  60. The Scheme48 heap is written into a file in a machine-independent and
  61. operating-system-independent format. The procedures described above,
  62. however, may be used to create objects in the Scheme heap that contain
  63. information specific to the current machine, operating system, or
  64. process. A heap image containing such objects may not work correctly
  65. when resumed.
  66. To address this problem, a record type may be given a @dfn{resumer}
  67. procedure. On startup, the resumer procedure for a record type is
  68. applied to each record of that type in the image being restarted. This
  69. procedure can update the record in a manner appropriate to the machine,
  70. operating system, or process used to resume the image. Note, though,
  71. that there is no reliable order in which record resumer procedures are
  72. applied. To specify the resumer for a record type, use the
  73. @code{define-record-resumer} procedure from the @embedref{Records,
  74. @code{record-types} structure}.