[racket] FFI questions

From: Neil Toronto (neil.toronto at gmail.com)
Date: Wed Oct 6 00:05:10 EDT 2010

1. Is there a way to associate a prop:custom-write to _cpointers or 
instances of ctypes defined using define-cstruct? I want to expose C 
data type instances as opaque values, whose printed representation 
constructs them; e.g. I want this to happen:

     > (mpfr-exp (mpfr 1))
     (mpfr #e2.718281828459045235360287471352662497759)

where mpfr constructs instances of _mpfr_t from Racket reals. As it is, 
I'm defining a struct wrapper, which seems like a pointless indirection.

2. There are a few cases where MPFR (the C library) allocates arrays and 
returns pointers to them. I want to interpret them as strings, lists, 
and vectors, but I also have to call "special free" functions to 
deallocate the memory. This is the way I'm doing it (simplified):

(define gimme-string
   (get-ffi-obj 'gimme_string libmpfr (_fun -> _pointer)))

     char* cstr_id(char* p)
         return p;

(define cstr->string
   (get-ffi-obj-from-this 'cstr_id (_fun _pointer -> _string)))

(define cstr (gimme-string))
(define str (cstr->string cstr))
(special-free cstr)

It's silly. But I can't figure out another way to have the FFI library 
interpret a char* pointer as a string AND give me access to the pointer 
itself so I can free the memory. I get the impression that it's 
possible, but I don't know the right incantations yet.

Neil T

(About the @c{...} syntax and get-ffi-obj-from-this: it means what you 
think it does. I'm using a version of Jay's superc package, modified to 
work on Linux. Yes, it's awesome.)

Posted on the users mailing list.