chicken-users
[Top][All Lists]
Advanced

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

Re: Blobs and GC


From: Aydar Zarifullin
Subject: Re: Blobs and GC
Date: Sat, 10 Jul 2021 17:49:59 +0400

Thank you

On Sat, Jul 10, 2021 at 5:28 PM Thomas Chust <thomas.chust@gmail.com> wrote:
Am Sa., 10. Juli 2021 um 15:15 Uhr schrieb Aydar Zarifullin <aydar.js@gmail.com>:
Hello everyone, I have a few questions:  Is it safe to pass a blob created in scheme to C functions? Will the garbage collector move it in memory? Is there any guarantee that the garbage collector won't move it in the middle of a C function? Is the situation the same with bytevectors?

Hello Aydar,

the answer to each of these questions is "yes", I believe ☺

To pass the contents of a non-immediate Scheme object to to a C function through the FFI, use the type [nonnull-]scheme-pointer. Unless it is a SRFI-4 vector you want to pass, in that case you need to use a type specific for the vector type in question.

Since CHICKEN is essentially single-threaded, as far as the operating system is concerned, once you pass control to a C function through the FFI, no Scheme code or CHICKEN support code is running in the background, hence a pointer to the inside of a scheme object stays stable during one foreign function call, but not necessarily across multiple foreign function calls. So the long answer to your first question is really that it depends on what the C function is doing with the pointer: If the function accesses something at the pointer address and then returns, all is well. If the pointer is remembered somewhere and used later on during another foreign function call, that is not safe.

Ciao,
Thomas

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

reply via email to

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