a handy little function :P
MemStrategy PROC nBytes:DWORD
;Determine method of memory allocation
;DednDave, 10-2013
;Allocation is attempted in the following order:
; 1) stack
; 2) heap
; 3) not available - the caller may use VirtualAlloc or otherwise alter strategy
;-----------------------------------
;Call With: nBytes = number of bytes requested
;
; Returns: If requested allocation is available on stack:
; EAX = 0
; ECX = total available stack space, less reserved bytes
; EDX = current StackLimit, from FS:[8]
;
; If requested allocation is available on heap:
; EAX = address of allocated block (block is zeroed and must be freed)
; ECX = 0
; EDX = hHeap, process heap handle
;
; If requested allocation not available on stack or heap:
; EAX = 0
; ECX = 0
; EDX = hHeap, process heap handle
;-----------------------------------
_StackReserved = 4096
mov edx,nBytes
lea ecx,[esp+8-_StackReserved]
.if edx<=ecx
neg edx
xor eax,eax
lea edx,[edx+esp+8]
ASSUME FS:Nothing
.repeat
push eax
mov esp,fs:[8]
.until edx>=esp
ASSUME FS:ERROR
mov edx,esp
lea esp,[ecx+_StackReserved-8]
.else
push edx
INVOKE GetProcessHeap
pop edx
push eax
INVOKE HeapAlloc,eax,HEAP_ZERO_MEMORY,edx
pop edx
xor ecx,ecx
.endif
ret
MemStrategy ENDP