jedi-outcast/code/qcommon/miniheap.h

67 lines
999 B
C
Raw Permalink Normal View History

2013-04-22 05:25:59 +00:00
#if !defined(MINIHEAP_H_INC)
#define MINIHEAP_H_INC
class CMiniHeap
{
char *mHeap;
char *mCurrentHeap;
int mSize;
int mMaxAlloc;
public:
// reset the heap back to the start
void ResetHeap()
{
2013-04-23 00:25:52 +00:00
if ((size_t)mCurrentHeap - (size_t)mHeap>mMaxAlloc)
2013-04-22 05:25:59 +00:00
{
2013-04-23 00:25:52 +00:00
mMaxAlloc=(size_t)mCurrentHeap - (size_t)mHeap;
2013-04-22 05:25:59 +00:00
}
mCurrentHeap = mHeap;
}
// initialise the heap
CMiniHeap(int size)
{
mHeap = (char *)Z_Malloc(size, TAG_GHOUL2, qtrue);
mSize = size;
mMaxAlloc=0;
if (mHeap)
{
ResetHeap();
}
}
// free up the heap
~CMiniHeap()
{
if (mHeap)
{
Z_Free(mHeap);
}
#if _DEBUG
char mess[1000];
sprintf(mess,"Max miniheap allocated %d\n",mMaxAlloc);
OutputDebugString(mess);
#endif
}
// give me some space from the heap please
char *MiniHeapAlloc(int size)
{
2013-04-23 00:25:52 +00:00
if (size < (mSize - ((size_t)mCurrentHeap - (size_t)mHeap)))
2013-04-22 05:25:59 +00:00
{
char *tempAddress = mCurrentHeap;
mCurrentHeap += size;
return tempAddress;
}
return NULL;
}
};
extern CMiniHeap *G2VertSpaceServer;
#endif //MINIHEAP_H_INC