Next: , Previous: Smob Cells, Up: Data Types


6.1.9 Defining Smobs

Here is an example of how to add a new type named foo to SCM. The following lines need to be added to your code:

long tc16_foo;
The type code which will be used to identify the new type.
static smobfuns foosmob = {markfoo,freefoo,printfoo,equalpfoo};
smobfuns is a structure composed of 4 functions:
          typedef struct {
            SCM   (*mark)P((SCM));
            sizet (*free)P((CELLPTR));
            int   (*print)P((SCM exp, SCM port, int writing));
            SCM   (*equalp)P((SCM, SCM));
          } smobfuns;
smob.mark
is a function of one argument of type SCM (the cell to mark) and returns type SCM which will then be marked. If no further objects need to be marked then return an immediate object such as BOOL_F. The smob cell itself will already have been marked. Note This is different from SCM versions prior to 5c5. Only additional data specific to a smob type need be marked by smob.mark.

2 functions are provided:

markcdr(ptr)
returns CDR(ptr).
mark0(ptr)
is a no-op used for smobs containing no additional SCM data. 0 may also be used in this case.

smob.free
is a function of one argument of type CELLPTR (the cell to collected) and returns type sizet which is the number of malloced bytes which were freed. Smob.free should free any malloced storage associated with this object. The function free0(ptr) is provided which does not free any storage and returns 0.
smob.print
is 0 or a function of 3 arguments. The first, of type SCM, is the smob object. The second, of type SCM, is the stream on which to write the result. The third, of type int, is 1 if the object should be writen, 0 if it should be displayed, and 2 if it should be writen for an error report. This function should return non-zero if it printed, and zero otherwise (in which case a hexadecimal number will be printed).
smob.equalp
is 0 or a function of 2 SCM arguments. Both of these arguments will be of type tc16foo. This function should return BOOL_T if the smobs are equal, BOOL_F if they are not. If smob.equalp is 0, equal? will return BOOL_F if they are not eq?.

tc16_foo = newsmob(&foosmob);
Allocates the new type with the functions from foosmob. This line goes in an init_ routine.

Promises and macros in eval.c and arbiters in repl.c provide examples of SMOBs. There are a maximum of 256 SMOBs. Smobs that must allocate blocks of memory should use, for example, must_malloc rather than malloc See Allocating memory.