Hi Dudes,

There has been talk recently on several threads about process address space, etc.. So I think it may be useful to post here a routine of mine that reserves the largest chunk of address space available. You can call it more times to reserve the remaining, smaller, chunks.

On exit eax will contain the starting address of the reserved memory chunk, and ebx its size in bytes. If the address is NULL then there's no more memory available, or an Win32 error.
The routine is not particularly optimized because with all those Win32 calls that ain't going to be anything more than a waste of development time.



RESERVE.LARGEST.CHUNK.OF.HEAP:
.start: MOV EBP,$80000000
XOR EBX,EBX
.loop: SHR EBP,1
CMP EBP,$00010000
JB .done
PUSH U32 PAGE_EXECUTE_READWRITE
PUSH U32 MEM_RESERVE
LEA EAX,[EBX+EBP]
PUSH U32 EAX
PUSH P32 NULL
CALL KERNEL32_VirtualAlloc
TEST EAX,EAX
JZ .loop
ADD EBX,EBP
PUSH U32 MEM_RELEASE
PUSH U32 NULL
PUSH P32 EAX
CALL KERNEL32_VirtualFree
JMP .loop
.done: TEST EBX,EBX
JZ .exit
PUSH U32 PAGE_EXECUTE_READWRITE
PUSH U32 MEM_RESERVE
PUSH U32 EBX
PUSH P32 NULL
CALL KERNEL32_VirtualAlloc
TEST EAX,EAX
JZ .start
.exit: RET
Posted on 2002-07-13 03:08:10 by Maverick