• Advertisement
Sign in to follow this  
  • entries
    37
  • comments
    46
  • views
    22002

Memory Manager

Sign in to follow this  

281 views

As a result of my recent thread on creating a memory manager, I decided to create my own. I already know how I'm going to do garbage collection, it will involve a smart pointer class. I will post the source when it's done.

Currently, the client programmer can create memory pools of varying sizes (these sizes are all rounded up to the nearest kilobyte).

Here's an example of the pools working (and they actually work, a rarity for my code).


#include
#include

#include "Pool.h"

using namespace std;
using namespace stratus;
using namespace memory;

int main(int argc, char** argv)
{
try
{
//TEST: Make sure all implemented pool functions work
////////////////////////////////////////////////////////////////////////////
//we want only 20 ints, but we get a Kilobyte worth
Pool<int> testPool("Test Integers.", 20);

//create one integer
int* testInt = testPool.Allocate();
int* testInt2 = testPool.Allocate();
int* testInt3 = testPool.Allocate();

*testInt = 10;
*testInt2 = 20;
*testInt3 = 3005;

cout << "Value of int 1: " << *testInt << endl
<< "Value of int 2: " << *testInt2 << endl
<< "Value of int 3: " << *testInt3 << endl;

cout << endl << "---------------------------------" << endl << endl;

cout << "Total size in bytes: " << testPool.GetTotalSize() << endl;
cout << "Total free blocks: " << testPool.NumberOfFreeBlocks() << endl;
cout << "Total free bytes: " << testPool.NumberOfFreeBytes() << endl;
cout << "Largest Free Strip in Blocks: " <<
testPool.GetLargestUnfragmentedBlockSize() << endl;
cout << "Largest Free Strip in Bytes: " <<
testPool.GetLargestUnfragmentedByteSize() << endl;


////////////////////////////////////////////////////////////////////////////
//RESULT:SUCCESS

system("PAUSE");

return 0;
}

catch(const MemoryException& error)
{
cout << error.what() << endl;
system("PAUSE");

exit(1);
}
}



this outputs:



Note that you have to name the memory pools. This is useful because whenever there is a memory error, it will throw the error as well as the pool that it happened in. Also, I plan on making a
"Memory Manager" class that will contain pools, and perform the garbage collection incrementally.

Also note that fragmentation is not an issue at all. The Allocate() function determines whether it is acceptable to allocate the memory at the end of the current strip or look from the beginning for a free block.

I don't like having to use Allocate so much, so I will make it to where other parts of my game engine allow you to just enter the name of the pool to use in their constructor.
Sign in to follow this  


3 Comments


Recommended Comments

Okay man, this is totally freakin' me out!

I'm looking at your Windows color settings in your screen shots and they are the EXACT colors I use! These were colors I customized. Coincidence?

3D objects: 131, 153, 177
Active Title bar: 79, 101, 125 fades to 128, 180, 208

Share this comment


Link to comment
BTW, in reply to your post...

I've created a template container class that allocates blocks of objects. The number of objects per block can be set or use a default defined in the header file. This can be access like an array but it grows on its own. From this class, I derived a stack class to allow data and objects to be managed on a LIFO structure. Finally, I derived another class from the original container that acts as a managed container.

This new templare class actually containes a stack of pointers to the objects being managed. So, as you add objects to the managed container, it passes back a pointer to the next available object. When you want to "destroy" this object, you pass the pointer back to the Remove() method. The pointer is placed on the stack so that the next time an object pointer is requested, rather than taking the next available object in the container, it pops the last pointer off the stack and passes that back instead.

This method ensures that you are only using the amount of memory that you need. Any "holes" in the container are filled first, and memory usage is only expanded when you absolutely need it. There's usually some overhead using the stack but it's usually not too bad. It's worked well for me in certain situations but I wouldn't recommend it for general purposes.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement