Bullet Collision Detection & Physics Library: btPoolAllocator.h Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btPoolAllocator.h
Go to the documentation of this file.
1/*
2Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans https://bulletphysics.org
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#ifndef _BT_POOL_ALLOCATOR_H
16#define _BT_POOL_ALLOCATOR_H
17
18#include "btScalar.h"
19#include "btAlignedAllocator.h"
20#include "btThreads.h"
21
23 class btPoolAllocator
24{
25 int m_elemSize;
26 int m_maxElements;
27 int m_freeCount;
28 void* m_firstFree;
29 unsigned char* m_pool;
30 btSpinMutex m_mutex; // only used if BT_THREADSAFE
31
32public:
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 {
45 *(void**)p = (p + m_elemSize);
46 p += m_elemSize;
47 }
48 *(void**)p = 0;
49 }
50
51 ~btPoolAllocator()
52 {
53 btAlignedFree(m_pool);
54 }
55
56 int getFreeCount() const
57 {
58 return m_freeCount;
59 }
60
61 int getUsedCount() const
62 {
63 return m_maxElements - m_freeCount;
64 }
65
66 int getMaxCount() const
67 {
68 return m_maxElements;
69 }
70
71 void* allocate(int size)
72 {
73 // release mode fix
74 (void)size;
75 btMutexLock(&m_mutex);
76 btAssert(!size || size <= m_elemSize);
77 //btAssert(m_freeCount>0); // should return null if all full
78 void* result = m_firstFree;
79 if (NULL != m_firstFree)
80 {
81 m_firstFree = *(void**)m_firstFree;
82 --m_freeCount;
83 }
84 btMutexUnlock(&m_mutex);
85 return result;
86 }
87
88 bool validPtr(void* ptr)
89 {
90 if (ptr)
91 {
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 {
104 btAssert((unsigned char*)ptr >= m_pool && (unsigned char*)ptr < m_pool + m_maxElements * m_elemSize);
105
106 btMutexLock(&m_mutex);
107 *(void**)ptr = m_firstFree;
108 m_firstFree = ptr;
109 ++m_freeCount;
110 btMutexUnlock(&m_mutex);
111 }
112 }
113
114 int getElementSize() const
115 {
116 return m_elemSize;
117 }
118
119 unsigned char* getPoolAddress()
120 {
121 return m_pool;
122 }
123
124 const unsigned char* getPoolAddress() const
125 {
126 return m_pool;
127 }
128};
129
130#endif //_BT_POOL_ALLOCATOR_H
btAlignedAllocator.h
btAlignedFree
#define btAlignedFree(ptr)
Definition btAlignedAllocator.h:47
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition btAlignedAllocator.h:46
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
btScalar.h
btAssert
#define btAssert(x)
Definition btScalar.h:153
btThreads.h
btMutexLock
void btMutexLock(btSpinMutex *mutex)
Definition btThreads.h:70
btMutexUnlock
void btMutexUnlock(btSpinMutex *mutex)
Definition btThreads.h:79
btPoolAllocator
The btPoolAllocator class allows to efficiently allocate a large pool of objects, instead of dynamica...
Definition btPoolAllocator.h:24
btPoolAllocator::btPoolAllocator
btPoolAllocator(int elemSize, int maxElements)
Definition btPoolAllocator.h:33
btPoolAllocator::m_firstFree
void * m_firstFree
Definition btPoolAllocator.h:28
btPoolAllocator::m_maxElements
int m_maxElements
Definition btPoolAllocator.h:26
btPoolAllocator::freeMemory
void freeMemory(void *ptr)
Definition btPoolAllocator.h:100
btPoolAllocator::getElementSize
int getElementSize() const
Definition btPoolAllocator.h:114
btPoolAllocator::validPtr
bool validPtr(void *ptr)
Definition btPoolAllocator.h:88
btPoolAllocator::getPoolAddress
unsigned char * getPoolAddress()
Definition btPoolAllocator.h:119
btPoolAllocator::getFreeCount
int getFreeCount() const
Definition btPoolAllocator.h:56
btPoolAllocator::m_mutex
btSpinMutex m_mutex
Definition btPoolAllocator.h:30
btPoolAllocator::getMaxCount
int getMaxCount() const
Definition btPoolAllocator.h:66
btPoolAllocator::allocate
void * allocate(int size)
Definition btPoolAllocator.h:71
btPoolAllocator::~btPoolAllocator
~btPoolAllocator()
Definition btPoolAllocator.h:51
btPoolAllocator::m_elemSize
int m_elemSize
Definition btPoolAllocator.h:25
btPoolAllocator::m_freeCount
int m_freeCount
Definition btPoolAllocator.h:27
btPoolAllocator::getUsedCount
int getUsedCount() const
Definition btPoolAllocator.h:61
btPoolAllocator::getPoolAddress
const unsigned char * getPoolAddress() const
Definition btPoolAllocator.h:124
btPoolAllocator::m_pool
unsigned char * m_pool
Definition btPoolAllocator.h:29
btSpinMutex
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becaus...
Definition btThreads.h:46