[plt-scheme] FFI and pointer manipulations

From: Jim Blandy (jimb at red-bean.com)
Date: Sun Feb 11 10:45:25 EST 2007

Many systems with copying GC's represent "large" objects (big strings;
long vectors) as small header objects that hold a pointer to the large
body of the object.  The header is allocated in the ordinary heap, and
does get copied by collections; the body is allocated elsewhere, and
stays put.

This is usually implemented as a speed optimization for the GC, but
once you've gone to the trouble of handling that representation, it
almost certainly doesn't matter whether the object is actually large
or not; the programmer could explicitly request that any string or
vector be represented this way, making an object's "mobility" a
programmer-visible aspect of its type.

I'll agree immediately that this is not beautiful; you now have a
special subtype of strings and vectors, with a new class of errors to
go along with it.  But once you have chosen to interact with C, it
inevitably follows that whether an object is mobile or not is a
necessary property to attend to.  It's as important to writing correct
code as its length or its address.

If the programmer can't designate particular objects as immobile, then
it seems to me she must depend on some even-harder-to-use property,
like "GC never runs while in C code, unless you (or any of your
callees) ever calls any of these functions: ...".

With more sophistication in the tagging, the FFI could even ensure
that only immoble objects' addresses are passed to C code.  So you'd
get a type-check exception, and not a crash.

Posted on the users mailing list.