[fpc-pascal] memory aligning

Micha Nelissen micha at neli.hopto.org
Mon Nov 5 20:29:59 CET 2007


David Pethes wrote:
> pointer. So I mixed the two codes and come with something like this:
> 
> function evk_malloc (size: longword): pointer;
> const
>   ALIGNMENT = 16;
> var
>   ptr: pointer;
> begin
>   ptr := getmem(size + ALIGNMENT);
>   result := Align (ptr, ALIGNMENT);
>   if result = ptr then
>       pbyte(result) += 16;

Why not use ALIGNMENT here also?

>   //store offset to original address
>   (pbyte(result) - 1)^ := result - ptr;
> end;
> 
> Is this correct to use? Wouldn't be there any catches for example when
> running on 64-bit machine?

This looks ok.

BTW, GetMem already returns 16 byte aligned blocks. If it does not, it's
considered a bug.

> Also, does FPC support aligning stack variables and record members on
> mod16 addresses like GCC
> (http://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/Variable-Attributes.html#Variable-Attributes)

Don't know.

Micha



More information about the fpc-pascal mailing list