[racket] FFI Pointing to racket objects

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Mar 4 12:26:26 EST 2013

At Mon, 4 Mar 2013 18:06:10 +0100, "Sam Vervaeck" wrote:
> Just one more question: when implementing malloc-immobile-cell and 
> free-immobile-cell, how do you check if a pointer has not yet been set? Doing 
> things like:
> (if (cpBody-data body1)
>      "I have some data"
>      "I have nothing")
> Always shows "I have nothing".

I expected that the `data' field would have an immutable-cell value ---
which might contain #f but not be #f itself --- so I expected "I have
some data" always.

You should use `ptr-ref' and `pre-set!' to get or change the value in
an immobile cell. Here's an example transcript:

 > (define-cstruct _cpBody ([data _racket]))
 > (define b (make-cpBody (malloc-immobile-cell #f)))
 > (cpBody-data b)
 > (ptr-ref (cpBody-data b) _racket)
 > (ptr-set! (cpBody-data b) _racket 'something)
 > (collect-garbage)
 > (ptr-ref (cpBody-data b) _racket)

Posted on the users mailing list.