[gambit-list] Allocate Scheme vector before or after C call?

Lassi Kortela lassi at lassi.io
Sat Jul 18 09:28:06 EDT 2020


Especially thinking of the common C idiom where we don't know the right 
buffer size in advance, and keep trying increasing powers-of-two until 
we find one that works. Pseudo-code:

for (n = 32; n < size_limit; n *= 2) {
     ___SCMOBJ vector = ___EXT(___alloc_scmobj)(NULL, ___sFooVECTOR, n * 
sizeof(foo_t));
     foo_t *body = ___CAST(foo_t *, ___BODY(vector));
     int result = get_value_from_some_c_api(body, n);
     ___EXT(___release_scmobj)(vector);
     if (result == success) {
         ___return(vector);
     } else if (result != buffer_too_small) {
         ___return(___FAL);
     }
     // dispose of the vector here
}
___return(___FAL);


Another option would be to do the above loop using standard C realloc(), 
but then have a Gambit API that "takes ownership" of a buffer that comes 
from malloc() and adds a Scheme object header to it. Does that kind of 
approach make any sense in Gambit?




More information about the Gambit-list mailing list