00001 /* 00002 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/ 00003 00004 This software is provided 'as-is', without any express or implied warranty. 00005 In no event will the authors be held liable for any damages arising from the use of this software. 00006 Permission is granted to anyone to use this software for any purpose, 00007 including commercial applications, and to alter it and redistribute it freely, 00008 subject to the following restrictions: 00009 00010 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. 00011 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 00012 3. This notice may not be removed or altered from any source distribution. 00013 */ 00014 00015 00016 #ifndef _BT_POOL_ALLOCATOR_H 00017 #define _BT_POOL_ALLOCATOR_H 00018 00019 #include "btScalar.h" 00020 #include "btAlignedAllocator.h" 00021 00023 class btPoolAllocator 00024 { 00025 int m_elemSize; 00026 int m_maxElements; 00027 int m_freeCount; 00028 void* m_firstFree; 00029 unsigned char* m_pool; 00030 00031 public: 00032 00033 btPoolAllocator(int elemSize, int maxElements) 00034 :m_elemSize(elemSize), 00035 m_maxElements(maxElements) 00036 { 00037 m_pool = (unsigned char*) btAlignedAlloc( static_cast<unsigned int>(m_elemSize*m_maxElements),16); 00038 00039 unsigned char* p = m_pool; 00040 m_firstFree = p; 00041 m_freeCount = m_maxElements; 00042 int count = m_maxElements; 00043 while (--count) { 00044 *(void**)p = (p + m_elemSize); 00045 p += m_elemSize; 00046 } 00047 *(void**)p = 0; 00048 } 00049 00050 ~btPoolAllocator() 00051 { 00052 btAlignedFree( m_pool); 00053 } 00054 00055 int getFreeCount() const 00056 { 00057 return m_freeCount; 00058 } 00059 00060 int getUsedCount() const 00061 { 00062 return m_maxElements - m_freeCount; 00063 } 00064 00065 int getMaxCount() const 00066 { 00067 return m_maxElements; 00068 } 00069 00070 void* allocate(int size) 00071 { 00072 // release mode fix 00073 (void)size; 00074 btAssert(!size || size<=m_elemSize); 00075 btAssert(m_freeCount>0); 00076 void* result = m_firstFree; 00077 m_firstFree = *(void**)m_firstFree; 00078 --m_freeCount; 00079 return result; 00080 } 00081 00082 bool validPtr(void* ptr) 00083 { 00084 if (ptr) { 00085 if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize)) 00086 { 00087 return true; 00088 } 00089 } 00090 return false; 00091 } 00092 00093 void freeMemory(void* ptr) 00094 { 00095 if (ptr) { 00096 btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize); 00097 00098 *(void**)ptr = m_firstFree; 00099 m_firstFree = ptr; 00100 ++m_freeCount; 00101 } 00102 } 00103 00104 int getElementSize() const 00105 { 00106 return m_elemSize; 00107 } 00108 00109 unsigned char* getPoolAddress() 00110 { 00111 return m_pool; 00112 } 00113 00114 const unsigned char* getPoolAddress() const 00115 { 00116 return m_pool; 00117 } 00118 00119 }; 00120 00121 #endif //_BT_POOL_ALLOCATOR_H