[Box Backup] Workaround for struct packing problem on ARM processors (was: Another wish for 2011)
Chris Wilson
chris at qwirx.com
Sat Feb 26 14:03:07 GMT 2011
Hi Leif,
On Sat, 26 Feb 2011, Leif Linderstam wrote:
> Another thing I just came to think of. If you want to check the size of the
> structs that form the network protocol there is a nice tool in the Boost
> library, the static assert:
>
> http://www.boost.org/doc/libs/1_46_0/doc/html/boost_staticassert.html
>
> It can be used as follows:
>
> typedef struct
> {
> int64_t mDependsNewer;
> int64_t mDependsOlder;
> } en_StreamFormatDepends;
>
> BOOST_STATIC_ASSERT(sizeof(en_StreamFormatDepends) == 16);
>
> This will generate a compilation error if the size of the struct is not 16,
> which is as early warning as you can get. (Well, actually it is not really
> just a warning as it will not compile if the size is wrong.)
I think that's a good idea if we continue to use packed structures
directly on the wire, i.e. if we think we can make the packing directives
do what we want (which looks doubtful).
I'd prefer not to import the whole of Boost to get assertion, and that
page doesn't give me a definition, but I imagine something like this would
work:
#if sizeof(en_StreamFormatDepends) != 16
#error Assertion failed
#endif
Regarding packing directives, this message:
http://www.mail-archive.com/debian-gcc@lists.debian.org/msg00435.html
claims that there are two types of packing restrictions, controlled by
different directives. The #pragma pack(1) apparently turns off padding
within the structure, but not padding out of the structure length, which
is what attribute ((packed)) should do. So I think we might need both in
order for these structures to be 100% cross-platform. This is also
completely not portable to other compilers than gcc.
I'm thinking seriously about implementing your solution, but before we
introduce another library for (de)serialising integer constants, I was
wondering if you had seen Archive.h? Perhaps it can be extended with your
template for size safety? (Or we could just wrap it around a fixed size
memory block that asserts if we read or write past the end).
Cheers, Chris.
--
_____ __ _
\ __/ / ,__(_)_ | Chris Wilson <chris+sig at qwirx.com> Cambs UK |
/ (_/ ,\/ _/ /_ \ | Security/C/C++/Java/Ruby/Perl/SQL Developer |
\__/_/_/_//_/___/ | We are GNU : free your mind & your software |
More information about the Boxbackup
mailing list