scheme-bytestructures icon indicating copy to clipboard operation
scheme-bytestructures copied to clipboard

support for flexible arrays in bs:struct is missing

Open mwette opened this issue 4 years ago • 2 comments

This is related to issue #39.

C99 allows

struct {
  int narg;
  int args[];
};

I have tried `(args ,(fh:pointer int) 0) but (bytestructure-ref xxx 'args) returns "No such struct field. args" (where fh:pointer is from issue #39. Using `(args ,(bs:vector 0 int) does not yield an error, but then not useful for accessing elements of args. My next step is to update fh:pointer to allow 0 size pointer.

mwette avatar Oct 29 '20 13:10 mwette

I'm going back to see if I can make (bs:vector 0 int) work with some changes to the bs:vector implementation.

mwette avatar Oct 29 '20 15:10 mwette

This is very difficult, since the bytevector API doesn't have a thing such as open-ended/variable-length byte vectors; one needs to define a bytevector of a given size before accessing the bytes within the defined region...

Of course, one can always create a new bytevector on-the-fly for every referencing operation for a given index, but that will thrash performance. (This is already a problem with pointer descriptors, which bothers me now that I realized it.)

I have an idea: vector descriptors of size zero could implement a sort of "caching" mechanism: whenever you give them an index, and the cached bytevector is not yet created OR is smaller than the given index, a new bytevector is created that's just big enough to allow using that index.

Disadvantage of that idea: imagine you're iterating through the possible indices into the array. Every index will be one bigger than the last, so the caching is useless; the bytevector is recreated for every iteration anyway. And such iterations are likely to be a common use-case, if not the most common.

Hmm, really not sure what to best do here. It would be rad if Guile had a small and dirty API to directly fetch values from memory addresses, without having to allocate a bytevector object first, but that's unlikely. :D

TaylanUB avatar May 11 '21 16:05 TaylanUB