summaryrefslogtreecommitdiff
path: root/src/BulletCollision/Gimpact/btGenericPoolAllocator.cpp
blob: f81f04be9074cc8899a856a484758d368e63f8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*! \file btGenericPoolAllocator.cpp
\author Francisco Leon Najera. email projectileman@yahoo.com

General purpose allocator class
*/
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans  https://bulletphysics.org

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;

	if (m_free_nodes_count == 0) return BT_UINT_MAX;
	// find an avaliable free node with the correct size
	size_t revindex = m_free_nodes_count;

	while (revindex-- && ptr == BT_UINT_MAX)
	{
		if (m_allocated_sizes[m_free_nodes[revindex]] >= num_elements)
		{
			ptr = revindex;
		}
	}
	if (ptr == BT_UINT_MAX) return BT_UINT_MAX;  // not found

	revindex = ptr;
	ptr = m_free_nodes[revindex];
	// post: ptr contains the node index, and revindex the index in m_free_nodes

	size_t finalsize = m_allocated_sizes[ptr];
	finalsize -= num_elements;

	m_allocated_sizes[ptr] = num_elements;

	// post: finalsize>=0, m_allocated_sizes[ptr] has the requested size

	if (finalsize > 0)  // preserve free node, there are some free memory
	{
		m_free_nodes[revindex] = ptr + num_elements;
		m_allocated_sizes[ptr + num_elements] = finalsize;
	}
	else  // delete free node
	{
		// swap with end
		m_free_nodes[revindex] = m_free_nodes[m_free_nodes_count - 1];
		m_free_nodes_count--;
	}

	return ptr;
}

size_t btGenericMemoryPool::allocate_from_pool(size_t num_elements)
{
	if (m_allocated_count + num_elements > m_max_element_count) return BT_UINT_MAX;

	size_t ptr = m_allocated_count;

	m_allocated_sizes[m_allocated_count] = num_elements;
	m_allocated_count += num_elements;

	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;

	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);

	for (size_t i = 0; i < m_max_element_count; i++)
	{
		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
*/
void *btGenericMemoryPool::allocate(size_t size_bytes)
{
	size_t module = size_bytes % m_element_size;
	size_t element_count = size_bytes / m_element_size;
	if (module > 0) element_count++;

	size_t alloc_pos = allocate_from_free_nodes(element_count);
	// a free node is found
	if (alloc_pos != BT_UINT_MAX)
	{
		return get_element_data(alloc_pos);
	}
	// allocate directly on pool
	alloc_pos = allocate_from_pool(element_count);

	if (alloc_pos == BT_UINT_MAX) return NULL;  // not space
	return get_element_data(alloc_pos);
}

bool btGenericMemoryPool::freeMemory(void *pointer)
{
	unsigned char *pointer_pos = (unsigned char *)pointer;
	unsigned char *pool_pos = (unsigned char *)m_pool;
	// calc offset
	if (pointer_pos < pool_pos) return false;  //other pool
	size_t offset = size_t(pointer_pos - pool_pos);
	if (offset >= get_pool_capacity()) return false;  // far away

	// find free position
	m_free_nodes[m_free_nodes_count] = offset / m_element_size;
	m_free_nodes_count++;
	return true;
}

/// *******************! btGenericPoolAllocator *******************!///

btGenericPoolAllocator::~btGenericPoolAllocator()
{
	// destroy pools
	size_t i;
	for (i = 0; i < m_pool_count; i++)
	{
		m_pools[i]->end_pool();
		btAlignedFree(m_pools[i]);
	}
}

// creates a pool
btGenericMemoryPool *btGenericPoolAllocator::push_new_pool()
{
	if (m_pool_count >= BT_DEFAULT_MAX_POOLS) return NULL;

	btGenericMemoryPool *newptr = (btGenericMemoryPool *)btAlignedAlloc(sizeof(btGenericMemoryPool), 16);

	m_pools[m_pool_count] = newptr;

	m_pools[m_pool_count]->init_pool(m_pool_element_size, m_pool_element_count);

	m_pool_count++;
	return newptr;
}

void *btGenericPoolAllocator::failback_alloc(size_t size_bytes)
{
	btGenericMemoryPool *pool = NULL;

	if (size_bytes <= get_pool_capacity())
	{
		pool = push_new_pool();
	}

	if (pool == NULL)  // failback
	{
		return btAlignedAlloc(size_bytes, 16);
	}

	return pool->allocate(size_bytes);
}

bool btGenericPoolAllocator::failback_free(void *pointer)
{
	btAlignedFree(pointer);
	return true;
}

//! Allocates memory in pool
/*!
\param size_bytes size in bytes of the buffer
*/
void *btGenericPoolAllocator::allocate(size_t size_bytes)
{
	void *ptr = NULL;

	size_t i = 0;
	while (i < m_pool_count && ptr == NULL)
	{
		ptr = m_pools[i]->allocate(size_bytes);
		++i;
	}

	if (ptr) return ptr;

	return failback_alloc(size_bytes);
}

bool btGenericPoolAllocator::freeMemory(void *pointer)
{
	bool result = false;

	size_t i = 0;
	while (i < m_pool_count && result == false)
	{
		result = m_pools[i]->freeMemory(pointer);
		++i;
	}

	if (result) return true;

	return failback_free(pointer);
}

/// ************** STANDARD ALLOCATOR ***************************///

#define BT_DEFAULT_POOL_SIZE 32768
#define BT_DEFAULT_POOL_ELEMENT_SIZE 8

// main allocator
class GIM_STANDARD_ALLOCATOR : public btGenericPoolAllocator
{
public:
	GIM_STANDARD_ALLOCATOR() : btGenericPoolAllocator(BT_DEFAULT_POOL_ELEMENT_SIZE, BT_DEFAULT_POOL_SIZE)
	{
	}
};

// global allocator
GIM_STANDARD_ALLOCATOR g_main_allocator;

void *btPoolAlloc(size_t size)
{
	return g_main_allocator.allocate(size);
}

void *btPoolRealloc(void *ptr, size_t oldsize, size_t newsize)
{
	void *newptr = btPoolAlloc(newsize);
	size_t copysize = oldsize < newsize ? oldsize : newsize;
	memcpy(newptr, ptr, copysize);
	btPoolFree(ptr);
	return newptr;
}

void btPoolFree(void *ptr)
{
	g_main_allocator.freeMemory(ptr);
}