On 2/5/07, Tony Sidaway <[EMAIL PROTECTED]> wrote:
>
> Would it be best to write something to do that in C as a
> foreign-primitive?  Basically a C function, declared
> foreign-primitive, that takes a C-pointer and an int, and allocates a
> byte vector of the appropriate size.  Presumably this would eat into
> the nursery until such time as a minor garbage collection takes place.

I suppose I mean something like this:

(define produce-byte-vector
  (foreign-primitive scheme-object
    ((int size) (c-pointer data))
    "C_word *p =
C_alloc(sizeof(C_header)+C_align(size));C_return(C_bytevector(&p,
size, data));"))

I expect I've got the arithmetic wrong, but this is the basic idea.

This is a nice easy implementation because you can then use
(byte-vector->u8vector), (byte-vector->s16vector) or whatever you want
to coerce the resulting byte vector to whatever you want.


Yes, that should work (with the caveat you mentioned: if the size of the
data is large or the nursery is already used up, the latter might not be
sufficiently big). An alternative is to create a suitably sized
byte-vector beforehand
(in Scheme, using the heap) and copying the data inside a block of foreign
code (or use "move-memory!").


cheers,
felix


_______________________________________________
Chicken-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to