Bullet Collision Detection & Physics Library
btGenericPoolAllocator.cpp
Go to the documentation of this file.
1
6/*
7Bullet Continuous Collision Detection and Physics Library
8Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
9
10This software is provided 'as-is', without any express or implied warranty.
11In no event will the authors be held liable for any damages arising from the use of this software.
12Permission is granted to anyone to use this software for any purpose,
13including commercial applications, and to alter it and redistribute it freely,
14subject to the following restrictions:
15
161. 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.
172. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
183. This notice may not be removed or altered from any source distribution.
19*/
20
22
24
26{
27 size_t ptr = BT_UINT_MAX;
28
29 if (m_free_nodes_count == 0) return BT_UINT_MAX;
30 // find an avaliable free node with the correct size
32
33 while (revindex-- && ptr == BT_UINT_MAX)
34 {
36 {
37 ptr = revindex;
38 }
39 }
40 if (ptr == BT_UINT_MAX) return BT_UINT_MAX; // not found
41
42 revindex = ptr;
44 // post: ptr contains the node index, and revindex the index in m_free_nodes
45
46 size_t finalsize = m_allocated_sizes[ptr];
48
50
51 // post: finalsize>=0, m_allocated_sizes[ptr] has the requested size
52
53 if (finalsize > 0) // preserve free node, there are some free memory
54 {
57 }
58 else // delete free node
59 {
60 // swap with end
63 }
64
65 return ptr;
66}
67
69{
71
72 size_t ptr = m_allocated_count;
73
76
77 return ptr;
78}
79
81{
84
87
89 m_free_nodes = (size_t *)btAlignedAlloc(sizeof(size_t) * m_max_element_count, 16);
90 m_allocated_sizes = (size_t *)btAlignedAlloc(sizeof(size_t) * m_max_element_count, 16);
91
92 for (size_t i = 0; i < m_max_element_count; i++)
93 {
94 m_allocated_sizes[i] = 0;
95 }
96}
97
99{
105}
106
108
112{
113 size_t module = size_bytes % m_element_size;
115 if (module > 0) element_count++;
116
118 // a free node is found
119 if (alloc_pos != BT_UINT_MAX)
120 {
122 }
123 // allocate directly on pool
125
126 if (alloc_pos == BT_UINT_MAX) return NULL; // not space
128}
129
131{
132 unsigned char *pointer_pos = (unsigned char *)pointer;
133 unsigned char *pool_pos = (unsigned char *)m_pool;
134 // calc offset
135 if (pointer_pos < pool_pos) return false; //other pool
136 size_t offset = size_t(pointer_pos - pool_pos);
137 if (offset >= get_pool_capacity()) return false; // far away
138
139 // find free position
142 return true;
143}
144
146
148{
149 // destroy pools
150 size_t i;
151 for (i = 0; i < m_pool_count; i++)
152 {
153 m_pools[i]->end_pool();
155 }
156}
157
158// creates a pool
160{
162
164
166
168
169 m_pool_count++;
170 return newptr;
171}
172
174{
176
178 {
180 }
181
182 if (pool == NULL) // failback
183 {
184 return btAlignedAlloc(size_bytes, 16);
185 }
186
187 return pool->allocate(size_bytes);
188}
189
191{
192 btAlignedFree(pointer);
193 return true;
194}
195
197
201{
202 void *ptr = NULL;
203
204 size_t i = 0;
205 while (i < m_pool_count && ptr == NULL)
206 {
207 ptr = m_pools[i]->allocate(size_bytes);
208 ++i;
209 }
210
211 if (ptr) return ptr;
212
214}
215
217{
218 bool result = false;
219
220 size_t i = 0;
221 while (i < m_pool_count && result == false)
222 {
223 result = m_pools[i]->freeMemory(pointer);
224 ++i;
225 }
226
227 if (result) return true;
228
229 return failback_free(pointer);
230}
231
233
234#define BT_DEFAULT_POOL_SIZE 32768
235#define BT_DEFAULT_POOL_ELEMENT_SIZE 8
236
237// main allocator
239{
240public:
242 {
243 }
244};
245
246// global allocator
248
249void *btPoolAlloc(size_t size)
250{
252}
253
254void *btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
255{
256 void *newptr = btPoolAlloc(newsize);
257 size_t copysize = oldsize < newsize ? oldsize : newsize;
258 memcpy(newptr, ptr, copysize);
259 btPoolFree(ptr);
260 return newptr;
261}
262
263void btPoolFree(void *ptr)
264{
266}
#define btAlignedFree(ptr)
#define btAlignedAlloc(size, alignment)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define BT_DEFAULT_POOL_SIZE
************** STANDARD ALLOCATOR ***************************‍///
GIM_STANDARD_ALLOCATOR g_main_allocator
void * btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
void * btPoolAlloc(size_t size)
#define BT_DEFAULT_POOL_ELEMENT_SIZE
void btPoolFree(void *ptr)
#define BT_DEFAULT_MAX_POOLS
#define BT_UINT_MAX
const T & btMax(const T &a, const T &b)
Definition btMinMax.h:27
void * get_element_data(size_t element_index)
bool freeMemory(void *pointer)
size_t allocate_from_pool(size_t num_elements)
void init_pool(size_t element_size, size_t element_count)
size_t allocate_from_free_nodes(size_t num_elements)
*************** btGenericMemoryPool ******************‍///////////
void * allocate(size_t size_bytes)
Allocates memory in pool.
Generic Allocator with pools.
btGenericMemoryPool * push_new_pool()
virtual ~btGenericPoolAllocator()
*******************! btGenericPoolAllocator *******************!///
btGenericMemoryPool * m_pools[BT_DEFAULT_MAX_POOLS]
void * failback_alloc(size_t size_bytes)
void * allocate(size_t size_bytes)
Allocates memory in pool.