[racket] FFI: Casting pointers

From: Michael W (mwilber at uccs.edu)
Date: Sun Jan 22 16:14:47 EST 2012


In the FFI library, is there a way to cast one pointer type
straight to another? I'm working with libvorbisfile (PLaneT
package forthcoming) which expects me to allocate my own
OggVorbis_File cstruct and pass that everywhere.

I ask because (malloc _OggVorbis_File) returns a _pointer, not an
_OggVorbis_File-pointer, and _fun and friends don't accept either
type in place of the other.

If I say:

    (malloc _OggVorbis_File)
    _pointer _OggVorbis_File-pointer)

...That's bad -- because I never keep the original pointer
returned by the (malloc) call, the garbage collector reclaims
that memory even though the _OggVorbis_File-pointer still points
to it.

My current workaround is to just use (malloc _OggVorbis_File)
everywhere and have _pointer types in my (_fun ...) declarations.
Is this the preferred way of doing it?

Is there a way to ask (malloc) to return a different pointer type
than _pointer?

Thanks for your help.

Until an asteroid,

Posted on the users mailing list.