Am Sa., 10. Juli 2021 um 15:15 Uhr schrieb Aydar Zarifullin <
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?
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.