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 /*
7 Bullet Continuous Collision Detection and Physics Library
8 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
9
10 This software is provided 'as-is', without any express or implied warranty.
11 In no event will the authors be held liable for any damages arising from the use of this software.
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it freely,
14 subject to the following restrictions:
15
16 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.
17 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
18 3. 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
32
34 class btGenericMemoryPool
35 {
36 public:
37 unsigned char * m_pool; //[m_element_size*m_max_element_count];
38 size_t * m_free_nodes; //[m_max_element_count];//! free nodes
39 size_t * m_allocated_sizes;//[m_max_element_count];//! Number of elements allocated per node
40 size_t m_allocated_count;
41 size_t m_free_nodes_count;
42 protected:
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
49 public:
50
51 void init_pool(size_t element_size, size_t element_count);
52
53 void end_pool();
54
55
56 btGenericMemoryPool(size_t element_size, size_t element_count)
57 {
58 init_pool(element_size, element_count);
59 }
60
61 ~btGenericMemoryPool()
62 {
63 end_pool();
64 }
65
66
67 inline size_t get_pool_capacity()
68 {
69 return m_element_size*m_max_element_count;
70 }
71
72 inline size_t gem_element_size()
73 {
74 return m_element_size;
75 }
76
77 inline size_t get_max_element_count()
78 {
79 return m_max_element_count;
80 }
81
82 inline size_t get_allocated_count()
83 {
84 return m_allocated_count;
85 }
86
87 inline size_t get_free_positions_count()
88 {
89 return m_free_nodes_count;
90 }
91
92 inline void * get_element_data(size_t element_index)
93 {
94 return &m_pool[element_index*m_element_size];
95 }
96
98
101 void * allocate(size_t size_bytes);
102
103 bool freeMemory(void * pointer);
104 };
105
106
107
108
110
113 class btGenericPoolAllocator
114 {
115 protected:
116 size_t m_pool_element_size;
117 size_t m_pool_element_count;
118 public:
119 btGenericMemoryPool * m_pools[BT_DEFAULT_MAX_POOLS];
120 size_t m_pool_count;
121
122
123 inline size_t get_pool_capacity()
124 {
125 return m_pool_element_size*m_pool_element_count;
126 }
127
128
129 protected:
130 // creates a pool
131 btGenericMemoryPool * push_new_pool();
132
133 void * failback_alloc(size_t size_bytes);
134
135 bool failback_free(void * pointer);
136 public:
137
138 btGenericPoolAllocator(size_t pool_element_size, size_t pool_element_count)
139 {
140 m_pool_count = 0;
141 m_pool_element_size = pool_element_size;
142 m_pool_element_count = pool_element_count;
143 }
144
145 virtual ~btGenericPoolAllocator();
146
148
151 void * allocate(size_t size_bytes);
152
153 bool freeMemory(void * pointer);
154 };
155
156
157
158 void * btPoolAlloc(size_t size);
159 void * btPoolRealloc(void *ptr, size_t oldsize, size_t newsize);
160 void btPoolFree(void *ptr);
161
162
163 #endif
btGenericMemoryPool::m_allocated_count
size_t m_allocated_count
Definition: btGenericPoolAllocator.h:40
btGenericMemoryPool::~btGenericMemoryPool
~btGenericMemoryPool()
Definition: btGenericPoolAllocator.h:61
btGenericMemoryPool::get_free_positions_count
size_t get_free_positions_count()
Definition: btGenericPoolAllocator.h:87
btGenericMemoryPool::m_pool
unsigned char * m_pool
Definition: btGenericPoolAllocator.h:37
btGenericMemoryPool::btGenericMemoryPool
btGenericMemoryPool(size_t element_size, size_t element_count)
Definition: btGenericPoolAllocator.h:56
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
btPoolAlloc
void * btPoolAlloc(size_t size)
Definition: btGenericPoolAllocator.cpp:266
btAlignedAllocator.h
btGenericMemoryPool::init_pool
void init_pool(size_t element_size, size_t element_count)
Definition: btGenericPoolAllocator.cpp:84
btGenericMemoryPool::allocate
void * allocate(size_t size_bytes)
Allocates memory in pool.
Definition: btGenericPoolAllocator.cpp:119
btGenericPoolAllocator
Generic Allocator with pools.
Definition: btGenericPoolAllocator.h:113
btGenericPoolAllocator::get_pool_capacity
size_t get_pool_capacity()
Definition: btGenericPoolAllocator.h:123
btGenericMemoryPool::allocate_from_free_nodes
size_t allocate_from_free_nodes(size_t num_elements)
*************** btGenericMemoryPool ******************///////////
Definition: btGenericPoolAllocator.cpp:27
btGenericMemoryPool::m_max_element_count
size_t m_max_element_count
Definition: btGenericPoolAllocator.h:44
btGenericMemoryPool::m_free_nodes
size_t * m_free_nodes
Definition: btGenericPoolAllocator.h:38
BT_DEFAULT_MAX_POOLS
#define BT_DEFAULT_MAX_POOLS
Definition: btGenericPoolAllocator.h:30
btGenericMemoryPool::allocate_from_pool
size_t allocate_from_pool(size_t num_elements)
Definition: btGenericPoolAllocator.cpp:71
btGenericPoolAllocator::btGenericPoolAllocator
btGenericPoolAllocator(size_t pool_element_size, size_t pool_element_count)
Definition: btGenericPoolAllocator.h:138
btGenericMemoryPool::m_allocated_sizes
size_t * m_allocated_sizes
Definition: btGenericPoolAllocator.h:39
btGenericMemoryPool
Generic Pool class.
Definition: btGenericPoolAllocator.h:34
btGenericMemoryPool::end_pool
void end_pool()
Definition: btGenericPoolAllocator.cpp:105
btGenericMemoryPool::m_element_size
size_t m_element_size
Definition: btGenericPoolAllocator.h:43
btPoolFree
void btPoolFree(void *ptr)
Definition: btGenericPoolAllocator.cpp:280
btGenericMemoryPool::get_pool_capacity
size_t get_pool_capacity()
Definition: btGenericPoolAllocator.h:67
btPoolRealloc
void * btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
Definition: btGenericPoolAllocator.cpp:271
btGenericPoolAllocator::m_pool_element_count
size_t m_pool_element_count
Definition: btGenericPoolAllocator.h:117
btGenericMemoryPool::freeMemory
bool freeMemory(void *pointer)
Definition: btGenericPoolAllocator.cpp:139
btGenericMemoryPool::gem_element_size
size_t gem_element_size()
Definition: btGenericPoolAllocator.h:72
btGenericMemoryPool::get_element_data
void * get_element_data(size_t element_index)
Definition: btGenericPoolAllocator.h:92
btGenericMemoryPool::get_max_element_count
size_t get_max_element_count()
Definition: btGenericPoolAllocator.h:77
btGenericMemoryPool::get_allocated_count
size_t get_allocated_count()
Definition: btGenericPoolAllocator.h:82
btGenericPoolAllocator::m_pool_element_size
size_t m_pool_element_size
Definition: btGenericPoolAllocator.h:116
btGenericPoolAllocator::m_pool_count
size_t m_pool_count
Definition: btGenericPoolAllocator.h:120
btGenericMemoryPool::m_free_nodes_count
size_t m_free_nodes_count
Definition: btGenericPoolAllocator.h:41