dataChunker.cpp
Engine/source/core/dataChunker.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "core/dataChunker.h" 26 27 28//---------------------------------------------------------------------------- 29 30DataChunker::DataChunker(S32 size) 31{ 32 mChunkSize = size; 33 mCurBlock = NULL; 34} 35 36DataChunker::~DataChunker() 37{ 38 freeBlocks(); 39} 40 41void *DataChunker::alloc(S32 size) 42{ 43 if (size > mChunkSize) 44 { 45 DataBlock * temp = (DataBlock*)dMalloc(DataChunker::PaddDBSize + size); 46 AssertFatal(temp, "Malloc failed"); 47 constructInPlace(temp); 48 if (mCurBlock) 49 { 50 temp->next = mCurBlock->next; 51 mCurBlock->next = temp; 52 } 53 else 54 { 55 mCurBlock = temp; 56 temp->curIndex = mChunkSize; 57 } 58 return temp->getData(); 59 } 60 61 if(!mCurBlock || size + mCurBlock->curIndex > mChunkSize) 62 { 63 const U32 paddDBSize = (sizeof(DataBlock) + 3) & ~3; 64 DataBlock *temp = (DataBlock*)dMalloc(paddDBSize+ mChunkSize); 65 AssertFatal(temp, "Malloc failed"); 66 constructInPlace(temp); 67 temp->next = mCurBlock; 68 mCurBlock = temp; 69 } 70 71 void *ret = mCurBlock->getData() + mCurBlock->curIndex; 72 mCurBlock->curIndex += (size + 3) & ~3; // dword align 73 return ret; 74} 75 76DataChunker::DataBlock::DataBlock() 77{ 78 curIndex = 0; 79 next = NULL; 80} 81 82DataChunker::DataBlock::~DataBlock() 83{ 84} 85 86void DataChunker::freeBlocks(bool keepOne) 87{ 88 while(mCurBlock && mCurBlock->next) 89 { 90 DataBlock *temp = mCurBlock->next; 91 dFree(mCurBlock); 92 mCurBlock = temp; 93 } 94 if (!keepOne) 95 { 96 if (mCurBlock) dFree(mCurBlock); 97 mCurBlock = NULL; 98 } 99 else if (mCurBlock) 100 { 101 mCurBlock->curIndex = 0; 102 mCurBlock->next = NULL; 103 } 104} 105