Game Development Community

Is DataChunker memory contiguous?

by Lateral Punk · in Torque Game Engine · 06/09/2006 (11:44 am) · 5 replies

What I mean is that are the DataBlocks that are allocated contigous? Now I know the DataBlocks themselves are not contigous in memory, but does the manner in which DataChunker link-list them up give the resemblence of them being contigous? Maybe an example will help:
DataChunker chunker = DataChunker(1024);

//this alloc will not really alloc new bytes because
//the DataChunker contrsctuor already allocates one DataBlock
U8 *data1 = chunker.alloc(1024);

//this alloc will allocate a new DataBlock
U8 *data2 = chunker.alloc(1024);

//now here is what I'm wondering if i can do:
dMemset(data1,0,2048);

//or could I do this
dMemcpy(data1,somebuffer,2048);

Are the last two statements valid, or am I going to crash my program because the DataBlocks are not really linked up like I thought they would be? Basically I'm just trying to copy/clean starting at data1 for 2048 bytes (after all I did allocate that much). I believe this will not work as I would hope it would, but if anyone knows better, please let me know.

thanks

ps.
what does this really do?
void *DataChunker::alloc(S32 size)
{
.....
curBlock->curIndex += (size + 3) & ~3; // dword align
}

#1
06/09/2006 (11:52 am)
I think treating two consecutively allocated blocks as actually contiguous would be a risky proposition.
#2
06/09/2006 (11:55 am)
That's what I thought too, but the DataChunker does some special things (that dword align thing) for one thing, so I am actually questioning the capability of Datachunker itself. And the way it links it up in a list makes me think that maybe it is contiguous, but I do highly doubt it.
#3
06/09/2006 (12:19 pm)
I ran some more trivial test cases and have concluded that the DataChunker is not really maintaining the DataBlocks in contiguous form. The link list is just for organization purpose. So in summation, don't try to use DataChunker as a replacement for this:


U8 *buffer = (U8*) dMalloc( 1024);
.....
buffer = (U8*) dRealloc(buffer,1024);

it won't work!
#4
06/09/2006 (12:29 pm)
Going through the DataChunker code I think there could be a potential issue if we don't insert this line in ::freeBlocks:

void DataChunker::freeBlocks()
{
   while(curBlock)
   {
      DataBlock *temp = curBlock->next;
      delete curBlock;
      curBlock = temp;
   }
   curBlock = NULL;         //insert this line
}


and just want peoples opionion on this. do you think cleaning out the newly allocated DataBlock like this:

DataChunker::DataBlock::DataBlock(S32 size)
{
   data = new U8[size];
   dMemset(data,0,size);   //I inserted this line
}

Do you think the dMemset that i inserted is good? I think it's always good to cleanup memory before use, but there's always been "discussion" in the C/C++ community about the speed of memset, memcpy, and all of those kinds of functions. Anyone got any input on this?
#5
06/09/2006 (12:46 pm)
Thanks to Orion for the ::freeBlocks() comment, I didn't realize curBlock would already be assigned to NULL at the end of the loop, what was I thinking :(
We also discussed the dMemset, and came to the conclusion that the rest of TGE doesn't follow the convention of clearing memory before use, so just to be consistent, we will not either :)