News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Help, creating a Malloc function for ASM

Started by RHL, May 21, 2012, 01:21:27 AM

Previous topic - Next topic

RHL

Hello guys, fine I'm trying create a Malloc function for ASM, I'm seeing the MSDN - but I have doubts... I wonder if I can give recommendations and help... I'm trying with RtlCreateHeap,RtlAllocateHeap,RtlFreeHeap... but I did see that there NtGetProcessHeap...

also - I have seen that RtlCreateHeap, return a pointer ( which must be freed with RtlFreeHeap ) but I think it is different from what  RtlAllocateHeap function return...
then I would need two pointer ( one for RtlCreareHeap - one for RtlAllocateHeap ).

I want to just simulate the Malloc, free function... any recommendations, thanks

Gunner

~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

RHL

thanks - but I want to do it with these functions :P

well...my problem is I do not understand how it should do... in rtlcreateheap say:
RtlCreateHeap returns a handle to be used in accessing the created heap.

how do it should do? I do not understand

Farabi

You can use this


mAlloc proc nSize:dword

add nSize,4
invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,nSize
.if eax==0
invoke MessageBox,NULL,CADD("Unable to allocate memory"),NULL,MB_OK
.endif

ret
mAlloc endp
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

dedndave

the Rtl (run-time library) functions are intended for drivers, i think

GlobalAlloc will work but, as i remember, it calls HeapAlloc anyways   :P

the Create functions create a heap - but the process already has a heap

i usually use HeapAlloc, as Rob suggested

during program initialization, do this one time
        INVOKE  GetProcessHeap
        mov     hHeap,eax


then, to allocate a block...
        INVOKE  HeapAlloc,hHeap,<Flags>,<BytesRequired>
        mov     hBlock,eax

for Flags, i use NULL or HEAP_ZERO_MEMORY
the value returned in EAX is the address of the allocated block - it is also a sort of handle used to free the block

when you are done using the allocated block...
        INVOKE  HeapFree,hHeap,NULL,hBlock

it is fast and simple   :bg

the Rtl functions probably work very similarly, if you want to play with them

Vortex

Hi RHL,

Did you try to use C run-time malloc function from msvcrt.dll ?

jj2007

Quote from: Vortex on May 21, 2012, 04:57:49 PM
Did you try to use C run-time malloc function from msvcrt.dll ?

The problem is that if you only have access to Kernel32, you cannot use the crt stuff...
Raùl, here is a little macro that needs only the kernel:

include \masm32\include\masm32rt.inc
include Malloc.inc

.code
start: call MyTest
inkey "bye"
exit

MyTest proc
LOCAL v1, v2, rc:RECT
  mov ebx, Malloc(1000000)
  invoke lstrcpy, ebx, chr$("Hey, that's more than enough: ")
  print ebx
  lea eax, rc
  sub eax, ebx
  print str$(eax), " bytes for your little proggies", 13, 10
  ret
MyTest endp
end start


Hey, that's more than enough: 1000000 bytes for your little proggies

... and you don't even have to bother freeing it :wink

RHL

Vortex: mmm that sound interesant... but I was able to do it :D
thanks a lot guys  :bg


RHL

guys I made this code, and wanted to ask if I could say if this is correct:  :bg

also, my question is... if is correct how to I use the CreateHeap function, I want to mean, I use just once to create two memallocs... is right?  :bg

include masm32rt.inc

CreatemyHeap proto
mymalloc proto :DWORD, :DWORD
myfree proto :DWORD, :DWORD

.data
hmyHeap dd 0
mtest db "test, testtttt dj a test ",0

hMylp1 dd 0
hMylp2 dd 0
.code
main:


call CreatemyHeap

;I need to 300h bytes
push 300h
push hmyHeap
call mymalloc
mov hMylp1,eax
invoke RtlMoveMemory,eax,addr mtest,10

push 200h
push hmyHeap
call mymalloc
mov hMylp2,eax

invoke RtlMoveMemory,eax,addr mtest+10,10


; free memory 1
push hMylp1
push hmyHeap
call myfree

; free memory 2
push hMylp2
push hmyHeap
call myfree


CreatemyHeap proc
invoke HeapCreate,NULL,1000h,1000h ; 1000h bytes, I think it's suficient lol
mov hmyHeap,eax
ret
CreatemyHeap endp

mymalloc proc hmyheap:DWORD, Memsize:DWORD
invoke HeapAlloc,hmyheap,HEAP_ZERO_MEMORY,Memsize
ret
mymalloc endp

myfree proc hMem:DWORD, hAlloc:DWORD
invoke HeapFree,hMem,NULL,hAlloc
invoke HeapDestroy,hMem
ret
myfree endp
end main

dedndave

you should only use HeapDestroy when you are done using the heap created by HeapCreate
there is no need to use either of these functions

when you call HeapFree, the first parameter should be the heap handle - not the allocated block

for such small allocations, you could just use the stack or the uninitialized data section   :P

include masm32rt.inc

mymalloc proto :DWORD
myfree proto :DWORD

.data
hmyHeap dd 0
mtest db "test, testtttt dj a test ",0

hMylp1 dd 0
hMylp2 dd 0

.code

main:

call InitHeap

;I need to 300h bytes
invoke mymalloc,300h
mov hMylp1,eax
invoke RtlMoveMemory,eax,addr mtest,10

invoke mymalloc,200h
mov hMylp2,eax
invoke RtlMoveMemory,eax,addr mtest+10,10

; free memory 1
invoke myfree, hMylp1

; free memory 2
invoke myfree, hMylp2

InitHeap proc
invoke GetProcessHeap
mov hmyHeap,eax
ret
InitHeap endp

mymalloc proc Memsize:DWORD
invoke HeapAlloc,hmyheap,HEAP_ZERO_MEMORY,Memsize
ret
mymalloc endp

myfree proc hAlloc:DWORD
invoke HeapFree,hmyHeap,NULL,hAlloc
ret
myfree endp

end main

RHL