[racket] FFI questions

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Oct 6 00:39:08 EDT 2010

20 minutes ago, Neil Toronto wrote:
> 1. Is there a way to associate a prop:custom-write to _cpointers or
> instances of ctypes defined using define-cstruct? [...]

No, it's just a thin wrapper around the C data.  But you can create
the C type that you need to talk to the library and a racket
struct (with anything you want there).  Make the racket struct hold
the C value in it, and write a ctype for it that translates trivially
to/from the lower level type.


> where mpfr constructs instances of _mpfr_t from Racket reals. As it is, 
> I'm defining a struct wrapper,

Uh, that.

> which seems like a pointless indirection.

(I don't see why, either way you need some pointer going to some
data.)


> 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)))
> 
> @c{
>      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.

Why aren't you using a byte string directly?

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


Posted on the users mailing list.