[linux-audio-dev] Wrote a real time safe memory allocator in C++

Benno Senoner sbenno at gardena.net
Wed Sep 3 17:38:00 UTC 2003


Hi, I wrote a simple but very efficient real time safe memory allocator
 in C++ that is useful in real time audio apps where you need to allocate
objects dynamically. 
(badly enough there are still too many audio developers that
call new,delete, malloc() and free() within the  audio RT thread !)

It works by pre-allocating a fixed pool of elements which can then
dynamically allocated and freed.
The alloc() and free() have fixed execution times and are inlined
thus if you look at the resulting assembly code it takes only
8-9 instruction for full allocation/deallocation.

It has the limitation that the pool contain elements of 
the same datatype/size. Each element is a node of a doubly linked
list (the free-list) so keep in mind that if you create a pool
of N elements with s=sizeof(your_datatype).
The total memory usage is (N+2)*(s+8).
So it is not ideal to create large memory pools of chars (1 byte)
because for each char you would need 1+8 bytes of mem.
But for larger data structures it is ok.


creation of the memory pool:
RTMemoryPool *mypool=RTMemoryPool<your_datatype>(number_of_elements);

allocation of a element:
my_datatype *element=mypool->alloc();

freeing of an element:
mypool->free(element);

destruction of the memorypool: (frees up all the memory):
delete mypool;

Using the RTMemoryPool class requires only the inclusion of 
the rtmemorypool.h header file, no .cpp files required.

You can download the RT memory allocator (which contains an small
example that user the class)
here:

http://www.linuxdesktop.it/download/rtallocator-0.0.2.tgz

if you find bugs or have suggestions let me know. 

(write to benno@ and not sbenno@ because I do not read that mailbox,
 too much spam).

cheers,
Benno



-------------------------------------------------
This mail sent through http://www.gardena.net



More information about the Linux-audio-dev mailing list