-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.c
237 lines (184 loc) · 4.68 KB
/
pool.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
/***************************************************************************
begin........: June 2012
copyright....: Sebastian Fedrau
email........: [email protected]
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License v3 for more details.
***************************************************************************/
/**
* \file pool.c
* \brief Allocate memory blocks of same sizes.
* \author Sebastian Fedrau <[email protected]>
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "pool.h"
static struct _MemoryBlock *
_memory_pool_create_block(const MemoryPool *pool)
{
assert(pool != NULL);
struct _MemoryBlock *block;
if(!(block = (struct _MemoryBlock *)malloc(sizeof(struct _MemoryBlock))))
{
perror("malloc()");
abort();
}
if(!(block->items = malloc(pool->item_size * pool->block_size)))
{
perror("malloc()");
abort();
}
block->offset = 0;
block->next = NULL;
return block;
}
static struct _MemoryPtrBlock *
_memory_pool_create_ptr_block(const MemoryPool *pool)
{
assert(pool != NULL);
struct _MemoryPtrBlock *block;
if(!(block = (struct _MemoryPtrBlock *)malloc(sizeof(struct _MemoryPtrBlock))))
{
perror("malloc()");
abort();
}
if(!(block->items = (void **)malloc(pool->block_size * sizeof(void *))))
{
perror("malloc()");
abort();
}
block->offset = 0;
block->next = NULL;
return block;
}
static void *
_memory_pool_try_get_detached_item(MemoryPool *pool)
{
assert(pool != NULL);
void *item = NULL;
if(pool->free_block)
{
assert(pool->free_block->offset > 0);
item = pool->free_block->items[--pool->free_block->offset];
if(!pool->free_block->offset)
{
struct _MemoryPtrBlock *pblock = pool->free_block;
pool->free_block = pblock->next;
free(pblock->items);
free(pblock);
}
}
return item;
}
static void *
_memory_pool_try_get_current_item(const MemoryPool *pool)
{
assert(pool != NULL);
void *item = NULL;
if(pool->block->offset < pool->block_size)
{
item = pool->block->items + (pool->item_size * pool->block->offset++);
}
return item;
}
static void *
_memory_pool_get_item_from_new_block(MemoryPool *pool)
{
assert(pool != NULL);
struct _MemoryBlock *block = _memory_pool_create_block(pool);
block->next = pool->block;
pool->block = block;
/* return first item from current block & increment offset */
void *item = pool->block->items;
++pool->block->offset;
return item;
}
static void *
_memory_pool_alloc(Pool *alloc)
{
MemoryPool *pool = (MemoryPool *)alloc;
void *item = _memory_pool_try_get_detached_item(pool);
if (!item)
{
item = _memory_pool_try_get_current_item(pool);
}
if (!item)
{
item = _memory_pool_get_item_from_new_block(pool);
}
return item;
}
static void
_memory_pool_free(Pool *alloc, void *item)
{
MemoryPool *pool = (MemoryPool *)alloc;
struct _MemoryPtrBlock *cur;
if(!(cur = pool->free_block))
{
pool->free_block = cur = _memory_pool_create_ptr_block(pool);
}
else if(cur->offset == pool->block_size)
{
cur = _memory_pool_create_ptr_block(pool);
cur->next = pool->free_block;
pool->free_block = cur;
}
cur->items[cur->offset++] = item;
}
MemoryPool *
memory_pool_new(size_t item_size, size_t block_size)
{
MemoryPool *pool;
assert(item_size > 1);
assert(block_size > 1);
assert(block_size < SIZE_MAX / item_size);
assert(block_size < SIZE_MAX / sizeof(void *));
if(!(pool = (MemoryPool *)malloc(sizeof(MemoryPool))))
{
perror("malloc()");
abort();
}
pool->free_block = NULL;
pool->item_size = item_size;
pool->block_size = block_size;
pool->block = _memory_pool_create_block(pool);
((Pool *)pool)->alloc = _memory_pool_alloc;
((Pool *)pool)->free = _memory_pool_free;
return pool;
}
void
memory_pool_destroy(MemoryPool *pool)
{
struct _MemoryBlock *iter;
struct _MemoryPtrBlock *piter;
assert(pool != NULL);
/* free memory blocks & list */
iter = pool->block;
while(iter)
{
struct _MemoryBlock *block;
block = iter;
iter = iter->next;
free(block->items);
free(block);
}
/* free list containing free items */
piter = pool->free_block;
while(piter)
{
struct _MemoryPtrBlock *pblock;
pblock = piter;
piter = piter->next;
free(pblock->items);
free(pblock);
}
free(pool);
}