-
Notifications
You must be signed in to change notification settings - Fork 1
/
ms_mem.c
343 lines (309 loc) · 9.71 KB
/
ms_mem.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
#include "ms_mem.h"
static void *ms_mem_pool_pcalloc_large(ms_mem_pool_t *pool, size_t size);
static void *ms_mem_pool_pcalloc_small(ms_mem_pool_t *pool, size_t size);
/***********************************************************
* @Func : ms_mem_pool_create()
* @Author : lwp
* @Brief : 创建内存池。
* @Param : [in] size : 小块内存可分配的最大内存
* @Return : NULL : 失败
* pool : 成功
* @Note :
***********************************************************/
ms_mem_pool_t *ms_mem_pool_create(size_t size)
{
int rev;
size_t nalloc;
ms_mem_pool_t *pool = NULL;
nalloc = size + sizeof(ms_mem_pool_t);
rev = posix_memalign((void **)&pool, MS_MEM_POOL_ALIGNMENT, nalloc);
if (rev)
{
ms_errlog(MS_ERRLOG_ERR, rev, MEM_TAG "posix_memalign() failed");
return NULL;
}
memset(pool, 0, nalloc);
pool->small.last = (char *)pool + sizeof(ms_mem_pool_t);
pool->small.end = (char *)pool + nalloc;
pool->small.next = NULL;
pool->small.failed = 0;
pool->large = NULL;
pool->current = pool;
pool->max = size;
return pool;
}
// @ms_mem_pool_create() ok
/***********************************************************
* @Func : ms_mem_pool_pcalloc()
* @Author : lwp
* @Brief : 从内存池 pool 以内存对齐方式分配并初始化 size 字节的内存。
* @Param : [in] pool
* @Param : [in] size
* @Return : NULL : 失败
* !NULL : 成功
* @Note :
***********************************************************/
void *ms_mem_pool_pcalloc(ms_mem_pool_t *pool, size_t size)
{
// 若申请的 size 小于 pool->max 则从小块内存链中分配
if (size <= pool->max)
{
return ms_mem_pool_pcalloc_small(pool, size);
}
// 若申请的 size 大于 pool->max 则从大块内存链中分配
else
{
return ms_mem_pool_pcalloc_large(pool, size);
}
}
// @ms_mem_pool_pcalloc() ok
/***********************************************************
* @Func : ms_mem_pool_destory()
* @Author : lwp
* @Brief : 销毁 pool 内存池。
* @Param : [in] pool
* @Return : NONE
* @Note :
***********************************************************/
void ms_mem_pool_destory(ms_mem_pool_t **pool_ptr)
{
ms_mem_pool_small_t *d, *n;
ms_mem_pool_large_t *l;
ms_mem_pool_t *pool = *pool_ptr;
if (NULL == pool)
{
return;
}
// 释放大块内存的存储空间
for (l = pool->large; l; l = l->next)
{
if (l->alloc)
{
free(l->alloc);
l->alloc = NULL;
}
}
// 释放大块内存的管理结构体,释放小块内存存储空间和管理结构体
for (d = &(pool->small), n = d->next; /* void */; d = n, n = d->next)
{
free(d);
d = NULL;
if (n == NULL)
{
break;
}
}
*pool_ptr = NULL;
}
// @ms_mem_pool_destory() ok
/***********************************************************
* @Func : ms_mem_pool_reset()
* @Author : lwp
* @Brief : 重置 pool 内存池。
* @Param : [in] pool
* @Return : NONE
* @Note :
***********************************************************/
void ms_mem_pool_reset(ms_mem_pool_t *pool)
{
ms_mem_pool_large_t *l;
ms_mem_pool_small_t *d;
// 释放大块内存的存储空间
for (l = pool->large; l; l = l->next)
{
if (l->alloc)
{
free(l->alloc);
l->alloc = NULL;
}
}
// 重置小块内存的指针位置
pool->small.last = (char *)pool + sizeof(ms_mem_pool_t);
pool->small.failed = 0;
pool->current = pool;
pool->large = NULL;
for (d = pool->small.next; d; d = d->next)
{
d->last = (char *)d + sizeof(ms_mem_pool_small_t);
d->failed = 0;
}
}
// @ms_mem_pool_reset() ok
/***********************************************************
* @Func : ms_mem_pool_test()
* @Author : lwp
* @Brief : 打印内存池堆栈信息。
* @Param : [in] pool
* @Return : NONE
* @Note :
***********************************************************/
void ms_mem_pool_test(ms_mem_pool_t *pool)
{
ms_mem_pool_small_t *d;
ms_mem_pool_large_t *l;
printf("[----------- pool infor -----------]\n"
"sizeof(ms_mem_pool_t) : \"%lud\"\n"
"sizeof(ms_mem_pool_small_t) : \"%lud\"\n"
"sizeof(ms_mem_pool_large_t) : \"%lud\"\n"
" pool : \"%p\"\n"
" pool->current : \"%p\"\n"
" pool->max : \"%lud\"\n",
sizeof(ms_mem_pool_t),
sizeof(ms_mem_pool_small_t),
sizeof(ms_mem_pool_large_t),
pool,
pool->current,
pool->max);
for (d = &(pool->small); d; d = d->next)
{
printf(" ->small : \"%p\"\n"
" ->last : \"%p\"\n"
" ->end : \"%p\"\n"
" ->used : \"%lud\"\n"
" ->unused : \"%lud\"\n"
" ->failed : \"%lud\"\n"
" ->next : \"%p\"\n",
d,
d->last,
d->end,
(pool->max - (d->end - d->last)),
(d->end - d->last),
d->failed,
d->next);
}
for (l = pool->large; l; l = l->next)
{
printf(" ->large : \"%p\"\n"
" ->alloc : \"%p\"\n"
" ->next : \"%p\"\n",
l,
l->alloc,
l->next);
}
printf("[----------------------------------]\n");
}
// @ms_mem_pool_test() ok
/***********************************************************
* @Func : ms_mem_pool_pcalloc_small()
* @Author : lwp
* @Brief : 从内存池 pool 小块内存中以内存对齐方式分配并初始化 size 字节的内存。
* @Param : [in] pool
* @Param : [in] size
* @Return : NULL : 失败
* !NULL : 成功
* @Note :
***********************************************************/
static void *ms_mem_pool_pcalloc_small(ms_mem_pool_t *pool, size_t size)
{
int rev;
size_t nalloc;
void *p = NULL;
ms_mem_pool_small_t *d = NULL;
ms_mem_pool_small_t *n = NULL;
// 从已经存在的小块内存链中查找空闲内存
d = (ms_mem_pool_small_t *)pool->current;
do
{
// 内存对齐
p = align_ptr(d->last, MS_MEM_POOL_ALIGNMENT);
// 若当前内存块空闲内存充足
if ((size_t)(d->end - (char *)p) >= size)
{
d->last = (char *)p + size;
memset(p, 0, size);
ms_errlog(MS_ERRLOG_DEBUG, 0, MEM_TAG
"alloc mem form a existed small mem block");
return p;
}
// 若当前内存块内存不足,则移动到下一个内存块
else
{
ms_errlog(MS_ERRLOG_DEBUG, 0, MEM_TAG
"try alloc form next small mem block");
d = d->next;
}
} while (d);
// 当前小块内存链中内存不足,新开辟一个小块内存
ms_errlog(MS_ERRLOG_DEBUG, 0, MEM_TAG "try alloc form new small mem block");
nalloc = sizeof(ms_mem_pool_small_t) + pool->max;
rev = posix_memalign((void **)&n, MS_MEM_POOL_ALIGNMENT, nalloc);
if (rev)
{
ms_errlog(MS_ERRLOG_ERR, rev, MEM_TAG "posix_memalign() failed");
return NULL;
}
memset(n, 0, nalloc);
n->last = (char *)n + sizeof(ms_mem_pool_small_t);
n->end = (char *)n + nalloc;
n->next = NULL;
n->failed = 0;
// 分配第 8 个 ms_mem_pool_small_t 时(包括 pool->small),开始移动 current。
// 第 6 次分配失败,current 才会后移,每次 current 后移一个 ms_mem_pool_small_t 结点
for (d = (ms_mem_pool_small_t *)pool->current; d->next; d = d->next)
{
if (d->failed++ > 4)
{
pool->current = d->next;
ms_errlog(MS_ERRLOG_DEBUG, 0, MEM_TAG "change current point");
}
}
// 将新内存块插入到小块内存链表的尾部
d->next = n;
// 内存对齐后空间不足
p = align_ptr(n->last, MS_MEM_POOL_ALIGNMENT);
if ((size_t)(n->end - (char *)p) < size)
{
ms_errlog(MS_ERRLOG_ERR, 0,
MEM_TAG "after memory alignment, memory not enough");
return NULL;
}
// 内存对齐后内存充足
else
{
ms_errlog(MS_ERRLOG_DEBUG, 0,
MEM_TAG "after memory alignment, memory enough");
n->last = (char *)p + size;
return p;
}
}
// @ms_mem_pool_pcalloc_small() ok
/***********************************************************
* @Func : ms_mem_pool_pcalloc_large()
* @Author : lwp
* @Brief : 从内存池 pool 大块内存中以内存对齐方式分配并初始化 size 字节的内存
* @Param : [in] pool
* @Param : [in] size
* @Return : NULL : 失败
* !NULL : 成功
* @Note :
***********************************************************/
static void *ms_mem_pool_pcalloc_large(ms_mem_pool_t *pool, size_t size)
{
int rev;
void *t;
void *p;
ms_mem_pool_large_t *l;
rev = posix_memalign(&t, MS_MEM_POOL_ALIGNMENT, size);
if (rev)
{
ms_errlog(MS_ERRLOG_ERR, rev, MEM_TAG "posix_memalign() failed");
return NULL;
}
memset(t, 0, size);
ms_errlog(MS_ERRLOG_DEBUG, 0, MEM_TAG
"try alloc large struct form small mem block");
// 从小块内存中为大块内存链分配管理结构体
p = ms_mem_pool_pcalloc_small(pool, sizeof(ms_mem_pool_large_t));
if (NULL == p)
{
free(t);
return NULL;
}
// 插入大块内存链首部
l = (ms_mem_pool_large_t *)p;
l->alloc = (char *)t;
l->next = pool->large;
pool->large = l;
return t;
}
// @ms_mem_pool_pcalloc_large() ok