to represent this object within Chicken. Unfortunately, if we do that, the procedures body-m, body-q, etc, will expect to have foreign pointer objects which contain a pointer to a body object. But, this body object must be allocated outside the GC-managed heap, because the pointer in a foreign-pointer is not scanned during garbage collection. To automatically manage such storage, we must use set-finalizer! (see Unit library), and if we want to create 1M bodies, we'll need 1M finalizers, which won't make Chicken very happy.

So, we'll have to do things by hand. First, let's define the body tag. Since bodies will be fixed-size bytevectors, we'll use

Note the return type of scheme-object, since a body is a scheme object. Note also, that we allocate result on the stack, and "return" the address of result at the end of the function. (The foreign-primitive actually never returns; it calls its continuation with the return value.)

We can define a body? predicate. This predicate is not exact---it cannot distinguish between bodies and strings of the same length---but there is no way to make it exact with Chicken's memory model as it stands now.

Allocating non-atomic data

"Non-atomic" data are data which contain only pointers. Suppose we have a binary tree datastructure which can store bodies from the above example (contrived, I know, but it should illustrate the technique):

Note that, for non-bytevector objects, the size part of the header is the size in C_words, not bytes. From here, things follow the examples in the last section.

Allocating mixed data

You can't. Sorry. Break your data up into one atomic struct which stores all the non-pointers, and then a "pointer" struct which stores a pointer to the atomic data, and all other pointers involved in your datastructure.