[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] A more detailed plea for help with FFI...

From: Ed Watkeys
Subject: [Chicken-users] A more detailed plea for help with FFI...
Date: Tue, 14 Dec 2004 22:46:50 -0500


I've been looking through various eggs, trying to find, to no avail, an example of the following...

Just say I have a C struct, like this:

struct whatever {
        FILE *fp;

I want to create a procedure visible to Scheme code that allocates the struct, opens a file, and places the file pointer in fp. I want Scheme to assume control of the object, and call my procedure that closes the file associated with fp before Scheme frees the memory. Essentially, I want to be able to specify a destructor.

This is something, suprisingly, has been far easier to deal with in Gambit than in Chicken. In Gambit (4.0b11), I can do something like this:

(c-declare "
        static ___SCMOBJ release_whatever(void *w) {
                return __FIX(__NO_ERR);

        (struct "whatever" whatever "release_whatever"))

(define whatever:make
        (c-lambda (char-string) whatever "
                struct whatever w;
                w.fp = fopen(___arg1, \"r\");
                ___result_voidstar = &w;"))

Now, of course, I'm running into problems with Gambit which have me pulling out my hair, so I'm running back to the warm embrace of Chicken.

What I do not want to do is require that user Scheme code do resource management and remember that it created an object and now needs to dispose of it. I consider that Pure Evil. Is there a way to accomplish what I'm trying to do in Chicken?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]