FFI and str5ucts with "nested arrays"

Ivo Rössling iroess at cs.uni-magdeburg.de
Thu May 22 22:29:57 UTC 2003


Hi, there!

I have a question on how to declare fields for a respective C-Struct 
that looks
like this:

--------------- snip ---------------
struct hci_dev_info {
    uint16_t dev_id;
    char     name[8];

    bdaddr_t bdaddr;
    uint32_t flags;
    uint8_t  type;

    uint8_t  features[8];

    uint32_t pkt_type;
    uint32_t link_policy;
    uint32_t link_mode;

    uint16_t acl_mtu;       
    uint16_t acl_pkts;
    uint16_t sco_mtu;
    uint16_t sco_pkts;

    struct   hci_dev_stats stat;
};


typedef struct {
        uint8_t b[6];
} __attribute__((packed)) bdaddr_t;

--------------- snip ---------------

OK the uint*_t's are easy to handle. The hci_dev_stats, too... (- these 
are just
some more uint*_t's)
The "problem" for me is how to handle the "char name[8]", the "bdaddr_t" 
and the
"unit8_t features[8]" properly...
Usually I always thought, C doesn't make ANY difference at all between 
"type*"
and "type[n]" (, except of the that for latter case already memory will 
might be
allocated...) But obviously in this case it does...  (we'll never stop 
learning,
i think... - maybe it was just the case for "type[]"...)
The offsets of the respective fields let me assume that all three, 
"name", "bdaddr"
and finally "features" ended up being embedded in the struct:

**** pointer adress: 134536384  ******
dev_id:      0(0)        [0]
name:        ****        [2]
bdaddr:      ****        [10]
flags:       29(1d)      [16]
type:        1(1)        [20]
features:    ****        [21]
pkt_type:    52472(ccf8) [32]
link_policy: 14(e)       [36]
link_mode:   32768(8000) [40]
acl_mtu:     192(c0)     [44]
acl_pkts:    8(8)        [46]
sco_mtu:     64(40)      [48]
sco_pkts:    8(8)        [50]
stats:       ****        [52]
***** total size: 92 *****

So, in the fields definition, defining elements shouldn't be defined as 
"void*",
as in this case the total size of the byte-array requested via ffi would 
be to
small and wouldn't contain all the data.

OK. I could create an extra ExternalStructure for each of them. But:
1) Does it make much sense to create an ExternalStructure for a 
"char[8]" which
   contains 8 single fields of type char?  I don't want to think of how 
to handle
   char[255] then... ;)
2) Isn't there an "easier" way to create an ExternalStructure being 
nothing s.th.
   like an "array of ExternalStructure"?!


Maybe one of the experts out there can help me... ;-)

Cheers,
Ivo



More information about the Squeak-dev mailing list