1 /*! \file btGenericPoolAllocator.cpp
2 \author Francisco Leon Najera. email projectileman@yahoo.com
4 General purpose allocator class
7 Bullet Continuous Collision Detection and Physics Library
8 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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:
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.
21 #include "btGenericPoolAllocator.h"
23 /// *************** btGenericMemoryPool ******************///////////
25 size_t btGenericMemoryPool::allocate_from_free_nodes(size_t num_elements)
27 size_t ptr = BT_UINT_MAX;
29 if (m_free_nodes_count == 0) return BT_UINT_MAX;
30 // find an avaliable free node with the correct size
31 size_t revindex = m_free_nodes_count;
33 while (revindex-- && ptr == BT_UINT_MAX)
35 if (m_allocated_sizes[m_free_nodes[revindex]] >= num_elements)
40 if (ptr == BT_UINT_MAX) return BT_UINT_MAX; // not found
43 ptr = m_free_nodes[revindex];
44 // post: ptr contains the node index, and revindex the index in m_free_nodes
46 size_t finalsize = m_allocated_sizes[ptr];
47 finalsize -= num_elements;
49 m_allocated_sizes[ptr] = num_elements;
51 // post: finalsize>=0, m_allocated_sizes[ptr] has the requested size
53 if (finalsize > 0) // preserve free node, there are some free memory
55 m_free_nodes[revindex] = ptr + num_elements;
56 m_allocated_sizes[ptr + num_elements] = finalsize;
58 else // delete free node
61 m_free_nodes[revindex] = m_free_nodes[m_free_nodes_count - 1];
68 size_t btGenericMemoryPool::allocate_from_pool(size_t num_elements)
70 if (m_allocated_count + num_elements > m_max_element_count) return BT_UINT_MAX;
72 size_t ptr = m_allocated_count;
74 m_allocated_sizes[m_allocated_count] = num_elements;
75 m_allocated_count += num_elements;
80 void btGenericMemoryPool::init_pool(size_t element_size, size_t element_count)
82 m_allocated_count = 0;
83 m_free_nodes_count = 0;
85 m_element_size = element_size;
86 m_max_element_count = element_count;
88 m_pool = (unsigned char *)btAlignedAlloc(m_element_size * m_max_element_count, 16);
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);
92 for (size_t i = 0; i < m_max_element_count; i++)
94 m_allocated_sizes[i] = 0;
98 void btGenericMemoryPool::end_pool()
100 btAlignedFree(m_pool);
101 btAlignedFree(m_free_nodes);
102 btAlignedFree(m_allocated_sizes);
103 m_allocated_count = 0;
104 m_free_nodes_count = 0;
107 //! Allocates memory in pool
109 \param size_bytes size in bytes of the buffer
111 void *btGenericMemoryPool::allocate(size_t size_bytes)
113 size_t module = size_bytes % m_element_size;
114 size_t element_count = size_bytes / m_element_size;
115 if (module > 0) element_count++;
117 size_t alloc_pos = allocate_from_free_nodes(element_count);
118 // a free node is found
119 if (alloc_pos != BT_UINT_MAX)
121 return get_element_data(alloc_pos);
123 // allocate directly on pool
124 alloc_pos = allocate_from_pool(element_count);
126 if (alloc_pos == BT_UINT_MAX) return NULL; // not space
127 return get_element_data(alloc_pos);
130 bool btGenericMemoryPool::freeMemory(void *pointer)
132 unsigned char *pointer_pos = (unsigned char *)pointer;
133 unsigned char *pool_pos = (unsigned char *)m_pool;
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
139 // find free position
140 m_free_nodes[m_free_nodes_count] = offset / m_element_size;
141 m_free_nodes_count++;
145 /// *******************! btGenericPoolAllocator *******************!///
147 btGenericPoolAllocator::~btGenericPoolAllocator()
151 for (i = 0; i < m_pool_count; i++)
153 m_pools[i]->end_pool();
154 btAlignedFree(m_pools[i]);
159 btGenericMemoryPool *btGenericPoolAllocator::push_new_pool()
161 if (m_pool_count >= BT_DEFAULT_MAX_POOLS) return NULL;
163 btGenericMemoryPool *newptr = (btGenericMemoryPool *)btAlignedAlloc(sizeof(btGenericMemoryPool), 16);
165 m_pools[m_pool_count] = newptr;
167 m_pools[m_pool_count]->init_pool(m_pool_element_size, m_pool_element_count);
173 void *btGenericPoolAllocator::failback_alloc(size_t size_bytes)
175 btGenericMemoryPool *pool = NULL;
177 if (size_bytes <= get_pool_capacity())
179 pool = push_new_pool();
182 if (pool == NULL) // failback
184 return btAlignedAlloc(size_bytes, 16);
187 return pool->allocate(size_bytes);
190 bool btGenericPoolAllocator::failback_free(void *pointer)
192 btAlignedFree(pointer);
196 //! Allocates memory in pool
198 \param size_bytes size in bytes of the buffer
200 void *btGenericPoolAllocator::allocate(size_t size_bytes)
205 while (i < m_pool_count && ptr == NULL)
207 ptr = m_pools[i]->allocate(size_bytes);
213 return failback_alloc(size_bytes);
216 bool btGenericPoolAllocator::freeMemory(void *pointer)
221 while (i < m_pool_count && result == false)
223 result = m_pools[i]->freeMemory(pointer);
227 if (result) return true;
229 return failback_free(pointer);
232 /// ************** STANDARD ALLOCATOR ***************************///
234 #define BT_DEFAULT_POOL_SIZE 32768
235 #define BT_DEFAULT_POOL_ELEMENT_SIZE 8
238 class GIM_STANDARD_ALLOCATOR : public btGenericPoolAllocator
241 GIM_STANDARD_ALLOCATOR() : btGenericPoolAllocator(BT_DEFAULT_POOL_ELEMENT_SIZE, BT_DEFAULT_POOL_SIZE)
247 GIM_STANDARD_ALLOCATOR g_main_allocator;
249 void *btPoolAlloc(size_t size)
251 return g_main_allocator.allocate(size);
254 void *btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
256 void *newptr = btPoolAlloc(newsize);
257 size_t copysize = oldsize < newsize ? oldsize : newsize;
258 memcpy(newptr, ptr, copysize);
263 void btPoolFree(void *ptr)
265 g_main_allocator.freeMemory(ptr);