custom memory allocator

Started by
3 comments, last by arbuckle911 16 years, 11 months ago
I'm going to be writing a custom memory allocator, and I wanted to run my idea by the community first. I've never written one before but the fact that I want to benefit from the speed this kind of thing can offer I decided it would be a good idea. Basically I want to write a C++ class Called MemoryPool. On creation MemoryPool will run memory = new char[sizeInByes]; where memory is defined as char *memory; During usage that memory chunk will be divided on the basis of what types it's going to store. For example, I'd like to use it for octree and scene object management. So I would do: On level load:

MemoryPool pool(neededBytes);  //creates the block of memory
pool.CreateBank(sizeof(StaticMapObject), numObjects);  //Sets a pointer to a place in memory where numObjects of StaticMapObjects can live in the char array. A counter returns sucessive pointers as they are requested.
pool.CreateBank(sizeof(OctreeNode), numNodes); //Same as other, but for OctreeNodes and in another place in the pool.


Here are my questions: Is it a good idea to use a char array? Should I use an unsigned char array? is malloc better for this application? should the object "banks" be closely packed together? would it matter if the first bank had a 100 meg empty space between it and the next bank? If the spacing between bank affects speed, how can I reduce the space between the banks? How should I handle deleted objects? make a list of pointers to locations of deleted ojects for reuse? That about sums it up. If anybody knows of any resources that do this already please let me know. Also if there are any optimizations I could make here I would be glad if they were pointed out. Thanks in advance.
Advertisement
Quote:Original post by arbuckle911
Is it a good idea to use a char array? Should I use an unsigned char array?
is malloc better for this application?

It's a good idea to use operator new for the particular datatype. That's the only way to ensure proper alignment.
Quote:should the object "banks" be closely packed together? would it matter if the first bank had a 100 meg empty space between it and the next bank?

It doesn't really matter, assuming a fairly large number of objects. Small object pooled allocators aren't great at cache coherence no matter what you do.

Quote:If anybody knows of any resources that do this already please let me know.

Check out boost::pool. There is very little reason for anyone to implement their own fixed-size pool allocator.
Quote:
It's a good idea to use operator new for the particular datatype. That's the only way to ensure proper alignment.

Ok but, what if I want to make SURE that all the data is in one chunk? Are you saying it would be best to make an array for each object type? If there is an array for each object type, wouldn't there be fragmentation between each array?
Quote:Original post by arbuckle911
Ok but, what if I want to make SURE that all the data is in one chunk? Are you saying it would be best to make an array for each object type? If there is an array for each object type, wouldn't there be fragmentation between each array?

Hmm? A fixed size pool allocator only works on objects of a single size, which pretty much means objects of a single type.
ok, I did some reading I get what you mean now. I'll probly just end up creating a "Factory" class that just dishes whatever class I need out from a fixed size array.

This topic is closed to new replies.

Advertisement