2017-08-01 12:30:58 +00:00
|
|
|
/*! \file btGenericPoolAllocator.cpp
|
|
|
|
\author Francisco Leon Najera. email projectileman@yahoo.com
|
|
|
|
|
|
|
|
General purpose allocator class
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
2022-01-06 22:37:49 +00:00
|
|
|
Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
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.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "btGenericPoolAllocator.h"
|
|
|
|
|
|
|
|
/// *************** btGenericMemoryPool ******************///////////
|
|
|
|
|
|
|
|
size_t btGenericMemoryPool::allocate_from_free_nodes(size_t num_elements)
|
|
|
|
{
|
|
|
|
size_t ptr = BT_UINT_MAX;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_free_nodes_count == 0) return BT_UINT_MAX;
|
2017-08-01 12:30:58 +00:00
|
|
|
// find an avaliable free node with the correct size
|
|
|
|
size_t revindex = m_free_nodes_count;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
while (revindex-- && ptr == BT_UINT_MAX)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_allocated_sizes[m_free_nodes[revindex]] >= num_elements)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
ptr = revindex;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (ptr == BT_UINT_MAX) return BT_UINT_MAX; // not found
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
revindex = ptr;
|
|
|
|
ptr = m_free_nodes[revindex];
|
|
|
|
// post: ptr contains the node index, and revindex the index in m_free_nodes
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
size_t finalsize = m_allocated_sizes[ptr];
|
2017-08-01 12:30:58 +00:00
|
|
|
finalsize -= num_elements;
|
|
|
|
|
|
|
|
m_allocated_sizes[ptr] = num_elements;
|
|
|
|
|
|
|
|
// post: finalsize>=0, m_allocated_sizes[ptr] has the requested size
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (finalsize > 0) // preserve free node, there are some free memory
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_free_nodes[revindex] = ptr + num_elements;
|
|
|
|
m_allocated_sizes[ptr + num_elements] = finalsize;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else // delete free node
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// swap with end
|
2019-01-03 13:26:51 +00:00
|
|
|
m_free_nodes[revindex] = m_free_nodes[m_free_nodes_count - 1];
|
2017-08-01 12:30:58 +00:00
|
|
|
m_free_nodes_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t btGenericMemoryPool::allocate_from_pool(size_t num_elements)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_allocated_count + num_elements > m_max_element_count) return BT_UINT_MAX;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
size_t ptr = m_allocated_count;
|
|
|
|
|
|
|
|
m_allocated_sizes[m_allocated_count] = num_elements;
|
2019-01-03 13:26:51 +00:00
|
|
|
m_allocated_count += num_elements;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void btGenericMemoryPool::init_pool(size_t element_size, size_t element_count)
|
|
|
|
{
|
|
|
|
m_allocated_count = 0;
|
|
|
|
m_free_nodes_count = 0;
|
|
|
|
|
|
|
|
m_element_size = element_size;
|
|
|
|
m_max_element_count = element_count;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
m_pool = (unsigned char *)btAlignedAlloc(m_element_size * m_max_element_count, 16);
|
|
|
|
m_free_nodes = (size_t *)btAlignedAlloc(sizeof(size_t) * m_max_element_count, 16);
|
|
|
|
m_allocated_sizes = (size_t *)btAlignedAlloc(sizeof(size_t) * m_max_element_count, 16);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (size_t i = 0; i < m_max_element_count; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_allocated_sizes[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void btGenericMemoryPool::end_pool()
|
|
|
|
{
|
|
|
|
btAlignedFree(m_pool);
|
|
|
|
btAlignedFree(m_free_nodes);
|
|
|
|
btAlignedFree(m_allocated_sizes);
|
|
|
|
m_allocated_count = 0;
|
|
|
|
m_free_nodes_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Allocates memory in pool
|
|
|
|
/*!
|
|
|
|
\param size_bytes size in bytes of the buffer
|
|
|
|
*/
|
2019-01-03 13:26:51 +00:00
|
|
|
void *btGenericMemoryPool::allocate(size_t size_bytes)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
size_t module = size_bytes % m_element_size;
|
|
|
|
size_t element_count = size_bytes / m_element_size;
|
|
|
|
if (module > 0) element_count++;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
size_t alloc_pos = allocate_from_free_nodes(element_count);
|
|
|
|
// a free node is found
|
2019-01-03 13:26:51 +00:00
|
|
|
if (alloc_pos != BT_UINT_MAX)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return get_element_data(alloc_pos);
|
|
|
|
}
|
|
|
|
// allocate directly on pool
|
|
|
|
alloc_pos = allocate_from_pool(element_count);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (alloc_pos == BT_UINT_MAX) return NULL; // not space
|
2017-08-01 12:30:58 +00:00
|
|
|
return get_element_data(alloc_pos);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool btGenericMemoryPool::freeMemory(void *pointer)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
unsigned char *pointer_pos = (unsigned char *)pointer;
|
|
|
|
unsigned char *pool_pos = (unsigned char *)m_pool;
|
2017-08-01 12:30:58 +00:00
|
|
|
// calc offset
|
2019-01-03 13:26:51 +00:00
|
|
|
if (pointer_pos < pool_pos) return false; //other pool
|
2017-08-01 12:30:58 +00:00
|
|
|
size_t offset = size_t(pointer_pos - pool_pos);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (offset >= get_pool_capacity()) return false; // far away
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// find free position
|
2019-01-03 13:26:51 +00:00
|
|
|
m_free_nodes[m_free_nodes_count] = offset / m_element_size;
|
2017-08-01 12:30:58 +00:00
|
|
|
m_free_nodes_count++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// *******************! btGenericPoolAllocator *******************!///
|
|
|
|
|
|
|
|
btGenericPoolAllocator::~btGenericPoolAllocator()
|
|
|
|
{
|
|
|
|
// destroy pools
|
|
|
|
size_t i;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 0; i < m_pool_count; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_pools[i]->end_pool();
|
|
|
|
btAlignedFree(m_pools[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a pool
|
2019-01-03 13:26:51 +00:00
|
|
|
btGenericMemoryPool *btGenericPoolAllocator::push_new_pool()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_pool_count >= BT_DEFAULT_MAX_POOLS) return NULL;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btGenericMemoryPool *newptr = (btGenericMemoryPool *)btAlignedAlloc(sizeof(btGenericMemoryPool), 16);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
m_pools[m_pool_count] = newptr;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
m_pools[m_pool_count]->init_pool(m_pool_element_size, m_pool_element_count);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
m_pool_count++;
|
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void *btGenericPoolAllocator::failback_alloc(size_t size_bytes)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btGenericMemoryPool *pool = NULL;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (size_bytes <= get_pool_capacity())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
pool = push_new_pool();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (pool == NULL) // failback
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return btAlignedAlloc(size_bytes, 16);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pool->allocate(size_bytes);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool btGenericPoolAllocator::failback_free(void *pointer)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btAlignedFree(pointer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Allocates memory in pool
|
|
|
|
/*!
|
|
|
|
\param size_bytes size in bytes of the buffer
|
|
|
|
*/
|
2019-01-03 13:26:51 +00:00
|
|
|
void *btGenericPoolAllocator::allocate(size_t size_bytes)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
void *ptr = NULL;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
size_t i = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
while (i < m_pool_count && ptr == NULL)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
ptr = m_pools[i]->allocate(size_bytes);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (ptr) return ptr;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return failback_alloc(size_bytes);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool btGenericPoolAllocator::freeMemory(void *pointer)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
size_t i = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
while (i < m_pool_count && result == false)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
result = m_pools[i]->freeMemory(pointer);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (result) return true;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return failback_free(pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ************** STANDARD ALLOCATOR ***************************///
|
|
|
|
|
|
|
|
#define BT_DEFAULT_POOL_SIZE 32768
|
|
|
|
#define BT_DEFAULT_POOL_ELEMENT_SIZE 8
|
|
|
|
|
|
|
|
// main allocator
|
2019-01-03 13:26:51 +00:00
|
|
|
class GIM_STANDARD_ALLOCATOR : public btGenericPoolAllocator
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-01-03 13:26:51 +00:00
|
|
|
GIM_STANDARD_ALLOCATOR() : btGenericPoolAllocator(BT_DEFAULT_POOL_ELEMENT_SIZE, BT_DEFAULT_POOL_SIZE)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// global allocator
|
|
|
|
GIM_STANDARD_ALLOCATOR g_main_allocator;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void *btPoolAlloc(size_t size)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return g_main_allocator.allocate(size);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void *btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
void *newptr = btPoolAlloc(newsize);
|
|
|
|
size_t copysize = oldsize < newsize ? oldsize : newsize;
|
|
|
|
memcpy(newptr, ptr, copysize);
|
|
|
|
btPoolFree(ptr);
|
|
|
|
return newptr;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void btPoolFree(void *ptr)
|
|
|
|
{
|
|
|
|
g_main_allocator.freeMemory(ptr);
|
|
|
|
}
|