Subject: Re: CMSG_* problems
To: None <tech-userlevel@netbsd.org>
From: Christos Zoulas <christos@astron.com>
List: tech-userlevel
Date: 02/12/2007 21:04:04
In article <200702122044.PAA29568@Sparkle.Rodents.Montreal.QC.CA>,
der Mouse  <mouse@Rodents.Montreal.QC.CA> wrote:
>>> (The only way I can think of to ensure alignment suitable for a
>>> struct cmsghdr, without machine- or compiler-dependent hackery, is
>>> to allocate the buffer as an array of struct cmsghdr, and that still
>>> doesn't ensure the buffer is suitably aligned for the data fields of
>>> the control messages.)
>> The way it is normally is make it an array of the maximally aligned
>> type (usually of type long or long long).
>
>Knowing what "the maximally aligned type" *is* is machine- and/or
>compiler-dependent.  (For example, I could easily imagine a 32-bit
>machine on which long and long long could be on any 32-bit boundary but
>double must be on a 64-bit boundary.)

You can always do the following:

typedef union memalign_t {
	char a;
	short b;
	int c;
	long d;
	long long e;
	float f;
	double g;
	long double h;
	void *i;
	void (*j)(void);
	union memalign_t *k;
} memalign_t;

and to return the alignment of a type:

#define alignof(type)   (sizeof (struct { char d; type x; }) - sizeof(type))

So alignof(memalign_t) should return in bytes the max alignment...

christos