Yet another ALIGN idea.

chris at umcp-cs.UUCP chris at umcp-cs.UUCP
Sat Jul 12 14:14:04 AEST 1986


In article <2600066 at ccvaxa> aglew at ccvaxa.UUCP writes:
>Since an alignment constant doesn't necessarily work, how about an
>alignmentof(object) operator, analagous to sizeof()? At least that'll work
>on all architectures that have alignments based on multiples.

I think this can still be implemented with a common header file, e.g.,

	#define OFFSET_TO_ALIGN(ptr)	/*something*/
	#define	IS_ALIGNED(ptr)	(OFFSET_TO_ALIGN(ptr) == 0)

On a Vax, and purely for efficiency, /*something*/ is

	#define	OFFSET_TO_ALIGN(ptr)	((4 - (int) (ptr)) & 3)

On a Pyramid, the same `define' works; on a Sun, it can be reduced to

	#define	OFFSET_TO_ALIGN(ptr)	((int) (ptr) & 1)

On your hypothetical machine, /*something*/ may be

	#define	OFFSET_TO_ALIGN(ptr)	(_offset_to_align(sizeof *(ptr)))

where _offset_to_align is a library function.

(N.B.: That it *can* be done this way does not mean that it *must*
be done this way.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list