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 18:54:05 -0500
User-agent: Mutt/1.2i

Hello,

On Sat, Jun 29, 2002 at 11:31:21PM +0200, felix wrote:

[ snip ]

> Since Chicken generates CPS code, the stack grows at every
> call. The `C_trace()' invocation here might already write into the 
> stack-space.
> But the invocation of the copier (`##sys#peek-c-string') will definitely
> overwrite it.

Ok, I think after careful inspection of the code you have provided,
I understand how my example did not work. The only reason I even went down
this road in the first place was because the FFI examples in the manual use
C_alloc, so I was led to believe it should be more commonly used than what
it should. Also, C_alloc isn't explained very well in the manual(in fact, not
too many of the C_* calls are--especially what context to use them in).

> BTW, I will add two new foreign type specifiers:
> 
> (c-pointer X)
>   The same as (pointer X), but more consistent, because `pointer'
>   is not the same as `c-pointer'.

I don't understand, I thought you already had this. Page 32 of the pdf manual
talks about it under "Foreign type specifers".

> c-string*
>   The same as `c-string', but will `free()' the returned ptr as soon as
>   the contents have been copied.

This brings up an interesting question.

Is there a generic way to convert a C pointer(to a dynamic piece of memory which
might or might not be an array of similar object) to any type into the
scheme equivalent and free the memory too?

For example, suppose I have a function:

struct foo* make_foo_array(int num);

This will allocate an num sized array of type struct foo and return
it. How do I cleanly represent this array in terms of the gc and the scheme
type that it will eventually become? The copier function pointer->c-string
made a lot of sense because it automatically copied the zero-delimited
string into the garbage collector and made it a "c-string" type which
was understood in the scheme code to copy the result into the gc.

How is this done for a function like make_foo_array?

I'm sorry for all of the questions, but I'm a little confused as to when
it is ok to use set-finalizer! and when you have to write the copy and
free yourself. 

> (define-syntax define-foreign-enum
>   (syntax-rules ()
>     [(_ "1" (name realname))
>      (define-foreign-variable name int realname) ]
>     [(_ "1" name)
>      (define-foreign-variable name int) ]
>     [(_ item ...)
>      (begin (define-foreign-enum "1" item) ...) ] ) )

Wonderful! Thanks.

I _really_ appreciate the time that you all are spending with me as I figure
this stuff out.

Thank you.

-pete






reply via email to

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