Can the Gambit FFI return a Scheme numeric vector that is a window into
an existing C array? (I.e. the Scheme object header specifies the base
and length of the C array but the array elements are not copied.)
For example, can it wrap the following C function
const void *get_some_data(size_t *size) {
const char *str = "hello world";
*size = strlen(str);
return str;
}
such that (get-some-data) returns #u8(104 101 108 108 111 32 119 111 114
108 100) and the contents of that bytevector are not a copy?
There are two interesting cases to consider: one where the C array is
constant, and one where its elements can be modified. (In both cases, a
simple object header pointing to the array only makes sense when the
array cannot shrink and cannot be freed. This is similar to slices in
Go, and displaced arrays in Common Lisp.)
Skimming gambit.h, there are:
#define ___U8VECTORREF(v,i) \
___FIX(___FETCH_U8(___BODY_AS(v,___tSUBTYPED),(i)>>___TB))
#define ___BODY_AS(obj,tag)___BODY0_AS(obj,tag)
#define ___BODY0_AS(obj,tag)(___UNTAG_AS(obj,tag)+___REFERENCE_TO_BODY)
#define ___UNTAG_AS(obj,tag)___CAST(___WORD*,(obj)-(tag))
#define ___FETCH_U8(base,i)*(___CAST(___U8*,base)+(i))
#define ___REFERENCE_TO_BODY 1
It looks to me like a vector's body is stored directly after the header,
but I'm not sure I deciphered the pointer indirection correctly.