[En-Nut-Discussion] heap.c code optimization

Dusan Ferbas dferbas at dfsoft.cz
Sun Jan 16 20:41:03 CET 2005


Harald,

I vote for alignment macro and I suggest first version because there are 2 
arithmetic operations in comparison to 3 in second variant. BTW if 
MEM_ALIGNMENT == 4 you get exactle same what I suggested.

   size = (size + MEM_ALIGNMENT - 1) & ~((size_t)(MEM_ALIGNMENT - 1));

If Tyou has the alignment in his code exactly in a way you presented, then 
there is a bug because -1 is missing on 2nd line. If condition is spare.

Dusan

>Date: Sun, 16 Jan 2005 11:18:56 +0100
>From: Harald Kipp <harald.kipp at egnite.de>
>
>Dusan,
>
>Same code can be found in NutThreadCreate(). As a more
>general solution (16-bit boundaries), we may combine it with
>Tyou's H8 port, which uses a defined alignment:
>
>      if (0 != (size % MEM_ALIGNMENT)) {
>          size += MEM_ALIGNMENT;
>          size &= ~((SIZE)MEM_ALIGNMENT-1);
>      }
>
>Thus we get
>
>#if MEM_ALIGNMENT > 1
>    size = (size + MEM_ALIGNMENT - 1) & ~((size_t)(MEM_ALIGNMENT - 1));
>#endif
>
>or, alternatively
>
>#if MEM_ALIGNMENT > 1
>    size += MEM_ALIGNMENT - (size & (MEM_ALIGNMENT - 1));
>#endif




More information about the En-Nut-Discussion mailing list