On Fri, March 14, 2014 11:11 pm, Martin Townsend wrote: > On 14/03/14 21:29, Phoebe Buckheister wrote: >> They would look contiguous, but they won't be contiguous in memory. >> Declarig the reserved field as u8:3 will cause the allocator to move >> that >> three-bit field to the next byte in memory, so that's certainly >> unwanted. > I don't follow, I was thinking that there would be reserved and > reserved2 in both versions
Nope. The little endian version lists all subfields of that field as they are defined by the standard. On an LE machine, the compiler will allocate those fields, in declaration order, from lowest to highest valued bit. Thus, type is at bit 0-2, security_enabled at bit 3, and so on. On BE, subfields are allocated, in declaration order, from highest to lowest valued bit. Additionally, on BE the bytes of our header will be swapped when compared to the actual bytes on the network. To have a BE bitfield represent the same values as an LE bitfield stored into it via memcpy, you'll first have to segment the subfields to match bytes exactly (giving rise to reserved and reserved2), swap all those bytes, and reverse the subfield order per byte - otherwise you won't get the exact same memory layout for both allocation methods. >> Using two u8s would also betray that this field is treated as a 16 bit >> entity by the standard and the protocol, and we couldn't cast the header >> pointer to u16* to get the raw value of the fc field, because u16 forces >> the compiler to allocate the struct as though the bitfield sequence was >> one u16. >> > union? maybe I need to do some reading on bitfields :) What would a union do for us here? The relevant standards documents treat this field as u16, so if it's not impossible for us to the same without breaking things, we should too :) Trying to fit it into single bytes will also be misleading, since it might lead you to think that the LE version and the BE version of these fields need only subfield-swaps, not byteswaps as well. Note that with two u8s, you would need two reserved fields on LE machines because bitfield allocation will not span storage units. ------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/13534_NeoTech _______________________________________________ Linux-zigbee-devel mailing list Linux-zigbee-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-zigbee-devel