forked from XadillaX/xmempool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmempool.c
366 lines (304 loc) · 9.32 KB
/
xmempool.c
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* =====================================================================================
*
* Filename: xmempool.c
*
* Description: XadillaX' memory pool for C.
*
* Version: 1.0
* Created: 2014/12/08 11时41分48秒
* Revision: none
* Compiler: gcc
*
* Author: XadillaX
* Organization: Huaban.com
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xmempool.h"
typedef struct xmem_pool_block {
void* start;
unsigned int block_size;
struct xmem_pool_block* next;
unsigned char is_block_start;
} xmem_pool_block;
typedef struct xmem_pool {
unsigned int block_size;
unsigned int block_count;
void* start;
void* end;
struct xmem_pool_block* free_blocks;
struct xmem_pool_block* free_blocks_tail;
struct xmem_pool* next;
} xmem_pool;
#define ALLOC_BLOCK_NODE_COUNT (1024)
#define MIN_ALLOC_LENGTH (1024)
xmem_pool_block* _free_block_ptr = 0;
xmem_pool_block* _free_block_ptr_end = 0;
void _alloc_block_nodes()
{
static unsigned int _block_size = sizeof(xmem_pool_block);
unsigned int size_count = _block_size * ALLOC_BLOCK_NODE_COUNT;
void* start;
void* block_ptr = start = malloc(size_count);
void* end = start + size_count;
// can't malloc more space(s).
if(!block_ptr) return;
for(; block_ptr < end; block_ptr += _block_size)
{
xmem_pool_block* block = (xmem_pool_block*)block_ptr;
block->is_block_start = (block_ptr == start);
block->start = 0;
// if it's not starting block,
// set previous block's next block
// to this.
if(block_ptr != start)
{
((xmem_pool_block*)(block_ptr - _block_size))->next = block;
}
block->block_size = 0;
}
((xmem_pool_block*)(end - _block_size))->next = 0;
if(_free_block_ptr && _free_block_ptr_end)
{
// there's block pointer(s) before,
// and still some in the `_free_block_ptr`,
// concat newer one list to the old one's tail.
_free_block_ptr_end->next = (xmem_pool_block*)start;
}
else
{
// otherwise, replace the whole `_free_block_ptr`.
_free_block_ptr = start;
}
_free_block_ptr_end = (xmem_pool_block*)(end - _block_size);
}
xmem_pool_block* _get_next_block_node()
{
// if no more block node,
// generate a new block node list
if(!_free_block_ptr)
{
_alloc_block_nodes();
}
// if still no more block node,
// return 0 to stand for error
if(!_free_block_ptr) return (xmem_pool_block*)0;
xmem_pool_block* should_return = _free_block_ptr;
_free_block_ptr = _free_block_ptr->next;
// if `should_return` is the last one,
// we should set the tail to 0.
if(should_return == _free_block_ptr_end)
{
_free_block_ptr_end = 0;
}
return should_return;
}
void _recover_block_node(xmem_pool_block* node)
{
node->start = 0;
node->next = 0;
node->block_size = 0;
if(_free_block_ptr && _free_block_ptr_end)
{
_free_block_ptr_end->next = node;
_free_block_ptr_end = node;
}
else
{
_free_block_ptr = _free_block_ptr_end = node;
}
}
int _xmem_count_free_blocks(xmem_pool* pool)
{
if(!pool->free_blocks) return 0;
xmem_pool_block* block = pool->free_blocks;
int count = 1;
while(block != pool->free_blocks_tail)
{
count++;
block = block->next;
}
return count;
}
xmem_pool_handle _create_pool(unsigned int block_size,
unsigned int block_count)
{
static unsigned int _pool_size = sizeof(xmem_pool);
xmem_pool* pool = (xmem_pool*)malloc(_pool_size);
void* space = malloc(block_count * block_size);
// can't malloc more space
if(!pool) return (void*)0;
if(!space)
{
free(pool);
return (void*)0;
}
// set the pool space's start and end
pool->start = space;
pool->end = space + (block_count * block_size);
pool->block_size = block_size;
pool->block_count = block_count;
pool->next = 0;
// create free nodes!
xmem_pool_block* start_block = 0;
xmem_pool_block* end_block = 0;
for(; space < pool->end; space += block_size)
{
xmem_pool_block* wrapper = _get_next_block_node();
if(!start_block)
{
start_block = wrapper;
}
// if we can't create any more wrapper,
// free `pool`, `space`,
// and recover all pool blocks genereated before
if(!wrapper)
{
free(pool);
free(space);
while(start_block)
{
xmem_pool_block* next = start_block->next;
_recover_block_node(start_block);
start_block = next;
}
return 0;
}
wrapper->block_size = block_size;
wrapper->start = space;
wrapper->next = 0;
// concat wrapper to the tail of block list
if(end_block) end_block->next = wrapper;
end_block = wrapper;
}
pool->free_blocks = start_block;
pool->free_blocks_tail = end_block;
return (void*)pool;
}
void xmem_print_info(xmem_pool_handle _pool)
{
if(!_pool) return;
xmem_pool* pool = _pool;
int pool_id = 0;
while(pool)
{
printf("----- POOL OF SIZE [%.4d] -----\n", pool->block_size);
printf(" + id: %d\n", pool_id++);
printf(" + count: %d\n", pool->block_count);
printf(" + spaces: [0x%.8X, 0x%.8X)\n", (unsigned int)pool->start,
(unsigned int)pool->end);
if(pool_id == 1)
{
printf(" + free blocks: %d\n", _xmem_count_free_blocks(pool));
}
else
{
printf(" + free blocks: -\n");
}
pool = pool->next;
}
}
xmem_pool_handle xmem_create_pool(unsigned int block_size)
{
#ifdef XMEM_DBG
void* pool = _create_pool(block_size, MIN_ALLOC_LENGTH);
printf("A new pool of [%d] is created!", block_size);
return pool;
#endif
return _create_pool(block_size, MIN_ALLOC_LENGTH);
}
void xmem_destroy_pool(xmem_pool_handle handle)
{
if(!handle) return;
xmem_pool* pool = (xmem_pool*)handle;
xmem_pool* next_pool;
// from pool head to pool tail
while(pool)
{
// free the space.
// and then remove all free blocks object
// from head to tail.
free(pool->start);
// all the blocks are mansged in the
// first pool.
if(pool == handle)
{
xmem_pool_block* block = pool->free_blocks;
xmem_pool_block* next_block;
// from block head to block tail
while(block)
{
next_block = block->next;
_recover_block_node(block);
block = next_block;
}
}
// move to next pool
next_pool = pool->next;
free(pool);
pool = next_pool;
}
return;
}
void* xmem_alloc(xmem_pool_handle handle)
{
static unsigned int pool_element_size = sizeof(xmem_pool);
xmem_pool* pool = (xmem_pool*)handle;
// if no more space, we create a new pool
if(!pool->free_blocks)
{
xmem_pool* new_pool = (xmem_pool*)_create_pool(pool->block_size,
pool->block_count << 1);
if(!new_pool)
{
return 0;
}
xmem_pool temp_pool;
memcpy(&temp_pool, pool, pool_element_size);
memcpy(pool, new_pool, pool_element_size);
memcpy(new_pool, &temp_pool, pool_element_size);
pool->next = new_pool;
// ** 我太特么聪明了 **
//
// no matter how many pools here, all free
// blocks are managed in the first pool.
//
// so we needn't do anything here.
}
// get the first free space
xmem_pool_block* block = pool->free_blocks;
pool->free_blocks = block->next;
void* space = block->start;
if(!pool->free_blocks) pool->free_blocks_tail = 0;
// initialize space data & recover block node
memset(space, 0, block->block_size);
_recover_block_node(block);
return space;
}
int xmem_free(xmem_pool_handle handle, void* pointer)
{
xmem_pool* pool = (xmem_pool*)handle;
// get a block.
// if no block, return false.
xmem_pool_block* block = _get_next_block_node();
if(!block)
{
return 0;
}
// create a new block to the free list.
block->block_size = pool->block_size;
block->start = pointer;
block->next = 0;
if(!pool->free_blocks && !pool->free_blocks_tail)
{
pool->free_blocks = pool->free_blocks_tail = block;
return 1;
}
pool->free_blocks_tail->next = block;
pool->free_blocks_tail = block;
return 1;
}