[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] C_alloc OK before C_invoke? (was Re: Constructing pa
From: |
Daniel B. Faken |
Subject: |
Re: [Chicken-users] C_alloc OK before C_invoke? (was Re: Constructing parameter lists in C) |
Date: |
Fri, 22 Jul 2005 11:41:10 -0400 (EDT) |
On Fri, 22 Jul 2005, felix winkelmann wrote:
> On 7/21/05, Daniel B. Faken <address@hidden> wrote:
> >
> > On a related note: is it OK to call C_alloc() to construct data to be
> > passed via CHICKEN_invoke()?
>
> Yes, that should work.
>
> > The manual entry for C_alloc() says "..Note that stack-allocated data
> > objects have to be passed to Scheme callback functions... This is really
> > only usable for callback procedure invokations, make sure not to use it in
> > normal code..."
> > - but I'm not sure if the point is to not re-use C_alloc() memory or if
> > this really means 'only proper callbacks' i.e. after entering scheme.
>
> What this means is that the storage used for the Scheme data may not be
> popped from the stack if it hasn't been copied/gc'd into the major
> heap yet. This may happen if you C_alloc in normal C procedures (which
> subsequently return). CHICKEN_invoke runs in proper CPS mode, and
> allocates merrily on the stack, so passing stack-allocated data to it should
> be fine.
OK.
>From what I see in chicken.h, we have:
# define C_alloca alloca
#define C_alloc(n) ((C_word *)C_alloca((n) * sizeof(C_word)))
And as I undestand, alloca() is the same as allocating things on the stack
directly, via C variable declarations.
So does this mean instead of (for example):
C_word *data = C_alloc(N*C_SIZEOF_FLONUM);
C_word one = C_flonum(&data, 1.0);
C_word two = C_flonum(&data, 2.0);
C_word three = C_flonum(&data, 3.0);
etc.
I could do
C_word realdata[N*C_SIZEOF_FLONUM];
C_word *data = realdata;
C_word one = C_flonum(&data, 1.0);
C_word two = C_flonum(&data, 2.0);
etc.
??
That is to say:
* is C_alloc() just a convenient way of avoiding getting the compiler to
understand &realdata (above) as a pointer-to-a-pointer?
(not sure if this is an ANSI-C issue - just know my compiler thought
&realdata was a pointer-to-a-C_word).
* are the issues of when to use C_alloc()-ated data identical to those
for the storing of data in the C stack?
- or is there more complexity hiding somewhere?
Maybe this should be obvious.. but the documentation for C_pair(),
C_list(), etc. saying "ptr should be the address of an allocation pointer
created with C_alloc" made it seem like it was doing something special
(beyond what just declaring the memory in a C local-variable would do).
If not, maybe this could be clarified in the documentation.
Again, thanks!
cheers,
Daniel