b2BlockAllocator.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Common/b2BlockAllocator.h>
  19. #include <limits.h>
  20. #include <string.h>
  21. #include <stddef.h>
  22. int32 b2BlockAllocator::s_blockSizes[b2_blockSizes] =
  23. {
  24. 16, // 0
  25. 32, // 1
  26. 64, // 2
  27. 96, // 3
  28. 128, // 4
  29. 160, // 5
  30. 192, // 6
  31. 224, // 7
  32. 256, // 8
  33. 320, // 9
  34. 384, // 10
  35. 448, // 11
  36. 512, // 12
  37. 640, // 13
  38. };
  39. uint8 b2BlockAllocator::s_blockSizeLookup[b2_maxBlockSize + 1];
  40. bool b2BlockAllocator::s_blockSizeLookupInitialized;
  41. struct b2Chunk
  42. {
  43. int32 blockSize;
  44. b2Block* blocks;
  45. };
  46. struct b2Block
  47. {
  48. b2Block* next;
  49. };
  50. b2BlockAllocator::b2BlockAllocator()
  51. {
  52. b2Assert(b2_blockSizes < UCHAR_MAX);
  53. m_chunkSpace = b2_chunkArrayIncrement;
  54. m_chunkCount = 0;
  55. m_chunks = (b2Chunk*)b2Alloc(m_chunkSpace * sizeof(b2Chunk));
  56. memset(m_chunks, 0, m_chunkSpace * sizeof(b2Chunk));
  57. memset(m_freeLists, 0, sizeof(m_freeLists));
  58. if (s_blockSizeLookupInitialized == false)
  59. {
  60. int32 j = 0;
  61. for (int32 i = 1; i <= b2_maxBlockSize; ++i)
  62. {
  63. b2Assert(j < b2_blockSizes);
  64. if (i <= s_blockSizes[j])
  65. {
  66. s_blockSizeLookup[i] = (uint8)j;
  67. }
  68. else
  69. {
  70. ++j;
  71. s_blockSizeLookup[i] = (uint8)j;
  72. }
  73. }
  74. s_blockSizeLookupInitialized = true;
  75. }
  76. }
  77. b2BlockAllocator::~b2BlockAllocator()
  78. {
  79. for (int32 i = 0; i < m_chunkCount; ++i)
  80. {
  81. b2Free(m_chunks[i].blocks);
  82. }
  83. b2Free(m_chunks);
  84. }
  85. void* b2BlockAllocator::Allocate(int32 size)
  86. {
  87. if (size == 0)
  88. return NULL;
  89. b2Assert(0 < size);
  90. if (size > b2_maxBlockSize)
  91. {
  92. return b2Alloc(size);
  93. }
  94. int32 index = s_blockSizeLookup[size];
  95. b2Assert(0 <= index && index < b2_blockSizes);
  96. if (m_freeLists[index])
  97. {
  98. b2Block* block = m_freeLists[index];
  99. m_freeLists[index] = block->next;
  100. return block;
  101. }
  102. else
  103. {
  104. if (m_chunkCount == m_chunkSpace)
  105. {
  106. b2Chunk* oldChunks = m_chunks;
  107. m_chunkSpace += b2_chunkArrayIncrement;
  108. m_chunks = (b2Chunk*)b2Alloc(m_chunkSpace * sizeof(b2Chunk));
  109. memcpy(m_chunks, oldChunks, m_chunkCount * sizeof(b2Chunk));
  110. memset(m_chunks + m_chunkCount, 0, b2_chunkArrayIncrement * sizeof(b2Chunk));
  111. b2Free(oldChunks);
  112. }
  113. b2Chunk* chunk = m_chunks + m_chunkCount;
  114. chunk->blocks = (b2Block*)b2Alloc(b2_chunkSize);
  115. #if defined(_DEBUG)
  116. memset(chunk->blocks, 0xcd, b2_chunkSize);
  117. #endif
  118. int32 blockSize = s_blockSizes[index];
  119. chunk->blockSize = blockSize;
  120. int32 blockCount = b2_chunkSize / blockSize;
  121. b2Assert(blockCount * blockSize <= b2_chunkSize);
  122. for (int32 i = 0; i < blockCount - 1; ++i)
  123. {
  124. b2Block* block = (b2Block*)((int8*)chunk->blocks + blockSize * i);
  125. b2Block* next = (b2Block*)((int8*)chunk->blocks + blockSize * (i + 1));
  126. block->next = next;
  127. }
  128. b2Block* last = (b2Block*)((int8*)chunk->blocks + blockSize * (blockCount - 1));
  129. last->next = NULL;
  130. m_freeLists[index] = chunk->blocks->next;
  131. ++m_chunkCount;
  132. return chunk->blocks;
  133. }
  134. }
  135. void b2BlockAllocator::Free(void* p, int32 size)
  136. {
  137. if (size == 0)
  138. {
  139. return;
  140. }
  141. b2Assert(0 < size);
  142. if (size > b2_maxBlockSize)
  143. {
  144. b2Free(p);
  145. return;
  146. }
  147. int32 index = s_blockSizeLookup[size];
  148. b2Assert(0 <= index && index < b2_blockSizes);
  149. #ifdef _DEBUG
  150. // Verify the memory address and size is valid.
  151. int32 blockSize = s_blockSizes[index];
  152. bool found = false;
  153. for (int32 i = 0; i < m_chunkCount; ++i)
  154. {
  155. b2Chunk* chunk = m_chunks + i;
  156. if (chunk->blockSize != blockSize)
  157. {
  158. b2Assert( (int8*)p + blockSize <= (int8*)chunk->blocks ||
  159. (int8*)chunk->blocks + b2_chunkSize <= (int8*)p);
  160. }
  161. else
  162. {
  163. if ((int8*)chunk->blocks <= (int8*)p && (int8*)p + blockSize <= (int8*)chunk->blocks + b2_chunkSize)
  164. {
  165. found = true;
  166. }
  167. }
  168. }
  169. b2Assert(found);
  170. memset(p, 0xfd, blockSize);
  171. #endif
  172. b2Block* block = (b2Block*)p;
  173. block->next = m_freeLists[index];
  174. m_freeLists[index] = block;
  175. }
  176. void b2BlockAllocator::Clear()
  177. {
  178. for (int32 i = 0; i < m_chunkCount; ++i)
  179. {
  180. b2Free(m_chunks[i].blocks);
  181. }
  182. m_chunkCount = 0;
  183. memset(m_chunks, 0, m_chunkSpace * sizeof(b2Chunk));
  184. memset(m_freeLists, 0, sizeof(m_freeLists));
  185. }