[plt-scheme] ANN: c.plt (C manipulation and FFI utilities)

From: Dave Herman (dherman at ccs.neu.edu)
Date: Mon Feb 2 17:34:21 EST 2009

I've just posted a new PLaneT package called c.plt:

http://planet.plt-scheme.org/package-source/dherman/c.plt/1/0/planet-docs/c/index.html

This may be of interest to anyone working with the FFI. You can use it 
to parse header info and query a system C compiler to get exact data 
structure sizes and byte offsets for struct fields etc. Example:

 > (define time.h
     (make-header
       #reader (planet dherman/c/reader) {
           struct tm {
               int tm_sec;
               int tm_min;
               int tm_hour;
               int tm_mday;
               int tm_mon;
               int tm_year;
               int tm_wday;
               int tm_yday;
               int tm_isdst;
           };
       }))
 > (define time-abi
     (compile-header time.h
       (system-compiler #:include<> '("time.h") gcc)))
 > (layout-size (time-abi 'tm))
36
 > (layout-offset (time-abi 'tm) 'tm_sec)
0
 > (layout-offset (time-abi 'tm) 'tm_year)
20

The first version is available but pretty rough. There's lots of missing 
functionality. I'm happy to accept requests and bug reports.

Dave


Posted on the users mailing list.