[plt-scheme] How to define C-structure type with bit fields using FFI?

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Sep 26 14:03:02 EDT 2006

On Sep 26, Nikolay Artamonov wrote:
> Hi all!
> Is it possible to define C-structure type, using FFI, for structure
> with bit fields?


> For example, we have this structure in C-side:
> typedef struct{
>    Uint32 hw_available:1;
>    Uint32 wm_available:1;
>    Uint32 blit_hw:1;
>    Uint32 blit_hw_CC:1;
>    Uint32 blit_hw_A:1;
>    Uint32 blit_sw:1;
>    Uint32 blit_sw_CC:1;
>    Uint32 blit_sw_A:1;
>    Uint32 blit_fill;
>    Uint32 video_mem;
>    SDL_PixelFormat *vfmt;
> } SDL_VideoInfo;
> (This is real structure from SDL library - http://www.libsdl.org)
> And how we can express that structure in our Scheme-wrapper with,
> for example, define-cstruct? Are there direct ways for it, or we
> should write some auxiliary C-code or even MzScheme extension?

You need to specify the actual representation (I'm not sure if C will
use a char or an int for that), then deal with number in Scheme using
the bitwise functions.  The above case looks particularly simple -- it
looks like you can use the _bitmask type to represent the field as a
list of up to 8 symbols.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.