dataChunker.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _DATACHUNKER_H_
  23. #define _DATACHUNKER_H_
  24. #ifndef _MMATHFN_H_
  25. #include "math/mMathFn.h"
  26. #endif
  27. //----------------------------------------------------------------------------
  28. /// Implements a chunked data allocater.
  29. ///
  30. /// Calling new/malloc all the time is a time consuming operation. Therefore,
  31. /// we provide the DataChunker, which allocates memory in blockss of
  32. /// chunkSize (by default 16k, see ChunkSize, though it can be set in
  33. /// the constructor), then doles it out as requested, in chunks of up to
  34. /// chunkSize in size.
  35. ///
  36. /// It will assert if you try to get more than ChunkSize bytes at a time,
  37. /// and it deals with the logic of allocating new blocks and giving out
  38. /// word-aligned chunks.
  39. ///
  40. /// Note that new/free/realloc WILL NOT WORK on memory gotten from the
  41. /// DataChunker. This also only grows (you can call freeBlocks to deallocate
  42. /// and reset things).
  43. class DataChunker
  44. {
  45. public:
  46. enum {
  47. ChunkSize = 16376 ///< Default size for chunks.
  48. };
  49. private:
  50. /// Block of allocated memory.
  51. ///
  52. /// <b>This has nothing to do with datablocks as used in the rest of Torque.</b>
  53. struct DataBlock
  54. {
  55. DataBlock *next;
  56. U8 *data;
  57. S32 curIndex;
  58. DataBlock(S32 size);
  59. ~DataBlock();
  60. };
  61. DataBlock *curBlock;
  62. S32 chunkSize;
  63. public:
  64. /// Return a pointer to a chunk of memory from a pre-allocated block.
  65. ///
  66. /// This memory goes away when you call freeBlocks.
  67. ///
  68. /// This memory is word-aligned.
  69. /// @param size Size of chunk to return. This must be less than chunkSize or else
  70. /// an assertion will occur.
  71. void *alloc(S32 size);
  72. /// Free all allocated memory blocks.
  73. ///
  74. /// This invalidates all pointers returned from alloc().
  75. void freeBlocks();
  76. /// Initialize using blocks of a given size.
  77. ///
  78. /// One new block is allocated at constructor-time.
  79. ///
  80. /// @param size Size in bytes of the space to allocate for each block.
  81. DataChunker(S32 size=ChunkSize);
  82. ~DataChunker();
  83. };
  84. //----------------------------------------------------------------------------
  85. template<class T>
  86. class Chunker: private DataChunker
  87. {
  88. public:
  89. Chunker(S32 size = DataChunker::ChunkSize) : DataChunker(size) {};
  90. T* alloc() { return reinterpret_cast<T*>(DataChunker::alloc(S32(sizeof(T)))); }
  91. void clear() { freeBlocks(); };
  92. };
  93. template<class T>
  94. class FreeListChunker: private DataChunker
  95. {
  96. S32 numAllocated;
  97. S32 elementSize;
  98. T *freeListHead;
  99. public:
  100. FreeListChunker(S32 size = DataChunker::ChunkSize) : DataChunker(size)
  101. {
  102. numAllocated = 0;
  103. elementSize = getMax(U32(sizeof(T)), U32(sizeof(T *)));
  104. freeListHead = NULL;
  105. }
  106. T *alloc()
  107. {
  108. numAllocated++;
  109. if(freeListHead == NULL)
  110. return reinterpret_cast<T*>(DataChunker::alloc(elementSize));
  111. T* ret = freeListHead;
  112. freeListHead = *(reinterpret_cast<T**>(freeListHead));
  113. return ret;
  114. }
  115. void free(T* elem)
  116. {
  117. numAllocated--;
  118. *(reinterpret_cast<T**>(elem)) = freeListHead;
  119. freeListHead = elem;
  120. // If nothing's allocated, clean up!
  121. if(!numAllocated)
  122. {
  123. freeBlocks();
  124. freeListHead = NULL;
  125. }
  126. }
  127. // Allow people to free all their memory if they want.
  128. void freeBlocks()
  129. {
  130. DataChunker::freeBlocks();
  131. // We have to terminate the freelist as well or else we'll run
  132. // into crazy unused memory.
  133. freeListHead = NULL;
  134. }
  135. };
  136. #endif