Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Memory Manager

Started by ZMaster Oct 11, 2009 at 12:52 PM 4 replies 3.6k views
Original Post
ZMaster
ZMaster
Hi there. I am looking for a speedy and proven-to-work implementation of a memory manager. Specifically, something like a custom heap would be great, where I can allocate a chunk of memory from the operating system beforehand and then redistribute that memory to quickly instantiate custom objects in my code. I am using a solid implementation of a small object allocator, but I would like something more flexible, which lets me allocate objects of different sizes, without knowing their exact byte size beforehand. Do you guys know of anything that would come close to my specifications? I would rather adopt / base on some well-tested code than re-invent the wheel myself. Thanks! Florian
SiCrane
SiCrane
Do you realize that your specifications are exactly what the built in allocator does for you?
ZMaster
ZMaster
Yep, you are right, but I would like to have it in a custom allocator, so I can have different types of objects use different heaps or deallocate all the objects stored on one of these heaps at once.

Plus, the problem with the built in allocator is that it is very slow when allocating chunks of memory and still relatively slow when releasing them.
tivolo
tivolo
Just some hints:

- Writing a fast, custom allocator is hard. Well-known, good allocators are Doug Lea's malloc, ptmalloc, and Hoard. Just google for these.

- If you want custom allocation just for some classes, override their new and delete operators, and use memory pools or something similar for that. Try looking into boost pools or google for "Memory Pool".

- Keep in mind that 3rd party code will not use your memory manager if memory is allocated via malloc and not via new. "Overriding" malloc needs platform-specific solutions, but can be done (if you really want to).


Hope that helps,
-tiv
ZMaster
ZMaster
Thanks tivolo. That helped a lot. I will look into all of these...
Hodgman
Hodgman
I use a really simple "mark and release" allocator for a lot of stuff.

Allocation is lightning fast for all objects, but the downside is that you can't deallocate a single object - you've got to deallocate the whole heap at once.

This is commonly used in games as you can do all of you allocations at the start of a level, and free them all once the player has moved on to the next level.

The code looks something like this:
class SimpleHeap{public:  explicit SimpleHeap( size_t size ) : data(size), used(0) {}  void  Clear() { used = 0; }  void* Alloc( size_t size, size_t alignment = 8 )  {    //Pad current usage up to correct alignment    size_t padding = used % alignment;    if( padding )      used += alignment - padding;    //Allocate aligned data, increase usage counter    void* ptr = &data[used];    used += size;    assert( used <= data.size() && "Out of memory, could return NULL instead of asserting..." );    return ptr;  }private:  SimpleHeap();  SimpleHeap( const SimpleHeap& );  SimpleHeap& operator=( const SimpleHeap& );  std::vector<char> data;  size_t used;};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.