chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Re: FFI to libgmp questions


From: Peter Keller
Subject: Re: [Chicken-users] Re: FFI to libgmp questions
Date: Sat, 29 Jun 2002 10:54:59 -0500
User-agent: Mutt/1.2i

On Sat, Jun 29, 2002 at 12:35:05PM +0200, felix wrote:
> Hello, Peter!

Hello,

> `C_alloc()' allocates memory on the C-stack. After returning from
> this procedure, the memory is invalid! `C_alloc()' should only be called
> in CPS-converted code, i.e. that was Chicken produces.
> Another thing is that a value of return-type `c-string' is always copied,
> so the `arena' buffer above will be copied again, leaking memory again
> (if we would have used `mallloc()' instead of `C_alloc()').

So I don't understand the difference between my implementation above and yours.
(Though I accept your version and will change my code to use it).

In your version, it is just "given" that the c-string is copied into
memory and I see now what you meant by doing that. However, wouldn't my
implementation do the same thing? The memory would have been declared
on the stack then during the return will have been copied into the gc,
and then the stack frame goes away taking the C_alloc memory with it.

I'm not arguing or anything, I just want to really understand the semantics
of these calls.

> (define c-pointer->c-string
>   (foreign-lambda* c-string ((c-pointer ptr)) "return(ptr);") )
> 
> (define mpz_get_str
>   (let ([alloc (foreign-lambda c-pointer "mpz_get_str" pointer int mpz_t)]
> [free (foreign-lambda void "free" c-pointer)] )
>     (lambda (ptr base mpz)
>       (let ([c-ptr (alloc ptr base mpz)])
> (if (not ptr)
>      (let ([c-str (c-pointer->c-string c-ptr)])
>        (free c-ptr)
>        c-str)
>      c-ptr) ) ) ) )
> 

Don't you mean:
>      c-ptr) ) ) ) )
to be:
>        ptr) ) ) ) )

If not, then why not? When I do that, I get back a pointer-like object
in chicken instead of a string.

> This is a little bit confusing. Sorry.

It is ok. Maybe through the efforts of people on this mailing list, the manual
can be fleshed out somewhat to hold more wisdom about the FFI.

> P.S. External enums can be quite simply accessed, using
> `define-foreign-variable':
> 
> (define-macro (define-foreign-enum . items)
>   `(begin
>       ,@(map
>                 (match-lambda
>                     [(name realname) `(define-foreign-variable ,name int 
> ,realname)]
>                     [name `(define-foreign-variable ,name int)] )
>                 items) ) )
> 
> ; so for "enum {abc=3, def};" we would have:
> 
> (define-foreign-enum abc def)

Can this be done in the R5RS define-syntax, etc, system?

Thanks.

-pete



reply via email to

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