Next: Procedures and Macros, Previous: Compound Data Types, Up: API Reference
This chapter contains reference information related to defining and working with smobs. See Defining New Types (Smobs) for a tutorial-like introduction to smobs.
This function adds a new smob type, named name, with instance size size, to the system. The return value is a tag that is used in creating instances of the type.
If size is 0, the default free function will do nothing.
If size is not 0, the default free function will deallocate the memory block pointed to by
SCM_SMOB_DATA
withscm_gc_free
. The WHAT parameter in the call toscm_gc_free
will be NAME.Default values are provided for the mark, free, print, and equalp functions, as described in Defining New Types (Smobs). If you want to customize any of these functions, the call to
scm_make_smob_type
should be immediately followed by calls to one or several ofscm_set_smob_mark
,scm_set_smob_free
,scm_set_smob_print
, and/orscm_set_smob_equalp
.
This function sets the smob marking procedure for the smob type specified by the tag tc. tc is the tag returned by
scm_make_smob_type
.The mark procedure must cause
scm_gc_mark
to be called for everySCM
value that is directly referenced by the smob instance obj. One of theseSCM
values can be returned from the procedure and Guile will callscm_gc_mark
for it. This can be used to avoid deep recursions for smob instances that form a list.It must not call any libguile function or macro except
scm_gc_mark
,SCM_SMOB_FLAGS
,SCM_SMOB_DATA
,SCM_SMOB_DATA_2
, andSCM_SMOB_DATA_3
.
This function sets the smob freeing procedure for the smob type specified by the tag tc. tc is the tag returned by
scm_make_smob_type
.The free procedure must deallocate all resources that are directly associated with the smob instance OBJ. It must assume that all
SCM
values that it references have already been freed and are thus invalid.It must also not call any libguile function or macro except
scm_gc_free
,SCM_SMOB_FLAGS
,SCM_SMOB_DATA
,SCM_SMOB_DATA_2
, andSCM_SMOB_DATA_3
.The free procedure must return 0.
This function sets the smob printing procedure for the smob type specified by the tag tc. tc is the tag returned by
scm_make_smob_type
.The print procedure should output a textual representation of the smob instance obj to port, using information in pstate.
The textual representation should be of the form
#<name ...>
. This ensures thatread
will not interpret it as some other Scheme value.It is often best to ignore pstate and just print to port with
scm_display
,scm_write
,scm_simple_format
, andscm_puts
.
This function sets the smob equality-testing predicate for the smob type specified by the tag tc. tc is the tag returned by
scm_make_smob_type
.The equalp procedure should return
SCM_BOOL_T
when obj1 isequal?
to obj2. Else it should return SCM_BOOL_F. Both obj1 and obj2 are instances of the smob type tc.
When val is a smob of the type indicated by tag, do nothing. Else, signal an error.
Return true iff exp is a smob instance of the type indicated by tag. The expression exp can be evaluated more than once, so it shouldn't contain any side effects.
Make value contain a smob instance of the type with tag tag and smob data data, data2, and data3, as appropriate.
The tag is what has been returned by
scm_make_smob_type
. The initial values data, data2, and data3 are of typescm_t_bits
; when you want to use them forSCM
values, these values need to be converted to ascm_t_bits
first by usingSCM_UNPACK
.The flags of the smob instance start out as zero.
Since it is often the case (e.g., in smob constructors) that you will create a smob instance and return it, there is also a slightly specialized macro for this situation:
This macro expands to a block of code that creates a smob instance of the type with tag tag and smob data data, data2, and data3, as with
SCM_NEWSMOB
, etc., and causes the surrounding function to return thatSCM
value. It should be the last piece of code in a block.
Return the 16 extra bits of the smob obj. No meaning is predefined for these bits, you can use them freely.
Set the 16 extra bits of the smob obj to flags. No meaning is predefined for these bits, you can use them freely.
Return the first (second, third) immediate word of the smob obj as a
scm_t_bits
value. When the word contains aSCM
value, useSCM_SMOB_OBJECT
(etc.) instead.
Set the first (second, third) immediate word of the smob obj to val. When the word should be set to a
SCM
value, useSCM_SMOB_SET_OBJECT
(etc.) instead.
Return the first (second, third) immediate word of the smob obj as a
SCM
value. When the word contains ascm_t_bits
value, useSCM_SMOB_DATA
(etc.) instead.
Set the first (second, third) immediate word of the smob obj to val. When the word should be set to a
scm_t_bits
value, useSCM_SMOB_SET_DATA
(etc.) instead.
Return a pointer to the first (second, third) immediate word of the smob obj. Note that this is a pointer to
SCM
. If you need to work withscm_t_bits
values, useSCM_PACK
andSCM_UNPACK
, as appropriate.