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