Comment 12 for bug 436758

Revision history for this message
Scott James Remnant (Canonical) (canonical-scott) wrote : Re: [Bug 436758] Re: upstart doesn't boot on sparc

On Thu, 2009-10-22 at 20:18 +0000, katre wrote:

> On Thu, Oct 22, 2009 at 3:57 PM, Scott James Remnant
> <email address hidden>wrote:
>
> > Shouldn't it be something more like:
> >
> > __allocof__ (struct) * ((sizeof (struct) - 1) / __allocof__ (struct) +
> > 1)
> >
> > Not entirely sure how __allocof__ works?
> >
> >
> I used __alignof__. While I had some trouble finding docs, it appears to
> work fairly simply. When given a type, it will return the number of bytes of
> padding needed to reach proper memory alignment. For example, the
> NihAllocCtx struct has sizeof 20 on my system, and __alignof__(NihAllicCtx)
> returns 4. In theory on x86 it would return 0 and hopefully the compiler
> will be smart enough to optimize it out.
>
Ah, sorry, I meant alignof. From the gcc documentation it says that it
returns on what byte boundary the object is aligned, not how much
padding it needs:

  The keyword `__alignof__' allows you to inquire about how an object is
  aligned, or the minimum alignment usually required by a type. Its
  syntax is just like `sizeof'.

   For example, if the target machine requires a `double' value to be
  aligned on an 8-byte boundary, then `__alignof__ (double)' is 8. This
  is true on many RISC machines. On more traditional machine designs,
  `__alignof__ (double)' is 4 or even 2.

   Some machines never actually require alignment; they allow reference
  to any data type even at an odd address. For these machines,
  `__alignof__' reports the smallest alignment that GCC will give the
  data type, usually as mandated by the target ABI.

This implies that you do need to divide as I've done, so (correcting the
code for my thinko):

__alignof__ (struct) * ((sizeof (struct) - 1) / __alignof__ (struct)
                        + 1)

ie. the number of alignment segments required to fit the entire
structure, plus padding

That being said, I couldn't find any difference between the results of
applying this code and not applying this code. The context block
contains entirely pointers, which are the largest alignment anyway.

faure% gcc -m64 alignof.c
faure% ./a.out
sizeof: 40
alignof: 8
aligned space: 40
faure% gcc -m32 alignof.c
faure% ./a.out
sizeof: 20
alignof: 4
aligned space: 20

(ie. aligned space is the same as sizeof)

Your code would just change the aligned size from 40 to 48 and 20 to 24,
which doesn't make any particular sense.

(Plus afaict, sizeof always includes the necessary padding to achieve
 alignment *anyway* - since otherwise malloc and arrays wouldn't work!)

Scott
--
Scott James Remnant
<email address hidden>