* scheme-binding.texi: Renamed to api-binding.texi.
[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 The @var{free} procedure must return 0.
59 @end deftypefn
60
61 @deftypefn {C Function} void scm_set_smob_print (scm_t_bits tc, int (*print) (SCM obj, SCM port, scm_print_state* pstate))
62 This function sets the smob printing procedure for the smob type
63 specified by the tag @var{tc}. @var{tc} is the tag returned by
64 @code{scm_make_smob_type}.
65
66 The @var{print} procedure should output a textual representation of
67 the smob instance @var{obj} to @var{port}, using information in
68 @var{pstate}.
69
70 The textual representation should be of the form @code{#<name ...>}.
71 This ensures that @code{read} will not interpret it as some other
72 Scheme value.
73
74 It is often best to ignore @var{pstate} and just print to @var{port}
75 with @code{scm_display}, @code{scm_write}, @code{scm_simple_format},
76 and @code{scm_puts}.
77 @end deftypefn
78
79 @deftypefn {C Function} void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM obj1, SCM obj1))
80 This function sets the smob equality-testing predicate for the smob
81 type specified by the tag @var{tc}. @var{tc} is the tag returned by
82 @code{scm_make_smob_type}.
83
84 The @var{equalp} procedure should return @code{SCM_BOOL_T} when
85 @var{obj1} is @code{equal?} to @var{obj2}. Else it should return
86 @var{SCM_BOOL_F}. Both @var{obj1} and @var{obj2} are instances of the
87 smob type @var{tc}.
88 @end deftypefn
89
90 @deftypefn {C Macro} int SCM_SMOB_PREDICATE (scm_t_bits tag, SCM exp)
91 Return true iff @var{exp} is a smob instance of the type indicated by
92 @var{tag}. The expression @var{exp} can be evaluated more than once,
93 so it shouldn't contain any side effects.
94 @end deftypefn
95
96 @deftypefn {C Macro} void SCM_NEWSMOB (SCM value, scm_t_bits tag, void *data)
97 @deftypefnx {C Macro} void SCM_NEWSMOB2 (SCM value, scm_t_bits tag, void *data, void *data2)
98 @deftypefnx {C Macro} void SCM_NEWSMOB3 (SCM value, scm_t_bits tag, void *data, void *data2, void *data3)
99 Make @var{value} contain a smob instance of the type with tag
100 @var{tag} and smob data @var{data}, @var{data2}, and @var{data3}, as
101 appropriate.
102
103 The @var{tag} is what has been returned by @code{scm_make_smob_type}.
104 The initial values @var{data}, @var{data2}, and @var{data3} are of
105 type @code{scm_t_bits}; when you want to use them for @code{SCM}
106 values, these values need to be converted to a @code{scm_t_bits} first
107 by using @code{SCM_UNPACK}.
108
109 The flags of the smob instance start out as zero.
110 @end deftypefn
111
112 Since it is often the case (e.g., in smob constructors) that you will
113 create a smob instance and return it, there is also a slightly specialized
114 macro for this situation:
115
116 @deftypefn {C Macro} {} SCM_RETURN_NEWSMOB (scm_t_bits tag, void *data)
117 @deftypefnx {C Macro} {} SCM_RETURN_NEWSMOB2 (scm_t_bits tag, void *data1, void *data2)
118 @deftypefnx {C Macro} {} SCM_RETURN_NEWSMOB3 (scm_t_bits tag, void *data1, void *data2, void *data3)
119 This macro expands to a block of code that creates a smob instance of
120 the type with tag @var{tag} and smob data @var{data}, @var{data2}, and
121 @var{data3}, as with @code{SCM_NEWSMOB}, etc., and causes the
122 surrounding function to return that @code{SCM} value. It should be
123 the last piece of code in a block.
124 @end deftypefn
125
126 @deftypefn {C Macro} scm_t_bits SCM_SMOB_FLAGS (SCM obj)
127 Return the 16 extra bits of the smob @var{obj}. No meaning is
128 predefined for these bits, you can use them freely.
129 @end deftypefn
130
131 @deftypefn {C Macro} scm_t_bits SCM_SET_SMOB_FLAGS (SCM obj, scm_t_bits flags)
132 Set the 16 extra bits of the smob @var{obj} to @var{flags}. No
133 meaning is predefined for these bits, you can use them freely.
134 @end deftypefn
135
136 @deftypefn {C Macro} scm_t_bits SCM_SMOB_DATA (SCM obj)
137 @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_2 (SCM obj)
138 @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_3 (SCM obj)
139 Return the first (second, third) immediate word of the smob @var{obj}
140 as a @code{scm_t_bits} value. When the word contains a @code{SCM}
141 value, use @code{SCM_SMOB_OBJECT} (etc.) instead.
142 @end deftypefn
143
144 @deftypefn {C Macro} void SCM_SET_SMOB_DATA (SCM obj, scm_t_bits val)
145 @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_2 (SCM obj, scm_t_bits val)
146 @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_3 (SCM obj, scm_t_bits 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} value, use
149 @code{SCM_SMOB_SET_OBJECT} (etc.) instead.
150 @end deftypefn
151
152 @deftypefn {C Macro} SCM SCM_SMOB_OBJECT (SCM obj)
153 @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_2 (SCM obj)
154 @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_3 (SCM obj)
155 Return the first (second, third) immediate word of the smob @var{obj}
156 as a @code{SCM} value. When the word contains a @code{scm_t_bits}
157 value, use @code{SCM_SMOB_DATA} (etc.) instead.
158 @end deftypefn
159
160 @deftypefn {C Macro} void SCM_SET_SMOB_OBJECT (SCM obj, SCM val)
161 @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_2 (SCM obj, SCM val)
162 @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_3 (SCM obj, SCM val)
163 Set the first (second, third) immediate word of the smob @var{obj} to
164 @var{val}. When the word should be set to a @code{scm_t_bits} value, use
165 @code{SCM_SMOB_SET_DATA} (etc.) instead.
166 @end deftypefn
167
168 @deftypefn {C Macro} {SCM *} SCM_SMOB_OBJECT_LOC (SCM obj)
169 @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_2_LOC (SCM obj)
170 @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_3_LOC (SCM obj)
171 Return a pointer to the first (second, third) immediate word of the
172 smob @var{obj}. Note that this is a pointer to @code{SCM}. If you
173 need to work with @code{scm_t_bits} values, use @code{SCM_PACK} and
174 @code{SCM_UNPACK}, as appropriate.
175 @end deftypefn
176
177 @deftypefun SCM scm_markcdr (SCM @var{x})
178 Mark the references in the smob @var{x}, assuming that @var{x}'s first
179 data word contains an ordinary Scheme object, and @var{x} refers to no
180 other objects. This function simply returns @var{x}'s first data word.
181 @end deftypefun
182
183 @c Local Variables:
184 @c TeX-master: "guile.texi"
185 @c End: