[racket-dev] ffi vectors

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Thu Sep 30 18:41:29 EDT 2010

I'd like to be able to define ctypes like I would make a typedef in C like

typedef float           float4[4];

But it doesn't seem like this works in the FFI. See the program below
with its awkward work-around:

#lang racket
(require ffi/unsafe
         ffi/unsafe/cvector
         ffi/vector
         tests/eli-tester)

(test
 (ctype-sizeof _float) => 4

 (ctype-sizeof _cvector) => 4
 (ctype-sizeof (_cvector i _float)) => 4
 (ctype-sizeof (_cvector o _float 4)) => (* 4 4)
 (ctype-sizeof (_cvector io _float 4)) => (* 4 4)

 (ctype-sizeof _f32vector) => 4
 (ctype-sizeof (_f32vector i)) => 4
 (ctype-sizeof (_f32vector o 4)) => (* 4 4)
 (ctype-sizeof (_f32vector io 4)) => (* 4 4)

 (local [(define-cstruct _float4
           ([f0 _float]
            [f1 _float]
            [f2 _float]
            [f3 _float]))]
   (test
    (ctype-sizeof _float4) => 16)))

Output is:

test: 5/11 test failures:
unsaved-editor4119:11:1: test failure in (ctype-sizeof (_cvector i _float))
  expected: 4
       got: error: expand: unbound identifier in module
unsaved-editor4119:12:1: test failure in (ctype-sizeof (_cvector o _float 4))
  expected: 16
       got: 4
unsaved-editor4119:13:1: test failure in (ctype-sizeof (_cvector io _float 4))
  expected: 16
       got: error: expand: unbound identifier in module
unsaved-editor4119:17:1: test failure in (ctype-sizeof (_f32vector o 4))
  expected: 16
       got: 4
unsaved-editor4119:18:1: test failure in (ctype-sizeof (_f32vector io 4))
  expected: 16
       got: error: expand: unbound identifier in module

Normally I would just go do this, but I don't really understand the
FFI. If someone can point me appropriately, I'll go do it.

Jay

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay

"The glory of God is Intelligence" - D&C 93


Posted on the dev mailing list.