Topic: An alignment-related question


Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sat, 19 May 2001 19:51:16 GMT
Raw View
Marcin 'Qrczak' Kowalczyk wrote:
>
> Fri, 18 May 2001 16:41:53 GMT, Al Grant <tnarga@arm.REVERSE-NAME.com> pisze:
>
> >> Hmmm... would align_of(T) be a useful new operator (a la sizeof)?
> >
> > Isn't it just   1 + (sizeof(T) & -sizeof(T))
>
> No. For example in some compilers
>     struct {char x[4];}
> and
>     int
> have the same size but different alignments.
>
> But I don't see where alignment would be usable by the programmer
> directly.

Any attempting a portable allocator needs it.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Mon, 21 May 2001 07:46:48 CST
Raw View
In article <9e10ph$humm$1@ID-14036.news.dfncis.de>, Andrei Alexandrescu says...
>
>Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible that
>T has more stringent alignment requirements than U? In other words, is it
>possible to fail to allocate a T in a chunk of memory properly aligned for
>an U?
>
>Andrei

As many have answered, yes. There are not many requirements on alignment.
Which is why I think we could use #include <align> :

template <typename T>
struct align_traits {
static T* align( void* ) // returns an aligned pointer
std::size_t max_adj();   // returns the max value for align(p)-p;
}

I think this can already be implemented on many compilers (i.e.
no compiler magic needed, just write'em out for all T.)

Comments?

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: Mon, 21 May 2001 18:48:48 GMT
Raw View
"Michiel Salters" <Michiel.Salters@cmg.nl> wrote in message
news:%P3O6.1428$r4.69199@www.newsranger.com...
> As many have answered, yes. There are not many requirements on alignment.
> Which is why I think we could use #include <align> :
>
> template <typename T>
> struct align_traits {
> static T* align( void* ) // returns an aligned pointer
> std::size_t max_adj();   // returns the max value for align(p)-p;
> }
>
> I think this can already be implemented on many compilers (i.e.
> no compiler magic needed, just write'em out for all T.)
>
> Comments?

The support you propose is entirely runtime. I was hoping for compile-time
support as well. For example, max_align<size_t>::type would give me the type
with the most stringent alignment requirement for a specific size.


Andrei


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Christopher Eltschka <celtschk@dollywood.itp.tuwien.ac.at>
Date: Mon, 21 May 2001 19:24:47 GMT
Raw View
Michiel Salters wrote:
>
> In article <9e10ph$humm$1@ID-14036.news.dfncis.de>, Andrei Alexandrescu says...
> >
> >Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible that
> >T has more stringent alignment requirements than U? In other words, is it
> >possible to fail to allocate a T in a chunk of memory properly aligned for
> >an U?
> >
> >Andrei
>
> As many have answered, yes. There are not many requirements on alignment.
> Which is why I think we could use #include <align> :
>
> template <typename T>
> struct align_traits {
> static T* align( void* ) // returns an aligned pointer
> std::size_t max_adj();   // returns the max value for align(p)-p;
> }
>
> I think this can already be implemented on many compilers (i.e.
> no compiler magic needed, just write'em out for all T.)
>
> Comments?

I'd prefer something like:

#include <align>

union
{
  std::align<int> int_align;
     // std::align<int> has the allignment of int, and the smallest
     // possible size for that alignment (i.e. sizeof align<int> is
     // the alignment of int)

  char int_data[1000*sizeof int];
     // this array is aligned correctly for int (due to the union
     // with a correctly aligned type)
};

union
{
  std::align<void> void_align;
     // std::align<void> is a special case: It is aligned correctly
     // for _all_ types. Again, it's the smallest type for which it
     // is possible (so sizeof align<void> is the minimum alignment
     // needed for arbitrary types)

  char data[1000];
     // 1000 bytes perfectly aligned for any type
};

align<T> should be allowed iff T is either a complete type, or void.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: Fri, 18 May 2001 07:43:49 GMT
Raw View
"Dennis Yelle" <dennis51@jps.net> wrote in message
news:3B041DF1.250D3498@jps.net...
> Andrei Alexandrescu wrote:
> >
> > Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible
that
> > T has more stringent alignment requirements than U? In other words, is
it
> > possible to fail to allocate a T in a chunk of memory properly aligned
for
> > an U?
>
> When you phrase the question that way, the answer
> is "of course":
>
> struct U {
>   char data[1+sizeof(double)];
> };
>
> typedef double T;

<Slap forehead>

Sorry. I actually was referring to the case when T (the smaller type) is a
user-defined type and U is a primitive type.


Andrei


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Fri, 18 May 2001 08:38:13 GMT
Raw View
"Andrei Alexandrescu" <andrewalex@hotmail.com> wrote in message
news:9e2ceq$n7sh$1@ID-14036.news.dfncis.de...
> "Dennis Yelle" <dennis51@jps.net> wrote in message
> news:3B041DF1.250D3498@jps.net...
> > Andrei Alexandrescu wrote:
> > >
> > > Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible
> that
> > > T has more stringent alignment requirements than U? In other words, is
> it
> > > possible to fail to allocate a T in a chunk of memory properly aligned
> for
> > > an U?
> >
> > When you phrase the question that way, the answer
> > is "of course":
> >
> > struct U {
> >   char data[1+sizeof(double)];
> > };
> >
> > typedef double T;
>
> <Slap forehead>
>
> Sorry. I actually was referring to the case when T (the smaller type) is a
> user-defined type and U is a primitive type.

In general this requires that there is a builtin type of smaller size with a
stricter alignment requirement than one of larger size, as a structure must
always be at least as aligned as its members, and no more aligned than its
size (otherwise arrays wouldn't work)

Imagine float being a 6-byte real (as for Turbo Pascal), but a 32-bit basic
architecture so that a int is 4-byte aligned. Since a 6-byte object requires
one half-word access, to get the odd 2 bytes, whether aligned on a 4-byte
boundary or a 2-byte boundary, the system may relax the alignment for float
to 2 bytes. Thus a structure T containing a single int (sizeof(T)==4) may
only have 4-byte alignment, but a single float (sizeof(U)==6) is 2-byte
aligned.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Fri, 18 May 2001 12:37:56 GMT
Raw View
Andrei Alexandrescu wrote:
>
> "Dennis Yelle" <dennis51@jps.net> wrote in message
> news:3B041DF1.250D3498@jps.net...
> > Andrei Alexandrescu wrote:
> > >
> > > Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible
> that
> > > T has more stringent alignment requirements than U? In other words, is
> it
> > > possible to fail to allocate a T in a chunk of memory properly aligned
> for
> > > an U?
> >
> > When you phrase the question that way, the answer
> > is "of course":
> >
> > struct U {
> >   char data[1+sizeof(double)];
> > };
> >
> > typedef double T;
>
> <Slap forehead>
>
> Sorry. I actually was referring to the case when T (the smaller type) is a
> user-defined type and U is a primitive type.

Still possible. The alignment can be any positive integer factor of
sizeof(U). A primitive type that's 8 bytes in size could have an
alignment requirement of 1. A user-defined struct could contain a
primitive type of size 2 with an alignement requirement of 2, in which
case the struct could have an alignment requirement of at least 2, while
still being smaller than U.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Al Grant" <tnarga@arm.REVERSE-NAME.com>
Date: Fri, 18 May 2001 16:41:53 GMT
Raw View
"Carl Daniel" <carl@pixami.com> wrote in message
news:9e19j0$n5v@dispatch.concentric.net...
> typedef long T;     // has alignment of 4 on many platforms
> typedef char U[5];   // has alignment of 1 on many platforms
>
> Now, if sizeof(U) >= 2*align_of(T)-1, there's got to be a correctly
aligned
> region within U where a T could reside.
>
> Hmmm... would align_of(T) be a useful new operator (a la sizeof)?

Isn't it just   1 + (sizeof(T) & -sizeof(T))

Easily defineable as a macro or as

  template<class T> struct align_of
  {
      enum { VAL = (sizeof(T) & -sizeof(T)) + 1; };
  };



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Michael Lee Finney <michael.finney@acm.org>
Date: Sat, 19 May 2001 08:58:14 GMT
Raw View
In article <9e2j1u$enj$1@cam-news1.cambridge.arm.com>,
tnarga@arm.REVERSE-NAME.com says...
> "Carl Daniel" <carl@pixami.com> wrote in message
> news:9e19j0$n5v@dispatch.concentric.net...
> > typedef long T;     // has alignment of 4 on many platforms
> > typedef char U[5];   // has alignment of 1 on many platforms
> >
> > Now, if sizeof(U) >= 2*align_of(T)-1, there's got to be a correctly
> aligned
> > region within U where a T could reside.
> >
> > Hmmm... would align_of(T) be a useful new operator (a la sizeof)?
>
> Isn't it just   1 + (sizeof(T) & -sizeof(T))
>
> Easily defineable as a macro or as
>
>   template<class T> struct align_of
>   {
>       enum { VAL = (sizeof(T) & -sizeof(T)) + 1; };
>   };

Only for types whose size is a power of 2. In most
cases long double is not a power of 2 -- I have seen
64, 80, 96 and 128. Two of which don't qualify. Of
course you could replace your macro with a big
conditional expression which would be optimized away
to a constant at compile time. Or put it in a traits
class.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
Date: Sat, 19 May 2001 10:04:33 GMT
Raw View
Fri, 18 May 2001 16:41:53 GMT, Al Grant <tnarga@arm.REVERSE-NAME.com> pis=
ze:

>> Hmmm... would align_of(T) be a useful new operator (a la sizeof)?
>=20
> Isn't it just   1 + (sizeof(T) & -sizeof(T))

No. For example in some compilers
    struct {char x[4];}
and
    int
have the same size but different alignments.

But I don't see where alignment would be usable by the programmer
directly.

--=20
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZAST=CAPCZA
QRCZAK

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: Thu, 17 May 2001 17:38:42 GMT
Raw View
Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible that
T has more stringent alignment requirements than U? In other words, is it
possible to fail to allocate a T in a chunk of memory properly aligned for
an U?


Andrei


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Dennis Yelle <dennis51@jps.net>
Date: Thu, 17 May 2001 19:59:39 GMT
Raw View
Andrei Alexandrescu wrote:
>
> Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible that
> T has more stringent alignment requirements than U? In other words, is it
> possible to fail to allocate a T in a chunk of memory properly aligned for
> an U?

When you phrase the question that way, the answer
is "of course":

struct U {
  char data[1+sizeof(double)];
};

typedef double T;


Dennis Yelle
--
I am a computer programmer and I am looking for a job.
There is a link to my resume here:
http://table.jps.net/~vert/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Carl Daniel" <carl@pixami.com>
Date: Thu, 17 May 2001 20:39:22 GMT
Raw View
"Andrei Alexandrescu" <andrewalex@hotmail.com> wrote in message
news:9e10ph$humm$1@ID-14036.news.dfncis.de...
> Consider two types T and U with sizeof(T) <= sizeof(U). Is it possible
that
> T has more stringent alignment requirements than U? In other words, is it
> possible to fail to allocate a T in a chunk of memory properly aligned for
> an U?
>
>
> Andrei
>

My $0.02 is: Yes, absolutely.

typedef long T;     // has alignment of 4 on many platforms
typedef char U[5];   // has alignment of 1 on many platforms

Now, if sizeof(U) >= 2*align_of(T)-1, there's got to be a correctly aligned
region within U where a T could reside.

Hmmm... would align_of(T) be a useful new operator (a la sizeof)?

-cd



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]