Reduce the amount of blocks of memory DataChunker uses

This commit is contained in:
James Urquhart 2016-09-30 18:35:14 +01:00
parent 57dfeb829a
commit c2cc7142de
2 changed files with 59 additions and 27 deletions

View file

@ -41,7 +41,9 @@ void *DataChunker::alloc(S32 size)
{ {
if (size > mChunkSize) if (size > mChunkSize)
{ {
DataBlock * temp = new DataBlock(size); DataBlock * temp = (DataBlock*)dMalloc(DataChunker::PaddDBSize + size);
AssertFatal(temp, "Malloc failed");
constructInPlace(temp);
if (mCurBlock) if (mCurBlock)
{ {
temp->next = mCurBlock->next; temp->next = mCurBlock->next;
@ -52,30 +54,32 @@ void *DataChunker::alloc(S32 size)
mCurBlock = temp; mCurBlock = temp;
temp->curIndex = mChunkSize; temp->curIndex = mChunkSize;
} }
return temp->data; return temp->getData();
} }
if(!mCurBlock || size + mCurBlock->curIndex > mChunkSize) if(!mCurBlock || size + mCurBlock->curIndex > mChunkSize)
{ {
DataBlock *temp = new DataBlock(mChunkSize); const U32 paddDBSize = (sizeof(DataBlock) + 3) & ~3;
DataBlock *temp = (DataBlock*)dMalloc(paddDBSize+ mChunkSize);
AssertFatal(temp, "Malloc failed");
constructInPlace(temp);
temp->next = mCurBlock; temp->next = mCurBlock;
temp->curIndex = 0;
mCurBlock = temp; mCurBlock = temp;
} }
void *ret = mCurBlock->data + mCurBlock->curIndex; void *ret = mCurBlock->getData() + mCurBlock->curIndex;
mCurBlock->curIndex += (size + 3) & ~3; // dword align mCurBlock->curIndex += (size + 3) & ~3; // dword align
return ret; return ret;
} }
DataChunker::DataBlock::DataBlock(S32 size) DataChunker::DataBlock::DataBlock()
{ {
data = new U8[size]; curIndex = 0;
next = NULL;
} }
DataChunker::DataBlock::~DataBlock() DataChunker::DataBlock::~DataBlock()
{ {
delete[] data;
} }
void DataChunker::freeBlocks(bool keepOne) void DataChunker::freeBlocks(bool keepOne)
@ -83,15 +87,17 @@ void DataChunker::freeBlocks(bool keepOne)
while(mCurBlock && mCurBlock->next) while(mCurBlock && mCurBlock->next)
{ {
DataBlock *temp = mCurBlock->next; DataBlock *temp = mCurBlock->next;
delete mCurBlock; dFree(mCurBlock);
mCurBlock = temp; mCurBlock = temp;
} }
if (!keepOne) if (!keepOne)
{ {
delete mCurBlock; if (mCurBlock) dFree(mCurBlock);
mCurBlock = NULL; mCurBlock = NULL;
} }
else if (mCurBlock) else if (mCurBlock)
{
mCurBlock->curIndex = 0; mCurBlock->curIndex = 0;
mCurBlock->next = NULL;
}
} }

View file

@ -46,8 +46,21 @@
class DataChunker class DataChunker
{ {
public: public:
/// Block of allocated memory.
///
/// <b>This has nothing to do with datablocks as used in the rest of Torque.</b>
struct DataBlock
{
DataBlock* next; ///< linked list pointer to the next DataBlock for this chunker
S32 curIndex; ///< current allocation point within this DataBlock
DataBlock();
~DataBlock();
inline U8 *getData();
};
enum { enum {
ChunkSize = 16376 ///< Default size of each DataBlock page in the DataChunker PaddDBSize = (sizeof(DataBlock) + 3) & ~3, ///< Padded size of DataBlock
ChunkSize = 16384 - PaddDBSize ///< Default size of each DataBlock page in the DataChunker
}; };
/// Return a pointer to a chunk of memory from a pre-allocated block. /// Return a pointer to a chunk of memory from a pre-allocated block.
@ -81,27 +94,40 @@ public:
mCurBlock = temp; mCurBlock = temp;
} }
private: public:
/// Block of allocated memory. U32 countUsedBlocks()
///
/// <b>This has nothing to do with datablocks as used in the rest of Torque.</b>
struct DataBlock
{ {
DataBlock* prev; U32 count = 0;
DataBlock* next; ///< linked list pointer to the next DataBlock for this chunker if (!mCurBlock)
U8 *data; ///< allocated pointer for the base of this page return 0;
S32 curIndex; ///< current allocation point within this DataBlock for (DataBlock *ptr = mCurBlock; ptr != NULL; ptr = ptr->next)
DataBlock(S32 size); {
~DataBlock(); count++;
}; }
return count;
}
void setChunkSize(U32 size)
{
AssertFatal(mCurBlock == NULL, "Cant resize now");
mChunkSize = size;
}
DataBlock* mFirstBlock;
DataBlock *mCurBlock; ///< current page we're allocating data from. If the public:
DataBlock* mCurBlock; ///< current page we're allocating data from. If the
///< data size request is greater than the memory space currently ///< data size request is greater than the memory space currently
///< available in the current page, a new page will be allocated. ///< available in the current page, a new page will be allocated.
S32 mChunkSize; ///< The size allocated for each page in the DataChunker S32 mChunkSize; ///< The size allocated for each page in the DataChunker
}; };
inline U8 *DataChunker::DataBlock::getData()
{
return (U8*)this + DataChunker::PaddDBSize;
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
template<class T> template<class T>