[plt-scheme] Manipulating Scheme vectors with FFI

From: Jakub Piotr Cłapa (jpc-ml at zenburn.net)
Date: Sun Nov 2 07:49:03 EST 2008

Ernie Smith wrote:
> /* vector inside structure.  How to declare the same in Scheeme?  */
> struct { int __val[ 2 ]; } ;  /* vector embedded in struct */

This is impossible because of libffi limitations. You can workaround by 
defining a custom type which is a struct with N fields and custom access 
methods. See [1] for an example:
    (define _path-type
      (make-ctype (make-cstruct-type (build-list (/ usb-max-path-len 8)
                                                 (lambda (i) _uint64)))
                  #f
                  (lambda (ptr)
                    (let ([v (make-bytes usb-max-path-len)]
                          [length #f])
                     (let loop ([i 0])
                        (let ([value (ptr-ref ptr _uint8 'abs i)])
                          (if (or (= i usb-max-path-len) (= value 0)) 
(set! length i)
                              (begin
                                (bytes-set! v i value)
                                (loop (add1 i))))))
                      (subbytes v 0 length)))))


[1] http://list.cs.brown.edu/pipermail/plt-scheme/2007-March/016671.html

-- 
regards,
Jakub Piotr Cłapa


Posted on the users mailing list.