I'm working on some C++ code for an embedded system. The I/O interface the code uses requires that the size of each message (in bytes) is a power of two. Right now, the code does something like this (in several places):
#pragma pack(1)
struct Message
{
struct internal_
{
unsigned long member1;
unsigned long member2;
unsigned long member3;
/* more members */
} internal;
char pad[64-sizeof(internal_)];
};
#pragma pack()
I'm trying to compile the code on a 64-bit Fedora for the first time, where long
is 64-bits. In this case, sizeof(internal_)
is greater than 64, the array size expression underflows, and the compiler complains that the array is too large.
Ideally, I'd like to be able to write a macro that will take the size of the structure and evaluate at compile time the required size of the padding array in order to round the size of the structure out to a power of two.
I've looked at the Bit Twiddling Hacks page, but I don't know if any of the techniques there can really be implemented in a macro to be evaluated at compile time.
Any other solutions to this problem? Or should I perpetuate the problem and just change the magical 64 to a magical 128?