Boost logo

Boost :

From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2003-02-11 09:42:15


David Abrahams wrote:
> "Philippe A. Bouchard" <philippeb_at_[hidden]> writes:
>
>> Hi again,
>>
>> I am doing some helper class that will pad the space after a
>> given type until it reaches a machine word boundary. This word
>> boundary would be the maximum of (alignment_of<T>::value -
>> sizeof(char)) where T is any 'typename', 'typename' is one of
>> {is_class<U>::value == false} and U is any type.
>
> Even if what you wrote made sense, which I'm not sure it does,
>
> [to me at least - how can a word boundary be a number (max over
> all types T of alignment_of<T> - sizeof(char)), rather than an
> address? And then why subtract sizeof(char), i.e. 1, from the
> maximal alignment?]

Forget the [- sizeof(char)] it is erroneous, what I meant was something
trivial [/ sizeof(char)].

> that algorithm relies on a big non-portable assumption, doesn't it?
> Even if you enumerated all non-class types, there's no reason to think
> that the maximal alignment has any relationship to a machine word.

It is purely theoretical. max(alignment<T>) should reach its maximum
somehow, which I cannot yet proove. STL has simplified this with a simple
constant set to 8.

>> I would like some help to isolate the most effective way to
>> define BOOST_DATA_ALIGNMENT in the following example (defined by
>> default to 8 -- based on std headers).
>
> Well, first of all, not ALL_CAPS; that should be reserved for macros.

Sorry about it, but this is just a dummy example. I wanted the name to be
easily recognisable with the one in shifted_ptr.hpp.

>> I would like to propose padding_of<> at the same time.
>>
>> template <typename T>
>> struct padding_of
>> {
>> size_t const BOOST_DATA_ALIGNMENT = 8;
>>
>> size_t const value = (BOOST_DATA_ALIGNMENT - sizeof(T) %
>> BOOST_DATA_ALIGNMENT) % BOOST_DATA_ALIGNMENT;
>> };
>>
>
> Since I can't figure out what any of this is actually supposed to
> mean, I guess I can't help with that.

It consist of determining the number of bytes required to reach the next
word boundary.

Ex.:

- The processor aligns each character to 8 bits.
- The processor aligns each integer to 32 bits.

0x8000 character
0x8001 padding
0x8002 padding
0x8003 padding
0x8004 integer
0x8005 integer
0x8006 integer
0x8007 integer

In this case: padding_of<char> will be equal to 3.

>> template <typename T>
>> struct padded
>> {
>> T type;
>> char padding[padding_of<T>::value]; // can be of size 0!
>
> zero-length arrays are not legal in C++.

Ah... nothing's easy. I'll do some partial specialization then.

[...]

It would be great getting it to work... to proove it.

Philippe A. Bouchard


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk