dataChunker.cpp 2.9 KB

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