Bullet Collision Detection & Physics Library
btPoolAllocator.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/
3 
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
9 
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. 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 #include "btThreads.h"
22 
25 {
29  void* m_firstFree;
30  unsigned char* m_pool;
31  btSpinMutex m_mutex; // only used if BT_THREADSAFE
32 
33 public:
34 
35  btPoolAllocator(int elemSize, int maxElements)
36  :m_elemSize(elemSize),
37  m_maxElements(maxElements)
38  {
39  m_pool = (unsigned char*) btAlignedAlloc( static_cast<unsigned int>(m_elemSize*m_maxElements),16);
40 
41  unsigned char* p = m_pool;
42  m_firstFree = p;
43  m_freeCount = m_maxElements;
44  int count = m_maxElements;
45  while (--count) {
46  *(void**)p = (p + m_elemSize);
47  p += m_elemSize;
48  }
49  *(void**)p = 0;
50  }
51 
53  {
54  btAlignedFree( m_pool);
55  }
56 
57  int getFreeCount() const
58  {
59  return m_freeCount;
60  }
61 
62  int getUsedCount() const
63  {
64  return m_maxElements - m_freeCount;
65  }
66 
67  int getMaxCount() const
68  {
69  return m_maxElements;
70  }
71 
72  void* allocate(int size)
73  {
74  // release mode fix
75  (void)size;
76  btMutexLock(&m_mutex);
77  btAssert(!size || size<=m_elemSize);
78  //btAssert(m_freeCount>0); // should return null if all full
79  void* result = m_firstFree;
80  if (NULL != m_firstFree)
81  {
82  m_firstFree = *(void**)m_firstFree;
83  --m_freeCount;
84  }
85  btMutexUnlock(&m_mutex);
86  return result;
87  }
88 
89  bool validPtr(void* ptr)
90  {
91  if (ptr) {
92  if (((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize))
93  {
94  return true;
95  }
96  }
97  return false;
98  }
99 
100  void freeMemory(void* ptr)
101  {
102  if (ptr) {
103  btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
104 
105  btMutexLock(&m_mutex);
106  *(void**)ptr = m_firstFree;
107  m_firstFree = ptr;
108  ++m_freeCount;
109  btMutexUnlock(&m_mutex);
110  }
111  }
112 
113  int getElementSize() const
114  {
115  return m_elemSize;
116  }
117 
118  unsigned char* getPoolAddress()
119  {
120  return m_pool;
121  }
122 
123  const unsigned char* getPoolAddress() const
124  {
125  return m_pool;
126  }
127 
128 };
129 
130 #endif //_BT_POOL_ALLOCATOR_H
btSpinMutex m_mutex
int getUsedCount() const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define btAssert(x)
Definition: btScalar.h:131
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becau...
Definition: btThreads.h:47
unsigned char * getPoolAddress()
bool validPtr(void *ptr)
#define btAlignedFree(ptr)
int getFreeCount() const
btPoolAllocator(int elemSize, int maxElements)
const unsigned char * getPoolAddress() const
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:82
void * allocate(int size)
The btPoolAllocator class allows to efficiently allocate a large pool of objects, instead of dynamica...
int getElementSize() const
int getMaxCount() const
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:73
#define btAlignedAlloc(size, alignment)
void freeMemory(void *ptr)
unsigned char * m_pool