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