chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Chicken C interface


From: Kristian Lein-Mathisen
Subject: Re: [Chicken-users] Chicken C interface
Date: Thu, 6 Jun 2013 11:46:28 +0200




On Thu, Jun 6, 2013 at 11:34 AM, Thomas Chust <address@hidden> wrote:
On 2013-06-05 23:36, Felix wrote:
> From: Dan Leslie <address@hidden>
>> [...]
>> Basically, use C_alloc to allocate the memory required to host both
>> the List structure and the data it is to contain, then use the C_list
>> macro to patch it all together.
>
> Note that this code is not correct: C_alloc allocates on the C stack and the
> data will be invalid once the function returns (Sorry). If this works, then
> it is just coincidental!
> [...]

Hello,

when I first saw that code I thought that this must be incorrect, too.
Then I checked the CHICKEN documentation for foreign-safe-lambda and read:

  "This is similar to foreign-lambda, but also allows the called
   function to call Scheme functions and allocate Scheme data-objects."

Now I'm confused. Of course C_alloc allocates on the stack and of course
this can likely break if a function just returns some pointer to stack
allocated data. However C_return could magically copy the return value
to the second generation heap or similar trickery to actually make
foreign-safe-lambda and C_alloc interoperate correctly or one could
perhaps use a special call ABI to prevent stack corruption upon return
from a foreign-safe-lambda and salvage stack allocated objects. Is any
such strategy actually implemented?

From what I understand, this is exactly what foreign-primitive does: wraps C_return in a CPS, keeping the stack-allocation alive. I posted a question on a similar topic a while back: http://lists.gnu.org/archive/html/chicken-users/2012-03/msg00034.html

K.

 

But maybe the documentation is just misleading and wanted to say
something about temporary allocation for the lifetime of the function
instead.

Ciao,
Thomas


--
When C++ is your hammer, every problem looks like your thumb.


_______________________________________________
Chicken-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-users


reply via email to

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