api-smobs.texi 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, 2009, 2013
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Smobs
  7. @section Smobs
  8. @cindex smob
  9. This chapter contains reference information related to defining and
  10. working with smobs. See @ref{Defining New Types (Smobs)} for a
  11. tutorial-like introduction to smobs.
  12. @deftypefun scm_t_bits scm_make_smob_type (const char *name, size_t size)
  13. This function adds a new smob type, named @var{name}, with instance size
  14. @var{size}, to the system. The return value is a tag that is used in
  15. creating instances of the type.
  16. If @var{size} is 0, the default @emph{free} function will do nothing.
  17. If @var{size} is not 0, the default @emph{free} function will
  18. deallocate the memory block pointed to by @code{SCM_SMOB_DATA} with
  19. @code{scm_gc_free}. The @var{what} parameter in the call to
  20. @code{scm_gc_free} will be @var{name}.
  21. Default values are provided for the @emph{mark}, @emph{free},
  22. @emph{print}, and @emph{equalp} functions, as described in
  23. @ref{Defining New Types (Smobs)}. If you want to customize any of
  24. these functions, the call to @code{scm_make_smob_type} should be
  25. immediately followed by calls to one or several of
  26. @code{scm_set_smob_mark}, @code{scm_set_smob_free},
  27. @code{scm_set_smob_print}, and/or @code{scm_set_smob_equalp}.
  28. @end deftypefun
  29. @cindex finalizer
  30. @cindex finalization
  31. @deftypefn {C Function} void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM obj))
  32. This function sets the smob freeing procedure (sometimes referred to as
  33. a @dfn{finalizer}) for the smob type specified by the tag
  34. @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
  35. The @var{free} procedure must deallocate all resources that are
  36. directly associated with the smob instance @var{obj}. It must assume
  37. that all @code{SCM} values that it references have already been freed
  38. and are thus invalid.
  39. It must also not call any libguile function or macro except
  40. @code{scm_gc_free}, @code{SCM_SMOB_FLAGS}, @code{SCM_SMOB_DATA},
  41. @code{SCM_SMOB_DATA_2}, and @code{SCM_SMOB_DATA_3}.
  42. The @var{free} procedure must return 0.
  43. Note that defining a freeing procedure is not necessary if the resources
  44. associated with @var{obj} consists only of memory allocated with
  45. @code{scm_gc_malloc} or @code{scm_gc_malloc_pointerless} because this
  46. memory is automatically reclaimed by the garbage collector when it is no
  47. longer needed (@pxref{Memory Blocks, @code{scm_gc_malloc}}).
  48. @end deftypefn
  49. @cindex precise marking
  50. @deftypefn {C Function} void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM obj))
  51. This function sets the smob marking procedure for the smob type specified by
  52. the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
  53. Defining a marking procedure may sometimes be unnecessary because large
  54. parts of the process' memory (with the exception of
  55. @code{scm_gc_malloc_pointerless} regions, and @code{malloc}- or
  56. @code{scm_malloc}-allocated memory) are scanned for live
  57. pointers@footnote{Conversely, in Guile up to the 1.8 series, the marking
  58. procedure was always required. The reason is that Guile's GC would only
  59. look for pointers in the memory area used for built-in types (the
  60. @dfn{cell heap}), not in user-allocated or statically allocated memory.
  61. This approach is often referred to as @dfn{precise marking}.}.
  62. The @var{mark} procedure must cause @code{scm_gc_mark} to be called
  63. for every @code{SCM} value that is directly referenced by the smob
  64. instance @var{obj}. One of these @code{SCM} values can be returned
  65. from the procedure and Guile will call @code{scm_gc_mark} for it.
  66. This can be used to avoid deep recursions for smob instances that form
  67. a list.
  68. It must not call any libguile function or macro except
  69. @code{scm_gc_mark}, @code{SCM_SMOB_FLAGS}, @code{SCM_SMOB_DATA},
  70. @code{SCM_SMOB_DATA_2}, and @code{SCM_SMOB_DATA_3}.
  71. @end deftypefn
  72. @deftypefn {C Function} void scm_set_smob_print (scm_t_bits tc, int (*print) (SCM obj, SCM port, scm_print_state* pstate))
  73. This function sets the smob printing procedure for the smob type
  74. specified by the tag @var{tc}. @var{tc} is the tag returned by
  75. @code{scm_make_smob_type}.
  76. The @var{print} procedure should output a textual representation of
  77. the smob instance @var{obj} to @var{port}, using information in
  78. @var{pstate}.
  79. The textual representation should be of the form @code{#<name ...>}.
  80. This ensures that @code{read} will not interpret it as some other
  81. Scheme value.
  82. It is often best to ignore @var{pstate} and just print to @var{port}
  83. with @code{scm_display}, @code{scm_write}, @code{scm_simple_format},
  84. and @code{scm_puts}.
  85. @end deftypefn
  86. @deftypefn {C Function} void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM obj1, SCM obj2))
  87. This function sets the smob equality-testing predicate for the smob
  88. type specified by the tag @var{tc}. @var{tc} is the tag returned by
  89. @code{scm_make_smob_type}.
  90. The @var{equalp} procedure should return @code{SCM_BOOL_T} when
  91. @var{obj1} is @code{equal?} to @var{obj2}. Else it should return
  92. @code{SCM_BOOL_F}. Both @var{obj1} and @var{obj2} are instances of the
  93. smob type @var{tc}.
  94. @end deftypefn
  95. @deftypefn {C Function} void scm_assert_smob_type (scm_t_bits tag, SCM val)
  96. When @var{val} is a smob of the type indicated by @var{tag}, do nothing.
  97. Else, signal an error.
  98. @end deftypefn
  99. @deftypefn {C Macro} int SCM_SMOB_PREDICATE (scm_t_bits tag, SCM exp)
  100. Return true if @var{exp} is a smob instance of the type indicated by
  101. @var{tag}, or false otherwise. The expression @var{exp} can be
  102. evaluated more than once, so it shouldn't contain any side effects.
  103. @end deftypefn
  104. @deftypefn {C Function} SCM scm_new_smob (scm_t_bits tag, void *data)
  105. @deftypefnx {C Function} SCM scm_new_double_smob (scm_t_bits tag, void *data, void *data2, void *data3)
  106. Make a new smob of the type with tag @var{tag} and smob data @var{data},
  107. @var{data2}, and @var{data3}, as appropriate.
  108. The @var{tag} is what has been returned by @code{scm_make_smob_type}.
  109. The initial values @var{data}, @var{data2}, and @var{data3} are of
  110. type @code{scm_t_bits}; when you want to use them for @code{SCM}
  111. values, these values need to be converted to a @code{scm_t_bits} first
  112. by using @code{SCM_UNPACK}.
  113. The flags of the smob instance start out as zero.
  114. @end deftypefn
  115. @deftypefn {C Macro} scm_t_bits SCM_SMOB_FLAGS (SCM obj)
  116. Return the 16 extra bits of the smob @var{obj}. No meaning is
  117. predefined for these bits, you can use them freely.
  118. @end deftypefn
  119. @deftypefn {C Macro} scm_t_bits SCM_SET_SMOB_FLAGS (SCM obj, scm_t_bits flags)
  120. Set the 16 extra bits of the smob @var{obj} to @var{flags}. No
  121. meaning is predefined for these bits, you can use them freely.
  122. @end deftypefn
  123. @deftypefn {C Macro} scm_t_bits SCM_SMOB_DATA (SCM obj)
  124. @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_2 (SCM obj)
  125. @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_3 (SCM obj)
  126. Return the first (second, third) immediate word of the smob @var{obj}
  127. as a @code{scm_t_bits} value. When the word contains a @code{SCM}
  128. value, use @code{SCM_SMOB_OBJECT} (etc.) instead.
  129. @end deftypefn
  130. @deftypefn {C Macro} void SCM_SET_SMOB_DATA (SCM obj, scm_t_bits val)
  131. @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_2 (SCM obj, scm_t_bits val)
  132. @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_3 (SCM obj, scm_t_bits val)
  133. Set the first (second, third) immediate word of the smob @var{obj} to
  134. @var{val}. When the word should be set to a @code{SCM} value, use
  135. @code{SCM_SMOB_SET_OBJECT} (etc.) instead.
  136. @end deftypefn
  137. @deftypefn {C Macro} SCM SCM_SMOB_OBJECT (SCM obj)
  138. @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_2 (SCM obj)
  139. @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_3 (SCM obj)
  140. Return the first (second, third) immediate word of the smob @var{obj}
  141. as a @code{SCM} value. When the word contains a @code{scm_t_bits}
  142. value, use @code{SCM_SMOB_DATA} (etc.) instead.
  143. @end deftypefn
  144. @deftypefn {C Macro} void SCM_SET_SMOB_OBJECT (SCM obj, SCM val)
  145. @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_2 (SCM obj, SCM val)
  146. @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_3 (SCM obj, SCM val)
  147. Set the first (second, third) immediate word of the smob @var{obj} to
  148. @var{val}. When the word should be set to a @code{scm_t_bits} value, use
  149. @code{SCM_SMOB_SET_DATA} (etc.) instead.
  150. @end deftypefn
  151. @deftypefn {C Macro} {SCM *} SCM_SMOB_OBJECT_LOC (SCM obj)
  152. @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_2_LOC (SCM obj)
  153. @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_3_LOC (SCM obj)
  154. Return a pointer to the first (second, third) immediate word of the
  155. smob @var{obj}. Note that this is a pointer to @code{SCM}. If you
  156. need to work with @code{scm_t_bits} values, use @code{SCM_PACK} and
  157. @code{SCM_UNPACK}, as appropriate.
  158. @end deftypefn
  159. @deftypefun SCM scm_markcdr (SCM @var{x})
  160. Mark the references in the smob @var{x}, assuming that @var{x}'s first
  161. data word contains an ordinary Scheme object, and @var{x} refers to no
  162. other objects. This function simply returns @var{x}'s first data word.
  163. @end deftypefun
  164. @c Local Variables:
  165. @c TeX-master: "guile.texi"
  166. @c End: