[ Home | Optimize | Current | Project | Tools | Made in France | F.A.Q | Forums | Links | Sources Codes ]

4 - ALLOCATING MEMORY

For allocating memory I use the VirtualAlloc function. This function allocates blocks of datas. Each allocated block has a size of 4096 bytes. So, when you call this function, the allocated memory will be rounded to the next 4096 bytes to entirely fill the block. If you need 8192 bytes, the function will allocate two blocks but if you need 256 bytes, you will have a 4096 data block allocated.

Here is an example of what such a function could be :

AllocMemory       PROC  __dwRequestedSize:DWord

                  mov    eax,__dwRequestedSize

                  test   eax,eax
                  jz     @Exit

                  add    eax,4095
                  shr    eax,12                     ; Requested Size / 4096
                  shl    eax,12                     ; Requested Size * 4096

                  push   PAGE_EXECUTE_READWRITE
                  push   MEM_COMMIT or MEM_RESERVE
                  push   eax
                  push   NULL
                  push   OFFSET @Exit
                  jmp    VirtualAlloc

@Error :          xor    eax,eax

@Exit :

                  test   eax,eax
                  ret
AllocMemory       ENDP

This function returns TRUE if the alloc operation returns successfully.
The function return FALSE if an error occured.

At the end of the function the ZERO flag is set if an error occured.

INVOKE AllocMemory ,4096
jz     @Error

<Return>

 

[ Home | Optimize | Current | Project | Tools | Made in France | F.A.Q | Forums | Links | Sources Codes ]