How to call _HeapAlloc@12 & _HeapReAlloc@16 ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

How to call _HeapAlloc@12 & _HeapReAlloc@16 ?

Post by 4RESTER »

I can use calling:
!PUSH dword [_PB_MemoryBase]
!CALL _HeapDestroy@4

I can use calling:
!PUSH dword 0
!PUSH dword 4096
!PUSH dword 8
!CALL _HeapCreate@12
!MOV [PB_MemoryBase], EAX

But can't calling:
!CALL _HeapAlloc@12
&
!CALL _HeapReAlloc@16

Error: undefined symbol

How to call _HeapAlloc@12 & _HeapReAlloc@16 ?
See ya... Image
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How to call _HeapAlloc@12 & _HeapReAlloc@16 ?

Post by Fred »

Look in the commented asm, you need to import them before using (just reference HeapCreate_() somewhere in your PB code and it will be the same).
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: How to call _HeapAlloc@12 & _HeapReAlloc@16 ?

Post by 4RESTER »

Fred wrote:Look in the commented asm, you need to import them before using (just reference HeapCreate_() somewhere in your PB code and it will be the same).
I have a few questions.

1. For what in the PureBasic is specially refused the HeapAlloc and HeapReAlloc with zero size?
Possibility to create zero size blocks is very usefuly.

2. For what in PureBasic creating one more Heap (HEAP_ID is storing into the [_PB_MemoryBase]), instead using default task HEAP_ID, that available via the GetProcessHeap()?

This functions into the PureBasic library working with the HEAP_ZERO_MEMORY flag, but check size parameter & generate the error when size is zero:

AllocateMemory (HeapAlloc):

Code: Select all

        MOV     EAX, [DWORD SS:ESP+4] ; !!!
        TEST    EAX, EAX                      ; !!! if SIZE = 0
        JLE     do_Return_False               ; !!! Then Return #False
        PUSH    EAX
        MOV   EAX, [_PB_MemoryBase]
        PUSH    8	; flag = HEAP_ZERO_MEMORY
        PUSH    EAX
        CALL    NEAR [DWORD DS:<&KERNEL32.HeapAlloc>]
        RETN    4
do_Return_False:    XOR     EAX, EAX
        RETN    4
ReAllocateMemory (HeapReAlloc):

Code: Select all

MOV     ECX, [DWORD SS:ESP+8]        ; !!!
TEST    ECX, ECX                             ; !!! if SIZE is 0
JLE     do_Return_False              ; !!! Then Return #False
MOV     EAX, [DWORD SS:ESP+4]
TEST    EAX, EAX
PUSH    ECX
JE      do_HeapAlloc
PUSH    EAX
MOV     EAX, [_PB_MemoryBase]
PUSH    8	; flag = HEAP_ZERO_MEMORY
PUSH    EAX
CALL    NEAR [DWORD DS:<&KERNEL32.HeapReAlloc>]
RETN    8
do_HeapAlloc: CALL    AllocateMemory
RETN    8
do_Return_False: XOR     EAX, EAX
RETN    8
See ya... Image
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How to call _HeapAlloc@12 & _HeapReAlloc@16 ?

Post by Fred »

Why is it useful to allocate a 0 bytes memory area ?
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: How to call _HeapAlloc@12 & _HeapReAlloc@16 ?

Post by 4RESTER »

Fred wrote:Why is it useful to allocate a 0 bytes memory area ?
Functions the AllocateMemory and ReAllocateMemory in windose PureBasic versions should not be a procedure, it must be a macro to create the calls to HeapAlloc and HeapReAlloc.

Compiled application should not be used initially artificially created with the storage identifier Heap in this variable [_PB_MemoryBase].

This is unnatural and incompatible with a common method through GetProcessHeap().

The functions HeapAlloc and HeapReAlloc themselves are bullproof.
Ability to create zero-sized blocks specially laid well the OS level.
Because it allows a very flexible to work with pre-created buffer, further fulfilling dynamic change buffer size.
I often use this feature when working with a structured management, especially when working with files stored resources (SFX, VFX, e.t.c.), working with the Chunks-based files (resources containers, resources into the firmwares, e.t.c.).

Into the PureBasic this is impossible, not because the procedures can not do that, but because the caps before the procedures:
MOV EAX, [DWORD SS:ESP+4]
TEST EAX, EAX ; !!! if SIZE = 0
JLE do_Return_False ; !!! Then Return #False

And why deny the possibility of artificially already implemented in the OS API?

If you remove the caps and will use heap of task GetProcessHeap(), will not suffer from the compatibility and completeness of the functions will be possible.

IMHO
See ya... Image
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: How to call _HeapAlloc@12 & _HeapReAlloc@16 ?

Post by 4RESTER »

Fred wrote:Look in the commented asm, you need to import them before using (just reference HeapCreate_() somewhere in your PB code and it will be the same).
You are right, this working as in pure FASM:

Code: Select all

!if ~ defined _HeapAlloc@12
!extrn _HeapAlloc@12
!end if

!if ~ defined _HeapReAlloc@14
!extrn _HeapAlloc@14
!end if
See ya... Image
Post Reply