# alignment

Manuel M T Chakravarty chak at cse.unsw.edu.au
Mon Jan 13 05:00:33 EST 2003

Fergus Henderson <fjh at cs.mu.OZ.AU> wrote,

> On 09-Jan-2003, Ross Paterson <ross at soi.city.ac.uk> wrote:
> > Two additions I think are required:
> >
> > 1) The spec should state that mallocBytes and allocaBytes return a block
> >    of memory sufficiently aligned for any of the primitive types supported
> >    by the architecture.
>
> I disagree.  These routines should only be required to align the memory
> sufficiently for any of the primitive types which could fit in the amount
> of space allocated.  For example, double precision floats might occupy
> 8 bytes, and require 8-byte alignment, but four-bytes allocations should
> not be required to be 8-byte aligned.
>
> (This is something the C standard got wrong, IMHO.)

I agree with Fergus.

Ross Paterson <ross at soi.city.ac.uk> wrote,

> 2) The description of Storable should require
>
> 	sizeOf x mod alignment x = 0
>
>    The library implementation of mallocArray implicitly assumes this, and
>    C also requires that elements of an array are allocated contiguously.

Ok.  I attach the amended description of these functions.

Thanks,
Manuel

-=-

\item[sizeOf~~~~::\ Storable a => a -> Int]
\item[alignment~::\ Storable a => a -> Int]\combineitems The function
\code{sizeOf} computes the storage requirements (in bytes) of the argument,
and alignment computes the alignment constraint of the argument.  An
alignment constraint \code{x} is fulfilled by any address divisible by
\code{x}. Both functions do not evaluate their argument, but compute the
result on the basis of the type of the argument alone.  We require that all
the elements of a contiguous array of storable values meet the alignment
constraint of these values; more formally, we require that
%
\begin{quote}
\begin{verbatim}
sizeOf v mod alignment v == 0
\end{verbatim}%
\end{quote}