[plt-scheme] Using SRFI-4 from C

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Mar 6 12:52:02 EST 2005

On Mar  6, Noel Welsh wrote:
> --- Eli Barzilay <eli at barzilay.org> wrote:
> > You don't need any headers -- just use plain C vectors.
> Right, but the layout isn't just a plain old C vector.  For
> example, when I create
>   (define code
>     (u32vector #x38600003 #x4e800020))
> and print out the first *6* int values from the vector on
> the C side I get:
>   code: 210000 22e300 6928f4 5 500000 2
> Clearly not the same.
> So there are some tags I need to skip?

There is nothing to skip in C -- what happens is that you do use plain
C vectors in C code, and these are used in Scheme via a cpointer
object that is a wrapper to the actual pointer to your vector (so
there's a double reference there).  What usually happens is that you
write plain C functions that work with vector pointers, and use the
right foreign type so that when these pointer values are going from C
to Scheme and back the cpointer objects are created and dereferenced
as needed.

If you want to write C code that allocates a Scheme pointer object,
then you could create a cpointer object with the right pointer value
in it, using scheme_make_cptr.  The thing is that to implement srfi-4
vectors, these cpointers are further wrapped in a plain (Scheme)
struct, one for each srfi-4 type, that holds the size of the C vector.
Doing this wouldn't be hard, but generally it's just easier to leave C
to deal with plain C vectors, and srfi-4 values in Scheme code.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.