Subject:
Re: [ruby-ffi] Bit-fields in automatic struct layout?
From:
Evan Phoenix
Date:
12/2/09 12:43 PM
To:
ruby-ffi@googlegroups.com


On Dec 2, 2009, at 10:07 AM, Simon Chiang wrote:

> I'm trying to convert the following struct into an FFI::Struct using
> the struct generator:
> 
>    struct symbolHashNode
>      {
>       struct symbolHashNode *next;
>       long count;
>       int depth;
>       unsigned int permanent : 1;
>       unsigned int markedEphemeral : 1;
>       unsigned int neededSymbol : 1;
>       unsigned int bucket : 29;
>       char *contents;
>      };
> 
> My question is, how do I specify the bit-fields?  When I do the naive
> thing:

Bitfields are not supported and there is no current desire to do so.

You should declare there to be a single :uint field for all 4 of those and do your own bit manipulation to extract the bits you need.

 - Evan

> 
>    class SymbolHashNode < FFI::Struct
>      @@@
>      struct do |s|
>        s.name 'struct symbolHashNode'
>        s.include 'symbol.h'
> 
>        s.field :next, :symbolHashNode
>        s.field :count, :long
>        s.field :depth, :int
>        s.field :permanent, :uint
>        s.field :markedEphemeral, :uint
>        s.field :neededSymbol, :uint
>        s.field :bucket, :uint
>        s.field :contents, :string
>      end
>      @@@
>    end
> 
> I get errors like:
> 
>  attempt to take address of bit-field structure member ‘permanent’
>  ‘sizeof’ applied to a bit-field
> 
> Thanks!
>