News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Lightweight fast Malloc replacement

Started by jj2007, May 22, 2012, 05:13:40 PM

Previous topic - Next topic

jj2007

In the old forum's campus, I was helping a member who needed a Malloc-type function but unfortunately, in his code, could find only the kernel. I thought I should share this revolutionary Malloc implementation here:

include \masm32\include\masm32rt.inc
include MallocLite.inc

.code
MyTest proc pStringL, pStringR
LOCAL v1, v2, rc:RECT    ; use Malloc directly after the locals
  mov ebx, Malloc(1000000)    ; allocate a Million bytes
  invoke lstrcpy, ebx, pStringL    ; copy the first passed string into the Malloc'ed buffer
  invoke lstrcat, ebx, str$(Msize(ebx))    ; add the size info
  invoke lstrcat, ebx, pStringR    ; add the second string
  print ebx    ; print everything
  Mfree(ebx)    ; don't forget to free the memory
  ret
MyTest endp

start: invoke MyTest, chr$("More than enough: "), chr$(" bytes for your proggies", 13, 10, 10)
inkey "bye"
exit
end start


Output: More than enough: 1000000 bytes for your proggies

I have not timed it yet, but I am pretty sure it beats both HeapAlloc and GlobalAlloc 8)