btPoolAllocator.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans https://bulletphysics.org
  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. #include "btThreads.h"
  17. ///The btPoolAllocator class allows to efficiently allocate a large pool of objects, instead of dynamically allocating them separately.
  18. class btPoolAllocator
  19. {
  20. int m_elemSize;
  21. int m_maxElements;
  22. int m_freeCount;
  23. void* m_firstFree;
  24. unsigned char* m_pool;
  25. btSpinMutex m_mutex; // only used if BT_THREADSAFE
  26. public:
  27. btPoolAllocator(int elemSize, int maxElements)
  28. : m_elemSize(elemSize),
  29. m_maxElements(maxElements)
  30. {
  31. m_pool = (unsigned char*)btAlignedAlloc(static_cast<unsigned int>(m_elemSize * m_maxElements), 16);
  32. unsigned char* p = m_pool;
  33. m_firstFree = p;
  34. m_freeCount = m_maxElements;
  35. int count = m_maxElements;
  36. while (--count)
  37. {
  38. *(void**)p = (p + m_elemSize);
  39. p += m_elemSize;
  40. }
  41. *(void**)p = 0;
  42. }
  43. ~btPoolAllocator()
  44. {
  45. btAlignedFree(m_pool);
  46. }
  47. int getFreeCount() const
  48. {
  49. return m_freeCount;
  50. }
  51. int getUsedCount() const
  52. {
  53. return m_maxElements - m_freeCount;
  54. }
  55. int getMaxCount() const
  56. {
  57. return m_maxElements;
  58. }
  59. void* allocate(int size)
  60. {
  61. // release mode fix
  62. (void)size;
  63. btMutexLock(&m_mutex);
  64. btAssert(!size || size <= m_elemSize);
  65. //btAssert(m_freeCount>0); // should return null if all full
  66. void* result = m_firstFree;
  67. if (NULL != m_firstFree)
  68. {
  69. m_firstFree = *(void**)m_firstFree;
  70. --m_freeCount;
  71. }
  72. btMutexUnlock(&m_mutex);
  73. return result;
  74. }
  75. bool validPtr(void* ptr)
  76. {
  77. if (ptr)
  78. {
  79. if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize))
  80. {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. void freeMemory(void* ptr)
  87. {
  88. if (ptr)
  89. {
  90. btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
  91. btMutexLock(&m_mutex);
  92. *(void**)ptr = m_firstFree;
  93. m_firstFree = ptr;
  94. ++m_freeCount;
  95. btMutexUnlock(&m_mutex);
  96. }
  97. }
  98. int getElementSize() const
  99. {
  100. return m_elemSize;
  101. }
  102. unsigned char* getPoolAddress()
  103. {
  104. return m_pool;
  105. }
  106. const unsigned char* getPoolAddress() const
  107. {
  108. return m_pool;
  109. }
  110. };
  111. #endif //_BT_POOL_ALLOCATOR_H