Blender V2.61 - r43446

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     int getUsedCount() const
00061     {
00062         return m_maxElements - m_freeCount;
00063     }
00064 
00065     void*   allocate(int size)
00066     {
00067         // release mode fix
00068         (void)size;
00069         btAssert(!size || size<=m_elemSize);
00070         btAssert(m_freeCount>0);
00071         void* result = m_firstFree;
00072         m_firstFree = *(void**)m_firstFree;
00073         --m_freeCount;
00074         return result;
00075     }
00076 
00077     bool validPtr(void* ptr)
00078     {
00079         if (ptr) {
00080             if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize))
00081             {
00082                 return true;
00083             }
00084         }
00085         return false;
00086     }
00087 
00088     void    freeMemory(void* ptr)
00089     {
00090          if (ptr) {
00091             btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
00092 
00093             *(void**)ptr = m_firstFree;
00094             m_firstFree = ptr;
00095             ++m_freeCount;
00096         }
00097     }
00098 
00099     int getElementSize() const
00100     {
00101         return m_elemSize;
00102     }
00103 
00104     unsigned char*  getPoolAddress()
00105     {
00106         return m_pool;
00107     }
00108 
00109     const unsigned char*    getPoolAddress() const
00110     {
00111         return m_pool;
00112     }
00113 
00114 };
00115 
00116 #endif //_BT_POOL_ALLOCATOR_H