Memory Manager

Published February 07, 2006
Advertisement
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.
Previous Entry I got an XGameStation!
Next Entry Engine status
0 likes 3 comments

Comments

coderx75
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
February 10, 2006 11:19 AM
coderx75
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.
February 10, 2006 01:23 PM
Drakkcon
I'm not going to use it, but that's a pretty cool idea. Thanks!
February 20, 2006 10:39 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

..............

550 views

Untitled

990 views

Untitled

1013 views

Back

1006 views

I'm finally back

1040 views

Random updates

915 views

Hou zhou bu jien

940 views

Update

1032 views
Advertisement