Tutorial :tiny memory manager



Question:

I have a chunk of memory, let us say 'NN'MB. I want a custom memory manager that will only allocate from this memory. The memory manager should be capable of allocating, freeing using the chunk already available. It will be great if it can also handle fragmentation.

Edited: I looking for some open source in C, or is there some API like malloc and free.


Solution:1

Being able to "handle" fragmentation is a rather steep requirement. If you mean that the manager must be able to de-fragment the memory, this means it can't have the standard C malloc() API. You need an indirect API, where memory allocations are referenced not by actual directly dereferencable addresses, but something more abstract.

This is because your memory manager must have the ability to move allocated memory blocks around during defragmentation, and it can't do that if the application is holding direct absolute pointers into allocated memory.

Of course, forcing the application to be indirect in its use of memory means many common C idioms and APIs break, since the expectation to use pointers freely is common in C.


Solution:2

I highly recommend checking Andrei Alexandrescu's Policy-Based Memory Allocation Fine-tuning your memory management

There's also a video of a talk he did on the subject, it's also highly recommended as a learning resource.


Solution:3

Does it have to be C? C++'s Loki allocator can do this


Solution:4

see glib malloc() and memory reduction

http://live.gnome.org/MemoryReduction


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »