[racket] FFI questions

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

Eli Barzilay wrote:
> 20 minutes ago, Neil Toronto wrote:
> 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.)

It looks like this:

(struct bigfloat (mpfr-value)
         #:property prop:custom-write ...)

I have a struct type with just one field. Every time I'm forced into 
doing that, it seems like a pointless indirection. (It's *only* an 
indirection; I'm not aggregating anything.) But if that's the only way, 
I'm fine with it.

>> (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?

Let me sum up.

The C library provides gimme_string (actually something with a more 
complex type signature), which allocates a char* in some special way and 
returns it. The library also provides special_free, which takes a char* 
and frees it in a special way.

I need to call gimme_string and use the return value as a string, but 
also have the return value as a pointer so I can pass it to 
special_free. If I get gimme_string with (_fun -> _string) or (_fun -> 
_bytes), I don't get a pointer, I get a string? or a bytes?.

Neil T

Posted on the users mailing list.