News:

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

FAST Fixed size memory allocator

Started by stanhebben, September 26, 2006, 07:10:10 PM

Previous topic - Next topic

stanhebben

I wrote this little thing a few days ago. The file contains three macros that resemble a simple memory manager. It basically allows you to allocate blocks of a (semi-) fixed size, and free them for later use. There is zero memory overhead, and zero fragmentation (after reusing the free blocks, of course)

This allocator can be used to allocate a _lot_ of blocks of the same size. It reuses free blocks at an extremely fast rate. (benchmarking the fsAlloc/fsFree macro combination gave me 3 cycles on my amd cpu)

The file contains three macros:

fsAlloc <size>: allocates a block of memory. Size must be a constant.
fsFree <mem>,<size>: frees a block of memory allocated with fsFree. Size must be a constant and must be the block size (using the wrong size can make for bad bugs)
fsReleaseAll <size>: releases all memory for that block size. Note that this automatically frees all memory you allocated using fsAlloc too (for that size)

I recommend calling fsReleaseAll before you close you program, there *might* be memory leaks if you don't. (I ever ran out of virtual memory, not sure if this was the reason though)

[attachment deleted by admin]

gabor

Hi!


Good work I must say! :) I played a bit around with your code. I tried to figure out how it works. I guess the main idea is to pre-allocate a given number of blocks:

push PAGE_EXECUTE_READWRITE
push MEM_COMMIT
push _size*1024
push NULL
call VirtualAlloc

Then passing the next free block from this "heap". If all the blocks are reserved but more are required then again 1024 blocks are allocated.
For small objects this is okay, but it was a big waste of memory for big objects.
I really liked your linked list-like idea about administering the free blocks. Brilliant!  :U
I think I will use your approach (if you won't mind) at my linked array structure.

Greets, Gábor

stanhebben

Yeah for large blocks 1024 pieces is a bit too much, and I'd like to solve it using conditional assembly. However, I didn't yet succeed to make something that works.

Feel free to use this code - it's public.