[cffi-devel] shareable-byte-vectors for Allegro
John Fremlin
jf at msi.co.jp
Mon Jan 19 05:50:06 UTC 2009
"Luís Oliveira" <luismbo at gmail.com> writes:
> On Mon, Jan 19, 2009 at 2:35 AM, John Fremlin <jf at msi.co.jp> wrote:
>> Duane at Franz has helpfully come up with a decent way to get the real
>> foreign address from a Lisp array.
>>
>> (let ((x (make-array 10 :element-type '(unsigned-byte 8) :allocation :static-reclaimable)))
>> (ff:fslot-address-typed :unsigned-char :lisp x))
>>
>> We will test a modification of with-shareable-byte-vector to use this
>> mechanism for ,ptr-var, hopefully it will work well.
>
> What's the advantage, compared to using :FOREIGN-ADDRESS?
We can continue to use foreign-address, so that people will be able to
(unportably) take advantage of Allegro's generous handling of Lisp arrays.
The advantage of the change is that ptr-var is now actually a foreign
pointer as it is in all other cffi-ports.
You might not want to pass the ptr-var directly to a function, but
actually use it as a foreign pointer.
For a concrete example, using the recvmsg(2) system call, you pass a
structure containing an iovec, which is itself a structure containing
the base address and length of the buffer into which the message body
should be written.
We want this buffer to be a shareable byte-vector array.
Here is part of a real example
(cffi:with-foreign-objects ((sockaddr-buf :unsigned-char +sockaddr-max-len+)
(iov-buf 'iovec)
(msghdr-buf 'msghdr)
(cmsg-buf :unsigned-char +cmsg-buf-len+))
(cffi-sys:with-pointer-to-vector-data (ptr buf)
(cffi:with-foreign-slots ((base len) iov-buf iovec)
(setf base ptr
len (length buf)))
...
(recvmsg (socket-fd socket)
msghdr-buf
(logior MSG_DONTWAIT MSG_ERRQUEUE))
))
More information about the cffi-devel
mailing list