6 Derived Utilities
6.1 Tagged C Pointer Types
The unsafe cpointer-has-tag? and cpointer-push-tag!
operations manage tags to distinguish pointer types.
Construct a kind of a pointer that gets a specific tag when converted
to Scheme, and accept only such tagged pointers when going to C. An
can be given to be used as the base pointer
type, instead of _pointer
Pointer tags are checked with cpointer-has-tag? and changed
with cpointer-push-tag! which means that other tags are
preserved. Specifically, if a base ptr-type is given and is
itself a _cpointer, then the new type will handle pointers
that have the new tag in addition to ptr-type’s tag(s). When
the tag is a pair, its first value is used for printing, so the most
recently pushed tag which corresponds to the inheriting type will be
Note that tags are compared with eq? (or memq), which means
an interface can hide its value from users (e.g., not provide the
cpointer-tag accessor), which makes such pointers un-fake-able.
_cpointer/null is similar to _cpointer except that
it tolerates NULL pointers both going to C and back. Note that
NULL pointers are represented as #f in Scheme, so they
are not tagged.
A macro version of _cpointer
using the defined name for a tag string, and defining a predicate
too. The _id
must start with _
The optional expression produces optional arguments to _cpointer.
In addition to defining _id to a type generated by
_cpointer, _id/null is bound to a
type produced by _cpointer/null type. Finally,
id? is defined as a predicate, and
id-tag is defined as an accessor to
obtain a tag. The tag is the string form of id.
6.1.1 Unsafe Tagged C Pointer Functions
These two functions treat pointer tags as lists of tags. As described
in Pointer Functions
, a pointer tag does not have any
role, except for Scheme code that uses it to distinguish pointers;
these functions treat the tag value as a list of tags, which makes it
possible to construct pointer types that can be treated as other
pointer types, mainly for implementing inheritance via upcasts (when a
struct contains a super struct as its first element).
The cpointer-has-tag? function checks whether if the given
cptr has the tag. A pointer has a tag tag
when its tag is either eq? to tag or a list that
contains (in the sense of memq) tag.
The cpointer-push-tag! function pushes the given tag
value on cptr’s tags. The main properties of this operation
are: (a) pushing any tag will make later calls to
cpointer-has-tag? succeed with this tag, and (b) the pushed tag
will be used when printing the pointer (until a new value is pushed).
Technically, pushing a tag will simply set it if there is no tag set,
otherwise push it on an existing list or an existing value (treated as
a single-element list).
6.2 Safe C Vectors
The cvector form can be used as a type C vectors (i.e., a
pointer to a memory block).
Allocates a C vector using the given type and
Creates a C vector of the given type, initialized to the
given list of vals.
Returns #t if v is a C vector, #f otherwise.
Returns the length of a C vector.
Returns the C type object of a C vector.
Returns the pointer that points at the beginning block of the given C vector.
References the kth element of the cvec C vector.
The result has the type that the C vector uses.
Sets the kth element of the cvec C vector to
val. The val argument should be a value that can be
used with the type that the C vector uses.
Converts the cvec C vector object to a list of values.
Converts the list lst to a C vector of the given
6.2.1 Unsafe C Vector Construction
Constructs a C vector using an existing pointer object. This
operation is not safe, so it is intended to be used in specific
situations where the type and length are known.
6.3 SRFI-4 Vectors
SRFI-4 vectors are similar to C vectors (see
Safe C Vectors), except that they define different types
of vectors, each with a hard-wired type.
An exception is the u8 family of bindings, which are
just aliases for byte-string bindings: make-u8vector,
u8vector. u8vector?, u8vector-length,
, but for vectors of _byte
elements. These are
aliases for byte