More words abot what a free function is allowed to do.
[bpt/guile.git] / doc / ref / api-smobs.texi
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
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @page
8 @node Smobs
9 @section Smobs
10
11 This chapter contains reference information related to defining and
12 working with smobs. See @ref{Defining New Types (Smobs)} for a
13 tutorial-like introduction to smobs.
14
15 @deftypefun scm_t_bits scm_make_smob_type (const char *name, size_t size)
16 This function adds a new smob type, named @var{name}, with instance size
17 @var{size}, to the system. The return value is a tag that is used in
18 creating instances of the type.
19
20 If @var{size} is 0, the default @emph{free} function will do nothing.
21
22 If @var{size} is not 0, the default @emph{free} function will
23 deallocate the memory block pointed to by @code{SCM_SMOB_DATA} with
24 @code{scm_gc_free}. The @var{WHAT} parameter in the call to
25 @code{scm_gc_free} will be @var{NAME}.
26
27 Default values are provided for the @emph{mark}, @emph{free},
28 @emph{print}, and @emph{equalp} functions, as described in
29 @ref{Defining New Types (Smobs)}. If you want to customize any of
30 these functions, the call to @code{scm_make_smob_type} should be
31 immediately followed by calls to one or several of
32 @code{scm_set_smob_mark}, @code{scm_set_smob_free},
33 @code{scm_set_smob_print}, and/or @code{scm_set_smob_equalp}.
34 @end deftypefun
35
36 @deftypefn {C Function} void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM obj))
37 This function sets the smob marking procedure for the smob type specified by
38 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
39
40 The @var{mark} procedure must cause @code{scm_gc_mark} to be called
41 for every @code{SCM} value that is directly referenced by the smob
42 instance @var{obj}. One of these @code{SCM} values can be returned
43 from the procedure and Guile will call @code{scm_gc_mark} for it.
44 This can be used to avoid deep recursions for smob instances that form
45 a list.
46 @end deftypefn
47
48 @deftypefn {C Function} void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM obj))
49 This function sets the smob freeing procedure for the smob type
50 specified by the tag @var{tc}. @var{tc} is the tag returned by
51 @code{scm_make_smob_type}.
52
53 The @var{free} procedure must deallocate all resources that are
54 directly associated with the smob instance @var{OBJ}. It must assume
55 that all @code{SCM} values that it references have already been freed
56 and are thus invalid.
57
58 It must also not call any libguile function or macro except
59 @code{scm_gc_free}, @code{SCM_SMOB_FLAGS}, @code{SCM_SMOB_DATA},
60 @code{SCM_SMOB_DATA_2}, and @code{SCM_SMOB_DATA_3}.
61
62 The @var{free} procedure must return 0.
63 @end deftypefn
64
65 @deftypefn {C Function} void scm_set_smob_print (scm_t_bits tc, int (*print) (SCM obj, SCM port, scm_print_state* pstate))
66 This function sets the smob printing procedure for the smob type
67 specified by the tag @var{tc}. @var{tc} is the tag returned by
68 @code{scm_make_smob_type}.
69
70 The @var{print} procedure should output a textual representation of
71 the smob instance @var{obj} to @var{port}, using information in
72 @var{pstate}.
73
74 The textual representation should be of the form @code{#<name ...>}.
75 This ensures that @code{read} will not interpret it as some other
76 Scheme value.
77
78 It is often best to ignore @var{pstate} and just print to @var{port}
79 with @code{scm_display}, @code{scm_write}, @code{scm_simple_format},
80 and @code{scm_puts}.
81 @end deftypefn
82
83 @deftypefn {C Function} void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM obj1, SCM obj1))
84 This function sets the smob equality-testing predicate for the smob
85 type specified by the tag @var{tc}. @var{tc} is the tag returned by
86 @code{scm_make_smob_type}.
87
88 The @var{equalp} procedure should return @code{SCM_BOOL_T} when
89 @var{obj1} is @code{equal?} to @var{obj2}. Else it should return
90 @var{SCM_BOOL_F}. Both @var{obj1} and @var{obj2} are instances of the
91 smob type @var{tc}.
92 @end deftypefn
93
94 @deftypefn {C Function} void scm_assert_smob_type (scm_t_bits tag, SCM val)
95 When @var{val} is a smob of the type indicated by @var{tag}, do nothing.
96 Else, signal an error.
97 @end deftypefn
98
99 @deftypefn {C Macro} int SCM_SMOB_PREDICATE (scm_t_bits tag, SCM exp)
100 Return true iff @var{exp} is a smob instance of the type indicated by
101 @var{tag}. The expression @var{exp} can be evaluated more than once,
102 so it shouldn't contain any side effects.
103 @end deftypefn
104
105 @deftypefn {C Macro} void SCM_NEWSMOB (SCM value, scm_t_bits tag, void *data)
106 @deftypefnx {C Macro} void SCM_NEWSMOB2 (SCM value, scm_t_bits tag, void *data, void *data2)
107 @deftypefnx {C Macro} void SCM_NEWSMOB3 (SCM value, scm_t_bits tag, void *data, void *data2, void *data3)
108 Make @var{value} contain a smob instance of the type with tag
109 @var{tag} and smob data @var{data}, @var{data2}, and @var{data3}, as
110 appropriate.
111
112 The @var{tag} is what has been returned by @code{scm_make_smob_type}.
113 The initial values @var{data}, @var{data2}, and @var{data3} are of
114 type @code{scm_t_bits}; when you want to use them for @code{SCM}
115 values, these values need to be converted to a @code{scm_t_bits} first
116 by using @code{SCM_UNPACK}.
117
118 The flags of the smob instance start out as zero.
119 @end deftypefn
120
121 Since it is often the case (e.g., in smob constructors) that you will
122 create a smob instance and return it, there is also a slightly specialized
123 macro for this situation:
124
125 @deftypefn {C Macro} {} SCM_RETURN_NEWSMOB (scm_t_bits tag, void *data)
126 @deftypefnx {C Macro} {} SCM_RETURN_NEWSMOB2 (scm_t_bits tag, void *data1, void *data2)
127 @deftypefnx {C Macro} {} SCM_RETURN_NEWSMOB3 (scm_t_bits tag, void *data1, void *data2, void *data3)
128 This macro expands to a block of code that creates a smob instance of
129 the type with tag @var{tag} and smob data @var{data}, @var{data2}, and
130 @var{data3}, as with @code{SCM_NEWSMOB}, etc., and causes the
131 surrounding function to return that @code{SCM} value. It should be
132 the last piece of code in a block.
133 @end deftypefn
134
135 @deftypefn {C Macro} scm_t_bits SCM_SMOB_FLAGS (SCM obj)
136 Return the 16 extra bits of the smob @var{obj}. No meaning is
137 predefined for these bits, you can use them freely.
138 @end deftypefn
139
140 @deftypefn {C Macro} scm_t_bits SCM_SET_SMOB_FLAGS (SCM obj, scm_t_bits flags)
141 Set the 16 extra bits of the smob @var{obj} to @var{flags}. No
142 meaning is predefined for these bits, you can use them freely.
143 @end deftypefn
144
145 @deftypefn {C Macro} scm_t_bits SCM_SMOB_DATA (SCM obj)
146 @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_2 (SCM obj)
147 @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_3 (SCM obj)
148 Return the first (second, third) immediate word of the smob @var{obj}
149 as a @code{scm_t_bits} value. When the word contains a @code{SCM}
150 value, use @code{SCM_SMOB_OBJECT} (etc.) instead.
151 @end deftypefn
152
153 @deftypefn {C Macro} void SCM_SET_SMOB_DATA (SCM obj, scm_t_bits val)
154 @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_2 (SCM obj, scm_t_bits val)
155 @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_3 (SCM obj, scm_t_bits val)
156 Set the first (second, third) immediate word of the smob @var{obj} to
157 @var{val}. When the word should be set to a @code{SCM} value, use
158 @code{SCM_SMOB_SET_OBJECT} (etc.) instead.
159 @end deftypefn
160
161 @deftypefn {C Macro} SCM SCM_SMOB_OBJECT (SCM obj)
162 @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_2 (SCM obj)
163 @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_3 (SCM obj)
164 Return the first (second, third) immediate word of the smob @var{obj}
165 as a @code{SCM} value. When the word contains a @code{scm_t_bits}
166 value, use @code{SCM_SMOB_DATA} (etc.) instead.
167 @end deftypefn
168
169 @deftypefn {C Macro} void SCM_SET_SMOB_OBJECT (SCM obj, SCM val)
170 @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_2 (SCM obj, SCM val)
171 @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_3 (SCM obj, SCM val)
172 Set the first (second, third) immediate word of the smob @var{obj} to
173 @var{val}. When the word should be set to a @code{scm_t_bits} value, use
174 @code{SCM_SMOB_SET_DATA} (etc.) instead.
175 @end deftypefn
176
177 @deftypefn {C Macro} {SCM *} SCM_SMOB_OBJECT_LOC (SCM obj)
178 @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_2_LOC (SCM obj)
179 @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_3_LOC (SCM obj)
180 Return a pointer to the first (second, third) immediate word of the
181 smob @var{obj}. Note that this is a pointer to @code{SCM}. If you
182 need to work with @code{scm_t_bits} values, use @code{SCM_PACK} and
183 @code{SCM_UNPACK}, as appropriate.
184 @end deftypefn
185
186 @deftypefun SCM scm_markcdr (SCM @var{x})
187 Mark the references in the smob @var{x}, assuming that @var{x}'s first
188 data word contains an ordinary Scheme object, and @var{x} refers to no
189 other objects. This function simply returns @var{x}'s first data word.
190 @end deftypefun
191
192 @c Local Variables:
193 @c TeX-master: "guile.texi"
194 @c End: