btGenericPoolAllocator.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*! \file btGenericPoolAllocator.cpp
  2. \author Francisco Leon Najera. email projectileman@yahoo.com
  3. General purpose allocator class
  4. */
  5. /*
  6. Bullet Continuous Collision Detection and Physics Library
  7. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  8. This software is provided 'as-is', without any express or implied warranty.
  9. In no event will the authors be held liable for any damages arising from the use of this software.
  10. Permission is granted to anyone to use this software for any purpose,
  11. including commercial applications, and to alter it and redistribute it freely,
  12. subject to the following restrictions:
  13. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "btGenericPoolAllocator.h"
  18. /// *************** btGenericMemoryPool ******************///////////
  19. size_t btGenericMemoryPool::allocate_from_free_nodes(size_t num_elements)
  20. {
  21. size_t ptr = BT_UINT_MAX;
  22. if(m_free_nodes_count == 0) return BT_UINT_MAX;
  23. // find an avaliable free node with the correct size
  24. size_t revindex = m_free_nodes_count;
  25. while(revindex-- && ptr == BT_UINT_MAX)
  26. {
  27. if(m_allocated_sizes[m_free_nodes[revindex]]>=num_elements)
  28. {
  29. ptr = revindex;
  30. }
  31. }
  32. if(ptr == BT_UINT_MAX) return BT_UINT_MAX; // not found
  33. revindex = ptr;
  34. ptr = m_free_nodes[revindex];
  35. // post: ptr contains the node index, and revindex the index in m_free_nodes
  36. size_t finalsize = m_allocated_sizes[ptr];
  37. finalsize -= num_elements;
  38. m_allocated_sizes[ptr] = num_elements;
  39. // post: finalsize>=0, m_allocated_sizes[ptr] has the requested size
  40. if(finalsize>0) // preserve free node, there are some free memory
  41. {
  42. m_free_nodes[revindex] = ptr + num_elements;
  43. m_allocated_sizes[ptr + num_elements] = finalsize;
  44. }
  45. else // delete free node
  46. {
  47. // swap with end
  48. m_free_nodes[revindex] = m_free_nodes[m_free_nodes_count-1];
  49. m_free_nodes_count--;
  50. }
  51. return ptr;
  52. }
  53. size_t btGenericMemoryPool::allocate_from_pool(size_t num_elements)
  54. {
  55. if(m_allocated_count+num_elements>m_max_element_count) return BT_UINT_MAX;
  56. size_t ptr = m_allocated_count;
  57. m_allocated_sizes[m_allocated_count] = num_elements;
  58. m_allocated_count+=num_elements;
  59. return ptr;
  60. }
  61. void btGenericMemoryPool::init_pool(size_t element_size, size_t element_count)
  62. {
  63. m_allocated_count = 0;
  64. m_free_nodes_count = 0;
  65. m_element_size = element_size;
  66. m_max_element_count = element_count;
  67. m_pool = (unsigned char *) btAlignedAlloc(m_element_size*m_max_element_count,16);
  68. m_free_nodes = (size_t *) btAlignedAlloc(sizeof(size_t)*m_max_element_count,16);
  69. m_allocated_sizes = (size_t *) btAlignedAlloc(sizeof(size_t)*m_max_element_count,16);
  70. for (size_t i = 0;i< m_max_element_count;i++ )
  71. {
  72. m_allocated_sizes[i] = 0;
  73. }
  74. }
  75. void btGenericMemoryPool::end_pool()
  76. {
  77. btAlignedFree(m_pool);
  78. btAlignedFree(m_free_nodes);
  79. btAlignedFree(m_allocated_sizes);
  80. m_allocated_count = 0;
  81. m_free_nodes_count = 0;
  82. }
  83. //! Allocates memory in pool
  84. /*!
  85. \param size_bytes size in bytes of the buffer
  86. */
  87. void * btGenericMemoryPool::allocate(size_t size_bytes)
  88. {
  89. size_t module = size_bytes%m_element_size;
  90. size_t element_count = size_bytes/m_element_size;
  91. if(module>0) element_count++;
  92. size_t alloc_pos = allocate_from_free_nodes(element_count);
  93. // a free node is found
  94. if(alloc_pos != BT_UINT_MAX)
  95. {
  96. return get_element_data(alloc_pos);
  97. }
  98. // allocate directly on pool
  99. alloc_pos = allocate_from_pool(element_count);
  100. if(alloc_pos == BT_UINT_MAX) return NULL; // not space
  101. return get_element_data(alloc_pos);
  102. }
  103. bool btGenericMemoryPool::freeMemory(void * pointer)
  104. {
  105. unsigned char * pointer_pos = (unsigned char *)pointer;
  106. unsigned char * pool_pos = (unsigned char *)m_pool;
  107. // calc offset
  108. if(pointer_pos<pool_pos) return false;//other pool
  109. size_t offset = size_t(pointer_pos - pool_pos);
  110. if(offset>=get_pool_capacity()) return false;// far away
  111. // find free position
  112. m_free_nodes[m_free_nodes_count] = offset/m_element_size;
  113. m_free_nodes_count++;
  114. return true;
  115. }
  116. /// *******************! btGenericPoolAllocator *******************!///
  117. btGenericPoolAllocator::~btGenericPoolAllocator()
  118. {
  119. // destroy pools
  120. size_t i;
  121. for (i=0;i<m_pool_count;i++)
  122. {
  123. m_pools[i]->end_pool();
  124. btAlignedFree(m_pools[i]);
  125. }
  126. }
  127. // creates a pool
  128. btGenericMemoryPool * btGenericPoolAllocator::push_new_pool()
  129. {
  130. if(m_pool_count >= BT_DEFAULT_MAX_POOLS) return NULL;
  131. btGenericMemoryPool * newptr = (btGenericMemoryPool *)btAlignedAlloc(sizeof(btGenericMemoryPool),16);
  132. m_pools[m_pool_count] = newptr;
  133. m_pools[m_pool_count]->init_pool(m_pool_element_size,m_pool_element_count);
  134. m_pool_count++;
  135. return newptr;
  136. }
  137. void * btGenericPoolAllocator::failback_alloc(size_t size_bytes)
  138. {
  139. btGenericMemoryPool * pool = NULL;
  140. if(size_bytes<=get_pool_capacity())
  141. {
  142. pool = push_new_pool();
  143. }
  144. if(pool==NULL) // failback
  145. {
  146. return btAlignedAlloc(size_bytes,16);
  147. }
  148. return pool->allocate(size_bytes);
  149. }
  150. bool btGenericPoolAllocator::failback_free(void * pointer)
  151. {
  152. btAlignedFree(pointer);
  153. return true;
  154. }
  155. //! Allocates memory in pool
  156. /*!
  157. \param size_bytes size in bytes of the buffer
  158. */
  159. void * btGenericPoolAllocator::allocate(size_t size_bytes)
  160. {
  161. void * ptr = NULL;
  162. size_t i = 0;
  163. while(i<m_pool_count && ptr == NULL)
  164. {
  165. ptr = m_pools[i]->allocate(size_bytes);
  166. ++i;
  167. }
  168. if(ptr) return ptr;
  169. return failback_alloc(size_bytes);
  170. }
  171. bool btGenericPoolAllocator::freeMemory(void * pointer)
  172. {
  173. bool result = false;
  174. size_t i = 0;
  175. while(i<m_pool_count && result == false)
  176. {
  177. result = m_pools[i]->freeMemory(pointer);
  178. ++i;
  179. }
  180. if(result) return true;
  181. return failback_free(pointer);
  182. }
  183. /// ************** STANDARD ALLOCATOR ***************************///
  184. #define BT_DEFAULT_POOL_SIZE 32768
  185. #define BT_DEFAULT_POOL_ELEMENT_SIZE 8
  186. // main allocator
  187. class GIM_STANDARD_ALLOCATOR: public btGenericPoolAllocator
  188. {
  189. public:
  190. GIM_STANDARD_ALLOCATOR():btGenericPoolAllocator(BT_DEFAULT_POOL_ELEMENT_SIZE,BT_DEFAULT_POOL_SIZE)
  191. {
  192. }
  193. };
  194. // global allocator
  195. GIM_STANDARD_ALLOCATOR g_main_allocator;
  196. void * btPoolAlloc(size_t size)
  197. {
  198. return g_main_allocator.allocate(size);
  199. }
  200. void * btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
  201. {
  202. void * newptr = btPoolAlloc(newsize);
  203. size_t copysize = oldsize<newsize?oldsize:newsize;
  204. memcpy(newptr,ptr,copysize);
  205. btPoolFree(ptr);
  206. return newptr;
  207. }
  208. void btPoolFree(void *ptr)
  209. {
  210. g_main_allocator.freeMemory(ptr);
  211. }