Jump to content
  • Advertisement
Sign in to follow this  
ZMaster

C++ Memory Manager

This topic is 3174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!