[plt-scheme] ffi equivalent of array in struct

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Apr 24 22:57:09 EDT 2006

On Apr 24, Danny Yoo wrote:
> This seems repetitive, so maybe a macro will help.

This is not a problem that should be solved by a macro.  (And once you
start going that way, it's a classic trap that keeps you from doing
the simple thing.)  If all you had was define-cstruct, then you would
need a macro to do generate things like

  (define-cstruct _Foo [x1 _int] [x2 _int] ... [x100 _int])

But in this case, make-cstruct-type is available, and can do something
equivalent to that, with no need for macros at all.  All you need is
to create a list of 100 _int values, like

  (build-list 100 (lambda (i) _int))

using (lib "etc.ss"),

  (make-list 100 _int)

using srfi 1,

  (list-of _int [i <- 1 .. 100])

using swindle, or whatever.

>      (define 65536bytes (n-bytes 65536))
> without too many problems.

Especially if all you need is a chunk of memory, you should just use
malloc with the number of bytes that you want.  Much better then doing
all that work (including the creation of a libffi type descriptor that
has 65536 copies of the same _int8 types).
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.