As ye know things like some SSE intructions benefit from having memory operands aligned at 16 bytes. But the Windows memory allocation doesn't support that (At least not without VirtualAlloc).

So I wrote these simple procs to allow me to allocate memory at any alignment. Its a real messy way of doing it though so does anyone have a better suggestion/idea.

proc aalloc ;eax=size,ecx=alignment

enter
push esi ebx
lea esi,[eax+ecx-1+4]
mov ebx,ecx

invoke HeapAlloc,[globalHeap],0,esi
mov ecx,eax
xor edx,edx
div ebx

sub esi,edx
sub ebx,edx
inc esi

invoke HeapReAlloc,[globalHeap],0,ecx,esi
add eax,ebx
mov [eax-4],ebx

pop ebx esi
return

proc afree ;eax=mem
enter
mov eax,[eax-4]
invoke HeapFree,[globalHeap],0,eax
return

This method stores an extra dword + some additional padding bytes before the returned address to ensure the correct alignment. The extra dword holds the actual start of the memory allowing you to free it.

Yeah for alignment < 256 bytes you could've gotten away with an extra byte not a dword but since 4/16 byte alignment is all I'd be using this for that saving would've been negated by extra padding bytes, so I made things easier fior myself.
Posted on 2003-07-21 19:10:18 by Eóin
What about


aalloc:
lea eax, [eax+ecx+4-1]
invoke HeapAlloc, globalHandle,0,eax
lea edx, [eax+4-1]
add edx, ecx
neg ecx
and edx, ecx
mov [eax], edx
retn 8

Based on the concept of

alignment =
add pointer, alignment - 1
and pointer, -alignment

*untested*

Just remember that ecx must be power of 2.
Posted on 2003-07-22 08:09:20 by roticv
Havn't tested that myself but it does seem nicer. The actual calculation of the alignment is pretty irrevelant compared to the two API calls I imagine.
Posted on 2003-07-22 12:43:01 by Eóin
roticv,
I'm wondering why you save the aligned buffer address in
Later you will obtain it with two operations:
mov eax, lpMemOrg
mov eax, ; one instruction more
I prefer simply:


lea edx, [eax+ebx-1] ; ebx->alignment power of 2.
invoke HeapAlloc, globalHandle,0,edx
mov lpMemOrg, eax ; use it for HeapFree
lea edx, [eax+ebx-1]
neg ebx
and edx, ebx
mov lpMem, edx ; use it for work

and next

mov eax, lpMem ; one instruction only

Regards,
Lingo
Posted on 2003-07-22 23:57:38 by lingo12
roticv,
I'm wondering why you save the aligned buffer address in

I am just doing what E?in wanted...

This method stores an extra dword + some additional padding bytes before the returned address to ensure the correct alignment. The extra dword holds the actual start of the memory allowing you to free it.
Posted on 2003-07-23 03:01:33 by roticv
OK, take it easy
Posted on 2003-07-23 08:29:53 by lingo12