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

Lassi Kortela lassi at lassi.io
Wed Jul 29 08:36:39 EDT 2020


Getting back to this; sorry about the delay.

Thanks for taking the time to write the detailed code examples. 
vector-shrink! and string-shrink! are also useful.

I've explored the FFI some more. There are lots of C APIs that return a 
vector of some datatype that would be useful to bring over to Scheme. 
All of the following are useful:

- collect N-bit integers into a buffer in C, possibly truncate, turn 
into a Scheme homogeneous numeric vector

- collect N-bit integers into a buffer in C, possibly truncate, turn 
into a generic Scheme vector (i.e. the elements are converted from C 
integers into Scheme fixnums, some might become bignums instead)

- collect char* string into a buffer in C, possibly truncate, turn into 
a Scheme string

- collect WCHAR string into a buffer in C, possibly truncate, turn into 
a Scheme string

When collecting C integers to be turned into fixnums later, would it 
make sense to pre-allocate a Scheme vector with space in its body for (* 
capacity (max size-of-that-c-integer-type size-of-a-scheme-value)) 
bytes? Then fill the buffer and do an in-place conversion from C 
integers to fixnums. If this works, we wouldn't have to allocate a 
separate vector for the fixnums.




More information about the Gambit-list mailing list