forked from userpro/MemoryPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemorypool.c
306 lines (254 loc) · 6.22 KB
/
memorypool.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
#include "memorypool.h"
void get_memory_list_count(MemoryPool *mp, mem_size_t *mlist_len)
{
mem_size_t mlist_l = 0;
Memory *mm = mp->mlist;
while (mm)
{
mlist_l++;
mm = mm->next;
}
*mlist_len = mlist_l;
}
void get_memory_info(Memory *mm, mem_size_t *free_list_len, mem_size_t *alloc_list_len)
{
mem_size_t free_l = 0, alloc_l = 0;
Chunk *p = mm->free_list;
while (p)
{
free_l++;
p = p->next;
}
p = mm->alloc_list;
while (p)
{
alloc_l++;
p = p->next;
}
*free_list_len = free_l;
*alloc_list_len = alloc_l;
}
int get_memory_id(Memory *mm)
{
return mm->id;
}
static Memory *extend_memory_list(MemoryPool *mp)
{
Memory *mm = (Memory *)malloc(sizeof(Memory));
if (!mm)
return NULL;
mm->start = (char *)malloc(mp->mem_pool_size * sizeof(char));
if (!mm->start)
return NULL;
init_Memory(mm);
mm->id = mp->last_id++;
mm->next = mp->mlist;
mp->mlist = mm;
return mm;
}
static Memory *find_memory_list(MemoryPool *mp, void *p)
{
Memory *tmp = mp->mlist;
while (tmp)
{
if (tmp->start <= (char *)p && tmp->start + mp->mem_pool_size > (char *)p)
break;
tmp = tmp->next;
}
return tmp;
}
static bool merge_free_chunk(MemoryPool *mp, Memory *mm, Chunk *c)
{
if (!mp || !mm || !c)
return 0;
Chunk *p0 = *(Chunk **)((char *)c - CHUNKEND), *p1 = c;
while ((char *)p0 > mm->start && p0->is_free)
{
p1 = p0;
p0 = *(Chunk **)((char *)p0 - CHUNKEND);
}
p0 = (Chunk *)((char *)p1 + p1->alloc_mem);
while ((char *)p0 < mm->start + mp->mem_pool_size && p0->is_free)
{
dlinklist_delete(mm->free_list, p0);
p1->alloc_mem += p0->alloc_mem;
p0 = (Chunk *)((char *)p0 + p0->alloc_mem);
}
*(Chunk **)((char *)p1 + p1->alloc_mem - CHUNKEND) = p1;
return 1;
}
MemoryPool *MemoryPool_Init(mem_size_t mempoolsize, bool auto_extend)
{
if (mempoolsize > MAX_MEM_SIZE)
{
// printf("[MemoryPool_Init] MemPool Init ERROR! Mempoolsize is too big! \n");
return NULL;
}
MemoryPool *mp = (MemoryPool *)malloc(sizeof(MemoryPool));
if (!mp)
{
// printf("[MemoryPool_Init] Alloc memorypool struct error!\n");
return NULL;
}
mp->total_mem_pool_size = mp->mem_pool_size = mempoolsize;
mp->auto_extend = auto_extend;
mp->last_id = 0;
mp->mlist = (Memory*)malloc(sizeof(Memory));
if (!mp->mlist)
return NULL;
char *s = (char *)malloc(sizeof(char) * mp->mem_pool_size); // memory pool
if (!s)
{
// printf("[MemoryPool_Init] No memory! \n");
return NULL;
}
mp->mlist->start = s;
init_Memory(mp->mlist);
mp->mlist->next = NULL;
mp->mlist->id = mp->last_id++;
return mp;
}
void *MemoryPool_Alloc(MemoryPool *mp, mem_size_t wantsize)
{
mem_size_t total_needed_size = wantsize + CHUNKHEADER + CHUNKEND;
if (total_needed_size > mp->mem_pool_size) return NULL;
Memory *mm = NULL, *mm1 = NULL;
Chunk *_free = NULL, *_not_free = NULL;
FIND_FREE_CHUNK:
mm = mp->mlist;
while (mm)
{
if (mp->mem_pool_size - mm->alloc_mem < total_needed_size)
{
mm = mm->next;
continue;
}
_free = mm->free_list;
_not_free = NULL;
while (_free)
{
if (_free->alloc_mem >= total_needed_size)
{
// 如果free块分割后剩余内存足够大 则进行分割
if (_free->alloc_mem - total_needed_size > CHUNKHEADER + CHUNKEND)
{
// 从free块头开始分割出alloc块
// nf指向分割后的free块
_not_free = _free;
_free = (Chunk *)((char *)_not_free + total_needed_size);
*_free = *_not_free;
_free->alloc_mem -= total_needed_size;
*(Chunk **)((char *)_free + _free->alloc_mem - CHUNKEND) = _free;
// update free_list
if (!_free->prev)
{
mm->free_list = _free;
if (_free->next) _free->next->prev = _free;
}
else
{
_free->prev->next = _free;
if (_free->next) _free->next->prev = _free;
}
_not_free->is_free = 0;
_not_free->alloc_mem = total_needed_size;
*(Chunk **)((char *)_not_free + total_needed_size - CHUNKEND) = _not_free;
}
// 不够 则整块分配为alloc
else
{
_not_free = _free;
dlinklist_delete(mm->free_list, _not_free);
_not_free->is_free = 0;
}
dlinklist_insert_front(mm->alloc_list, _not_free);
mm->alloc_mem += _not_free->alloc_mem;
mm->alloc_prog_mem += (_not_free->alloc_mem - CHUNKHEADER - CHUNKEND);
return (void *)((char *)_not_free + CHUNKHEADER);
}
_free = _free->next;
}
mm = mm->next;
}
if (mp->auto_extend)
{
mm1 = extend_memory_list(mp);
if (!mm1)
return NULL;
mp->total_mem_pool_size += mp->mem_pool_size;
goto FIND_FREE_CHUNK;
}
// printf("[MemoryPool_Alloc] No enough memory! \n");
return NULL;
}
bool MemoryPool_Free(MemoryPool *mp, void *p)
{
if (!p || !mp)
return 0;
Memory *mm = mp->mlist;
if (mp->auto_extend)
mm = find_memory_list(mp, p);
Chunk *ck = (Chunk *)((char *)p - CHUNKHEADER);
dlinklist_delete(mm->alloc_list, ck);
dlinklist_insert_front(mm->free_list, ck);
ck->is_free = 1;
mm->alloc_mem -= ck->alloc_mem;
mm->alloc_prog_mem -= (ck->alloc_mem - CHUNKHEADER - CHUNKEND);
merge_free_chunk(mp, mm, ck);
return 1;
}
MemoryPool *MemoryPool_Clear(MemoryPool *mp)
{
if (!mp)
{
// printf("[MemoryPool_Clear] Memory Pool is NULL! \n");
return 0;
}
Memory *mm = mp->mlist;
while (mm)
{
init_Memory(mm);
mm = mm->next;
}
return mp;
}
bool MemoryPool_Destroy(MemoryPool *mp)
{
if (!mp)
{
// printf("[MemoryPool_Destroy] Memory Pool is NULL! \n");
return 0;
}
Memory *mm = mp->mlist, *mm1 = NULL;
while (mm)
{
free(mm->start);
mm1 = mm;
mm = mm->next;
free(mm1);
}
free(mp);
return 1;
}
double get_mempool_usage(MemoryPool *mp)
{
mem_size_t total_alloc = 0;
Memory *mm = mp->mlist;
while (mm)
{
total_alloc += mm->alloc_mem;
mm = mm->next;
}
return (double)total_alloc / mp->total_mem_pool_size;
}
double get_mempool_prog_usage(MemoryPool *mp)
{
mem_size_t total_alloc_prog = 0;
Memory *mm = mp->mlist;
while (mm)
{
total_alloc_prog += mm->alloc_prog_mem;
mm = mm->next;
}
return (double)total_alloc_prog / mp->total_mem_pool_size;
}