Back to index

supertuxkart  0.5+dfsg1
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 
00022 class btPoolAllocator
00023 {
00024        int                         m_elemSize;
00025        int                         m_maxElements;
00026        int                         m_freeCount;
00027        void*                m_firstFree;
00028        unsigned char*       m_pool;
00029 
00030 public:
00031 
00032        btPoolAllocator(int elemSize, int maxElements)
00033               :m_elemSize(elemSize),
00034               m_maxElements(maxElements)
00035        {
00036               m_pool = (unsigned char*) btAlignedAlloc(m_elemSize*m_maxElements,16);
00037 
00038               unsigned char* p = m_pool;
00039         m_firstFree = p;
00040         m_freeCount = m_maxElements;
00041         int count = m_maxElements;
00042         while (--count) {
00043             *(void**)p = (p + m_elemSize);
00044             p += m_elemSize;
00045         }
00046         *(void**)p = 0;
00047     }
00048 
00049        ~btPoolAllocator()
00050        {
00051               btAlignedFree( m_pool);
00052        }
00053 
00054        int    getFreeCount() const
00055        {
00056               return m_freeCount;
00057        }
00058 
00059        void*  allocate(int size)
00060        {
00061               btAssert(!size || size<=m_elemSize);
00062               btAssert(m_freeCount>0);
00063         void* result = m_firstFree;
00064         m_firstFree = *(void**)m_firstFree;
00065         --m_freeCount;
00066         return result;
00067        }
00068 
00069        bool validPtr(void* ptr)
00070        {
00071               if (ptr) {
00072                      if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize))
00073                      {
00074                             return true;
00075                      }
00076               }
00077               return false;
00078        }
00079 
00080        void   freeMemory(void* ptr)
00081        {
00082                if (ptr) {
00083             btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
00084 
00085             *(void**)ptr = m_firstFree;
00086             m_firstFree = ptr;
00087             ++m_freeCount;
00088         }
00089        }
00090 
00091 
00092 };
00093 
00094 #endif //_BT_POOL_ALLOCATOR_H