Sign in to follow this  
Eric_B

Preventing Memory Fragmentation in C++

Recommended Posts

Which are good libraries for memory pool management and garbage compacting? Give me both free and commerical.

BC++B ships with memory pool management and garbage compacting classes?

I heard VC++2005 will come with memory management classes like GC? Is that true?

Share this post


Link to post
Share on other sites
You may consider looking into templated free lists. I first read about them in one of the Game Programming Gem books. They're light weight, uber fast, and simple as pie.

They work by creating a contigious block of resources. It then uses a stack to manage the wouldbe allocs and deallocs of memory. They're only applicable if you have many of the same type of objects though. Below is the TFreeList I wrote use in all my code.

You'd create a list of ints as so-

<code>
TFreeList<int> list(100); // Create 100 ints

int* lpInt = list.newInstance(); // Get an instance
// Use it in here to our hearts content
list.freeInstance(lpInt); // Free it when we're done
</code>


<code>

template <class Class_Type>
class TFreeList
{
private:
Class_Type* lpObj;
Class_Type** lpObjTable;
int iTop;

public:
int iNumObjects;

TFreeList(int iNumObjects);
~TFreeList();

Class_Type* newInstance();
void freeInstance(Class_Type* lpObj);
};

template <class Class_Type>
TFreeList<Class_Type>::TFreeList(int iNumObjects)
{
this->iNumObjects = iNumObjects;

lpObj = new Class_Type[iNumObjects];
lpObjTable = new Class_Type*[iNumObjects];
for (int cnt=0;cnt<iNumObjects;cnt++)
lpObjTable[cnt] = &lpObj[cnt];

iTop = iNumObjects;
}

template <class Class_Type>
TFreeList<Class_Type>::~TFreeList()
{
delete[] lpObj;
delete[] lpObjTable;
lpObj = NULL;
lpObjTable = NULL;
}

template <class Class_Type>
Class_Type* TFreeList<Class_Type>::newInstance()
{
if (--iTop < 0)
{
iTop = 0;
return NULL;
}

return lpObjTable[iTop];
}

template <class Class_Type>
void TFreeList<Class_Type>::freeInstance(Class_Type* lpObj)
{
lpObjTable[iTop++] = lpObj;
}

</code>

Share this post


Link to post
Share on other sites
I dont think it will directly, std::string is just a typedef of std::basic_string iirc, so you'll have to make a new typedef with the same traits as std::string but change the memory allocator to a pool allocator (assuming that can be done, I would assume so but i've never really looked into it)

Share this post


Link to post
Share on other sites
"They're only applicable if you have many of the same type of objects though. Below is the TFreeList I wrote use in all my code."
Thanks for the code, and my only trouble is list of std::string for now. The program simply keeps a list of memos on the server, but the length of the list is unpredictable... it can grow or shrink very quickly... and I don't want to restart the server everyday.

Share this post


Link to post
Share on other sites

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

Sign in to follow this