Author Topic: Lightweight fast Malloc replacement  (Read 3866 times)


  • Member
  • *****
  • Posts: 10348
  • Assembler is fun ;-)
    • MasmBasic
Lightweight fast Malloc replacement
« on: May 22, 2012, 05:13:40 PM »
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:

Code: [Select]
include \masm32\include\

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
MyTest endp

start: invoke MyTest, chr$("More than enough: "), chr$(" bytes for your proggies", 13, 10, 10)
inkey "bye"
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)