views:

229

answers:

5

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.

+1  A: 

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

Robert Gould
+1  A: 

Similar one, here

aJ
+4  A: 

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.

unwind
Unless the are "locked" into pointers, used in the standard API and then "released" and the manager does not move locked blocks around... but that would be clumsy in the actual use and so I agree, handling fragmentation is a big ask.
Software Monkey
+3  A: 

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.

Pop Catalin
+1  A: 

see glib malloc() and memory reduction

http://live.gnome.org/MemoryReduction

plan9assembler