Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-02-11 10:11:38


"Philippe A. Bouchard" <philippeb_at_[hidden]> writes:

> 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)].

OK, so what you really want is alignment_of<boost::detail::max_align>.

>> 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.

By "STL" I presume you mean your particular STL implementation.

>> 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

    template <class T>
    struct padding_of
    {
        BOOST_STATIC_CONSTANT(
            std::size_t, value = alignment_of<max_align>::value
                     - sizeof(T) % alignment_of<T>::value
        );
    };

or, if you don't trust max_align, then:

    template <class T>
    struct padding_of
    {
        BOOST_STATIC_CONSTANT(
            std::size_t, value = alignment_of<max_align>::value
                     % (sizeof(T) % alignment_of<T>::value)
        );
    };

I guess.

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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