btPoolAllocator.h

Go to the documentation of this file.
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         void*   allocate(int size)
00061         {
00062                 // release mode fix
00063                 (void)size;
00064                 btAssert(!size || size<=m_elemSize);
00065                 btAssert(m_freeCount>0);
00066         void* result = m_firstFree;
00067         m_firstFree = *(void**)m_firstFree;
00068         --m_freeCount;
00069         return result;
00070         }
00071 
00072         bool validPtr(void* ptr)
00073         {
00074                 if (ptr) {
00075                         if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize))
00076                         {
00077                                 return true;
00078                         }
00079                 }
00080                 return false;
00081         }
00082 
00083         void    freeMemory(void* ptr)
00084         {
00085                  if (ptr) {
00086             btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
00087 
00088             *(void**)ptr = m_firstFree;
00089             m_firstFree = ptr;
00090             ++m_freeCount;
00091         }
00092         }
00093 
00094         int     getElementSize() const
00095         {
00096                 return m_elemSize;
00097         }
00098 
00099 
00100 };
00101 
00102 #endif //_BT_POOL_ALLOCATOR_H

Generated on Mon Feb 15 22:17:05 2010 for Bullet Collision Detection & Physics Library by  doxygen 1.6.1