I\'m integrating some code into my library. It is a complex data structure well optimized for speed, so i\'m trying not to modify it too much. The integration process goes well
Although I realise that this is an old thread, I would like to give my pure c++11 solution to the OP's question. The idea is to wrap the to be allocated object, adding in the padding to align the objects in array to power of 2 adresses, in the following way:
template
struct PaddedType : private T { private: char padding [ ObjectPaddingSize ]; };
template // No padding.
struct PaddedType : private T { };
template
struct PaddedT : private PaddedType::value - sizeof ( T )> { };
The objects padding size can be calculated at compile-time with the following class (returns L if L is power of 2, else the next power of 2 gt L):
template
class NextPowerOfTwo {
template
struct NextPowerOfTwo1 {
enum { value = NextPowerOfTwo1::value };
};
template
struct NextPowerOfTwo1 {
enum { value = M << 1 };
};
// Determine whether S is a power of 2, if not dispatch.
template
struct NextPowerOfTwo2 {
enum { value = NextPowerOfTwo1::value };
};
template
struct NextPowerOfTwo2 {
enum { value = M };
};
public:
enum { value = NextPowerOfTwo2::value };
};