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

Bullet Collision Detection & Physics Library

btGenericPoolAllocator.h
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
21#ifndef BT_GENERIC_POOL_ALLOCATOR_H
22#define BT_GENERIC_POOL_ALLOCATOR_H
23
24#include <limits.h>
25#include <stdio.h>
26#include <string.h>
27#include "LinearMath/btAlignedAllocator.h"
28
29#define BT_UINT_MAX UINT_MAX
30#define BT_DEFAULT_MAX_POOLS 16
31
33 class btGenericMemoryPool
34{
35public:
36 unsigned char *m_pool; //[m_element_size*m_max_element_count];
37 size_t *m_free_nodes; //[m_max_element_count];//! free nodes
38 size_t *m_allocated_sizes; //[m_max_element_count];//! Number of elements allocated per node
39 size_t m_allocated_count;
40 size_t m_free_nodes_count;
41
42protected:
43 size_t m_element_size;
44 size_t m_max_element_count;
45
46 size_t allocate_from_free_nodes(size_t num_elements);
47 size_t allocate_from_pool(size_t num_elements);
48
49public:
50 void init_pool(size_t element_size, size_t element_count);
51
52 void end_pool();
53
54 btGenericMemoryPool(size_t element_size, size_t element_count)
55 {
56 init_pool(element_size, element_count);
57 }
58
59 ~btGenericMemoryPool()
60 {
61 end_pool();
62 }
63
64 inline size_t get_pool_capacity()
65 {
66 return m_element_size * m_max_element_count;
67 }
68
69 inline size_t gem_element_size()
70 {
71 return m_element_size;
72 }
73
74 inline size_t get_max_element_count()
75 {
76 return m_max_element_count;
77 }
78
79 inline size_t get_allocated_count()
80 {
81 return m_allocated_count;
82 }
83
84 inline size_t get_free_positions_count()
85 {
86 return m_free_nodes_count;
87 }
88
89 inline void *get_element_data(size_t element_index)
90 {
91 return &m_pool[element_index * m_element_size];
92 }
93
95
98 void *allocate(size_t size_bytes);
99
100 bool freeMemory(void *pointer);
101};
102
104
107 class btGenericPoolAllocator
108{
109protected:
110 size_t m_pool_element_size;
111 size_t m_pool_element_count;
112
113public:
114 btGenericMemoryPool *m_pools[BT_DEFAULT_MAX_POOLS];
115 size_t m_pool_count;
116
117 inline size_t get_pool_capacity()
118 {
119 return m_pool_element_size * m_pool_element_count;
120 }
121
122protected:
123 // creates a pool
124 btGenericMemoryPool *push_new_pool();
125
126 void *failback_alloc(size_t size_bytes);
127
128 bool failback_free(void *pointer);
129
130public:
131 btGenericPoolAllocator(size_t pool_element_size, size_t pool_element_count)
132 {
133 m_pool_count = 0;
134 m_pool_element_size = pool_element_size;
135 m_pool_element_count = pool_element_count;
136 }
137
138 virtual ~btGenericPoolAllocator();
139
141
144 void *allocate(size_t size_bytes);
145
146 bool freeMemory(void *pointer);
147};
148
149void *btPoolAlloc(size_t size);
150void *btPoolRealloc(void *ptr, size_t oldsize, size_t newsize);
151void btPoolFree(void *ptr);
152
153#endif
btAlignedAllocator.h
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
BT_DEFAULT_MAX_POOLS
#define BT_DEFAULT_MAX_POOLS
Definition btGenericPoolAllocator.h:30
btPoolRealloc
void * btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
Definition btGenericPoolAllocator.cpp:254
btPoolAlloc
void * btPoolAlloc(size_t size)
Definition btGenericPoolAllocator.cpp:249
btPoolFree
void btPoolFree(void *ptr)
Definition btGenericPoolAllocator.cpp:263
btGenericMemoryPool
Generic Pool class.
Definition btGenericPoolAllocator.h:34
btGenericMemoryPool::m_free_nodes_count
size_t m_free_nodes_count
Definition btGenericPoolAllocator.h:40
btGenericMemoryPool::get_element_data
void * get_element_data(size_t element_index)
Definition btGenericPoolAllocator.h:89
btGenericMemoryPool::btGenericMemoryPool
btGenericMemoryPool(size_t element_size, size_t element_count)
Definition btGenericPoolAllocator.h:54
btGenericMemoryPool::m_allocated_count
size_t m_allocated_count
Definition btGenericPoolAllocator.h:39
btGenericMemoryPool::freeMemory
bool freeMemory(void *pointer)
Definition btGenericPoolAllocator.cpp:130
btGenericMemoryPool::allocate_from_pool
size_t allocate_from_pool(size_t num_elements)
Definition btGenericPoolAllocator.cpp:68
btGenericMemoryPool::get_allocated_count
size_t get_allocated_count()
Definition btGenericPoolAllocator.h:79
btGenericMemoryPool::gem_element_size
size_t gem_element_size()
Definition btGenericPoolAllocator.h:69
btGenericMemoryPool::init_pool
void init_pool(size_t element_size, size_t element_count)
Definition btGenericPoolAllocator.cpp:80
btGenericMemoryPool::end_pool
void end_pool()
Definition btGenericPoolAllocator.cpp:98
btGenericMemoryPool::get_free_positions_count
size_t get_free_positions_count()
Definition btGenericPoolAllocator.h:84
btGenericMemoryPool::m_allocated_sizes
size_t * m_allocated_sizes
Definition btGenericPoolAllocator.h:38
btGenericMemoryPool::allocate_from_free_nodes
size_t allocate_from_free_nodes(size_t num_elements)
*************** btGenericMemoryPool ******************‍///////////
Definition btGenericPoolAllocator.cpp:25
btGenericMemoryPool::m_free_nodes
size_t * m_free_nodes
Definition btGenericPoolAllocator.h:37
btGenericMemoryPool::allocate
void * allocate(size_t size_bytes)
Allocates memory in pool.
Definition btGenericPoolAllocator.cpp:111
btGenericMemoryPool::get_pool_capacity
size_t get_pool_capacity()
Definition btGenericPoolAllocator.h:64
btGenericMemoryPool::m_max_element_count
size_t m_max_element_count
Definition btGenericPoolAllocator.h:44
btGenericMemoryPool::get_max_element_count
size_t get_max_element_count()
Definition btGenericPoolAllocator.h:74
btGenericMemoryPool::m_pool
unsigned char * m_pool
Definition btGenericPoolAllocator.h:36
btGenericMemoryPool::m_element_size
size_t m_element_size
Definition btGenericPoolAllocator.h:43
btGenericMemoryPool::~btGenericMemoryPool
~btGenericMemoryPool()
Definition btGenericPoolAllocator.h:59
btGenericPoolAllocator::m_pool_element_count
size_t m_pool_element_count
Definition btGenericPoolAllocator.h:111
btGenericPoolAllocator::push_new_pool
btGenericMemoryPool * push_new_pool()
Definition btGenericPoolAllocator.cpp:159
btGenericPoolAllocator::~btGenericPoolAllocator
virtual ~btGenericPoolAllocator()
*******************! btGenericPoolAllocator *******************!///
Definition btGenericPoolAllocator.cpp:147
btGenericPoolAllocator::m_pool_count
size_t m_pool_count
Definition btGenericPoolAllocator.h:115
btGenericPoolAllocator::freeMemory
bool freeMemory(void *pointer)
Definition btGenericPoolAllocator.cpp:216
btGenericPoolAllocator::m_pools
btGenericMemoryPool * m_pools[BT_DEFAULT_MAX_POOLS]
Definition btGenericPoolAllocator.h:114
btGenericPoolAllocator::failback_alloc
void * failback_alloc(size_t size_bytes)
Definition btGenericPoolAllocator.cpp:173
btGenericPoolAllocator::failback_free
bool failback_free(void *pointer)
Definition btGenericPoolAllocator.cpp:190
btGenericPoolAllocator::get_pool_capacity
size_t get_pool_capacity()
Definition btGenericPoolAllocator.h:117
btGenericPoolAllocator::btGenericPoolAllocator
btGenericPoolAllocator(size_t pool_element_size, size_t pool_element_count)
Definition btGenericPoolAllocator.h:131
btGenericPoolAllocator::allocate
void * allocate(size_t size_bytes)
Allocates memory in pool.
Definition btGenericPoolAllocator.cpp:200
btGenericPoolAllocator::m_pool_element_size
size_t m_pool_element_size
Definition btGenericPoolAllocator.h:110