The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.0KB

  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 "b2BlockAllocator.h"
  19. #include <cstdlib>
  20. #include <climits>
  21. #include <cstring>
  22. #include <memory>
  23. using namespace std;
  24. int32 b2BlockAllocator::s_blockSizes[b2_blockSizes] =
  25. {
  26. 16, // 0
  27. 32, // 1
  28. 64, // 2
  29. 96, // 3
  30. 128, // 4
  31. 160, // 5
  32. 192, // 6
  33. 224, // 7
  34. 256, // 8
  35. 320, // 9
  36. 384, // 10
  37. 448, // 11
  38. 512, // 12
  39. 640, // 13
  40. };
  41. uint8 b2BlockAllocator::s_blockSizeLookup[b2_maxBlockSize + 1];
  42. bool b2BlockAllocator::s_blockSizeLookupInitialized;
  43. struct b2Chunk
  44. {
  45. int32 blockSize;
  46. b2Block* blocks;
  47. };
  48. struct b2Block
  49. {
  50. b2Block* next;
  51. };
  52. b2BlockAllocator::b2BlockAllocator()
  53. {
  54. b2Assert(b2_blockSizes < UCHAR_MAX);
  55. m_chunkSpace = b2_chunkArrayIncrement;
  56. m_chunkCount = 0;
  57. m_chunks = (b2Chunk*)b2Alloc(m_chunkSpace * sizeof(b2Chunk));
  58. memset(m_chunks, 0, m_chunkSpace * sizeof(b2Chunk));
  59. memset(m_freeLists, 0, sizeof(m_freeLists));
  60. if (s_blockSizeLookupInitialized == false)
  61. {
  62. int32 j = 0;
  63. for (int32 i = 1; i <= b2_maxBlockSize; ++i)
  64. {
  65. b2Assert(j < b2_blockSizes);
  66. if (i <= s_blockSizes[j])
  67. {
  68. s_blockSizeLookup[i] = (uint8)j;
  69. }
  70. else
  71. {
  72. ++j;
  73. s_blockSizeLookup[i] = (uint8)j;
  74. }
  75. }
  76. s_blockSizeLookupInitialized = true;
  77. }
  78. }
  79. b2BlockAllocator::~b2BlockAllocator()
  80. {
  81. for (int32 i = 0; i < m_chunkCount; ++i)
  82. {
  83. b2Free(m_chunks[i].blocks);
  84. }
  85. b2Free(m_chunks);
  86. }
  87. void* b2BlockAllocator::Allocate(int32 size)
  88. {
  89. if (size == 0)
  90. return NULL;
  91. b2Assert(0 < size);
  92. if (size > b2_maxBlockSize)
  93. {
  94. return b2Alloc(size);
  95. }
  96. int32 index = s_blockSizeLookup[size];
  97. b2Assert(0 <= index && index < b2_blockSizes);
  98. if (m_freeLists[index])
  99. {
  100. b2Block* block = m_freeLists[index];
  101. m_freeLists[index] = block->next;
  102. return block;
  103. }
  104. else
  105. {
  106. if (m_chunkCount == m_chunkSpace)
  107. {
  108. b2Chunk* oldChunks = m_chunks;
  109. m_chunkSpace += b2_chunkArrayIncrement;
  110. m_chunks = (b2Chunk*)b2Alloc(m_chunkSpace * sizeof(b2Chunk));
  111. memcpy(m_chunks, oldChunks, m_chunkCount * sizeof(b2Chunk));
  112. memset(m_chunks + m_chunkCount, 0, b2_chunkArrayIncrement * sizeof(b2Chunk));
  113. b2Free(oldChunks);
  114. }
  115. b2Chunk* chunk = m_chunks + m_chunkCount;
  116. chunk->blocks = (b2Block*)b2Alloc(b2_chunkSize);
  117. #if defined(_DEBUG)
  118. memset(chunk->blocks, 0xcd, b2_chunkSize);
  119. #endif
  120. int32 blockSize = s_blockSizes[index];
  121. chunk->blockSize = blockSize;
  122. int32 blockCount = b2_chunkSize / blockSize;
  123. b2Assert(blockCount * blockSize <= b2_chunkSize);
  124. for (int32 i = 0; i < blockCount - 1; ++i)
  125. {
  126. b2Block* block = (b2Block*)((int8*)chunk->blocks + blockSize * i);
  127. b2Block* next = (b2Block*)((int8*)chunk->blocks + blockSize * (i + 1));
  128. block->next = next;
  129. }
  130. b2Block* last = (b2Block*)((int8*)chunk->blocks + blockSize * (blockCount - 1));
  131. last->next = NULL;
  132. m_freeLists[index] = chunk->blocks->next;
  133. ++m_chunkCount;
  134. return chunk->blocks;
  135. }
  136. }
  137. void b2BlockAllocator::Free(void* p, int32 size)
  138. {
  139. if (size == 0)
  140. {
  141. return;
  142. }
  143. b2Assert(0 < size);
  144. if (size > b2_maxBlockSize)
  145. {
  146. b2Free(p);
  147. return;
  148. }
  149. int32 index = s_blockSizeLookup[size];
  150. b2Assert(0 <= index && index < b2_blockSizes);
  151. #ifdef _DEBUG
  152. // Verify the memory address and size is valid.
  153. int32 blockSize = s_blockSizes[index];
  154. bool found = false;
  155. for (int32 i = 0; i < m_chunkCount; ++i)
  156. {
  157. b2Chunk* chunk = m_chunks + i;
  158. if (chunk->blockSize != blockSize)
  159. {
  160. b2Assert( (int8*)p + blockSize <= (int8*)chunk->blocks ||
  161. (int8*)chunk->blocks + b2_chunkSize <= (int8*)p);
  162. }
  163. else
  164. {
  165. if ((int8*)chunk->blocks <= (int8*)p && (int8*)p + blockSize <= (int8*)chunk->blocks + b2_chunkSize)
  166. {
  167. found = true;
  168. }
  169. }
  170. }
  171. b2Assert(found);
  172. memset(p, 0xfd, blockSize);
  173. #endif
  174. b2Block* block = (b2Block*)p;
  175. block->next = m_freeLists[index];
  176. m_freeLists[index] = block;
  177. }
  178. void b2BlockAllocator::Clear()
  179. {
  180. for (int32 i = 0; i < m_chunkCount; ++i)
  181. {
  182. b2Free(m_chunks[i].blocks);
  183. }
  184. m_chunkCount = 0;
  185. memset(m_chunks, 0, m_chunkSpace * sizeof(b2Chunk));
  186. memset(m_freeLists, 0, sizeof(m_freeLists));
  187. }