[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] FFI question
From: |
felix |
Subject: |
Re: [Chicken-users] FFI question |
Date: |
Fri, 16 Aug 2002 09:13:16 +0200 |
> I want to implement an FFI to a C function that returns a pointer to a
> static structure.
>
> So, let's say the C function is this:
>
> struct foo
> {
> int x;
> float y;
> size_t z[3];
> };
>
> foo* foo_func(void)
> {
> static foo f;
>
> f.x = 10;
> f.y = 20;
> f.z[0] = 30;
> f.z[1] = 40;
> f.z[2] = 50;
>
> return &f;
> }
>
> How would I write an FFI to foo_func so that the returns structure pointer
> can be accessed nicely. I suspect I'm going to have to do something like
> this:
>
> >>> (foo_func)
> (10 20 (30 40 50))
(declare
(foreign-declare #<<EOF
struct foo
{
int x;
float y;
size_t z[3];
};
struct foo* foo_func(void)
{
static struct foo f;
f.x = 10;
f.y = 20;
f.z[0] = 30;
f.z[1] = 40;
f.z[2] = 50;
return &f;
}
EOF
) )
(define-record foo x y z)
(define-external (foo_callback (integer x) (float y) (int z0) (int z1) (int
z2)) scheme-object
(make-foo x y (vector z0 z1 z2)) )
(define foo-func
(foreign-callback-lambda* scheme-object ()
"struct foo *fp = foo_func();"
"return(foo_callback(fp->x, fp->y, fp->z[ 0 ], fp->z[ 1 ], fp->z[
2 ]));") )
(let ([f1 (foo-func)])
(print (list (foo-x f1) (foo-y f1) (foo-z f1))) )
> P.S. If the structure returned was dynamic, I suspect I'll have to use
> the set_finalizer! trick I was already taught for the GNU MP interface?
>
Right.
cheers,
felix