chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Re: object-evict, string ports, safe-foreign-wrapper, f


From: Tony Sidaway
Subject: [Chicken-users] Re: object-evict, string ports, safe-foreign-wrapper, foreign-primitive, Cheney on the Victoria Line, etc (was: What happens to a (non-simple) Scheme object sent to a foreign function?)
Date: Mon, 5 Feb 2007 00:04:51 +0000

On 2/4/07, Tony Sidaway <address@hidden> wrote:

Another problem that it would be helpful to have advice on is how to
cast or coerce arbitrary data received in a Chicken
safe-foreign-wrapper.  Say I receive a foreign c-pointer to some data
and a couple of parameters nmemb and size that when multiplied
together tell me the number of bytes in the foreign object, it would
be nice to have a Schemish way of converting that into a SRFI-4
u8vector of size (* nmemb size).  I've examined locations and
locatives, but those don't seem to help with that particular problem
not least because there is no underlying Scheme data object, only a
random bin containing binary bits whose significance is completely
unknown to Scheme.

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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]