btPoolAllocator.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 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.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #ifndef _BT_POOL_ALLOCATOR_H
  13. #define _BT_POOL_ALLOCATOR_H
  14. #include "btScalar.h"
  15. #include "btAlignedAllocator.h"
  16. ///The btPoolAllocator class allows to efficiently allocate a large pool of objects, instead of dynamically allocating them separately.
  17. class btPoolAllocator
  18. {
  19. int m_elemSize;
  20. int m_maxElements;
  21. int m_freeCount;
  22. void* m_firstFree;
  23. unsigned char* m_pool;
  24. public:
  25. btPoolAllocator(int elemSize, int maxElements)
  26. :m_elemSize(elemSize),
  27. m_maxElements(maxElements)
  28. {
  29. m_pool = (unsigned char*) btAlignedAlloc( static_cast<unsigned int>(m_elemSize*m_maxElements),16);
  30. unsigned char* p = m_pool;
  31. m_firstFree = p;
  32. m_freeCount = m_maxElements;
  33. int count = m_maxElements;
  34. while (--count) {
  35. *(void**)p = (p + m_elemSize);
  36. p += m_elemSize;
  37. }
  38. *(void**)p = 0;
  39. }
  40. ~btPoolAllocator()
  41. {
  42. btAlignedFree( m_pool);
  43. }
  44. int getFreeCount() const
  45. {
  46. return m_freeCount;
  47. }
  48. int getUsedCount() const
  49. {
  50. return m_maxElements - m_freeCount;
  51. }
  52. int getMaxCount() const
  53. {
  54. return m_maxElements;
  55. }
  56. void* allocate(int size)
  57. {
  58. // release mode fix
  59. (void)size;
  60. btAssert(!size || size<=m_elemSize);
  61. btAssert(m_freeCount>0);
  62. void* result = m_firstFree;
  63. m_firstFree = *(void**)m_firstFree;
  64. --m_freeCount;
  65. return result;
  66. }
  67. bool validPtr(void* ptr)
  68. {
  69. if (ptr) {
  70. if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize))
  71. {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. void freeMemory(void* ptr)
  78. {
  79. if (ptr) {
  80. btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
  81. *(void**)ptr = m_firstFree;
  82. m_firstFree = ptr;
  83. ++m_freeCount;
  84. }
  85. }
  86. int getElementSize() const
  87. {
  88. return m_elemSize;
  89. }
  90. unsigned char* getPoolAddress()
  91. {
  92. return m_pool;
  93. }
  94. const unsigned char* getPoolAddress() const
  95. {
  96. return m_pool;
  97. }
  98. };
  99. #endif //_BT_POOL_ALLOCATOR_H