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