Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/external/bullet/LinearMath/btPoolAllocator.h @ 11468

Last change on this file since 11468 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1/*
2Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
3
4This software is provided 'as-is', without any express or implied warranty.
5In no event will the authors be held liable for any damages arising from the use of this software.
6Permission is granted to anyone to use this software for any purpose,
7including commercial applications, and to alter it and redistribute it freely,
8subject to the following restrictions:
9
101. 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.
112. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
123. This notice may not be removed or altered from any source distribution.
13*/
14
15
16#ifndef _BT_POOL_ALLOCATOR_H
17#define _BT_POOL_ALLOCATOR_H
18
19#include "btScalar.h"
20#include "btAlignedAllocator.h"
21
22///The btPoolAllocator class allows to efficiently allocate a large pool of objects, instead of dynamically allocating them separately.
23class btPoolAllocator
24{
25        int                             m_elemSize;
26        int                             m_maxElements;
27        int                             m_freeCount;
28        void*                   m_firstFree;
29        unsigned char*  m_pool;
30
31public:
32
33        btPoolAllocator(int elemSize, int maxElements)
34                :m_elemSize(elemSize),
35                m_maxElements(maxElements)
36        {
37                m_pool = (unsigned char*) btAlignedAlloc( static_cast<unsigned int>(m_elemSize*m_maxElements),16);
38
39                unsigned char* p = m_pool;
40        m_firstFree = p;
41        m_freeCount = m_maxElements;
42        int count = m_maxElements;
43        while (--count) {
44            *(void**)p = (p + m_elemSize);
45            p += m_elemSize;
46        }
47        *(void**)p = 0;
48    }
49
50        ~btPoolAllocator()
51        {
52                btAlignedFree( m_pool);
53        }
54
55        int     getFreeCount() const
56        {
57                return m_freeCount;
58        }
59
60        void*   allocate(int size)
61        {
62                // release mode fix
63                (void)size;
64                btAssert(!size || size<=m_elemSize);
65                btAssert(m_freeCount>0);
66        void* result = m_firstFree;
67        m_firstFree = *(void**)m_firstFree;
68        --m_freeCount;
69        return result;
70        }
71
72        bool validPtr(void* ptr)
73        {
74                if (ptr) {
75                        if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize))
76                        {
77                                return true;
78                        }
79                }
80                return false;
81        }
82
83        void    freeMemory(void* ptr)
84        {
85                 if (ptr) {
86            btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
87
88            *(void**)ptr = m_firstFree;
89            m_firstFree = ptr;
90            ++m_freeCount;
91        }
92        }
93
94        int     getElementSize() const
95        {
96                return m_elemSize;
97        }
98
99
100};
101
102#endif //_BT_POOL_ALLOCATOR_H
Note: See TracBrowser for help on using the repository browser.