4 @setfilename data-rep.info
5 @settitle Data Representation in Guile
10 @dircategory The Algorithmic Language Scheme
12 * data-rep: (data-rep). Data Representation in Guile --- how to use
13 Guile objects in your C code.
16 @setchapternewpage off
19 Data Representation in Guile
21 Copyright (C) 1998, 1999, 2000 Free Software Foundation
23 Permission is granted to make and distribute verbatim copies of
24 this manual provided the copyright notice and this permission notice
25 are preserved on all copies.
28 Permission is granted to process this file through TeX and print the
29 results, provided the printed document carries copying permission
30 notice identical to this one except for the removal of this paragraph
31 (this paragraph not being relevant to the printed manual).
34 Permission is granted to copy and distribute modified versions of this
35 manual under the conditions for verbatim copying, provided that the entire
36 resulting derived work is distributed under the terms of a permission
37 notice identical to this one.
39 Permission is granted to copy and distribute translations of this manual
40 into another language, under the above conditions for modified versions,
41 except that this permission notice may be stated in a translation approved
42 by the Free Software Foundation.
47 @comment The title is printed in a large font.
48 @title Data Representation in Guile
49 @subtitle $Id: data-rep.texi,v 1.12 2000-06-30 10:46:33 dirk Exp $
50 @subtitle For use with Guile @value{VERSION}
52 @author Free Software Foundation
53 @author @email{jimb@@red-bean.com}
54 @c The following two commands start the copyright page.
56 @vskip 0pt plus 1filll
57 @vskip 0pt plus 1filll
58 Copyright @copyright{} 1998 Free Software Foundation
60 Permission is granted to make and distribute verbatim copies of
61 this manual provided the copyright notice and this permission notice
62 are preserved on all copies.
64 Permission is granted to copy and distribute modified versions of this
65 manual under the conditions for verbatim copying, provided that the entire
66 resulting derived work is distributed under the terms of a permission
67 notice identical to this one.
69 Permission is granted to copy and distribute translations of this manual
70 into another language, under the above conditions for modified versions,
71 except that this permission notice may be stated in a translation approved
72 by Free Software Foundation.
80 @node Top, Data Representation in Scheme, (dir), (dir)
81 @top Data Representation in Guile
84 This essay is meant to provide the background necessary to read and
85 write C code that manipulates Scheme values in a way that conforms to
86 libguile's interface. If you would like to write or maintain a
87 Guile-based application in C or C++, this is the first information you
90 In order to make sense of Guile's @code{SCM_} functions, or read
91 libguile's source code, it's essential to have a good grasp of how Guile
92 actually represents Scheme values. Otherwise, a lot of the code, and
93 the conventions it follows, won't make very much sense.
95 We assume you know both C and Scheme, but we do not assume you are
96 familiar with Guile's C interface.
100 * Data Representation in Scheme:: Why things aren't just totally
101 straightforward, in general terms.
102 * How Guile does it:: How to write C code that manipulates
103 Guile values, with an explanation
104 of Guile's garbage collector.
105 * Defining New Types (Smobs):: How to extend Guile with your own
106 application-specific datatypes.
109 @node Data Representation in Scheme, How Guile does it, Top, Top
110 @section Data Representation in Scheme
112 Scheme is a latently-typed language; this means that the system cannot,
113 in general, determine the type of a given expression at compile time.
114 Types only become apparent at run time. Variables do not have fixed
115 types; a variable may hold a pair at one point, an integer at the next,
116 and a thousand-element vector later. Instead, values, not variables,
119 In order to implement standard Scheme functions like @code{pair?} and
120 @code{string?} and provide garbage collection, the representation of
121 every value must contain enough information to accurately determine its
122 type at run time. Often, Scheme systems also use this information to
123 determine whether a program has attempted to apply an operation to an
124 inappropriately typed value (such as taking the @code{car} of a string).
126 Because variables, pairs, and vectors may hold values of any type,
127 Scheme implementations use a uniform representation for values --- a
128 single type large enough to hold either a complete value or a pointer
129 to a complete value, along with the necessary typing information.
131 The following sections will present a simple typing system, and then
132 make some refinements to correct its major weaknesses. However, this is
133 not a description of the system Guile actually uses. It is only an
134 illustration of the issues Guile's system must address. We provide all
135 the information one needs to work with Guile's data in @ref{How Guile
140 * A Simple Representation::
146 @node A Simple Representation, Faster Integers, Data Representation in Scheme, Data Representation in Scheme
147 @subsection A Simple Representation
149 The simplest way to meet the above requirements in C would be to
150 represent each value as a pointer to a structure containing a type
151 indicator, followed by a union carrying the real value. Assuming that
152 @code{SCM} is the name of our universal type, we can write:
155 enum type @{ integer, pair, string, vector, ... @};
157 typedef struct value *SCM;
163 struct @{ SCM car, cdr; @} pair;
164 struct @{ int length; char *elts; @} string;
165 struct @{ int length; SCM *elts; @} vector;
170 with the ellipses replaced with code for the remaining Scheme types.
172 This representation is sufficient to implement all of Scheme's
173 semantics. If @var{x} is an @code{SCM} value:
176 To test if @var{x} is an integer, we can write @code{@var{x}->type == integer}.
178 To find its value, we can write @code{@var{x}->value.integer}.
180 To test if @var{x} is a vector, we can write @code{@var{x}->type == vector}.
182 If we know @var{x} is a vector, we can write
183 @code{@var{x}->value.vector.elts[0]} to refer to its first element.
185 If we know @var{x} is a pair, we can write
186 @code{@var{x}->value.pair.car} to extract its car.
190 @node Faster Integers, Cheaper Pairs, A Simple Representation, Data Representation in Scheme
191 @subsection Faster Integers
193 Unfortunately, the above representation has a serious disadvantage. In
194 order to return an integer, an expression must allocate a @code{struct
195 value}, initialize it to represent that integer, and return a pointer to
196 it. Furthermore, fetching an integer's value requires a memory
197 reference, which is much slower than a register reference on most
198 processors. Since integers are extremely common, this representation is
199 too costly, in both time and space. Integers should be very cheap to
200 create and manipulate.
202 One possible solution comes from the observation that, on many
203 architectures, structures must be aligned on a four-byte boundary.
204 (Whether or not the machine actually requires it, we can write our own
205 allocator for @code{struct value} objects that assures this is true.)
206 In this case, the lower two bits of the structure's address are known to
209 This gives us the room we need to provide an improved representation
210 for integers. We make the following rules:
213 If the lower two bits of an @code{SCM} value are zero, then the SCM
214 value is a pointer to a @code{struct value}, and everything proceeds as
217 Otherwise, the @code{SCM} value represents an integer, whose value
218 appears in its upper bits.
221 Here is C code implementing this convention:
223 enum type @{ pair, string, vector, ... @};
225 typedef struct value *SCM;
230 struct @{ SCM car, cdr; @} pair;
231 struct @{ int length; char *elts; @} string;
232 struct @{ int length; SCM *elts; @} vector;
237 #define POINTER_P(x) (((int) (x) & 3) == 0)
238 #define INTEGER_P(x) (! POINTER_P (x))
240 #define GET_INTEGER(x) ((int) (x) >> 2)
241 #define MAKE_INTEGER(x) ((SCM) (((x) << 2) | 1))
244 Notice that @code{integer} no longer appears as an element of @code{enum
245 type}, and the union has lost its @code{integer} member. Instead, we
246 use the @code{POINTER_P} and @code{INTEGER_P} macros to make a coarse
247 classification of values into integers and non-integers, and do further
248 type testing as before.
250 Here's how we would answer the questions posed above (again, assume
251 @var{x} is an @code{SCM} value):
254 To test if @var{x} is an integer, we can write @code{INTEGER_P (@var{x})}.
256 To find its value, we can write @code{GET_INTEGER (@var{x})}.
258 To test if @var{x} is a vector, we can write:
260 @code{POINTER_P (@var{x}) && @var{x}->type == vector}
262 Given the new representation, we must make sure @var{x} is truly a
263 pointer before we dereference it to determine its complete type.
265 If we know @var{x} is a vector, we can write
266 @code{@var{x}->value.vector.elts[0]} to refer to its first element, as
269 If we know @var{x} is a pair, we can write
270 @code{@var{x}->value.pair.car} to extract its car, just as before.
273 This representation allows us to operate more efficiently on integers
274 than the first. For example, if @var{x} and @var{y} are known to be
275 integers, we can compute their sum as follows:
277 MAKE_INTEGER (GET_INTEGER (@var{x}) + GET_INTEGER (@var{y}))
279 Now, integer math requires no allocation or memory references. Most
280 real Scheme systems actually use an even more efficient representation,
281 but this essay isn't about bit-twiddling. (Hint: what if pointers had
282 @code{01} in their least significant bits, and integers had @code{00}?)
285 @node Cheaper Pairs, Guile Is Hairier, Faster Integers, Data Representation in Scheme
286 @subsection Cheaper Pairs
288 However, there is yet another issue to confront. Most Scheme heaps
289 contain more pairs than any other type of object; Jonathan Rees says
290 that pairs occupy 45% of the heap in his Scheme implementation, Scheme
291 48. However, our representation above spends three @code{SCM}-sized
292 words per pair --- one for the type, and two for the @sc{car} and
293 @sc{cdr}. Is there any way to represent pairs using only two words?
295 Let us refine the convention we established earlier. Let us assert
299 If the bottom two bits of an @code{SCM} value are @code{#b00}, then
300 it is a pointer, as before.
302 If the bottom two bits are @code{#b01}, then the upper bits are an
303 integer. This is a bit more restrictive than before.
305 If the bottom two bits are @code{#b10}, then the value, with the bottom
306 two bits masked out, is the address of a pair.
309 Here is the new C code:
311 enum type @{ string, vector, ... @};
313 typedef struct value *SCM;
318 struct @{ int length; char *elts; @} string;
319 struct @{ int length; SCM *elts; @} vector;
328 #define POINTER_P(x) (((int) (x) & 3) == 0)
330 #define INTEGER_P(x) (((int) (x) & 3) == 1)
331 #define GET_INTEGER(x) ((int) (x) >> 2)
332 #define MAKE_INTEGER(x) ((SCM) (((x) << 2) | 1))
334 #define PAIR_P(x) (((int) (x) & 3) == 2)
335 #define GET_PAIR(x) ((struct pair *) ((int) (x) & ~3))
338 Notice that @code{enum type} and @code{struct value} now only contain
339 provisions for vectors and strings; both integers and pairs have become
340 special cases. The code above also assumes that an @code{int} is large
341 enough to hold a pointer, which isn't generally true.
344 Our list of examples is now as follows:
347 To test if @var{x} is an integer, we can write @code{INTEGER_P
348 (@var{x})}; this is as before.
350 To find its value, we can write @code{GET_INTEGER (@var{x})}, as
353 To test if @var{x} is a vector, we can write:
355 @code{POINTER_P (@var{x}) && @var{x}->type == vector}
357 We must still make sure that @var{x} is a pointer to a @code{struct
358 value} before dereferencing it to find its type.
360 If we know @var{x} is a vector, we can write
361 @code{@var{x}->value.vector.elts[0]} to refer to its first element, as
364 We can write @code{PAIR_P (@var{x})} to determine if @var{x} is a
365 pair, and then write @code{GET_PAIR (@var{x})->car} to refer to its
369 This change in representation reduces our heap size by 15%. It also
370 makes it cheaper to decide if a value is a pair, because no memory
371 references are necessary; it suffices to check the bottom two bits of
372 the @code{SCM} value. This may be significant when traversing lists, a
373 common activity in a Scheme system.
375 Again, most real Scheme systems use a slighty different implementation;
376 for example, if GET_PAIR subtracts off the low bits of @code{x}, instead
377 of masking them off, the optimizer will often be able to combine that
378 subtraction with the addition of the offset of the structure member we
379 are referencing, making a modified pointer as fast to use as an
383 @node Guile Is Hairier, , Cheaper Pairs, Data Representation in Scheme
384 @subsection Guile Is Hairier
386 We originally started with a very simple typing system --- each object
387 has a field that indicates its type. Then, for the sake of efficiency
388 in both time and space, we moved some of the typing information directly
389 into the @code{SCM} value, and left the rest in the @code{struct value}.
390 Guile itself employs a more complex hierarchy, storing finer and finer
391 gradations of type information in different places, depending on the
392 object's coarser type.
394 In the author's opinion, Guile could be simplified greatly without
395 significant loss of efficiency, but the simplified system would still be
396 more complex than what we've presented above.
399 @node How Guile does it, Defining New Types (Smobs), Data Representation in Scheme, Top
400 @section How Guile does it
402 Here we present the specifics of how Guile represents its data. We
403 don't go into complete detail; an exhaustive description of Guile's
404 system would be boring, and we do not wish to encourage people to write
405 code which depends on its details anyway. We do, however, present
406 everything one need know to use Guile's data.
411 * Garbage Collection::
412 * Immediates vs. Non-immediates::
413 * Immediate Datatypes::
414 * Non-immediate Datatypes::
415 * Signalling Type Errors::
418 @node General Rules, Garbage Collection, How Guile does it, How Guile does it
419 @subsection General Rules
421 Any code which operates on Guile datatypes must @code{#include} the
422 header file @code{<libguile.h>}. This file contains a definition for
423 the @code{SCM} typedef (Guile's universal type, as in the examples
424 above), and definitions and declarations for a host of macros and
425 functions that operate on @code{SCM} values.
427 All identifiers declared by @code{<libguile.h>} begin with @code{scm_}
430 @c [[I wish this were true, but I don't think it is at the moment. -JimB]]
431 @c Macros do not evaluate their arguments more than once, unless documented
434 The functions described here generally check the types of their
435 @code{SCM} arguments, and signal an error if their arguments are of an
436 inappropriate type. Macros generally do not, unless that is their
437 specified purpose. You must verify their argument types beforehand, as
440 Macros and functions that return a boolean value have names ending in
441 @code{P} or @code{_p} (for ``predicate''). Those that return a negated
442 boolean value have names starting with @code{SCM_N}. For example,
443 @code{SCM_IMP (@var{x})} is a predicate which returns non-zero iff
444 @var{x} is an immediate value (an @code{IM}). @code{SCM_NCONSP
445 (@var{x})} is a predicate which returns non-zero iff @var{x} is
446 @emph{not} a pair object (a @code{CONS}).
449 @node Garbage Collection, Immediates vs. Non-immediates, General Rules, How Guile does it
450 @subsection Garbage Collection
452 Aside from the latent typing, the major source of constraints on a
453 Scheme implementation's data representation is the garbage collector.
454 The collector must be able to traverse every live object in the heap, to
455 determine which objects are not live.
457 There are many ways to implement this, but Guile uses an algorithm
458 called @dfn{mark and sweep}. The collector scans the system's global
459 variables and the local variables on the stack to determine which
460 objects are immediately accessible by the C code. It then scans those
461 objects to find the objects they point to, @i{et cetera}. The collector
462 sets a @dfn{mark bit} on each object it finds, so each object is
463 traversed only once. This process is called @dfn{tracing}.
465 When the collector can find no unmarked objects pointed to by marked
466 objects, it assumes that any objects that are still unmarked will never
467 be used by the program (since there is no path of dereferences from any
468 global or local variable that reaches them) and deallocates them.
470 In the above paragraphs, we did not specify how the garbage collector
471 finds the global and local variables; as usual, there are many different
472 approaches. Frequently, the programmer must maintain a list of pointers
473 to all global variables that refer to the heap, and another list
474 (adjusted upon entry to and exit from each function) of local variables,
475 for the collector's benefit.
477 The list of global variables is usually not too difficult to maintain,
478 since global variables are relatively rare. However, an explicitly
479 maintained list of local variables (in the author's personal experience)
480 is a nightmare to maintain. Thus, Guile uses a technique called
481 @dfn{conservative garbage collection}, to make the local variable list
484 The trick to conservative collection is to treat the stack as an
485 ordinary range of memory, and assume that @emph{every} word on the stack
486 is a pointer into the heap. Thus, the collector marks all objects whose
487 addresses appear anywhere in the stack, without knowing for sure how
488 that word is meant to be interpreted.
490 Obviously, such a system will occasionally retain objects that are
491 actually garbage, and should be freed. In practice, this is not a
492 problem. The alternative, an explicitly maintained list of local
493 variable addresses, is effectively much less reliable, due to programmer
496 To accomodate this technique, data must be represented so that the
497 collector can accurately determine whether a given stack word is a
498 pointer or not. Guile does this as follows:
502 Every heap object has a two-word header, called a @dfn{cell}. Some
503 objects, like pairs, fit entirely in a cell's two words; others may
504 store pointers to additional memory in either of the words. For
505 example, strings and vectors store their length in the first word, and a
506 pointer to their elements in the second.
509 Guile allocates whole arrays of cells at a time, called @dfn{heap
510 segments}. These segments are always allocated so that the cells they
511 contain fall on eight-byte boundaries, or whatever is appropriate for
512 the machine's word size. Guile keeps all cells in a heap segment
513 initialized, whether or not they are currently in use.
516 Guile maintains a sorted table of heap segments.
520 Thus, given any random word @var{w} fetched from the stack, Guile's
521 garbage collector can consult the table to see if @var{w} falls within a
522 known heap segment, and check @var{w}'s alignment. If both tests pass,
523 the collector knows that @var{w} is a valid pointer to a cell,
524 intentional or not, and proceeds to trace the cell.
526 Note that heap segments do not contain all the data Guile uses; cells
527 for objects like vectors and strings contain pointers to other memory
528 areas. However, since those pointers are internal, and not shared among
529 many pieces of code, it is enough for the collector to find the cell,
530 and then use the cell's type to find more pointers to trace.
533 @node Immediates vs. Non-immediates, Immediate Datatypes, Garbage Collection, How Guile does it
534 @subsection Immediates vs. Non-immediates
536 Guile classifies Scheme objects into two kinds: those that fit entirely
537 within an @code{SCM}, and those that require heap storage.
539 The former class are called @dfn{immediates}. The class of immediates
540 includes small integers, characters, boolean values, the empty list, the
541 mysterious end-of-file object, and some others.
543 The remaining types are called, not suprisingly, @dfn{non-immediates}.
544 They include pairs, procedures, strings, vectors, and all other data
547 @deftypefn Macro int SCM_IMP (SCM @var{x})
548 Return non-zero iff @var{x} is an immediate object.
551 @deftypefn Macro int SCM_NIMP (SCM @var{x})
552 Return non-zero iff @var{x} is a non-immediate object. This is the
553 exact complement of @code{SCM_IMP}, above.
555 You must use this macro before calling a finer-grained predicate to
556 determine @var{x}'s type. For example, to see if @var{x} is a pair, you
559 SCM_NIMP (@var{x}) && SCM_CONSP (@var{x})
561 This is because Guile stores typing information for non-immediate values
562 in their cells, rather than in the @code{SCM} value itself; thus, you
563 must determine whether @var{x} refers to a cell before looking inside
566 This is somewhat of a pity, because it means that the programmer needs
567 to know which types Guile implements as immediates vs. non-immediates.
568 There are (possibly better) representations in which @code{SCM_CONSP}
569 can be self-sufficient. The immediate type predicates do not suffer
574 @node Immediate Datatypes, Non-immediate Datatypes, Immediates vs. Non-immediates, How Guile does it
575 @subsection Immediate Datatypes
577 The following datatypes are immediate values; that is, they fit entirely
578 within an @code{SCM} value. The @code{SCM_IMP} and @code{SCM_NIMP}
579 macros will distinguish these from non-immediates; see @ref{Immediates
580 vs. Non-immediates} for an explanation of the distinction.
582 Note that the type predicates for immediate values work correctly on any
583 @code{SCM} value; you do not need to call @code{SCM_IMP} first, to
584 establish that a value is immediate. This differs from the
585 non-immediate type predicates, which work correctly only on
586 non-immediate values; you must be sure the value is @code{SCM_NIMP}
587 before applying them.
597 @node Integers, Characters, Immediate Datatypes, Immediate Datatypes
598 @subsubsection Integers
600 Here are functions for operating on small integers, that fit within an
601 @code{SCM}. Such integers are called @dfn{immediate numbers}, or
602 @dfn{INUMs}. In general, INUMs occupy all but two bits of an
605 Bignums and floating-point numbers are non-immediate objects, and have
606 their own, separate accessors. The functions here will not work on
607 them. This is not as much of a problem as you might think, however,
608 because the system never constructs bignums that could fit in an INUM,
609 and never uses floating point values for exact integers.
611 @deftypefn Macro int SCM_INUMP (SCM @var{x})
612 Return non-zero iff @var{x} is a small integer value.
615 @deftypefn Macro int SCM_NINUMP (SCM @var{x})
616 The complement of SCM_INUMP.
619 @deftypefn Macro int SCM_INUM (SCM @var{x})
620 Return the value of @var{x} as an ordinary, C integer. If @var{x}
621 is not an INUM, the result is undefined.
624 @deftypefn Macro SCM SCM_MAKINUM (int @var{i})
625 Given a C integer @var{i}, return its representation as an @code{SCM}.
626 This function does not check for overflow.
630 @node Characters, Booleans, Integers, Immediate Datatypes
631 @subsubsection Characters
633 Here are functions for operating on characters.
635 @deftypefn Macro int SCM_CHARP (SCM @var{x})
636 Return non-zero iff @var{x} is a character value.
639 @deftypefn Macro {unsigned int} SCM_CHAR (SCM @var{x})
640 Return the value of @code{x} as a C character. If @var{x} is not a
641 Scheme character, the result is undefined.
644 @deftypefn Macro SCM SCM_MAKE_CHAR (int @var{c})
645 Given a C character @var{c}, return its representation as a Scheme
650 @node Booleans, Unique Values, Characters, Immediate Datatypes
651 @subsubsection Booleans
653 Here are functions and macros for operating on booleans.
655 @deftypefn Macro SCM SCM_BOOL_T
656 @deftypefnx Macro SCM SCM_BOOL_F
657 The Scheme true and false values.
660 @deftypefn Macro int SCM_NFALSEP (@var{x})
661 Convert the Scheme boolean value to a C boolean. Since every object in
662 Scheme except @code{#f} is true, this amounts to comparing @var{x} to
663 @code{#f}; hence the name.
664 @c Noel feels a chill here.
667 @deftypefn Macro SCM SCM_BOOL_NOT (@var{x})
668 Return the boolean inverse of @var{x}. If @var{x} is not a
669 Scheme boolean, the result is undefined.
673 @node Unique Values, , Booleans, Immediate Datatypes
674 @subsubsection Unique Values
676 The immediate values that are neither small integers, characters, nor
677 booleans are all unique values --- that is, datatypes with only one
680 @deftypefn Macro SCM SCM_EOL
681 The Scheme empty list object, or ``End Of List'' object, usually written
682 in Scheme as @code{'()}.
685 @deftypefn Macro SCM SCM_EOF_VAL
686 The Scheme end-of-file value. It has no standard written
687 representation, for obvious reasons.
690 @deftypefn Macro SCM SCM_UNSPECIFIED
691 The value returned by expressions which the Scheme standard says return
692 an ``unspecified'' value.
694 This is sort of a weirdly literal way to take things, but the standard
695 read-eval-print loop prints nothing when the expression returns this
696 value, so it's not a bad idea to return this when you can't think of
697 anything else helpful.
700 @deftypefn Macro SCM SCM_UNDEFINED
701 The ``undefined'' value. Its most important property is that is not
702 equal to any valid Scheme value. This is put to various internal uses
703 by C code interacting with Guile.
705 For example, when you write a C function that is callable from Scheme
706 and which takes optional arguments, the interpreter passes
707 @code{SCM_UNDEFINED} for any arguments you did not receive.
709 We also use this to mark unbound variables.
712 @deftypefn Macro int SCM_UNBNDP (SCM @var{x})
713 Return true if @var{x} is @code{SCM_UNDEFINED}. Apply this to a
714 symbol's value to see if it has a binding as a global variable.
718 @node Non-immediate Datatypes, Signalling Type Errors, Immediate Datatypes, How Guile does it
719 @subsection Non-immediate Datatypes
721 A non-immediate datatype is one which lives in the heap, either because
722 it cannot fit entirely within a @code{SCM} word, or because it denotes a
723 specific storage location (in the nomenclature of the Revised^4 Report
726 The @code{SCM_IMP} and @code{SCM_NIMP} macros will distinguish these
727 from immediates; see @ref{Immediates vs. Non-immediates}.
729 Given a cell, Guile distinguishes between pairs and other non-immediate
730 types by storing special @dfn{tag} values in a non-pair cell's car, that
731 cannot appear in normal pairs. A cell with a non-tag value in its car
732 is an ordinary pair. The type of a cell with a tag in its car depends
733 on the tag; the non-immediate type predicates test this value. If a tag
734 value appears elsewhere (in a vector, for example), the heap may become
739 * Non-immediate Type Predicates:: Special rules for using the type
740 predicates described here.
749 @node Non-immediate Type Predicates, Pairs, Non-immediate Datatypes, Non-immediate Datatypes
750 @subsubsection Non-immediate Type Predicates
752 As mentioned in @ref{Garbage Collection}, all non-immediate objects
753 start with a @dfn{cell}, or a pair of words. Furthermore, all type
754 information that distinguishes one kind of non-immediate from another is
755 stored in the cell. The type information in the @code{SCM} value
756 indicates only that the object is a non-immediate; all finer
757 distinctions require one to examine the cell itself, usually with the
758 appropriate type predicate macro.
760 The type predicates for non-immediate objects generally assume that
761 their argument is a non-immediate value. Thus, you must be sure that a
762 value is @code{SCM_NIMP} first before passing it to a non-immediate type
763 predicate. Thus, the idiom for testing whether a value is a cell or not
766 SCM_NIMP (@var{x}) && SCM_CONSP (@var{x})
770 @node Pairs, Vectors, Non-immediate Type Predicates, Non-immediate Datatypes
773 Pairs are the essential building block of list structure in Scheme. A
774 pair object has two fields, called the @dfn{car} and the @dfn{cdr}.
776 It is conventional for a pair's @sc{car} to contain an element of a
777 list, and the @sc{cdr} to point to the next pair in the list, or to
778 contain @code{SCM_EOL}, indicating the end of the list. Thus, a set of
779 pairs chained through their @sc{cdr}s constitutes a singly-linked list.
780 Scheme and libguile define many functions which operate on lists
781 constructed in this fashion, so although lists chained through the
782 @sc{car}s of pairs will work fine too, they may be less convenient to
783 manipulate, and receive less support from the community.
785 Guile implements pairs by mapping the @sc{car} and @sc{cdr} of a pair
786 directly into the two words of the cell.
789 @deftypefn Macro int SCM_CONSP (SCM @var{x})
790 Return non-zero iff @var{x} is a Scheme pair object.
791 The results are undefined if @var{x} is an immediate value.
794 @deftypefn Macro int SCM_NCONSP (SCM @var{x})
795 The complement of SCM_CONSP.
798 @deftypefn Macro void SCM_NEWCELL (SCM @var{into})
799 Allocate a new cell, and set @var{into} to point to it. This macro
800 expands to a statement, not an expression, and @var{into} must be an
803 This is the most primitive way to allocate a cell; it is quite fast.
805 The @sc{car} of the cell initially tags it as a ``free cell''. If the
806 caller intends to use it as an ordinary cons, she must store ordinary
807 SCM values in its @sc{car} and @sc{cdr}.
809 If the caller intends to use it as a header for some other type, she
810 must store an appropriate magic value in the cell's @sc{car}, to mark
811 it as a member of that type, and store whatever value in the @sc{cdr}
812 that type expects. You should generally not do this, unless you are
813 implementing a new datatype, and thoroughly understand the code in
814 @code{<libguile/tags.h>}.
817 @deftypefun SCM scm_cons (SCM @var{car}, SCM @var{cdr})
818 Allocate (``CONStruct'') a new pair, with @var{car} and @var{cdr} as its
823 The macros below perform no typechecking. The results are undefined if
824 @var{cell} is an immediate. However, since all non-immediate Guile
825 objects are constructed from cells, and these macros simply return the
826 first element of a cell, they actually can be useful on datatypes other
827 than pairs. (Of course, it is not very modular to use them outside of
828 the code which implements that datatype.)
830 @deftypefn Macro SCM SCM_CAR (SCM @var{cell})
831 Return the @sc{car}, or first field, of @var{cell}.
834 @deftypefn Macro SCM SCM_CDR (SCM @var{cell})
835 Return the @sc{cdr}, or second field, of @var{cell}.
838 @deftypefn Macro void SCM_SETCAR (SCM @var{cell}, SCM @var{x})
839 Set the @sc{car} of @var{cell} to @var{x}.
842 @deftypefn Macro void SCM_SETCDR (SCM @var{cell}, SCM @var{x})
843 Set the @sc{cdr} of @var{cell} to @var{x}.
846 @deftypefn Macro SCM SCM_CAAR (SCM @var{cell})
847 @deftypefnx Macro SCM SCM_CADR (SCM @var{cell})
848 @deftypefnx Macro SCM SCM_CDAR (SCM @var{cell}) @dots{}
849 @deftypefnx Macro SCM SCM_CDDDDR (SCM @var{cell})
850 Return the @sc{car} of the @sc{car} of @var{cell}, the @sc{car} of the
851 @sc{cdr} of @var{cell}, @i{et cetera}.
855 @node Vectors, Procedures, Pairs, Non-immediate Datatypes
856 @subsubsection Vectors, Strings, and Symbols
858 Vectors, strings, and symbols have some properties in common. They all
859 have a length, and they all have an array of elements. In the case of a
860 vector, the elements are @code{SCM} values; in the case of a string or
861 symbol, the elements are characters.
863 All these types store their length (along with some tagging bits) in the
864 @sc{car} of their header cell, and store a pointer to the elements in
865 their @sc{cdr}. Thus, the @code{SCM_CAR} and @code{SCM_CDR} macros
866 are (somewhat) meaningful when applied to these datatypes.
868 @deftypefn Macro int SCM_VECTORP (SCM @var{x})
869 Return non-zero iff @var{x} is a vector.
870 The results are undefined if @var{x} is an immediate value.
873 @deftypefn Macro int SCM_STRINGP (SCM @var{x})
874 Return non-zero iff @var{x} is a string.
875 The results are undefined if @var{x} is an immediate value.
878 @deftypefn Macro int SCM_SYMBOLP (SCM @var{x})
879 Return non-zero iff @var{x} is a symbol.
880 The results are undefined if @var{x} is an immediate value.
883 @deftypefn Macro int SCM_LENGTH (SCM @var{x})
884 Return the length of the object @var{x}.
885 The results are undefined if @var{x} is not a vector, string, or symbol.
888 @deftypefn Macro {SCM *} SCM_VELTS (SCM @var{x})
889 Return a pointer to the array of elements of the vector @var{x}.
890 The results are undefined if @var{x} is not a vector.
893 @deftypefn Macro {char *} SCM_CHARS (SCM @var{x})
894 Return a pointer to the characters of @var{x}.
895 The results are undefined if @var{x} is not a symbol or a string.
898 There are also a few magic values stuffed into memory before a symbol's
899 characters, but you don't want to know about those. What cruft!
902 @node Procedures, Closures, Vectors, Non-immediate Datatypes
903 @subsubsection Procedures
905 Guile provides two kinds of procedures: @dfn{closures}, which are the
906 result of evaluating a @code{lambda} expression, and @dfn{subrs}, which
907 are C functions packaged up as Scheme objects, to make them available to
910 (There are actually other sorts of procedures: compiled closures, and
911 continuations; see the source code for details about them.)
913 @deftypefun SCM scm_procedure_p (SCM @var{x})
914 Return @code{SCM_BOOL_T} iff @var{x} is a Scheme procedure object, of
915 any sort. Otherwise, return @code{SCM_BOOL_F}.
919 @node Closures, Subrs, Procedures, Non-immediate Datatypes
920 @subsubsection Closures
922 [FIXME: this needs to be further subbed, but texinfo has no subsubsub]
924 A closure is a procedure object, generated as the value of a
925 @code{lambda} expression in Scheme. The representation of a closure is
926 straightforward --- it contains a pointer to the code of the lambda
927 expression from which it was created, and a pointer to the environment
930 In Guile, each closure also has a property list, allowing the system to
931 store information about the closure. I'm not sure what this is used for
932 at the moment --- the debugger, maybe?
934 @deftypefn Macro int SCM_CLOSUREP (SCM @var{x})
935 Return non-zero iff @var{x} is a closure. The results are
936 undefined if @var{x} is an immediate value.
939 @deftypefn Macro SCM SCM_PROCPROPS (SCM @var{x})
940 Return the property list of the closure @var{x}. The results are
941 undefined if @var{x} is not a closure.
944 @deftypefn Macro void SCM_SETPROCPROPS (SCM @var{x}, SCM @var{p})
945 Set the property list of the closure @var{x} to @var{p}. The results
946 are undefined if @var{x} is not a closure.
949 @deftypefn Macro SCM SCM_CODE (SCM @var{x})
950 Return the code of the closure @var{x}. The results are undefined if
951 @var{x} is not a closure.
953 This function should probably only be used internally by the
954 interpreter, since the representation of the code is intimately
955 connected with the interpreter's implementation.
958 @deftypefn Macro SCM SCM_ENV (SCM @var{x})
959 Return the environment enclosed by @var{x}.
960 The results are undefined if @var{x} is not a closure.
962 This function should probably only be used internally by the
963 interpreter, since the representation of the environment is intimately
964 connected with the interpreter's implementation.
968 @node Subrs, Ports, Closures, Non-immediate Datatypes
971 [FIXME: this needs to be further subbed, but texinfo has no subsubsub]
973 A subr is a pointer to a C function, packaged up as a Scheme object to
974 make it callable by Scheme code. In addition to the function pointer,
975 the subr also contains a pointer to the name of the function, and
976 information about the number of arguments accepted by the C fuction, for
977 the sake of error checking.
979 There is no single type predicate macro that recognizes subrs, as
980 distinct from other kinds of procedures. The closest thing is
981 @code{scm_procedure_p}; see @ref{Procedures}.
983 @deftypefn Macro {char *} SCM_SNAME (@var{x})
984 Return the name of the subr @var{x}. The results are undefined if
985 @var{x} is not a subr.
988 @deftypefun SCM scm_make_gsubr (char *@var{name}, int @var{req}, int @var{opt}, int @var{rest}, SCM (*@var{function})())
989 Create a new subr object named @var{name}, based on the C function
990 @var{function}, make it visible to Scheme the value of as a global
991 variable named @var{name}, and return the subr object.
993 The subr object accepts @var{req} required arguments, @var{opt} optional
994 arguments, and a @var{rest} argument iff @var{rest} is non-zero. The C
995 function @var{function} should accept @code{@var{req} + @var{opt}}
996 arguments, or @code{@var{req} + @var{opt} + 1} arguments if @code{rest}
999 When a subr object is applied, it must be applied to at least @var{req}
1000 arguments, or else Guile signals an error. @var{function} receives the
1001 subr's first @var{req} arguments as its first @var{req} arguments. If
1002 there are fewer than @var{opt} arguments remaining, then @var{function}
1003 receives the value @code{SCM_UNDEFINED} for any missing optional
1004 arguments. If @var{rst} is non-zero, then any arguments after the first
1005 @code{@var{req} + @var{opt}} are packaged up as a list as passed as
1006 @var{function}'s last argument.
1008 Note that subrs can actually only accept a predefined set of
1009 combinations of required, optional, and rest arguments. For example, a
1010 subr can take one required argument, or one required and one optional
1011 argument, but a subr can't take one required and two optional arguments.
1012 It's bizarre, but that's the way the interpreter was written. If the
1013 arguments to @code{scm_make_gsubr} do not fit one of the predefined
1014 patterns, then @code{scm_make_gsubr} will return a compiled closure
1015 object instead of a subr object.
1019 @node Ports, , Subrs, Non-immediate Datatypes
1020 @subsubsection Ports
1022 Haven't written this yet, 'cos I don't understand ports yet.
1025 @node Signalling Type Errors, , Non-immediate Datatypes, How Guile does it
1026 @subsection Signalling Type Errors
1028 Every function visible at the Scheme level should aggressively check the
1029 types of its arguments, to avoid misinterpreting a value, and perhaps
1030 causing a segmentation fault. Guile provides some macros to make this
1033 @deftypefn Macro void SCM_ASSERT (int @var{test}, SCM @var{obj}, int @var{position}, char *@var{subr})
1034 If @var{test} is zero, signal an error, attributed to the subroutine
1035 named @var{subr}, operating on the value @var{obj}. The @var{position}
1036 value determines exactly what sort of error to signal.
1038 If @var{position} is a string, @code{SCM_ASSERT} raises a
1039 ``miscellaneous'' error whose message is that string.
1041 Otherwise, @var{position} should be one of the values defined below.
1044 @deftypefn Macro int SCM_ARG1
1045 @deftypefnx Macro int SCM_ARG2
1046 @deftypefnx Macro int SCM_ARG3
1047 @deftypefnx Macro int SCM_ARG4
1048 @deftypefnx Macro int SCM_ARG5
1049 Signal a ``wrong type argument'' error. When used as the @var{position}
1050 argument of @code{SCM_ASSERT}, @code{SCM_ARG@var{n}} claims that
1051 @var{obj} has the wrong type for the @var{n}'th argument of @var{subr}.
1053 The only way to complain about the type of an argument after the fifth
1054 is to use @code{SCM_ARGn}, defined below, which doesn't specify which
1055 argument is wrong. You could pass your own error message to
1056 @code{SCM_ASSERT} as the @var{position}, but then the error signalled is
1057 a ``miscellaneous'' error, not a ``wrong type argument'' error. This
1059 @comment Any function with more than two arguments is wrong --- Perlis
1060 @comment Despite Perlis, I agree. Why not have two Macros, one with
1061 @comment a string error message, and the other with an integer position
1062 @comment that only claims a type error in an argument?
1063 @comment --- Keith Wright
1066 @deftypefn Macro int SCM_ARGn
1067 As above, but does not specify which argument's type is incorrect.
1070 @deftypefn Macro int SCM_WNA
1071 Signal an error complaining that the function received the wrong number
1074 Interestingly, the message is attributed to the function named by
1075 @var{obj}, not @var{subr}, so @var{obj} must be a Scheme string object
1076 naming the function. Usually, Guile catches these errors before ever
1077 invoking the subr, so we don't run into these problems.
1081 @node Defining New Types (Smobs), , How Guile does it, Top
1082 @section Defining New Types (Smobs)
1084 @dfn{Smobs} are Guile's mechanism for adding new non-immediate types to
1085 the system.@footnote{The term ``smob'' was coined by Aubrey Jaffer, who
1086 says it comes from ``small object'', referring to the fact that only the
1087 @sc{cdr} and part of the @sc{car} of a smob's cell are available for
1088 use.} To define a new smob type, the programmer provides Guile with
1089 some essential information about the type --- how to print it, how to
1090 garbage collect it, and so on --- and Guile returns a fresh type tag for
1091 use in the @sc{car} of new cells. The programmer can then use
1092 @code{scm_make_gsubr} to make a set of C functions that create and
1093 operate on these objects visible to Scheme code.
1095 (You can find a complete version of the example code used in this
1096 section in the Guile distribution, in @file{doc/example-smob}. That
1097 directory includes a makefile and a suitable @code{main} function, so
1098 you can build a complete interactive Guile shell, extended with the
1099 datatypes described here.)
1102 * Describing a New Type::
1103 * Creating Instances::
1105 * Garbage Collecting Smobs::
1106 * A Common Mistake In Allocating Smobs::
1107 * Garbage Collecting Simple Smobs::
1108 * A Complete Example::
1111 @node Describing a New Type, Creating Instances, Defining New Types (Smobs), Defining New Types (Smobs)
1112 @subsection Describing a New Type
1114 To define a new type, the programmer must write four functions to
1115 manage instances of the type:
1119 Guile will apply this function to each instance of the new type it
1120 encounters during garbage collection. This function is responsible for
1121 telling the collector about any other non-immediate objects the object
1122 refers to. The default smob mark function is to not mark any data.
1123 @xref{Garbage Collecting Smobs}, for more details.
1126 Guile will apply this function to each instance of the new type it could
1127 not find any live pointers to. The function should release all
1128 resources held by the object and return the number of bytes released.
1129 This is analagous to the Java finalization method-- it is invoked at
1130 an unspecified time (when garbage collection occurs) after the object
1132 The default free function frees the smob data (if the size of the struct
1133 passed to @code{scm_make_smob_type} or @code{scm_make_smob_type_mfpe} is
1134 non-zero) using @code{scm_must_free} and returns the size of that
1135 struct. @xref{Garbage Collecting Smobs}, for more details.
1138 @c GJB:FIXME:: @var{exp} and @var{port} need to refer to a prototype of
1139 @c the print function.... where is that, or where should it go?
1140 Guile will apply this function to each instance of the new type to print
1141 the value, as for @code{display} or @code{write}. The function should
1142 write a printed representation of @var{exp} on @var{port}, in accordance
1143 with the parameters in @var{pstate}. (For more information on print
1144 states, see @ref{Ports}.) The default print function prints @code{#<NAME ADDRESS>}
1145 where @code{NAME} is the first argument passed to @code{scm_make_smob_type} or
1146 @code{scm_make_smob_type_mfpe}.
1149 If Scheme code asks the @code{equal?} function to compare two instances
1150 of the same smob type, Guile calls this function. It should return
1151 @code{SCM_BOOL_T} if @var{a} and @var{b} should be considered
1152 @code{equal?}, or @code{SCM_BOOL_F} otherwise. If @code{equalp} is
1153 @code{NULL}, @code{equal?} will assume that two instances of this type are
1154 never @code{equal?} unless they are @code{eq?}.
1158 To actually register the new smob type, call @code{scm_make_smob_type}:
1160 @deftypefun long scm_make_smob_type (const char *name, scm_sizet size)
1161 This function implements the standard way of adding a new smob type,
1162 named @var{name}, with instance size @var{size}, to the system. The
1163 return value is a tag that is used in creating instances of the type.
1164 If @var{size} is 0, then no memory will be allocated when instances of
1165 the smob are created, and nothing will be freed by the default free
1166 function. Default values are provided for mark, free, print, and,
1167 equalp, as described above. If you want to customize any of these
1168 functions, the call to @code{scm_make_smob_type} should be immediately
1169 followed by calls to one or several of @code{scm_set_smob_mark},
1170 @code{scm_set_smob_free}, @code{scm_set_smob_print}, and/or
1171 @code{scm_set_smob_equalp}.
1174 Each of the below @code{scm_set_smob_XXX} functions registers a smob
1175 special function for a given type. Each function is intended to be used
1176 only zero or one time per type, and the call should be placed
1177 immediately following the call to @code{scm_make_smob_type}.
1179 @deftypefun void scm_set_smob_mark (long tc, SCM (*mark) (SCM))
1180 This function sets the smob marking procedure for the smob type specified by
1181 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1184 @deftypefun void scm_set_smob_free (long tc, scm_sizet (*free) (SCM))
1185 This function sets the smob freeing procedure for the smob type specified by
1186 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1189 @deftypefun void scm_set_smob_print (long tc, int (*print) (SCM,SCM,scm_print_state*))
1190 This function sets the smob printing procedure for the smob type specified by
1191 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1194 @deftypefun void scm_set_smob_equalp (long tc, SCM (*equalp) (SCM,SCM))
1195 This function sets the smob equality-testing predicate for the smob type specified by
1196 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
1199 Instead of using @code{scm_make_smob_type} and calling each of the
1200 individual @code{scm_set_smob_XXX} functions to register each special
1201 function independently, you can use @code{scm_make_smob_type_mfpe} to
1202 register all of the special functions at once as you create the smob
1203 type@footnote{Warning: There is an ongoing discussion among the developers which
1204 may result in deprecating @code{scm_make_smob_type_mfpe} in next release
1207 @deftypefun long scm_make_smob_type_mfpe(const char *name, scm_sizet size, SCM (*mark) (SCM), scm_sizet (*free) (SCM), int (*print) (SCM, SCM, scm_print_state*), SCM (*equalp) (SCM, SCM))
1208 This function invokes @code{scm_make_smob_type} on its first two arguments
1209 to add a new smob type named @var{name}, with instance size @var{size} to the system.
1210 It also registers the @var{mark}, @var{free}, @var{print}, @var{equalp} smob
1211 special functions for that new type. Any of these parameters can be @code{NULL}
1212 to have that special function use the default behaviour for guile.
1213 The return value is a tag that is used in creating instances of the type. If @var{size}
1214 is 0, then no memory will be allocated when instances of the smob are created, and
1215 nothing will be freed by the default free function.
1218 For example, here is how one might declare and register a new type
1219 representing eight-bit grayscale images:
1221 #include <libguile.h>
1228 image_tag = scm_make_smob_type_mfpe ("image",sizeof(struct image),
1229 mark_image, free_image, print_image, NULL);
1234 @node Creating Instances, Typechecking, Describing a New Type, Defining New Types (Smobs)
1235 @subsection Creating Instances
1237 Like other non-immediate types, smobs start with a cell whose @sc{car}
1238 contains typing information, and whose @code{cdr} is free for any use. For smobs,
1239 the @code{cdr} stores a pointer to the internal C structure holding the
1241 To create an instance of a smob type following these standards, you should
1242 use @code{SCM_NEWSMOB}:
1244 @deftypefn Macro void SCM_NEWSMOB(SCM value,long tag,void *data)
1245 Make @var{value} contain a smob instance of the type with tag @var{tag}
1246 and smob data @var{data}. @var{value} must be previously declared
1247 as C type @code{SCM}.
1250 Since it is often the case (e.g., in smob constructors) that you will
1251 create a smob instance and return it, there is also a slightly specialized
1252 macro for this situation:
1254 @deftypefn Macro fn_returns SCM_RETURN_NEWSMOB(long tab, void *data)
1255 This macro expands to a block of code that creates a smob instance of
1256 the type with tag @var{tag} and smob data @var{data}, and returns
1257 that @code{SCM} value. It should be the last piece of code in
1261 Guile provides the following functions for managing memory, which are
1262 often helpful when implementing smobs:
1264 @deftypefun {char *} scm_must_malloc (long @var{len}, char *@var{what})
1265 Allocate @var{len} bytes of memory, using @code{malloc}, and return a
1268 If there is not enough memory available, invoke the garbage collector,
1269 and try once more. If there is still not enough, signal an error,
1270 reporting that we could not allocate @var{what}.
1272 This function also helps maintain statistics about the size of the heap.
1275 @deftypefun {char *} scm_must_realloc (char *@var{addr}, long @var{olen}, long @var{len}, char *@var{what})
1276 Resize (and possibly relocate) the block of memory at @var{addr}, to
1277 have a size of @var{len} bytes, by calling @code{realloc}. Return a
1278 pointer to the new block.
1280 If there is not enough memory available, invoke the garbage collector,
1281 and try once more. If there is still not enough, signal an error,
1282 reporting that we could not allocate @var{what}.
1284 The value @var{olen} should be the old size of the block of memory at
1285 @var{addr}; it is only used for keeping statistics on the size of the
1289 @deftypefun void scm_must_free (char *@var{addr})
1290 Free the block of memory at @var{addr}, using @code{free}. If
1291 @var{addr} is zero, signal an error, complaining of an attempt to free
1292 something that is already free.
1294 This does no record-keeping; instead, the smob's @code{free} function
1295 must take care of that.
1297 This function isn't usually sufficiently different from the usual
1298 @code{free} function to be worth using.
1302 Continuing the above example, if the global variable @code{image_tag}
1303 contains a tag returned by @code{scm_newsmob}, here is how we could
1304 construct a smob whose @sc{cdr} contains a pointer to a freshly
1305 allocated @code{struct image}:
1312 /* The name of this image */
1315 /* A function to call when this image is
1316 modified, e.g., to update the screen,
1317 or SCM_BOOL_F if no action necessary */
1322 make_image (SCM name, SCM s_width, SCM s_height)
1324 struct image *image;
1327 SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
1328 SCM_ARG1, "make-image");
1329 SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
1330 SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
1332 width = SCM_INUM (s_width);
1333 height = SCM_INUM (s_height);
1335 image = (struct image *) scm_must_malloc (sizeof (struct image), "image");
1336 image->width = width;
1337 image->height = height;
1338 image->pixels = scm_must_malloc (width * height, "image pixels");
1340 image->update_func = SCM_BOOL_F;
1342 SCM_RETURN_NEWSMOB (image_tag, image);
1347 @node Typechecking, Garbage Collecting Smobs, Creating Instances, Defining New Types (Smobs)
1348 @subsection Typechecking
1350 Functions that operate on smobs should aggressively check the types of
1351 their arguments, to avoid misinterpreting some other datatype as a smob,
1352 and perhaps causing a segmentation fault. Fortunately, this is pretty
1353 simple to do. The function need only verify that its argument is a
1354 non-immediate, whose @sc{car} is the type tag returned by
1357 For example, here is a simple function that operates on an image smob,
1358 and checks the type of its argument. We also present an expanded
1359 version of the @code{init_image_type} function, to make
1360 @code{clear_image} and the image constructor function @code{make_image}
1361 visible to Scheme code.
1364 clear_image (SCM image_smob)
1367 struct image *image;
1369 SCM_ASSERT ((SCM_NIMP (image_smob)
1370 && SCM_CAR (image_smob) == image_tag),
1371 image_smob, SCM_ARG1, "clear-image");
1373 image = (struct image *) SCM_CDR (image_smob);
1374 area = image->width * image->height;
1375 memset (image->pixels, 0, area);
1377 /* Invoke the image's update function. */
1378 if (image->update_func != SCM_BOOL_F)
1379 scm_apply (image->update_func, SCM_EOL, SCM_EOL);
1381 return SCM_UNSPECIFIED;
1388 image_tag = scm_newsmob (&image_funs);
1390 scm_make_gsubr ("make-image", 3, 0, 0, make_image);
1391 scm_make_gsubr ("clear-image", 1, 0, 0, clear_image);
1395 Note that checking types is a little more complicated during garbage
1396 collection; see the description of @code{SCM_GCTYP16} in @ref{Garbage
1399 @c GJB:FIXME:: should talk about guile-snarf somewhere!
1401 @node Garbage Collecting Smobs, A Common Mistake In Allocating Smobs, Typechecking, Defining New Types (Smobs)
1402 @subsection Garbage Collecting Smobs
1404 Once a smob has been released to the tender mercies of the Scheme
1405 system, it must be prepared to survive garbage collection. Guile calls
1406 the @code{mark} and @code{free} functions of the @code{scm_smobfuns}
1407 structure to manage this.
1409 As described before (@pxref{Garbage Collection}), every object in the
1410 Scheme system has a @dfn{mark bit}, which the garbage collector uses to
1411 tell live objects from dead ones. When collection starts, every
1412 object's mark bit is clear. The collector traces pointers through the
1413 heap, starting from objects known to be live, and sets the mark bit on
1414 each object it encounters. When it can find no more unmarked objects,
1415 the collector walks all objects, live and dead, frees those whose mark
1416 bits are still clear, and clears the mark bit on the others.
1418 The two main portions of the collection are called the @dfn{mark phase},
1419 during which the collector marks live objects, and the @dfn{sweep
1420 phase}, during which the collector frees all unmarked objects.
1422 The mark bit of a smob lives in its @sc{car}, along with the smob's type
1423 tag. When the collector encounters a smob, it sets the smob's mark bit,
1424 and uses the smob's type tag to find the appropriate @code{mark}
1425 function for that smob: the one listed in that smob's
1426 @code{scm_smobfuns} structure. It then calls the @code{mark} function,
1427 passing it the smob as its only argument.
1429 The @code{mark} function is responsible for marking any other Scheme
1430 objects the smob refers to. If it does not do so, the objects' mark
1431 bits will still be clear when the collector begins to sweep, and the
1432 collector will free them. If this occurs, it will probably break, or at
1433 least confuse, any code operating on the smob; the smob's @code{SCM}
1434 values will have become dangling references.
1436 To mark an arbitrary Scheme object, the @code{mark} function may call
1439 @deftypefun void scm_gc_mark (SCM @var{x})
1440 Mark the object @var{x}, and recurse on any objects @var{x} refers to.
1441 If @var{x}'s mark bit is already set, return immediately.
1444 Thus, here is how we might write the @code{mark} function for the image
1445 smob type discussed above:
1449 mark_image (SCM image_smob)
1451 /* Mark the image's name and update function. */
1452 struct image *image = (struct image *) SCM_CDR (image_smob);
1454 scm_gc_mark (image->name);
1455 scm_gc_mark (image->update_func);
1462 Note that, even though the image's @code{update_func} could be an
1463 arbitrarily complex structure (representing a procedure and any values
1464 enclosed in its environment), @code{scm_gc_mark} will recurse as
1465 necessary to mark all its components. Because @code{scm_gc_mark} sets
1466 an object's mark bit before it recurses, it is not confused by
1467 circular structures.
1469 As an optimization, the collector will mark whatever value is returned
1470 by the @code{mark} function; this helps limit depth of recursion during
1471 the mark phase. Thus, the code above could also be written as:
1475 mark_image (SCM image_smob)
1477 /* Mark the image's name and update function. */
1478 struct image *image = (struct image *) SCM_CDR (image_smob);
1480 scm_gc_mark (image->name);
1481 return image->update_func;
1487 Finally, when the collector encounters an unmarked smob during the sweep
1488 phase, it uses the smob's tag to find the appropriate @code{free}
1489 function for the smob. It then calls the function, passing it the smob
1490 as its only argument.
1492 The @code{free} function must release any resources used by the smob.
1493 However, it need not free objects managed by the collector; the
1494 collector will take care of them. The return type of the @code{free}
1495 function should be @code{scm_sizet}, an unsigned integral type; the
1496 @code{free} function should return the number of bytes released, to help
1497 the collector maintain statistics on the size of the heap.
1499 Here is how we might write the @code{free} function for the image smob
1503 free_image (SCM image_smob)
1505 struct image *image = (struct image *) SCM_CDR (image_smob);
1506 scm_sizet size = image->width * image->height + sizeof (*image);
1508 free (image->pixels);
1515 During the sweep phase, the garbage collector will clear the mark bits
1516 on all live objects. The code which implements a smob need not do this
1519 There is no way for smob code to be notified when collection is
1522 Note that, since a smob's mark bit lives in its @sc{car}, along with the
1523 smob's type tag, the technique for checking the type of a smob described
1524 in @ref{Typechecking} will not necessarily work during GC. If you need
1525 to find out whether a given object is a particular smob type during GC,
1526 use the following macro:
1528 @deftypefn Macro void SCM_GCTYP16 (SCM @var{x})
1529 Return the type bits of the smob @var{x}, with the mark bit clear.
1531 Use this macro instead of @code{SCM_CAR} to check the type of a smob
1532 during GC. Usually, only code called by the smob's @code{mark} function
1533 need worry about this.
1536 It is usually a good idea to minimize the amount of processing done
1537 during garbage collection; keep @code{mark} and @code{free} functions
1538 very simple. Since collections occur at unpredictable times, it is easy
1539 for any unusual activity to interfere with normal code.
1542 @node A Common Mistake In Allocating Smobs, Garbage Collecting Simple Smobs, Garbage Collecting Smobs, Defining New Types (Smobs)
1543 @subsection A Common Mistake In Allocating Smobs
1545 When constructing new objects, you must be careful that the garbage
1546 collector can always find any new objects you allocate. For example,
1547 suppose we wrote the @code{make_image} function this way:
1551 make_image (SCM name, SCM s_width, SCM s_height)
1553 struct image *image;
1557 SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
1558 SCM_ARG1, "make-image");
1559 SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
1560 SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
1562 width = SCM_INUM (s_width);
1563 height = SCM_INUM (s_height);
1565 image = (struct image *) scm_must_malloc (sizeof (struct image), "image");
1566 image->width = width;
1567 image->height = height;
1568 image->pixels = scm_must_malloc (width * height, "image pixels");
1570 /* THESE TWO LINES HAVE CHANGED: */
1571 image->name = scm_string_copy (name);
1572 image->update_func = scm_make_gsubr (@dots{});
1574 SCM_NEWCELL (image_smob);
1575 SCM_SETCDR (image_smob, image);
1576 SCM_SETCAR (image_smob, image_tag);
1582 This code is incorrect. The calls to @code{scm_string_copy} and
1583 @code{scm_make_gsubr} allocate fresh objects. Allocating any new object
1584 may cause the garbage collector to run. If @code{scm_make_gsubr}
1585 invokes a collection, the garbage collector has no way to discover that
1586 @code{image->name} points to the new string object; the @code{image}
1587 structure is not yet part of any Scheme object, so the garbage collector
1588 will not traverse it. Since the garbage collector cannot find any
1589 references to the new string object, it will free it, leaving
1590 @code{image} pointing to a dead object.
1592 A correct implementation might say, instead:
1594 image->name = SCM_BOOL_F;
1595 image->update_func = SCM_BOOL_F;
1597 SCM_NEWCELL (image_smob);
1598 SCM_SETCDR (image_smob, image);
1599 SCM_SETCAR (image_smob, image_tag);
1601 image->name = scm_string_copy (name);
1602 image->update_func = scm_make_gsubr (@dots{});
1607 Now, by the time we allocate the new string and function objects,
1608 @code{image_smob} points to @code{image}. If the garbage collector
1609 scans the stack, it will find a reference to @code{image_smob} and
1610 traverse @code{image}, so any objects @code{image} points to will be
1614 @node Garbage Collecting Simple Smobs, A Complete Example, A Common Mistake In Allocating Smobs, Defining New Types (Smobs)
1615 @subsection Garbage Collecting Simple Smobs
1617 It is often useful to define very simple smob types --- smobs which have
1618 no data to mark, other than the cell itself, or smobs whose @sc{cdr} is
1619 simply an ordinary Scheme object, to be marked recursively. Guile
1620 provides some functions to handle these common cases; you can use these
1621 functions as your smob type's @code{mark} function, if your smob's
1622 structure is simple enough.
1624 If the smob refers to no other Scheme objects, then no action is
1625 necessary; the garbage collector has already marked the smob cell
1626 itself. In that case, you can use zero as your mark function.
1628 @deftypefun SCM scm_markcdr (SCM @var{x})
1629 Mark the references in the smob @var{x}, assuming that @var{x}'s
1630 @sc{cdr} contains an ordinary Scheme object, and @var{x} refers to no
1631 other objects. This function simply returns @var{x}'s @sc{cdr}.
1634 @deftypefun scm_sizet scm_free0 (SCM @var{x})
1635 Do nothing; return zero. This function is appropriate for smobs that
1636 use either zero or @code{scm_markcdr} as their marking functions, and
1637 refer to no heap storage, including memory managed by @code{malloc},
1638 other than the smob's header cell.
1642 @node A Complete Example, , Garbage Collecting Simple Smobs, Defining New Types (Smobs)
1643 @subsection A Complete Example
1645 Here is the complete text of the implementation of the image datatype,
1646 as presented in the sections above. We also provide a definition for
1647 the smob's @code{print} function, and make some objects and functions
1648 static, to clarify exactly what the surrounding code is using.
1650 As mentioned above, you can find this code in the Guile distribution, in
1651 @file{doc/example-smob}. That directory includes a makefile and a
1652 suitable @code{main} function, so you can build a complete interactive
1653 Guile shell, extended with the datatypes described here.)
1656 /* file "image-type.c" */
1659 #include <libguile.h>
1661 static long image_tag;
1667 /* The name of this image */
1670 /* A function to call when this image is
1671 modified, e.g., to update the screen,
1672 or SCM_BOOL_F if no action necessary */
1677 make_image (SCM name, SCM s_width, SCM s_height)
1679 struct image *image;
1683 SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
1684 SCM_ARG1, "make-image");
1685 SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
1686 SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
1688 width = SCM_INUM (s_width);
1689 height = SCM_INUM (s_height);
1691 image = (struct image *) scm_must_malloc (sizeof (struct image), "image");
1692 image->width = width;
1693 image->height = height;
1694 image->pixels = scm_must_malloc (width * height, "image pixels");
1696 image->update_func = SCM_BOOL_F;
1698 SCM_NEWCELL (image_smob);
1699 SCM_SETCDR (image_smob, image);
1700 SCM_SETCAR (image_smob, image_tag);
1706 clear_image (SCM image_smob)
1709 struct image *image;
1711 SCM_ASSERT ((SCM_NIMP (image_smob)
1712 && SCM_CAR (image_smob) == image_tag),
1713 image_smob, SCM_ARG1, "clear-image");
1715 image = (struct image *) SCM_CDR (image_smob);
1716 area = image->width * image->height;
1717 memset (image->pixels, 0, area);
1719 /* Invoke the image's update function. */
1720 if (image->update_func != SCM_BOOL_F)
1721 scm_apply (image->update_func, SCM_EOL, SCM_EOL);
1723 return SCM_UNSPECIFIED;
1727 mark_image (SCM image_smob)
1729 struct image *image = (struct image *) SCM_CDR (image_smob);
1731 scm_gc_mark (image->name);
1732 return image->update_func;
1736 free_image (SCM image_smob)
1738 struct image *image = (struct image *) SCM_CDR (image_smob);
1739 scm_sizet size = image->width * image->height + sizeof (struct image);
1741 free (image->pixels);
1748 print_image (SCM image_smob, SCM port, scm_print_state *pstate)
1750 struct image *image = (struct image *) SCM_CDR (image_smob);
1752 scm_puts ("#<image ", port);
1753 scm_display (image->name, port);
1754 scm_puts (">", port);
1756 /* non-zero means success */
1760 static scm_smobfuns image_funs = @{
1761 mark_image, free_image, print_image, 0
1767 image_tag = scm_newsmob (&image_funs);
1769 scm_make_gsubr ("clear-image", 1, 0, 0, clear_image);
1770 scm_make_gsubr ("make-image", 3, 0, 0, make_image);
1774 Here is a sample build and interaction with the code from the
1775 @file{example-smob} directory, on the author's machine:
1778 zwingli:example-smob$ make CC=gcc
1779 gcc `guile-config compile` -c image-type.c -o image-type.o
1780 gcc `guile-config compile` -c myguile.c -o myguile.o
1781 gcc image-type.o myguile.o `guile-config link` -o myguile
1782 zwingli:example-smob$ ./myguile
1784 #<primitive-procedure make-image>
1785 guile> (define i (make-image "Whistler's Mother" 100 100))
1787 #<image Whistler's Mother>
1788 guile> (clear-image i)
1789 guile> (clear-image 4)
1790 ERROR: In procedure clear-image in expression (clear-image 4):
1791 ERROR: Wrong type argument in position 1: 4
1792 ABORT: (wrong-type-arg)
1794 Type "(backtrace)" to get more information.