(Remembering During Operations): Refer to all "Guile
[bpt/guile.git] / doc / ref / data-rep.texi
1 @c essay \input texinfo
2 @c essay @c -*-texinfo-*-
3 @c essay @c %**start of header
4 @c essay @setfilename data-rep.info
5 @c essay @settitle Data Representation in Guile
6 @c essay @c %**end of header
7
8 @c essay @include version.texi
9
10 @c essay @dircategory The Algorithmic Language Scheme
11 @c essay @direntry
12 @c essay * data-rep: (data-rep). Data Representation in Guile --- how to use
13 @c essay Guile objects in your C code.
14 @c essay @end direntry
15
16 @c essay @setchapternewpage off
17
18 @c essay @ifinfo
19 @c essay Data Representation in Guile
20
21 @c essay Copyright (C) 1998, 1999, 2000 Free Software Foundation
22
23 @c essay Permission is granted to make and distribute verbatim copies of
24 @c essay this manual provided the copyright notice and this permission notice
25 @c essay are preserved on all copies.
26
27 @c essay @ignore
28 @c essay Permission is granted to process this file through TeX and print the
29 @c essay results, provided the printed document carries copying permission
30 @c essay notice identical to this one except for the removal of this paragraph
31 @c essay (this paragraph not being relevant to the printed manual).
32 @c essay @end ignore
33
34 @c essay Permission is granted to copy and distribute modified versions of this
35 @c essay manual under the conditions for verbatim copying, provided that the entire
36 @c essay resulting derived work is distributed under the terms of a permission
37 @c essay notice identical to this one.
38
39 @c essay Permission is granted to copy and distribute translations of this manual
40 @c essay into another language, under the above conditions for modified versions,
41 @c essay except that this permission notice may be stated in a translation approved
42 @c essay by the Free Software Foundation.
43 @c essay @end ifinfo
44
45 @c essay @titlepage
46 @c essay @sp 10
47 @c essay @comment The title is printed in a large font.
48 @c essay @title Data Representation in Guile
49 @c essay @subtitle $Id: data-rep.texi,v 1.13 2003-06-21 23:02:58 kryde Exp $
50 @c essay @subtitle For use with Guile @value{VERSION}
51 @c essay @author Jim Blandy
52 @c essay @author Free Software Foundation
53 @c essay @author @email{jimb@@red-bean.com}
54 @c essay @c The following two commands start the copyright page.
55 @c essay @page
56 @c essay @vskip 0pt plus 1filll
57 @c essay @vskip 0pt plus 1filll
58 @c essay Copyright @copyright{} 1998 Free Software Foundation
59
60 @c essay Permission is granted to make and distribute verbatim copies of
61 @c essay this manual provided the copyright notice and this permission notice
62 @c essay are preserved on all copies.
63
64 @c essay Permission is granted to copy and distribute modified versions of this
65 @c essay manual under the conditions for verbatim copying, provided that the entire
66 @c essay resulting derived work is distributed under the terms of a permission
67 @c essay notice identical to this one.
68
69 @c essay Permission is granted to copy and distribute translations of this manual
70 @c essay into another language, under the above conditions for modified versions,
71 @c essay except that this permission notice may be stated in a translation approved
72 @c essay by Free Software Foundation.
73 @c essay @end titlepage
74
75 @c essay @c @smallbook
76 @c essay @c @finalout
77 @c essay @headings double
78
79
80 @c essay @node Top, Data Representation in Scheme, (dir), (dir)
81 @c essay @top Data Representation in Guile
82
83 @c essay @ifinfo
84 @c essay This essay is meant to provide the background necessary to read and
85 @c essay write C code that manipulates Scheme values in a way that conforms to
86 @c essay libguile's interface. If you would like to write or maintain a
87 @c essay Guile-based application in C or C++, this is the first information you
88 @c essay need.
89
90 @c essay In order to make sense of Guile's @code{SCM_} functions, or read
91 @c essay libguile's source code, it's essential to have a good grasp of how Guile
92 @c essay actually represents Scheme values. Otherwise, a lot of the code, and
93 @c essay the conventions it follows, won't make very much sense.
94
95 @c essay We assume you know both C and Scheme, but we do not assume you are
96 @c essay familiar with Guile's C interface.
97 @c essay @end ifinfo
98
99
100 @page
101 @node Data Representation
102 @chapter Data Representation in Guile
103
104 @strong{by Jim Blandy}
105
106 [Due to the rather non-orthogonal and performance-oriented nature of the
107 SCM interface, you need to understand SCM internals *before* you can use
108 the SCM API. That's why this chapter comes first.]
109
110 [NOTE: this is Jim Blandy's essay almost entirely unmodified. It has to
111 be adapted to fit this manual smoothly.]
112
113 In order to make sense of Guile's SCM_ functions, or read libguile's
114 source code, it's essential to have a good grasp of how Guile actually
115 represents Scheme values. Otherwise, a lot of the code, and the
116 conventions it follows, won't make very much sense. This essay is meant
117 to provide the background necessary to read and write C code that
118 manipulates Scheme values in a way that is compatible with libguile.
119
120 We assume you know both C and Scheme, but we do not assume you are
121 familiar with Guile's implementation.
122
123 @menu
124 * Data Representation in Scheme:: Why things aren't just totally
125 straightforward, in general terms.
126 * How Guile does it:: How to write C code that manipulates
127 Guile values, with an explanation
128 of Guile's garbage collector.
129 * Defining New Types (Smobs):: How to extend Guile with your own
130 application-specific datatypes.
131 @end menu
132
133 @node Data Representation in Scheme
134 @section Data Representation in Scheme
135
136 Scheme is a latently-typed language; this means that the system cannot,
137 in general, determine the type of a given expression at compile time.
138 Types only become apparent at run time. Variables do not have fixed
139 types; a variable may hold a pair at one point, an integer at the next,
140 and a thousand-element vector later. Instead, values, not variables,
141 have fixed types.
142
143 In order to implement standard Scheme functions like @code{pair?} and
144 @code{string?} and provide garbage collection, the representation of
145 every value must contain enough information to accurately determine its
146 type at run time. Often, Scheme systems also use this information to
147 determine whether a program has attempted to apply an operation to an
148 inappropriately typed value (such as taking the @code{car} of a string).
149
150 Because variables, pairs, and vectors may hold values of any type,
151 Scheme implementations use a uniform representation for values --- a
152 single type large enough to hold either a complete value or a pointer
153 to a complete value, along with the necessary typing information.
154
155 The following sections will present a simple typing system, and then
156 make some refinements to correct its major weaknesses. However, this is
157 not a description of the system Guile actually uses. It is only an
158 illustration of the issues Guile's system must address. We provide all
159 the information one needs to work with Guile's data in @ref{How Guile
160 does it}.
161
162
163 @menu
164 * A Simple Representation::
165 * Faster Integers::
166 * Cheaper Pairs::
167 * Guile Is Hairier::
168 @end menu
169
170 @node A Simple Representation
171 @subsection A Simple Representation
172
173 The simplest way to meet the above requirements in C would be to
174 represent each value as a pointer to a structure containing a type
175 indicator, followed by a union carrying the real value. Assuming that
176 @code{SCM} is the name of our universal type, we can write:
177
178 @example
179 enum type @{ integer, pair, string, vector, ... @};
180
181 typedef struct value *SCM;
182
183 struct value @{
184 enum type type;
185 union @{
186 int integer;
187 struct @{ SCM car, cdr; @} pair;
188 struct @{ int length; char *elts; @} string;
189 struct @{ int length; SCM *elts; @} vector;
190 ...
191 @} value;
192 @};
193 @end example
194 with the ellipses replaced with code for the remaining Scheme types.
195
196 This representation is sufficient to implement all of Scheme's
197 semantics. If @var{x} is an @code{SCM} value:
198 @itemize @bullet
199 @item
200 To test if @var{x} is an integer, we can write @code{@var{x}->type == integer}.
201 @item
202 To find its value, we can write @code{@var{x}->value.integer}.
203 @item
204 To test if @var{x} is a vector, we can write @code{@var{x}->type == vector}.
205 @item
206 If we know @var{x} is a vector, we can write
207 @code{@var{x}->value.vector.elts[0]} to refer to its first element.
208 @item
209 If we know @var{x} is a pair, we can write
210 @code{@var{x}->value.pair.car} to extract its car.
211 @end itemize
212
213
214 @node Faster Integers
215 @subsection Faster Integers
216
217 Unfortunately, the above representation has a serious disadvantage. In
218 order to return an integer, an expression must allocate a @code{struct
219 value}, initialize it to represent that integer, and return a pointer to
220 it. Furthermore, fetching an integer's value requires a memory
221 reference, which is much slower than a register reference on most
222 processors. Since integers are extremely common, this representation is
223 too costly, in both time and space. Integers should be very cheap to
224 create and manipulate.
225
226 One possible solution comes from the observation that, on many
227 architectures, structures must be aligned on a four-byte boundary.
228 (Whether or not the machine actually requires it, we can write our own
229 allocator for @code{struct value} objects that assures this is true.)
230 In this case, the lower two bits of the structure's address are known to
231 be zero.
232
233 This gives us the room we need to provide an improved representation
234 for integers. We make the following rules:
235 @itemize @bullet
236 @item
237 If the lower two bits of an @code{SCM} value are zero, then the SCM
238 value is a pointer to a @code{struct value}, and everything proceeds as
239 before.
240 @item
241 Otherwise, the @code{SCM} value represents an integer, whose value
242 appears in its upper bits.
243 @end itemize
244
245 Here is C code implementing this convention:
246 @example
247 enum type @{ pair, string, vector, ... @};
248
249 typedef struct value *SCM;
250
251 struct value @{
252 enum type type;
253 union @{
254 struct @{ SCM car, cdr; @} pair;
255 struct @{ int length; char *elts; @} string;
256 struct @{ int length; SCM *elts; @} vector;
257 ...
258 @} value;
259 @};
260
261 #define POINTER_P(x) (((int) (x) & 3) == 0)
262 #define INTEGER_P(x) (! POINTER_P (x))
263
264 #define GET_INTEGER(x) ((int) (x) >> 2)
265 #define MAKE_INTEGER(x) ((SCM) (((x) << 2) | 1))
266 @end example
267
268 Notice that @code{integer} no longer appears as an element of @code{enum
269 type}, and the union has lost its @code{integer} member. Instead, we
270 use the @code{POINTER_P} and @code{INTEGER_P} macros to make a coarse
271 classification of values into integers and non-integers, and do further
272 type testing as before.
273
274 Here's how we would answer the questions posed above (again, assume
275 @var{x} is an @code{SCM} value):
276 @itemize @bullet
277 @item
278 To test if @var{x} is an integer, we can write @code{INTEGER_P (@var{x})}.
279 @item
280 To find its value, we can write @code{GET_INTEGER (@var{x})}.
281 @item
282 To test if @var{x} is a vector, we can write:
283 @example
284 @code{POINTER_P (@var{x}) && @var{x}->type == vector}
285 @end example
286 Given the new representation, we must make sure @var{x} is truly a
287 pointer before we dereference it to determine its complete type.
288 @item
289 If we know @var{x} is a vector, we can write
290 @code{@var{x}->value.vector.elts[0]} to refer to its first element, as
291 before.
292 @item
293 If we know @var{x} is a pair, we can write
294 @code{@var{x}->value.pair.car} to extract its car, just as before.
295 @end itemize
296
297 This representation allows us to operate more efficiently on integers
298 than the first. For example, if @var{x} and @var{y} are known to be
299 integers, we can compute their sum as follows:
300 @example
301 MAKE_INTEGER (GET_INTEGER (@var{x}) + GET_INTEGER (@var{y}))
302 @end example
303 Now, integer math requires no allocation or memory references. Most
304 real Scheme systems actually use an even more efficient representation,
305 but this essay isn't about bit-twiddling. (Hint: what if pointers had
306 @code{01} in their least significant bits, and integers had @code{00}?)
307
308
309 @node Cheaper Pairs
310 @subsection Cheaper Pairs
311
312 However, there is yet another issue to confront. Most Scheme heaps
313 contain more pairs than any other type of object; Jonathan Rees says
314 that pairs occupy 45% of the heap in his Scheme implementation, Scheme
315 48. However, our representation above spends three @code{SCM}-sized
316 words per pair --- one for the type, and two for the @sc{car} and
317 @sc{cdr}. Is there any way to represent pairs using only two words?
318
319 Let us refine the convention we established earlier. Let us assert
320 that:
321 @itemize @bullet
322 @item
323 If the bottom two bits of an @code{SCM} value are @code{#b00}, then
324 it is a pointer, as before.
325 @item
326 If the bottom two bits are @code{#b01}, then the upper bits are an
327 integer. This is a bit more restrictive than before.
328 @item
329 If the bottom two bits are @code{#b10}, then the value, with the bottom
330 two bits masked out, is the address of a pair.
331 @end itemize
332
333 Here is the new C code:
334 @example
335 enum type @{ string, vector, ... @};
336
337 typedef struct value *SCM;
338
339 struct value @{
340 enum type type;
341 union @{
342 struct @{ int length; char *elts; @} string;
343 struct @{ int length; SCM *elts; @} vector;
344 ...
345 @} value;
346 @};
347
348 struct pair @{
349 SCM car, cdr;
350 @};
351
352 #define POINTER_P(x) (((int) (x) & 3) == 0)
353
354 #define INTEGER_P(x) (((int) (x) & 3) == 1)
355 #define GET_INTEGER(x) ((int) (x) >> 2)
356 #define MAKE_INTEGER(x) ((SCM) (((x) << 2) | 1))
357
358 #define PAIR_P(x) (((int) (x) & 3) == 2)
359 #define GET_PAIR(x) ((struct pair *) ((int) (x) & ~3))
360 @end example
361
362 Notice that @code{enum type} and @code{struct value} now only contain
363 provisions for vectors and strings; both integers and pairs have become
364 special cases. The code above also assumes that an @code{int} is large
365 enough to hold a pointer, which isn't generally true.
366
367
368 Our list of examples is now as follows:
369 @itemize @bullet
370 @item
371 To test if @var{x} is an integer, we can write @code{INTEGER_P
372 (@var{x})}; this is as before.
373 @item
374 To find its value, we can write @code{GET_INTEGER (@var{x})}, as
375 before.
376 @item
377 To test if @var{x} is a vector, we can write:
378 @example
379 @code{POINTER_P (@var{x}) && @var{x}->type == vector}
380 @end example
381 We must still make sure that @var{x} is a pointer to a @code{struct
382 value} before dereferencing it to find its type.
383 @item
384 If we know @var{x} is a vector, we can write
385 @code{@var{x}->value.vector.elts[0]} to refer to its first element, as
386 before.
387 @item
388 We can write @code{PAIR_P (@var{x})} to determine if @var{x} is a
389 pair, and then write @code{GET_PAIR (@var{x})->car} to refer to its
390 car.
391 @end itemize
392
393 This change in representation reduces our heap size by 15%. It also
394 makes it cheaper to decide if a value is a pair, because no memory
395 references are necessary; it suffices to check the bottom two bits of
396 the @code{SCM} value. This may be significant when traversing lists, a
397 common activity in a Scheme system.
398
399 Again, most real Scheme systems use a slightly different implementation;
400 for example, if GET_PAIR subtracts off the low bits of @code{x}, instead
401 of masking them off, the optimizer will often be able to combine that
402 subtraction with the addition of the offset of the structure member we
403 are referencing, making a modified pointer as fast to use as an
404 unmodified pointer.
405
406
407 @node Guile Is Hairier
408 @subsection Guile Is Hairier
409
410 We originally started with a very simple typing system --- each object
411 has a field that indicates its type. Then, for the sake of efficiency
412 in both time and space, we moved some of the typing information directly
413 into the @code{SCM} value, and left the rest in the @code{struct value}.
414 Guile itself employs a more complex hierarchy, storing finer and finer
415 gradations of type information in different places, depending on the
416 object's coarser type.
417
418 In the author's opinion, Guile could be simplified greatly without
419 significant loss of efficiency, but the simplified system would still be
420 more complex than what we've presented above.
421
422
423 @node How Guile does it
424 @section How Guile does it
425
426 Here we present the specifics of how Guile represents its data. We
427 don't go into complete detail; an exhaustive description of Guile's
428 system would be boring, and we do not wish to encourage people to write
429 code which depends on its details anyway. We do, however, present
430 everything one need know to use Guile's data.
431
432
433 @menu
434 * General Rules::
435 * Conservative GC::
436 * Immediates vs Non-immediates::
437 * Immediate Datatypes::
438 * Non-immediate Datatypes::
439 * Signalling Type Errors::
440 * Unpacking the SCM type::
441 @end menu
442
443 @node General Rules
444 @subsection General Rules
445
446 Any code which operates on Guile datatypes must @code{#include} the
447 header file @code{<libguile.h>}. This file contains a definition for
448 the @code{SCM} typedef (Guile's universal type, as in the examples
449 above), and definitions and declarations for a host of macros and
450 functions that operate on @code{SCM} values.
451
452 All identifiers declared by @code{<libguile.h>} begin with @code{scm_}
453 or @code{SCM_}.
454
455 @c [[I wish this were true, but I don't think it is at the moment. -JimB]]
456 @c Macros do not evaluate their arguments more than once, unless documented
457 @c to do so.
458
459 The functions described here generally check the types of their
460 @code{SCM} arguments, and signal an error if their arguments are of an
461 inappropriate type. Macros generally do not, unless that is their
462 specified purpose. You must verify their argument types beforehand, as
463 necessary.
464
465 Macros and functions that return a boolean value have names ending in
466 @code{P} or @code{_p} (for ``predicate''). Those that return a negated
467 boolean value have names starting with @code{SCM_N}. For example,
468 @code{SCM_IMP (@var{x})} is a predicate which returns non-zero iff
469 @var{x} is an immediate value (an @code{IM}). @code{SCM_NCONSP
470 (@var{x})} is a predicate which returns non-zero iff @var{x} is
471 @emph{not} a pair object (a @code{CONS}).
472
473
474 @node Conservative GC
475 @subsection Conservative Garbage Collection
476
477 Aside from the latent typing, the major source of constraints on a
478 Scheme implementation's data representation is the garbage collector.
479 The collector must be able to traverse every live object in the heap, to
480 determine which objects are not live.
481
482 There are many ways to implement this, but Guile uses an algorithm
483 called @dfn{mark and sweep}. The collector scans the system's global
484 variables and the local variables on the stack to determine which
485 objects are immediately accessible by the C code. It then scans those
486 objects to find the objects they point to, @i{et cetera}. The collector
487 sets a @dfn{mark bit} on each object it finds, so each object is
488 traversed only once. This process is called @dfn{tracing}.
489
490 When the collector can find no unmarked objects pointed to by marked
491 objects, it assumes that any objects that are still unmarked will never
492 be used by the program (since there is no path of dereferences from any
493 global or local variable that reaches them) and deallocates them.
494
495 In the above paragraphs, we did not specify how the garbage collector
496 finds the global and local variables; as usual, there are many different
497 approaches. Frequently, the programmer must maintain a list of pointers
498 to all global variables that refer to the heap, and another list
499 (adjusted upon entry to and exit from each function) of local variables,
500 for the collector's benefit.
501
502 The list of global variables is usually not too difficult to maintain,
503 since global variables are relatively rare. However, an explicitly
504 maintained list of local variables (in the author's personal experience)
505 is a nightmare to maintain. Thus, Guile uses a technique called
506 @dfn{conservative garbage collection}, to make the local variable list
507 unnecessary.
508
509 The trick to conservative collection is to treat the stack as an
510 ordinary range of memory, and assume that @emph{every} word on the stack
511 is a pointer into the heap. Thus, the collector marks all objects whose
512 addresses appear anywhere in the stack, without knowing for sure how
513 that word is meant to be interpreted.
514
515 Obviously, such a system will occasionally retain objects that are
516 actually garbage, and should be freed. In practice, this is not a
517 problem. The alternative, an explicitly maintained list of local
518 variable addresses, is effectively much less reliable, due to programmer
519 error.
520
521 To accommodate this technique, data must be represented so that the
522 collector can accurately determine whether a given stack word is a
523 pointer or not. Guile does this as follows:
524
525 @itemize @bullet
526 @item
527 Every heap object has a two-word header, called a @dfn{cell}. Some
528 objects, like pairs, fit entirely in a cell's two words; others may
529 store pointers to additional memory in either of the words. For
530 example, strings and vectors store their length in the first word, and a
531 pointer to their elements in the second.
532
533 @item
534 Guile allocates whole arrays of cells at a time, called @dfn{heap
535 segments}. These segments are always allocated so that the cells they
536 contain fall on eight-byte boundaries, or whatever is appropriate for
537 the machine's word size. Guile keeps all cells in a heap segment
538 initialized, whether or not they are currently in use.
539
540 @item
541 Guile maintains a sorted table of heap segments.
542 @end itemize
543
544 Thus, given any random word @var{w} fetched from the stack, Guile's
545 garbage collector can consult the table to see if @var{w} falls within a
546 known heap segment, and check @var{w}'s alignment. If both tests pass,
547 the collector knows that @var{w} is a valid pointer to a cell,
548 intentional or not, and proceeds to trace the cell.
549
550 Note that heap segments do not contain all the data Guile uses; cells
551 for objects like vectors and strings contain pointers to other memory
552 areas. However, since those pointers are internal, and not shared among
553 many pieces of code, it is enough for the collector to find the cell,
554 and then use the cell's type to find more pointers to trace.
555
556
557 @node Immediates vs Non-immediates
558 @subsection Immediates vs Non-immediates
559
560 Guile classifies Scheme objects into two kinds: those that fit entirely
561 within an @code{SCM}, and those that require heap storage.
562
563 The former class are called @dfn{immediates}. The class of immediates
564 includes small integers, characters, boolean values, the empty list, the
565 mysterious end-of-file object, and some others.
566
567 The remaining types are called, not surprisingly, @dfn{non-immediates}.
568 They include pairs, procedures, strings, vectors, and all other data
569 types in Guile.
570
571 @deftypefn Macro int SCM_IMP (SCM @var{x})
572 Return non-zero iff @var{x} is an immediate object.
573 @end deftypefn
574
575 @deftypefn Macro int SCM_NIMP (SCM @var{x})
576 Return non-zero iff @var{x} is a non-immediate object. This is the
577 exact complement of @code{SCM_IMP}, above.
578 @end deftypefn
579
580 Note that for versions of Guile prior to 1.4 it was necessary to use the
581 @code{SCM_NIMP} macro before calling a finer-grained predicate to
582 determine @var{x}'s type, such as @code{SCM_CONSP} or
583 @code{SCM_VECTORP}. This is no longer required: the definitions of all
584 Guile type predicates now include a call to @code{SCM_NIMP} where
585 necessary.
586
587
588 @node Immediate Datatypes
589 @subsection Immediate Datatypes
590
591 The following datatypes are immediate values; that is, they fit entirely
592 within an @code{SCM} value. The @code{SCM_IMP} and @code{SCM_NIMP}
593 macros will distinguish these from non-immediates; see @ref{Immediates
594 vs Non-immediates} for an explanation of the distinction.
595
596 Note that the type predicates for immediate values work correctly on any
597 @code{SCM} value; you do not need to call @code{SCM_IMP} first, to
598 establish that a value is immediate.
599
600 @menu
601 * Integer Data::
602 * Character Data::
603 * Boolean Data::
604 * Unique Values::
605 @end menu
606
607 @node Integer Data
608 @subsubsection Integers
609
610 Here are functions for operating on small integers, that fit within an
611 @code{SCM}. Such integers are called @dfn{immediate numbers}, or
612 @dfn{INUMs}. In general, INUMs occupy all but two bits of an
613 @code{SCM}.
614
615 Bignums and floating-point numbers are non-immediate objects, and have
616 their own, separate accessors. The functions here will not work on
617 them. This is not as much of a problem as you might think, however,
618 because the system never constructs bignums that could fit in an INUM,
619 and never uses floating point values for exact integers.
620
621 @deftypefn Macro int SCM_INUMP (SCM @var{x})
622 Return non-zero iff @var{x} is a small integer value.
623 @end deftypefn
624
625 @deftypefn Macro int SCM_NINUMP (SCM @var{x})
626 The complement of SCM_INUMP.
627 @end deftypefn
628
629 @deftypefn Macro int SCM_INUM (SCM @var{x})
630 Return the value of @var{x} as an ordinary, C integer. If @var{x}
631 is not an INUM, the result is undefined.
632 @end deftypefn
633
634 @deftypefn Macro SCM SCM_MAKINUM (int @var{i})
635 Given a C integer @var{i}, return its representation as an @code{SCM}.
636 This function does not check for overflow.
637 @end deftypefn
638
639
640 @node Character Data
641 @subsubsection Characters
642
643 Here are functions for operating on characters.
644
645 @deftypefn Macro int SCM_CHARP (SCM @var{x})
646 Return non-zero iff @var{x} is a character value.
647 @end deftypefn
648
649 @deftypefn Macro {unsigned int} SCM_CHAR (SCM @var{x})
650 Return the value of @code{x} as a C character. If @var{x} is not a
651 Scheme character, the result is undefined.
652 @end deftypefn
653
654 @deftypefn Macro SCM SCM_MAKE_CHAR (int @var{c})
655 Given a C character @var{c}, return its representation as a Scheme
656 character value.
657 @end deftypefn
658
659
660 @node Boolean Data
661 @subsubsection Booleans
662
663 Here are functions and macros for operating on booleans.
664
665 @deftypefn Macro SCM SCM_BOOL_T
666 @deftypefnx Macro SCM SCM_BOOL_F
667 The Scheme true and false values.
668 @end deftypefn
669
670 @deftypefn Macro int SCM_NFALSEP (@var{x})
671 Convert the Scheme boolean value to a C boolean. Since every object in
672 Scheme except @code{#f} is true, this amounts to comparing @var{x} to
673 @code{#f}; hence the name.
674 @c Noel feels a chill here.
675 @end deftypefn
676
677 @deftypefn Macro SCM SCM_BOOL_NOT (@var{x})
678 Return the boolean inverse of @var{x}. If @var{x} is not a
679 Scheme boolean, the result is undefined.
680 @end deftypefn
681
682
683 @node Unique Values
684 @subsubsection Unique Values
685
686 The immediate values that are neither small integers, characters, nor
687 booleans are all unique values --- that is, datatypes with only one
688 instance.
689
690 @deftypefn Macro SCM SCM_EOL
691 The Scheme empty list object, or ``End Of List'' object, usually written
692 in Scheme as @code{'()}.
693 @end deftypefn
694
695 @deftypefn Macro SCM SCM_EOF_VAL
696 The Scheme end-of-file value. It has no standard written
697 representation, for obvious reasons.
698 @end deftypefn
699
700 @deftypefn Macro SCM SCM_UNSPECIFIED
701 The value returned by expressions which the Scheme standard says return
702 an ``unspecified'' value.
703
704 This is sort of a weirdly literal way to take things, but the standard
705 read-eval-print loop prints nothing when the expression returns this
706 value, so it's not a bad idea to return this when you can't think of
707 anything else helpful.
708 @end deftypefn
709
710 @deftypefn Macro SCM SCM_UNDEFINED
711 The ``undefined'' value. Its most important property is that is not
712 equal to any valid Scheme value. This is put to various internal uses
713 by C code interacting with Guile.
714
715 For example, when you write a C function that is callable from Scheme
716 and which takes optional arguments, the interpreter passes
717 @code{SCM_UNDEFINED} for any arguments you did not receive.
718
719 We also use this to mark unbound variables.
720 @end deftypefn
721
722 @deftypefn Macro int SCM_UNBNDP (SCM @var{x})
723 Return true if @var{x} is @code{SCM_UNDEFINED}. Apply this to a
724 symbol's value to see if it has a binding as a global variable.
725 @end deftypefn
726
727
728 @node Non-immediate Datatypes
729 @subsection Non-immediate Datatypes
730
731 A non-immediate datatype is one which lives in the heap, either because
732 it cannot fit entirely within a @code{SCM} word, or because it denotes a
733 specific storage location (in the nomenclature of the Revised^5 Report
734 on Scheme).
735
736 The @code{SCM_IMP} and @code{SCM_NIMP} macros will distinguish these
737 from immediates; see @ref{Immediates vs Non-immediates}.
738
739 Given a cell, Guile distinguishes between pairs and other non-immediate
740 types by storing special @dfn{tag} values in a non-pair cell's car, that
741 cannot appear in normal pairs. A cell with a non-tag value in its car
742 is an ordinary pair. The type of a cell with a tag in its car depends
743 on the tag; the non-immediate type predicates test this value. If a tag
744 value appears elsewhere (in a vector, for example), the heap may become
745 corrupted.
746
747 Note how the type information for a non-immediate object is split
748 between the @code{SCM} word and the cell that the @code{SCM} word points
749 to. The @code{SCM} word itself only indicates that the object is
750 non-immediate --- in other words stored in a heap cell. The tag stored
751 in the first word of the heap cell indicates more precisely the type of
752 that object.
753
754 The type predicates for non-immediate values work correctly on any
755 @code{SCM} value; you do not need to call @code{SCM_NIMP} first, to
756 establish that a value is non-immediate.
757
758 @menu
759 * Pair Data::
760 * Vector Data::
761 * Procedures::
762 * Closures::
763 * Subrs::
764 * Port Data::
765 @end menu
766
767
768 @node Pair Data
769 @subsubsection Pairs
770
771 Pairs are the essential building block of list structure in Scheme. A
772 pair object has two fields, called the @dfn{car} and the @dfn{cdr}.
773
774 It is conventional for a pair's @sc{car} to contain an element of a
775 list, and the @sc{cdr} to point to the next pair in the list, or to
776 contain @code{SCM_EOL}, indicating the end of the list. Thus, a set of
777 pairs chained through their @sc{cdr}s constitutes a singly-linked list.
778 Scheme and libguile define many functions which operate on lists
779 constructed in this fashion, so although lists chained through the
780 @sc{car}s of pairs will work fine too, they may be less convenient to
781 manipulate, and receive less support from the community.
782
783 Guile implements pairs by mapping the @sc{car} and @sc{cdr} of a pair
784 directly into the two words of the cell.
785
786
787 @deftypefn Macro int SCM_CONSP (SCM @var{x})
788 Return non-zero iff @var{x} is a Scheme pair object.
789 @end deftypefn
790
791 @deftypefn Macro int SCM_NCONSP (SCM @var{x})
792 The complement of SCM_CONSP.
793 @end deftypefn
794
795 @deftypefun SCM scm_cons (SCM @var{car}, SCM @var{cdr})
796 Allocate (``CONStruct'') a new pair, with @var{car} and @var{cdr} as its
797 contents.
798 @end deftypefun
799
800 The macros below perform no type checking. The results are undefined if
801 @var{cell} is an immediate. However, since all non-immediate Guile
802 objects are constructed from cells, and these macros simply return the
803 first element of a cell, they actually can be useful on datatypes other
804 than pairs. (Of course, it is not very modular to use them outside of
805 the code which implements that datatype.)
806
807 @deftypefn Macro SCM SCM_CAR (SCM @var{cell})
808 Return the @sc{car}, or first field, of @var{cell}.
809 @end deftypefn
810
811 @deftypefn Macro SCM SCM_CDR (SCM @var{cell})
812 Return the @sc{cdr}, or second field, of @var{cell}.
813 @end deftypefn
814
815 @deftypefn Macro void SCM_SETCAR (SCM @var{cell}, SCM @var{x})
816 Set the @sc{car} of @var{cell} to @var{x}.
817 @end deftypefn
818
819 @deftypefn Macro void SCM_SETCDR (SCM @var{cell}, SCM @var{x})
820 Set the @sc{cdr} of @var{cell} to @var{x}.
821 @end deftypefn
822
823 @deftypefn Macro SCM SCM_CAAR (SCM @var{cell})
824 @deftypefnx Macro SCM SCM_CADR (SCM @var{cell})
825 @deftypefnx Macro SCM SCM_CDAR (SCM @var{cell}) @dots{}
826 @deftypefnx Macro SCM SCM_CDDDDR (SCM @var{cell})
827 Return the @sc{car} of the @sc{car} of @var{cell}, the @sc{car} of the
828 @sc{cdr} of @var{cell}, @i{et cetera}.
829 @end deftypefn
830
831
832 @node Vector Data
833 @subsubsection Vectors, Strings, and Symbols
834
835 Vectors, strings, and symbols have some properties in common. They all
836 have a length, and they all have an array of elements. In the case of a
837 vector, the elements are @code{SCM} values; in the case of a string or
838 symbol, the elements are characters.
839
840 All these types store their length (along with some tagging bits) in the
841 @sc{car} of their header cell, and store a pointer to the elements in
842 their @sc{cdr}. Thus, the @code{SCM_CAR} and @code{SCM_CDR} macros
843 are (somewhat) meaningful when applied to these datatypes.
844
845 @deftypefn Macro int SCM_VECTORP (SCM @var{x})
846 Return non-zero iff @var{x} is a vector.
847 @end deftypefn
848
849 @deftypefn Macro int SCM_STRINGP (SCM @var{x})
850 Return non-zero iff @var{x} is a string.
851 @end deftypefn
852
853 @deftypefn Macro int SCM_SYMBOLP (SCM @var{x})
854 Return non-zero iff @var{x} is a symbol.
855 @end deftypefn
856
857 @deftypefn Macro int SCM_VECTOR_LENGTH (SCM @var{x})
858 @deftypefnx Macro int SCM_STRING_LENGTH (SCM @var{x})
859 @deftypefnx Macro int SCM_SYMBOL_LENGTH (SCM @var{x})
860 Return the length of the object @var{x}. The result is undefined if
861 @var{x} is not a vector, string, or symbol, respectively.
862 @end deftypefn
863
864 @deftypefn Macro {SCM *} SCM_VECTOR_BASE (SCM @var{x})
865 Return a pointer to the array of elements of the vector @var{x}.
866 The result is undefined if @var{x} is not a vector.
867 @end deftypefn
868
869 @deftypefn Macro {char *} SCM_STRING_CHARS (SCM @var{x})
870 @deftypefnx Macro {char *} SCM_SYMBOL_CHARS (SCM @var{x})
871 Return a pointer to the characters of @var{x}. The result is undefined
872 if @var{x} is not a symbol or string, respectively.
873 @end deftypefn
874
875 There are also a few magic values stuffed into memory before a symbol's
876 characters, but you don't want to know about those. What cruft!
877
878 Note that @code{SCM_VECTOR_BASE}, @code{SCM_STRING_CHARS} and
879 @code{SCM_SYMBOL_CHARS} return pointers to data within the respective
880 object. Care must be taken that the object is not garbage collected
881 while that data is still being accessed. This is the same as for a
882 smob, @xref{Remembering During Operations}.
883
884
885 @node Procedures
886 @subsubsection Procedures
887
888 Guile provides two kinds of procedures: @dfn{closures}, which are the
889 result of evaluating a @code{lambda} expression, and @dfn{subrs}, which
890 are C functions packaged up as Scheme objects, to make them available to
891 Scheme programmers.
892
893 (There are actually other sorts of procedures: compiled closures, and
894 continuations; see the source code for details about them.)
895
896 @deftypefun SCM scm_procedure_p (SCM @var{x})
897 Return @code{SCM_BOOL_T} iff @var{x} is a Scheme procedure object, of
898 any sort. Otherwise, return @code{SCM_BOOL_F}.
899 @end deftypefun
900
901
902 @node Closures
903 @subsubsection Closures
904
905 [FIXME: this needs to be further subbed, but texinfo has no subsubsub]
906
907 A closure is a procedure object, generated as the value of a
908 @code{lambda} expression in Scheme. The representation of a closure is
909 straightforward --- it contains a pointer to the code of the lambda
910 expression from which it was created, and a pointer to the environment
911 it closes over.
912
913 In Guile, each closure also has a property list, allowing the system to
914 store information about the closure. I'm not sure what this is used for
915 at the moment --- the debugger, maybe?
916
917 @deftypefn Macro int SCM_CLOSUREP (SCM @var{x})
918 Return non-zero iff @var{x} is a closure.
919 @end deftypefn
920
921 @deftypefn Macro SCM SCM_PROCPROPS (SCM @var{x})
922 Return the property list of the closure @var{x}. The results are
923 undefined if @var{x} is not a closure.
924 @end deftypefn
925
926 @deftypefn Macro void SCM_SETPROCPROPS (SCM @var{x}, SCM @var{p})
927 Set the property list of the closure @var{x} to @var{p}. The results
928 are undefined if @var{x} is not a closure.
929 @end deftypefn
930
931 @deftypefn Macro SCM SCM_CODE (SCM @var{x})
932 Return the code of the closure @var{x}. The result is undefined if
933 @var{x} is not a closure.
934
935 This function should probably only be used internally by the
936 interpreter, since the representation of the code is intimately
937 connected with the interpreter's implementation.
938 @end deftypefn
939
940 @deftypefn Macro SCM SCM_ENV (SCM @var{x})
941 Return the environment enclosed by @var{x}.
942 The result is undefined if @var{x} is not a closure.
943
944 This function should probably only be used internally by the
945 interpreter, since the representation of the environment is intimately
946 connected with the interpreter's implementation.
947 @end deftypefn
948
949
950 @node Subrs
951 @subsubsection Subrs
952
953 [FIXME: this needs to be further subbed, but texinfo has no subsubsub]
954
955 A subr is a pointer to a C function, packaged up as a Scheme object to
956 make it callable by Scheme code. In addition to the function pointer,
957 the subr also contains a pointer to the name of the function, and
958 information about the number of arguments accepted by the C function, for
959 the sake of error checking.
960
961 There is no single type predicate macro that recognizes subrs, as
962 distinct from other kinds of procedures. The closest thing is
963 @code{scm_procedure_p}; see @ref{Procedures}.
964
965 @deftypefn Macro {char *} SCM_SNAME (@var{x})
966 Return the name of the subr @var{x}. The result is undefined if
967 @var{x} is not a subr.
968 @end deftypefn
969
970 @deftypefun SCM scm_c_define_gsubr (char *@var{name}, int @var{req}, int @var{opt}, int @var{rest}, SCM (*@var{function})())
971 Create a new subr object named @var{name}, based on the C function
972 @var{function}, make it visible to Scheme the value of as a global
973 variable named @var{name}, and return the subr object.
974
975 The subr object accepts @var{req} required arguments, @var{opt} optional
976 arguments, and a @var{rest} argument iff @var{rest} is non-zero. The C
977 function @var{function} should accept @code{@var{req} + @var{opt}}
978 arguments, or @code{@var{req} + @var{opt} + 1} arguments if @code{rest}
979 is non-zero.
980
981 When a subr object is applied, it must be applied to at least @var{req}
982 arguments, or else Guile signals an error. @var{function} receives the
983 subr's first @var{req} arguments as its first @var{req} arguments. If
984 there are fewer than @var{opt} arguments remaining, then @var{function}
985 receives the value @code{SCM_UNDEFINED} for any missing optional
986 arguments. If @var{rst} is non-zero, then any arguments after the first
987 @code{@var{req} + @var{opt}} are packaged up as a list as passed as
988 @var{function}'s last argument.
989
990 Note that subrs can actually only accept a predefined set of
991 combinations of required, optional, and rest arguments. For example, a
992 subr can take one required argument, or one required and one optional
993 argument, but a subr can't take one required and two optional arguments.
994 It's bizarre, but that's the way the interpreter was written. If the
995 arguments to @code{scm_c_define_gsubr} do not fit one of the predefined
996 patterns, then @code{scm_c_define_gsubr} will return a compiled closure
997 object instead of a subr object.
998 @end deftypefun
999
1000
1001 @node Port Data
1002 @subsubsection Ports
1003
1004 Haven't written this yet, 'cos I don't understand ports yet.
1005
1006
1007 @node Signalling Type Errors
1008 @subsection Signalling Type Errors
1009
1010 Every function visible at the Scheme level should aggressively check the
1011 types of its arguments, to avoid misinterpreting a value, and perhaps
1012 causing a segmentation fault. Guile provides some macros to make this
1013 easier.
1014
1015 @deftypefn Macro void SCM_ASSERT (int @var{test}, SCM @var{obj}, unsigned int @var{position}, const char *@var{subr})
1016 If @var{test} is zero, signal a ``wrong type argument'' error,
1017 attributed to the subroutine named @var{subr}, operating on the value
1018 @var{obj}, which is the @var{position}'th argument of @var{subr}.
1019 @end deftypefn
1020
1021 @deftypefn Macro int SCM_ARG1
1022 @deftypefnx Macro int SCM_ARG2
1023 @deftypefnx Macro int SCM_ARG3
1024 @deftypefnx Macro int SCM_ARG4
1025 @deftypefnx Macro int SCM_ARG5
1026 @deftypefnx Macro int SCM_ARG6
1027 @deftypefnx Macro int SCM_ARG7
1028 One of the above values can be used for @var{position} to indicate the
1029 number of the argument of @var{subr} which is being checked.
1030 Alternatively, a positive integer number can be used, which allows to
1031 check arguments after the seventh. However, for parameter numbers up to
1032 seven it is preferable to use @code{SCM_ARGN} instead of the
1033 corresponding raw number, since it will make the code easier to
1034 understand.
1035 @end deftypefn
1036
1037 @deftypefn Macro int SCM_ARGn
1038 Passing a value of zero or @code{SCM_ARGn} for @var{position} allows to
1039 leave it unspecified which argument's type is incorrect. Again,
1040 @code{SCM_ARGn} should be preferred over a raw zero constant.
1041 @end deftypefn
1042
1043
1044 @node Unpacking the SCM type
1045 @subsection Unpacking the SCM Type
1046
1047 The previous sections have explained how @code{SCM} values can refer to
1048 immediate and non-immediate Scheme objects. For immediate objects, the
1049 complete object value is stored in the @code{SCM} word itself, while for
1050 non-immediates, the @code{SCM} word contains a pointer to a heap cell,
1051 and further information about the object in question is stored in that
1052 cell. This section describes how the @code{SCM} type is actually
1053 represented and used at the C level.
1054
1055 In fact, there are two basic C data types to represent objects in Guile:
1056
1057 @deftp {Data type} SCM
1058 @code{SCM} is the user level abstract C type that is used to represent
1059 all of Guile's Scheme objects, no matter what the Scheme object type is.
1060 No C operation except assignment is guaranteed to work with variables of
1061 type @code{SCM}, so you should only use macros and functions to work
1062 with @code{SCM} values. Values are converted between C data types and
1063 the @code{SCM} type with utility functions and macros.
1064 @end deftp
1065 @cindex SCM data type
1066
1067 @deftp {Data type} scm_t_bits
1068 @code{scm_t_bits} is an integral data type that is guaranteed to be
1069 large enough to hold all information that is required to represent any
1070 Scheme object. While this data type is mostly used to implement Guile's
1071 internals, the use of this type is also necessary to write certain kinds
1072 of extensions to Guile.
1073 @end deftp
1074
1075 @menu
1076 * Relationship between SCM and scm_t_bits::
1077 * Immediate objects::
1078 * Non-immediate objects::
1079 * Allocating Cells::
1080 * Heap Cell Type Information::
1081 * Accessing Cell Entries::
1082 * Basic Rules for Accessing Cell Entries::
1083 @end menu
1084
1085
1086 @node Relationship between SCM and scm_t_bits
1087 @subsubsection Relationship between @code{SCM} and @code{scm_t_bits}
1088
1089 A variable of type @code{SCM} is guaranteed to hold a valid Scheme
1090 object. A variable of type @code{scm_t_bits}, on the other hand, may
1091 hold a representation of a @code{SCM} value as a C integral type, but
1092 may also hold any C value, even if it does not correspond to a valid
1093 Scheme object.
1094
1095 For a variable @var{x} of type @code{SCM}, the Scheme object's type
1096 information is stored in a form that is not directly usable. To be able
1097 to work on the type encoding of the scheme value, the @code{SCM}
1098 variable has to be transformed into the corresponding representation as
1099 a @code{scm_t_bits} variable @var{y} by using the @code{SCM_UNPACK}
1100 macro. Once this has been done, the type of the scheme object @var{x}
1101 can be derived from the content of the bits of the @code{scm_t_bits}
1102 value @var{y}, in the way illustrated by the example earlier in this
1103 chapter (@pxref{Cheaper Pairs}). Conversely, a valid bit encoding of a
1104 Scheme value as a @code{scm_t_bits} variable can be transformed into the
1105 corresponding @code{SCM} value using the @code{SCM_PACK} macro.
1106
1107 @deftypefn Macro scm_t_bits SCM_UNPACK (SCM @var{x})
1108 Transforms the @code{SCM} value @var{x} into its representation as an
1109 integral type. Only after applying @code{SCM_UNPACK} it is possible to
1110 access the bits and contents of the @code{SCM} value.
1111 @end deftypefn
1112
1113 @deftypefn Macro SCM SCM_PACK (scm_t_bits @var{x})
1114 Takes a valid integral representation of a Scheme object and transforms
1115 it into its representation as a @code{SCM} value.
1116 @end deftypefn
1117
1118
1119 @node Immediate objects
1120 @subsubsection Immediate objects
1121
1122 A Scheme object may either be an immediate, i.e. carrying all necessary
1123 information by itself, or it may contain a reference to a @dfn{cell}
1124 with additional information on the heap. Although in general it should
1125 be irrelevant for user code whether an object is an immediate or not,
1126 within Guile's own code the distinction is sometimes of importance.
1127 Thus, the following low level macro is provided:
1128
1129 @deftypefn Macro int SCM_IMP (SCM @var{x})
1130 A Scheme object is an immediate if it fulfills the @code{SCM_IMP}
1131 predicate, otherwise it holds an encoded reference to a heap cell. The
1132 result of the predicate is delivered as a C style boolean value. User
1133 code and code that extends Guile should normally not be required to use
1134 this macro.
1135 @end deftypefn
1136
1137 @noindent
1138 Summary:
1139 @itemize @bullet
1140 @item
1141 Given a Scheme object @var{x} of unknown type, check first
1142 with @code{SCM_IMP (@var{x})} if it is an immediate object.
1143 @item
1144 If so, all of the type and value information can be determined from the
1145 @code{scm_t_bits} value that is delivered by @code{SCM_UNPACK
1146 (@var{x})}.
1147 @end itemize
1148
1149
1150 @node Non-immediate objects
1151 @subsubsection Non-immediate objects
1152
1153 A Scheme object of type @code{SCM} that does not fulfill the
1154 @code{SCM_IMP} predicate holds an encoded reference to a heap cell.
1155 This reference can be decoded to a C pointer to a heap cell using the
1156 @code{SCM2PTR} macro. The encoding of a pointer to a heap cell into a
1157 @code{SCM} value is done using the @code{PTR2SCM} macro.
1158
1159 @c (FIXME:: this name should be changed)
1160 @deftypefn Macro (scm_t_cell *) SCM2PTR (SCM @var{x})
1161 Extract and return the heap cell pointer from a non-immediate @code{SCM}
1162 object @var{x}.
1163 @end deftypefn
1164
1165 @c (FIXME:: this name should be changed)
1166 @deftypefn Macro SCM PTR2SCM (scm_t_cell * @var{x})
1167 Return a @code{SCM} value that encodes a reference to the heap cell
1168 pointer @var{x}.
1169 @end deftypefn
1170
1171 Note that it is also possible to transform a non-immediate @code{SCM}
1172 value by using @code{SCM_UNPACK} into a @code{scm_t_bits} variable.
1173 However, the result of @code{SCM_UNPACK} may not be used as a pointer to
1174 a @code{scm_t_cell}: only @code{SCM2PTR} is guaranteed to transform a
1175 @code{SCM} object into a valid pointer to a heap cell. Also, it is not
1176 allowed to apply @code{PTR2SCM} to anything that is not a valid pointer
1177 to a heap cell.
1178
1179 @noindent
1180 Summary:
1181 @itemize @bullet
1182 @item
1183 Only use @code{SCM2PTR} on @code{SCM} values for which @code{SCM_IMP} is
1184 false!
1185 @item
1186 Don't use @code{(scm_t_cell *) SCM_UNPACK (@var{x})}! Use @code{SCM2PTR
1187 (@var{x})} instead!
1188 @item
1189 Don't use @code{PTR2SCM} for anything but a cell pointer!
1190 @end itemize
1191
1192 @node Allocating Cells
1193 @subsubsection Allocating Cells
1194
1195 Guile provides both ordinary cells with two slots, and double cells
1196 with four slots. The following two function are the most primitive
1197 way to allocate such cells.
1198
1199 If the caller intends to use it as a header for some other type, she
1200 must pass an appropriate magic value in @var{word_0}, to mark it as a
1201 member of that type, and pass whatever value as @var{word_1}, etc that
1202 the type expects. You should generally not need these functions,
1203 unless you are implementing a new datatype, and thoroughly understand
1204 the code in @code{<libguile/tags.h>}.
1205
1206 If you just want to allocate pairs, use @code{scm_cons}.
1207
1208 @deftypefn Function SCM scm_cell (scm_t_bits word_0, scm_t_bits word_1)
1209 Allocate a new cell, initialize the two slots with @var{word_0} and
1210 @var{word_1}, and return it.
1211
1212 Note that @var{word_0} and @var{word_1} are of type @code{scm_t_bits}.
1213 If you want to pass a @code{SCM} object, you need to use
1214 @code{SCM_UNPACK}.
1215 @end deftypefn
1216
1217 @deftypefn Function SCM scm_double_cell (scm_t_bits word_0, scm_t_bits word_1, scm_t_bits word_2, scm_t_bits word_3)
1218 Like @code{scm_cell}, but allocates a double cell with four
1219 slots.
1220 @end deftypefn
1221
1222 @node Heap Cell Type Information
1223 @subsubsection Heap Cell Type Information
1224
1225 Heap cells contain a number of entries, each of which is either a scheme
1226 object of type @code{SCM} or a raw C value of type @code{scm_t_bits}.
1227 Which of the cell entries contain Scheme objects and which contain raw C
1228 values is determined by the first entry of the cell, which holds the
1229 cell type information.
1230
1231 @deftypefn Macro scm_t_bits SCM_CELL_TYPE (SCM @var{x})
1232 For a non-immediate Scheme object @var{x}, deliver the content of the
1233 first entry of the heap cell referenced by @var{x}. This value holds
1234 the information about the cell type.
1235 @end deftypefn
1236
1237 @deftypefn Macro void SCM_SET_CELL_TYPE (SCM @var{x}, scm_t_bits @var{t})
1238 For a non-immediate Scheme object @var{x}, write the value @var{t} into
1239 the first entry of the heap cell referenced by @var{x}. The value
1240 @var{t} must hold a valid cell type.
1241 @end deftypefn
1242
1243
1244 @node Accessing Cell Entries
1245 @subsubsection Accessing Cell Entries
1246
1247 For a non-immediate Scheme object @var{x}, the object type can be
1248 determined by reading the cell type entry using the @code{SCM_CELL_TYPE}
1249 macro. For each different type of cell it is known which cell entries
1250 hold Scheme objects and which cell entries hold raw C data. To access
1251 the different cell entries appropriately, the following macros are
1252 provided.
1253
1254 @deftypefn Macro scm_t_bits SCM_CELL_WORD (SCM @var{x}, unsigned int @var{n})
1255 Deliver the cell entry @var{n} of the heap cell referenced by the
1256 non-immediate Scheme object @var{x} as raw data. It is illegal, to
1257 access cell entries that hold Scheme objects by using these macros. For
1258 convenience, the following macros are also provided.
1259 @itemize @bullet
1260 @item
1261 SCM_CELL_WORD_0 (@var{x}) @result{} SCM_CELL_WORD (@var{x}, 0)
1262 @item
1263 SCM_CELL_WORD_1 (@var{x}) @result{} SCM_CELL_WORD (@var{x}, 1)
1264 @item
1265 @dots{}
1266 @item
1267 SCM_CELL_WORD_@var{n} (@var{x}) @result{} SCM_CELL_WORD (@var{x}, @var{n})
1268 @end itemize
1269 @end deftypefn
1270
1271 @deftypefn Macro SCM SCM_CELL_OBJECT (SCM @var{x}, unsigned int @var{n})
1272 Deliver the cell entry @var{n} of the heap cell referenced by the
1273 non-immediate Scheme object @var{x} as a Scheme object. It is illegal,
1274 to access cell entries that do not hold Scheme objects by using these
1275 macros. For convenience, the following macros are also provided.
1276 @itemize @bullet
1277 @item
1278 SCM_CELL_OBJECT_0 (@var{x}) @result{} SCM_CELL_OBJECT (@var{x}, 0)
1279 @item
1280 SCM_CELL_OBJECT_1 (@var{x}) @result{} SCM_CELL_OBJECT (@var{x}, 1)
1281 @item
1282 @dots{}
1283 @item
1284 SCM_CELL_OBJECT_@var{n} (@var{x}) @result{} SCM_CELL_OBJECT (@var{x},
1285 @var{n})
1286 @end itemize
1287 @end deftypefn
1288
1289 @deftypefn Macro void SCM_SET_CELL_WORD (SCM @var{x}, unsigned int @var{n}, scm_t_bits @var{w})
1290 Write the raw C value @var{w} into entry number @var{n} of the heap cell
1291 referenced by the non-immediate Scheme value @var{x}. Values that are
1292 written into cells this way may only be read from the cells using the
1293 @code{SCM_CELL_WORD} macros or, in case cell entry 0 is written, using
1294 the @code{SCM_CELL_TYPE} macro. For the special case of cell entry 0 it
1295 has to be made sure that @var{w} contains a cell type information which
1296 does not describe a Scheme object. For convenience, the following
1297 macros are also provided.
1298 @itemize @bullet
1299 @item
1300 SCM_SET_CELL_WORD_0 (@var{x}, @var{w}) @result{} SCM_SET_CELL_WORD
1301 (@var{x}, 0, @var{w})
1302 @item
1303 SCM_SET_CELL_WORD_1 (@var{x}, @var{w}) @result{} SCM_SET_CELL_WORD
1304 (@var{x}, 1, @var{w})
1305 @item
1306 @dots{}
1307 @item
1308 SCM_SET_CELL_WORD_@var{n} (@var{x}, @var{w}) @result{} SCM_SET_CELL_WORD
1309 (@var{x}, @var{n}, @var{w})
1310 @end itemize
1311 @end deftypefn
1312
1313 @deftypefn Macro void SCM_SET_CELL_OBJECT (SCM @var{x}, unsigned int @var{n}, SCM @var{o})
1314 Write the Scheme object @var{o} into entry number @var{n} of the heap
1315 cell referenced by the non-immediate Scheme value @var{x}. Values that
1316 are written into cells this way may only be read from the cells using
1317 the @code{SCM_CELL_OBJECT} macros or, in case cell entry 0 is written,
1318 using the @code{SCM_CELL_TYPE} macro. For the special case of cell
1319 entry 0 the writing of a Scheme object into this cell is only allowed
1320 if the cell forms a Scheme pair. For convenience, the following macros
1321 are also provided.
1322 @itemize @bullet
1323 @item
1324 SCM_SET_CELL_OBJECT_0 (@var{x}, @var{o}) @result{} SCM_SET_CELL_OBJECT
1325 (@var{x}, 0, @var{o})
1326 @item
1327 SCM_SET_CELL_OBJECT_1 (@var{x}, @var{o}) @result{} SCM_SET_CELL_OBJECT
1328 (@var{x}, 1, @var{o})
1329 @item
1330 @dots{}
1331 @item
1332 SCM_SET_CELL_OBJECT_@var{n} (@var{x}, @var{o}) @result{}
1333 SCM_SET_CELL_OBJECT (@var{x}, @var{n}, @var{o})
1334 @end itemize
1335 @end deftypefn
1336
1337 @noindent
1338 Summary:
1339 @itemize @bullet
1340 @item
1341 For a non-immediate Scheme object @var{x} of unknown type, get the type
1342 information by using @code{SCM_CELL_TYPE (@var{x})}.
1343 @item
1344 As soon as the cell type information is available, only use the
1345 appropriate access methods to read and write data to the different cell
1346 entries.
1347 @end itemize
1348
1349
1350 @node Basic Rules for Accessing Cell Entries
1351 @subsubsection Basic Rules for Accessing Cell Entries
1352
1353 For each cell type it is generally up to the implementation of that type
1354 which of the corresponding cell entries hold Scheme objects and which
1355 hold raw C values. However, there is one basic rule that has to be
1356 followed: Scheme pairs consist of exactly two cell entries, which both
1357 contain Scheme objects. Further, a cell which contains a Scheme object
1358 in it first entry has to be a Scheme pair. In other words, it is not
1359 allowed to store a Scheme object in the first cell entry and a non
1360 Scheme object in the second cell entry.
1361
1362 @c Fixme:shouldn't this rather be SCM_PAIRP / SCM_PAIR_P ?
1363 @deftypefn Macro int SCM_CONSP (SCM @var{x})
1364 Determine, whether the Scheme object @var{x} is a Scheme pair,
1365 i.e. whether @var{x} references a heap cell consisting of exactly two
1366 entries, where both entries contain a Scheme object. In this case, both
1367 entries will have to be accessed using the @code{SCM_CELL_OBJECT}
1368 macros. On the contrary, if the @code{SCM_CONSP} predicate is not
1369 fulfilled, the first entry of the Scheme cell is guaranteed not to be a
1370 Scheme value and thus the first cell entry must be accessed using the
1371 @code{SCM_CELL_WORD_0} macro.
1372 @end deftypefn
1373
1374
1375 @node Defining New Types (Smobs)
1376 @section Defining New Types (Smobs)
1377
1378 @dfn{Smobs} are Guile's mechanism for adding new non-immediate types to
1379 the system.@footnote{The term ``smob'' was coined by Aubrey Jaffer, who
1380 says it comes from ``small object'', referring to the fact that only the
1381 @sc{cdr} and part of the @sc{car} of a smob's cell are available for
1382 use.} To define a new smob type, the programmer provides Guile with
1383 some essential information about the type --- how to print it, how to
1384 garbage collect it, and so on --- and Guile returns a fresh type tag for
1385 use in the first word of new cells. The programmer can then use
1386 @code{scm_c_define_gsubr} to make a set of C functions that create and
1387 operate on these objects visible to Scheme code.
1388
1389 (You can find a complete version of the example code used in this
1390 section in the Guile distribution, in @file{doc/example-smob}. That
1391 directory includes a makefile and a suitable @code{main} function, so
1392 you can build a complete interactive Guile shell, extended with the
1393 datatypes described here.)
1394
1395 @menu
1396 * Describing a New Type::
1397 * Creating Instances::
1398 * Type checking::
1399 * Garbage Collecting Smobs::
1400 * A Common Mistake In Allocating Smobs::
1401 * Garbage Collecting Simple Smobs::
1402 * Remembering During Operations::
1403 * A Complete Example::
1404 @end menu
1405
1406 @node Describing a New Type
1407 @subsection Describing a New Type
1408
1409 To define a new type, the programmer must write four functions to
1410 manage instances of the type:
1411
1412 @table @code
1413 @item mark
1414 Guile will apply this function to each instance of the new type it
1415 encounters during garbage collection. This function is responsible for
1416 telling the collector about any other non-immediate objects the object
1417 refers to. The default smob mark function is to not mark any data.
1418 @xref{Garbage Collecting Smobs}, for more details.
1419
1420 @item free
1421 Guile will apply this function to each instance of the new type it could
1422 not find any live pointers to. The function should release all
1423 resources held by the object and return the number of bytes released.
1424 This is analogous to the Java finalization method-- it is invoked at
1425 an unspecified time (when garbage collection occurs) after the object
1426 is dead. The default free function frees the smob data (if the size
1427 of the struct passed to @code{scm_make_smob_type} is non-zero) using
1428 @code{scm_gc_free}. @xref{Garbage Collecting Smobs}, for more
1429 details.
1430
1431 @item print
1432 @c GJB:FIXME:: @var{exp} and @var{port} need to refer to a prototype of
1433 @c the print function.... where is that, or where should it go?
1434 Guile will apply this function to each instance of the new type to print
1435 the value, as for @code{display} or @code{write}. The function should
1436 write a printed representation of @var{exp} on @var{port}, in accordance
1437 with the parameters in @var{pstate}. (For more information on print
1438 states, see @ref{Port Data}.) The default print function prints
1439 @code{#<NAME ADDRESS>} where @code{NAME} is the first argument passed to
1440 @code{scm_make_smob_type}.
1441
1442 @item equalp
1443 If Scheme code asks the @code{equal?} function to compare two instances
1444 of the same smob type, Guile calls this function. It should return
1445 @code{SCM_BOOL_T} if @var{a} and @var{b} should be considered
1446 @code{equal?}, or @code{SCM_BOOL_F} otherwise. If @code{equalp} is
1447 @code{NULL}, @code{equal?} will assume that two instances of this type are
1448 never @code{equal?} unless they are @code{eq?}.
1449
1450 @end table
1451
1452 To actually register the new smob type, call @code{scm_make_smob_type}:
1453
1454 @deftypefun scm_t_bits scm_make_smob_type (const char *name, size_t size)
1455 This function implements the standard way of adding a new smob type,
1456 named @var{name}, with instance size @var{size}, to the system. The
1457 return value is a tag that is used in creating instances of the type.
1458 If @var{size} is 0, then no memory will be allocated when instances of
1459 the smob are created, and nothing will be freed by the default free
1460 function. Default values are provided for mark, free, print, and,
1461 equalp, as described above. If you want to customize any of these
1462 functions, the call to @code{scm_make_smob_type} should be immediately
1463 followed by calls to one or several of @code{scm_set_smob_mark},
1464 @code{scm_set_smob_free}, @code{scm_set_smob_print}, and/or
1465 @code{scm_set_smob_equalp}.
1466 @end deftypefun
1467
1468 Each of the below @code{scm_set_smob_XXX} functions registers a smob
1469 special function for a given type. Each function is intended to be used
1470 only zero or one time per type, and the call should be placed
1471 immediately following the call to @code{scm_make_smob_type}.
1472
1473 @deftypefun void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
1474 This function sets the smob marking procedure for the smob type specified by
1475 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1476 @end deftypefun
1477
1478 @deftypefun void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
1479 This function sets the smob freeing procedure for the smob type specified by
1480 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1481 @end deftypefun
1482
1483 @deftypefun void scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
1484 This function sets the smob printing procedure for the smob type specified by
1485 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1486 @end deftypefun
1487
1488 @deftypefun void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
1489 This function sets the smob equality-testing predicate for the smob type specified by
1490 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1491 @end deftypefun
1492
1493 In versions 1.4 and earlier, there was another way of creating smob
1494 types, using @code{scm_make_smob_type_mfpe}. This function is now
1495 deprecated and will be removed in a future version of Guile. You should
1496 use the mechanism described above for new code, and change old code not
1497 to use deprecated features.
1498
1499 @deftypefun long scm_make_smob_type_mfpe (const char *name, size_t size, SCM (*mark) (SCM), size_t (*free) (SCM), int (*print) (SCM, SCM, scm_print_state*), SCM (*equalp) (SCM, SCM))
1500 This function invokes @code{scm_make_smob_type} on its first two arguments
1501 to add a new smob type named @var{name}, with instance size @var{size} to the system.
1502 It also registers the @var{mark}, @var{free}, @var{print}, @var{equalp} smob
1503 special functions for that new type. Any of these parameters can be @code{NULL}
1504 to have that special function use the default behavior for guile.
1505 The return value is a tag that is used in creating instances of the type. If @var{size}
1506 is 0, then no memory will be allocated when instances of the smob are created, and
1507 nothing will be freed by the default free function.
1508
1509 @emph{This function is deprecated}
1510 @end deftypefun
1511
1512 For example, here is how one might declare and register a new type
1513 representing eight-bit gray-scale images:
1514
1515 @example
1516 #include <libguile.h>
1517
1518 static scm_t_bits image_tag;
1519
1520 void
1521 init_image_type (void)
1522 @{
1523 image_tag = scm_make_smob_type ("image", sizeof (struct image));
1524 scm_set_smob_mark (image_tag, mark_image);
1525 scm_set_smob_free (image_tag, free_image);
1526 scm_set_smob_print (image_tag, print_image);
1527 @}
1528 @end example
1529
1530
1531 @node Creating Instances
1532 @subsection Creating Instances
1533
1534 Like other non-immediate types, smobs start with a cell whose first word
1535 contains typing information, and whose remaining words are free for any
1536 use.
1537
1538 After the header word containing the type code, smobs can have either
1539 one, two or three additional words of data. These words store either a
1540 pointer to the internal C structure holding the smob-specific data, or
1541 the smob data itself. To create an instance of a smob type following
1542 these standards, you should use @code{SCM_NEWSMOB}, @code{SCM_NEWSMOB2}
1543 or @code{SCM_NEWSMOB3}:@footnote{The @code{SCM_NEWSMOB2} and
1544 @code{SCM_NEWSMOB3} variants will allocate double cells and thus use
1545 twice as much memory as smobs created by @code{SCM_NEWSMOB}.}
1546
1547 @deftypefn Macro void SCM_NEWSMOB (SCM value, scm_t_bits tag, void *data)
1548 @deftypefnx Macro void SCM_NEWSMOB2 (SCM value, scm_t_bits tag, void *data1, void *data2)
1549 @deftypefnx Macro void SCM_NEWSMOB3 (SCM value, scm_t_bits tag, void *data1, void *data2, void *data3)
1550 Make @var{value} contain a smob instance of the type with tag @var{tag}
1551 and smob data @var{data} (or @var{data1}, @var{data2}, and @var{data3}).
1552 @var{value} must be previously declared as C type @code{SCM}.
1553 @end deftypefn
1554
1555 Since it is often the case (e.g., in smob constructors) that you will
1556 create a smob instance and return it, there is also a slightly specialized
1557 macro for this situation:
1558
1559 @deftypefn Macro fn_returns SCM_RETURN_NEWSMOB (scm_t_bits tag, void *data)
1560 @deftypefnx Macro fn_returns SCM_RETURN_NEWSMOB2 (scm_t_bits tag, void *data1, void *data2)
1561 @deftypefnx Macro fn_returns SCM_RETURN_NEWSMOB3 (scm_t_bits tag, void *data1, void *data2, void *data3)
1562 This macro expands to a block of code that creates a smob instance of
1563 the type with tag @var{tag} and smob data @var{data} (or @var{data1},
1564 @var{data2}, and @var{data3}), and causes the surrounding function to
1565 return that @code{SCM} value. It should be the last piece of code in
1566 a block.
1567 @end deftypefn
1568
1569 Guile provides some functions for managing memory, which are often
1570 helpful when implementing smobs. @xref{Memory Blocks}.
1571
1572
1573 Continuing the above example, if the global variable @code{image_tag}
1574 contains a tag returned by @code{scm_make_smob_type}, here is how we
1575 could construct a smob whose @sc{cdr} contains a pointer to a freshly
1576 allocated @code{struct image}:
1577
1578 @example
1579 struct image @{
1580 int width, height;
1581 char *pixels;
1582
1583 /* The name of this image */
1584 SCM name;
1585
1586 /* A function to call when this image is
1587 modified, e.g., to update the screen,
1588 or SCM_BOOL_F if no action necessary */
1589 SCM update_func;
1590 @};
1591
1592 SCM
1593 make_image (SCM name, SCM s_width, SCM s_height)
1594 @{
1595 struct image *image;
1596 int width, height;
1597
1598 SCM_ASSERT (SCM_STRINGP (name), name, SCM_ARG1, "make-image");
1599 SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
1600 SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
1601
1602 width = SCM_INUM (s_width);
1603 height = SCM_INUM (s_height);
1604
1605 image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
1606 image->width = width;
1607 image->height = height;
1608 image->pixels = scm_gc_malloc (width * height, "image pixels");
1609 image->name = name;
1610 image->update_func = SCM_BOOL_F;
1611
1612 SCM_RETURN_NEWSMOB (image_tag, image);
1613 @}
1614 @end example
1615
1616
1617 @node Type checking
1618 @subsection Type checking
1619
1620 Functions that operate on smobs should aggressively check the types of
1621 their arguments, to avoid misinterpreting some other datatype as a smob,
1622 and perhaps causing a segmentation fault. Fortunately, this is pretty
1623 simple to do. The function need only verify that its argument is a
1624 non-immediate, whose first word is the type tag returned by
1625 @code{scm_make_smob_type}.
1626
1627 For example, here is a simple function that operates on an image smob,
1628 and checks the type of its argument. We also present an expanded
1629 version of the @code{init_image_type} function, to make
1630 @code{clear_image} and the image constructor function @code{make_image}
1631 visible to Scheme code.
1632
1633 @example
1634 SCM
1635 clear_image (SCM image_smob)
1636 @{
1637 int area;
1638 struct image *image;
1639
1640 SCM_ASSERT (SCM_SMOB_PREDICATE (image_tag, image_smob),
1641 image_smob, SCM_ARG1, "clear-image");
1642
1643 image = (struct image *) SCM_SMOB_DATA (image_smob);
1644 area = image->width * image->height;
1645 memset (image->pixels, 0, area);
1646
1647 /* Invoke the image's update function. */
1648 if (image->update_func != SCM_BOOL_F)
1649 scm_apply (image->update_func, SCM_EOL, SCM_EOL);
1650
1651 return SCM_UNSPECIFIED;
1652 @}
1653
1654
1655 void
1656 init_image_type (void)
1657 @{
1658 image_tag = scm_make_smob_type ("image", sizeof (struct image));
1659 scm_set_smob_mark (image_tag, mark_image);
1660 scm_set_smob_free (image_tag, free_image);
1661 scm_set_smob_print (image_tag, print_image);
1662
1663 scm_c_define_gsubr ("clear-image", 1, 0, 0, clear_image);
1664 scm_c_define_gsubr ("make-image", 3, 0, 0, make_image);
1665 @}
1666 @end example
1667
1668 @c GJB:FIXME:: should talk about guile-snarf somewhere!
1669
1670
1671 @node Garbage Collecting Smobs
1672 @subsection Garbage Collecting Smobs
1673
1674 Once a smob has been released to the tender mercies of the Scheme
1675 system, it must be prepared to survive garbage collection. Guile calls
1676 the @code{mark} and @code{free} functions of the @code{scm_smobfuns}
1677 structure to manage this.
1678
1679 As described before (@pxref{Conservative GC}), every object in the
1680 Scheme system has a @dfn{mark bit}, which the garbage collector uses to
1681 tell live objects from dead ones. When collection starts, every
1682 object's mark bit is clear. The collector traces pointers through the
1683 heap, starting from objects known to be live, and sets the mark bit on
1684 each object it encounters. When it can find no more unmarked objects,
1685 the collector walks all objects, live and dead, frees those whose mark
1686 bits are still clear, and clears the mark bit on the others.
1687
1688 The two main portions of the collection are called the @dfn{mark phase},
1689 during which the collector marks live objects, and the @dfn{sweep
1690 phase}, during which the collector frees all unmarked objects.
1691
1692 The mark bit of a smob lives in a special memory region. When the
1693 collector encounters a smob, it sets the smob's mark bit, and uses the
1694 smob's type tag to find the appropriate @code{mark} function for that
1695 smob: the one listed in that smob's @code{scm_smobfuns} structure. It
1696 then calls the @code{mark} function, passing it the smob as its only
1697 argument.
1698
1699 The @code{mark} function is responsible for marking any other Scheme
1700 objects the smob refers to. If it does not do so, the objects' mark
1701 bits will still be clear when the collector begins to sweep, and the
1702 collector will free them. If this occurs, it will probably break, or at
1703 least confuse, any code operating on the smob; the smob's @code{SCM}
1704 values will have become dangling references.
1705
1706 To mark an arbitrary Scheme object, the @code{mark} function may call
1707 this function:
1708
1709 @deftypefun void scm_gc_mark (SCM @var{x})
1710 Mark the object @var{x}, and recurse on any objects @var{x} refers to.
1711 If @var{x}'s mark bit is already set, return immediately.
1712 @end deftypefun
1713
1714 Thus, here is how we might write the @code{mark} function for the image
1715 smob type discussed above:
1716
1717 @example
1718 @group
1719 SCM
1720 mark_image (SCM image_smob)
1721 @{
1722 /* Mark the image's name and update function. */
1723 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
1724
1725 scm_gc_mark (image->name);
1726 scm_gc_mark (image->update_func);
1727
1728 return SCM_BOOL_F;
1729 @}
1730 @end group
1731 @end example
1732
1733 Note that, even though the image's @code{update_func} could be an
1734 arbitrarily complex structure (representing a procedure and any values
1735 enclosed in its environment), @code{scm_gc_mark} will recurse as
1736 necessary to mark all its components. Because @code{scm_gc_mark} sets
1737 an object's mark bit before it recurses, it is not confused by
1738 circular structures.
1739
1740 As an optimization, the collector will mark whatever value is returned
1741 by the @code{mark} function; this helps limit depth of recursion during
1742 the mark phase. Thus, the code above could also be written as:
1743 @example
1744 @group
1745 SCM
1746 mark_image (SCM image_smob)
1747 @{
1748 /* Mark the image's name and update function. */
1749 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
1750
1751 scm_gc_mark (image->name);
1752 return image->update_func;
1753 @}
1754 @end group
1755 @end example
1756
1757
1758 Finally, when the collector encounters an unmarked smob during the sweep
1759 phase, it uses the smob's tag to find the appropriate @code{free}
1760 function for the smob. It then calls the function, passing it the smob
1761 as its only argument.
1762
1763 The @code{free} function must release any resources used by the smob.
1764 However, it need not free objects managed by the collector; the
1765 collector will take care of them. For historical reasons, the return
1766 type of the @code{free} function should be @code{size_t}, an unsigned
1767 integral type; the @code{free} function should always return zero.
1768
1769 Here is how we might write the @code{free} function for the image smob
1770 type:
1771 @example
1772 size_t
1773 free_image (SCM image_smob)
1774 @{
1775 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
1776
1777 scm_gc_free (image->pixels, image->width * image->height, "image pixels");
1778 scm_gc_free (image, sizeof (struct image), "image");
1779
1780 return 0;
1781 @}
1782 @end example
1783
1784 During the sweep phase, the garbage collector will clear the mark bits
1785 on all live objects. The code which implements a smob need not do this
1786 itself.
1787
1788 There is no way for smob code to be notified when collection is
1789 complete.
1790
1791 It is usually a good idea to minimize the amount of processing done
1792 during garbage collection; keep @code{mark} and @code{free} functions
1793 very simple. Since collections occur at unpredictable times, it is easy
1794 for any unusual activity to interfere with normal code.
1795
1796
1797 @node A Common Mistake In Allocating Smobs, Garbage Collecting Simple Smobs, Garbage Collecting Smobs, Defining New Types (Smobs)
1798 @subsection A Common Mistake In Allocating Smobs
1799
1800 When constructing new objects, you must be careful that the garbage
1801 collector can always find any new objects you allocate. For example,
1802 suppose we wrote the @code{make_image} function this way:
1803
1804 @example
1805 SCM
1806 make_image (SCM name, SCM s_width, SCM s_height)
1807 @{
1808 struct image *image;
1809 SCM image_smob;
1810 int width, height;
1811
1812 SCM_ASSERT (SCM_STRINGP (name), name, SCM_ARG1, "make-image");
1813 SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
1814 SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
1815
1816 width = SCM_INUM (s_width);
1817 height = SCM_INUM (s_height);
1818
1819 image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
1820 image->width = width;
1821 image->height = height;
1822 image->pixels = scm_gc_malloc (width * height, "image pixels");
1823
1824 /* THESE TWO LINES HAVE CHANGED: */
1825 image->name = scm_string_copy (name);
1826 image->update_func = scm_c_define_gsubr (@dots{});
1827
1828 SCM_NEWCELL (image_smob);
1829 SCM_SET_CELL_WORD_1 (image_smob, image);
1830 SCM_SET_CELL_TYPE (image_smob, image_tag);
1831
1832 return image_smob;
1833 @}
1834 @end example
1835
1836 This code is incorrect. The calls to @code{scm_string_copy} and
1837 @code{scm_c_define_gsubr} allocate fresh objects. Allocating any new object
1838 may cause the garbage collector to run. If @code{scm_c_define_gsubr}
1839 invokes a collection, the garbage collector has no way to discover that
1840 @code{image->name} points to the new string object; the @code{image}
1841 structure is not yet part of any Scheme object, so the garbage collector
1842 will not traverse it. Since the garbage collector cannot find any
1843 references to the new string object, it will free it, leaving
1844 @code{image} pointing to a dead object.
1845
1846 A correct implementation might say, instead:
1847
1848 @example
1849 image->name = SCM_BOOL_F;
1850 image->update_func = SCM_BOOL_F;
1851
1852 SCM_NEWCELL (image_smob);
1853 SCM_SET_CELL_WORD_1 (image_smob, image);
1854 SCM_SET_CELL_TYPE (image_smob, image_tag);
1855
1856 image->name = scm_string_copy (name);
1857 image->update_func = scm_c_define_gsubr (@dots{});
1858
1859 return image_smob;
1860 @end example
1861
1862 Now, by the time we allocate the new string and function objects,
1863 @code{image_smob} points to @code{image}. If the garbage collector
1864 scans the stack, it will find a reference to @code{image_smob} and
1865 traverse @code{image}, so any objects @code{image} points to will be
1866 preserved.
1867
1868
1869 @node Garbage Collecting Simple Smobs
1870 @subsection Garbage Collecting Simple Smobs
1871
1872 It is often useful to define very simple smob types --- smobs which have
1873 no data to mark, other than the cell itself, or smobs whose first data
1874 word is simply an ordinary Scheme object, to be marked recursively.
1875 Guile provides some functions to handle these common cases; you can use
1876 this function as your smob type's @code{mark} function, if your smob's
1877 structure is simple enough.
1878
1879 If the smob refers to no other Scheme objects, then no action is
1880 necessary; the garbage collector has already marked the smob cell
1881 itself. In that case, you can use zero as your mark function.
1882
1883 @deftypefun SCM scm_markcdr (SCM @var{x})
1884 Mark the references in the smob @var{x}, assuming that @var{x}'s first
1885 data word contains an ordinary Scheme object, and @var{x} refers to no
1886 other objects. This function simply returns @var{x}'s first data word.
1887
1888 This is only useful for simple smobs created by @code{SCM_NEWSMOB} or
1889 @code{SCM_RETURN_NEWSMOB}, not for smobs allocated as double cells.
1890 @end deftypefun
1891
1892 @deftypefun size_t scm_free0 (SCM @var{x})
1893 Do nothing; return zero. This function is appropriate for smobs that
1894 use either zero or @code{scm_markcdr} as their marking functions, and
1895 refer to no heap storage, including memory managed by @code{malloc},
1896 other than the smob's header cell.
1897
1898 This function should not be needed anymore, because simply passing
1899 @code{NULL} as the free function does the same.
1900 @end deftypefun
1901
1902
1903 @node Remembering During Operations
1904 @subsection Remembering During Operations
1905 @cindex Remembering
1906
1907 It's important that a smob is visible to the garbage collector
1908 whenever its contents are being accessed. Otherwise it could be freed
1909 while code is still using it.
1910
1911 @c NOTE: The varargs scm_remember_upto_here is deliberately not
1912 @c documented, because we don't think it can be implemented as a nice
1913 @c inline compiler directive or asm block. New _3, _4 or whatever
1914 @c forms could certainly be added though, if needed.
1915
1916 @deftypefn {C Macro} void scm_remember_upto_here_1 (SCM obj)
1917 @deftypefnx {C Macro} void scm_remember_upto_here_2 (SCM obj1, SCM obj2)
1918 Create a reference to the given object or objects, so they're certain
1919 to be present on the stack or in a register and hence will not be
1920 freed by the garbage collector before this point.
1921 @end deftypefn
1922
1923 For example, consider a procedure to convert image data to a list of
1924 pixel values.
1925
1926 @example
1927 SCM
1928 image_to_list (SCM image_smob)
1929 @{
1930 struct image *image;
1931 SCM lst;
1932 int i;
1933 SCM_ASSERT (SCM_SMOB_PREDICATE (image_tag, image_smob),
1934 image_smob, SCM_ARG1, "image->list");
1935
1936 image = (struct image *) SCM_SMOB_DATA (image_smob);
1937 lst = SCM_EOL;
1938 for (i = image->width * image->height - 1; i >= 0; i--)
1939 lst = scm_cons (SCM_MAKINUM (image->pixels[i]), lst);
1940
1941 scm_remember_upto_here_1 (image_smob);
1942 return lst;
1943 @}
1944 @end example
1945
1946 In the loop, only the @code{image} pointer is used and the C compiler
1947 has no reason to keep the @code{image_smob} value anywhere. If
1948 @code{scm_cons} results in a garbage collect, @code{image_smob} might
1949 not be on the stack or anywhere else and could be freed, leaving the
1950 loop accessing freed data. The use of @code{scm_remember_upto_here_1}
1951 prevents this, by creating a reference to @code{image_smob} after all
1952 data accesses.
1953
1954 There's no need to do the same for @code{lst}, since that's the return
1955 value and the compiler will certainly keep it in a register or
1956 somewhere throughout the routine.
1957
1958 The @code{clear_image} example previously shown (@pxref{Type
1959 checking}) didn't use @code{scm_remember_upto_here_1}. This is
1960 because it didn't do anything that could result in a garbage collect.
1961
1962 It's only in quite rare circumstances that a missing
1963 @code{scm_remember_upto_here_1} will bite, but when it happens the
1964 consequences are serious. Fortunately the rule is simple: whenever
1965 calling a Guile library function or doing something that might, ensure
1966 the @code{SCM} of a smob is referenced past all accesses to its
1967 insides. Do this by adding an @code{scm_remember_upto_here_1} if
1968 there are no other references.
1969
1970 In a multi-threaded program, the rule is the same. As far as a given
1971 thread is concerned, a garbage collect still only occurs within a
1972 Guile library function, not at an arbitrary time. (Guile waits for
1973 all threads to reach one of its library functions, and holds them
1974 there while the collector runs.)
1975
1976
1977 @node A Complete Example
1978 @subsection A Complete Example
1979
1980 Here is the complete text of the implementation of the image datatype,
1981 as presented in the sections above. We also provide a definition for
1982 the smob's @code{print} function, and make some objects and functions
1983 static, to clarify exactly what the surrounding code is using.
1984
1985 As mentioned above, you can find this code in the Guile distribution, in
1986 @file{doc/example-smob}. That directory includes a makefile and a
1987 suitable @code{main} function, so you can build a complete interactive
1988 Guile shell, extended with the datatypes described here.)
1989
1990 @example
1991 /* file "image-type.c" */
1992
1993 #include <stdlib.h>
1994 #include <libguile.h>
1995
1996 static scm_t_bits image_tag;
1997
1998 struct image @{
1999 int width, height;
2000 char *pixels;
2001
2002 /* The name of this image */
2003 SCM name;
2004
2005 /* A function to call when this image is
2006 modified, e.g., to update the screen,
2007 or SCM_BOOL_F if no action necessary */
2008 SCM update_func;
2009 @};
2010
2011 static SCM
2012 make_image (SCM name, SCM s_width, SCM s_height)
2013 @{
2014 struct image *image;
2015 int width, height;
2016
2017 SCM_ASSERT (SCM_STRINGP (name), name, SCM_ARG1, "make-image");
2018 SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
2019 SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
2020
2021 width = SCM_INUM (s_width);
2022 height = SCM_INUM (s_height);
2023
2024 image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
2025 image->width = width;
2026 image->height = height;
2027 image->pixels = scm_gc_malloc (width * height, "image pixels");
2028 image->name = name;
2029 image->update_func = SCM_BOOL_F;
2030
2031 SCM_RETURN_NEWSMOB (image_tag, image);
2032 @}
2033
2034 static SCM
2035 clear_image (SCM image_smob)
2036 @{
2037 int area;
2038 struct image *image;
2039
2040 SCM_ASSERT (SCM_SMOB_PREDICATE (image_tag, image_smob),
2041 image_smob, SCM_ARG1, "clear-image");
2042
2043 image = (struct image *) SCM_SMOB_DATA (image_smob);
2044 area = image->width * image->height;
2045 memset (image->pixels, 0, area);
2046
2047 /* Invoke the image's update function. */
2048 if (image->update_func != SCM_BOOL_F)
2049 scm_apply (image->update_func, SCM_EOL, SCM_EOL);
2050
2051 return SCM_UNSPECIFIED;
2052 @}
2053
2054 static SCM
2055 mark_image (SCM image_smob)
2056 @{
2057 /* Mark the image's name and update function. */
2058 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
2059
2060 scm_gc_mark (image->name);
2061 return image->update_func;
2062 @}
2063
2064 static size_t
2065 free_image (SCM image_smob)
2066 @{
2067 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
2068
2069 scm_gc_free (image->pixels, image->width * image->height, "image pixels");
2070 scm_gc_free (image, sizeof (struct image), "image");
2071
2072 return 0;
2073 @}
2074
2075 static int
2076 print_image (SCM image_smob, SCM port, scm_print_state *pstate)
2077 @{
2078 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
2079
2080 scm_puts ("#<image ", port);
2081 scm_display (image->name, port);
2082 scm_puts (">", port);
2083
2084 /* non-zero means success */
2085 return 1;
2086 @}
2087
2088 void
2089 init_image_type (void)
2090 @{
2091 image_tag = scm_make_smob_type ("image", sizeof (struct image));
2092 scm_set_smob_mark (image_tag, mark_image);
2093 scm_set_smob_free (image_tag, free_image);
2094 scm_set_smob_print (image_tag, print_image);
2095
2096 scm_c_define_gsubr ("clear-image", 1, 0, 0, clear_image);
2097 scm_c_define_gsubr ("make-image", 3, 0, 0, make_image);
2098 @}
2099 @end example
2100
2101 Here is a sample build and interaction with the code from the
2102 @file{example-smob} directory, on the author's machine:
2103
2104 @example
2105 zwingli:example-smob$ make CC=gcc
2106 gcc `guile-config compile` -c image-type.c -o image-type.o
2107 gcc `guile-config compile` -c myguile.c -o myguile.o
2108 gcc image-type.o myguile.o `guile-config link` -o myguile
2109 zwingli:example-smob$ ./myguile
2110 guile> make-image
2111 #<primitive-procedure make-image>
2112 guile> (define i (make-image "Whistler's Mother" 100 100))
2113 guile> i
2114 #<image Whistler's Mother>
2115 guile> (clear-image i)
2116 guile> (clear-image 4)
2117 ERROR: In procedure clear-image in expression (clear-image 4):
2118 ERROR: Wrong type argument in position 1: 4
2119 ABORT: (wrong-type-arg)
2120
2121 Type "(backtrace)" to get more information.
2122 guile>
2123 @end example
2124
2125 @c essay @bye