Skip to content

Commit 7fd1214

Browse files
committed
improving variable name
1 parent dda2fc8 commit 7fd1214

4 files changed

Lines changed: 77 additions & 56 deletions

File tree

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
要一口气预分配大内存来管理
66

7+
## Log
8+
9+
- 18-1-7 12.53 增加了自动扩展 (内存池耗尽时自动新扩展一个mempoolsize大小的内存)
10+
- 18-5-27 1.10 改进输出信息 增强测试程序(详见main.cpp)
11+
- 19-3-18 11.05 改进格式, 修复潜在bug
12+
- 19-4-1 20:46 增加线程安全选项, 修改了自动扩展逻辑.
13+
14+
## Next
15+
16+
- 伙伴内存管理
717

818
## Example
919

@@ -58,6 +68,7 @@ void* MemoryPool_Alloc (MemoryPool *mp, mem_size_t wantsize);
5868
int MemoryPool_Free (MemoryPool *mp, void *p);
5969
MemoryPool* MemoryPool_Clear (MemoryPool *mp);
6070
int MemoryPool_Destroy(MemoryPool *mp);
71+
int MemoryPool_SetThreadSafe(MemoryPool *mp, int thread_safe);
6172
~~~
6273
6374
- 获取内存池信息
@@ -75,13 +86,6 @@ mem_size_t get_prog_memory (MemoryPool *mp);
7586
float get_mempool_prog_usage(MemoryPool *mp);
7687
~~~
7788

78-
## Update
79-
80-
- 18-1-7 12.53 增加了自动扩展 (内存池耗尽时自动新扩展一个mempoolsize大小的内存)
81-
- 18-5-27 1.10 改进输出信息 增强测试程序(详见main.cpp)
82-
- 19-3-18 11.05 改进格式, 修复潜在bug
83-
- 19-4-1 20:46 增加线程安全选项, 修改了自动扩展逻辑.
84-
8589
## Tips
8690

8791
- 可通过注释`test.c`里的`#include "memorypool.h"`来切换对比系统`malloc` `free`和内存池

memorypool.c

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ void
44
get_memory_list_count(MemoryPool *mp, mem_size_t *mlist_len) {
55
MP_LOCK(mp->thread_safe, mp);
66
mem_size_t mlist_l = 0;
7-
_Memory *mm = mp->mlist;
7+
_MP_Memory *mm = mp->mlist;
88
while (mm) {
99
mlist_l++;
1010
mm = mm->next;
@@ -14,10 +14,10 @@ get_memory_list_count(MemoryPool *mp, mem_size_t *mlist_len) {
1414
}
1515

1616
void
17-
get_memory_info(MemoryPool *mp, _Memory *mm, mem_size_t *free_list_len, mem_size_t *alloc_list_len) {
17+
get_memory_info(MemoryPool *mp, _MP_Memory *mm, mem_size_t *free_list_len, mem_size_t *alloc_list_len) {
1818
MP_LOCK(mp->thread_safe, mp);
1919
mem_size_t free_l = 0, alloc_l = 0;
20-
_Chunk *p = mm->free_list;
20+
_MP_Chunk *p = mm->free_list;
2121
while (p) {
2222
free_l++;
2323
p = p->next;
@@ -34,19 +34,19 @@ get_memory_info(MemoryPool *mp, _Memory *mm, mem_size_t *free_list_len, mem_size
3434
}
3535

3636
int
37-
get_memory_id(_Memory *mm) {
37+
get_memory_id(_MP_Memory *mm) {
3838
return mm->id;
3939
}
4040

4141

42-
static _Memory *
42+
static _MP_Memory *
4343
extend_memory_list(MemoryPool *mp, mem_size_t new_mem_sz) {
44-
char *s = (char *)malloc(sizeof(_Memory)
44+
char *s = (char *)malloc(sizeof(_MP_Memory)
4545
+ new_mem_sz * sizeof(char));
4646
if (!s) return NULL;
4747

48-
_Memory *mm = (_Memory *)s;
49-
mm->start = s + sizeof(_Memory);
48+
_MP_Memory *mm = (_MP_Memory *)s;
49+
mm->start = s + sizeof(_MP_Memory);
5050

5151
MP_INIT_MEMORY_STRUCT(mm, new_mem_sz);
5252
mm->id = mp->last_id++;
@@ -56,9 +56,9 @@ extend_memory_list(MemoryPool *mp, mem_size_t new_mem_sz) {
5656
return mm;
5757
}
5858

59-
static _Memory *
59+
static _MP_Memory *
6060
find_memory_list(MemoryPool *mp, void *p) {
61-
_Memory *tmp = mp->mlist;
61+
_MP_Memory *tmp = mp->mlist;
6262
while (tmp) {
6363
if (tmp->start <= (char *)p
6464
&& tmp->start + mp->mem_pool_size > (char *)p)
@@ -70,26 +70,26 @@ find_memory_list(MemoryPool *mp, void *p) {
7070
}
7171

7272
static int
73-
merge_free_chunk(MemoryPool *mp, _Memory *mm, _Chunk *c) {
73+
merge_free_chunk(MemoryPool *mp, _MP_Memory *mm, _MP_Chunk *c) {
7474
if (mp == NULL || mm == NULL || c == NULL || !c->is_free)
7575
return 1;
7676

77-
_Chunk *p0 = c, *p1 = c;
77+
_MP_Chunk *p0 = c, *p1 = c;
7878
while (p0->is_free) {
7979
p1 = p0;
8080
if ((char *)p0 - MP_CHUNKEND - MP_CHUNKHEADER <= mm->start)
8181
break;
82-
p0 = *(_Chunk **)((char *)p0 - MP_CHUNKEND);
82+
p0 = *(_MP_Chunk **)((char *)p0 - MP_CHUNKEND);
8383
}
8484

85-
p0 = (_Chunk *)((char *)p1 + p1->alloc_mem);
85+
p0 = (_MP_Chunk *)((char *)p1 + p1->alloc_mem);
8686
while ((char *)p0 < mm->start + mp->mem_pool_size && p0->is_free) {
8787
MP_DLINKLIST_DEL(mm->free_list, p0);
8888
p1->alloc_mem += p0->alloc_mem;
89-
p0 = (_Chunk *)((char *)p0 + p0->alloc_mem);
89+
p0 = (_MP_Chunk *)((char *)p0 + p0->alloc_mem);
9090
}
9191

92-
*(_Chunk **)((char *)p1 + p1->alloc_mem - MP_CHUNKEND) = p1;
92+
*(_MP_Chunk **)((char *)p1 + p1->alloc_mem - MP_CHUNKEND) = p1;
9393
MP_UNLOCK(mp->thread_safe, mp);
9494
return 0;
9595
}
@@ -112,12 +112,12 @@ MemoryPool_Init(mem_size_t maxmempoolsize, mem_size_t mempoolsize, int thread_sa
112112
mp->total_mem_pool_size = mp->mem_pool_size = mempoolsize;
113113
pthread_mutex_init(&mp->lock, NULL);
114114

115-
char *s = (char *)malloc(sizeof(_Memory)
115+
char *s = (char *)malloc(sizeof(_MP_Memory)
116116
+ sizeof(char) * mp->mem_pool_size);
117117
if (!s) return NULL;
118118

119-
mp->mlist = (_Memory *)s;
120-
mp->mlist->start = s + sizeof(_Memory);
119+
mp->mlist = (_MP_Memory *)s;
120+
mp->mlist->start = s + sizeof(_MP_Memory);
121121
MP_INIT_MEMORY_STRUCT(mp->mlist, mp->mem_pool_size);
122122
mp->mlist->next = NULL;
123123
mp->mlist->id = mp->last_id++;
@@ -132,8 +132,8 @@ MemoryPool_Alloc(MemoryPool *mp, mem_size_t wantsize)
132132
mem_size_t total_needed_size = MP_ALIGN_SIZE(wantsize + MP_CHUNKHEADER + MP_CHUNKEND);
133133
if (total_needed_size > mp->mem_pool_size) return NULL;
134134

135-
_Memory *mm = NULL, *mm1 = NULL;
136-
_Chunk *_free = NULL, *_not_free = NULL;
135+
_MP_Memory *mm = NULL, *mm1 = NULL;
136+
_MP_Chunk *_free = NULL, *_not_free = NULL;
137137

138138
MP_LOCK(mp->thread_safe, mp);
139139
FIND_FREE_CHUNK:
@@ -154,10 +154,10 @@ MemoryPool_Alloc(MemoryPool *mp, mem_size_t wantsize)
154154
// 从free块头开始分割出alloc块
155155
_not_free = _free;
156156

157-
_free = (_Chunk *)((char *)_not_free + total_needed_size);
157+
_free = (_MP_Chunk *)((char *)_not_free + total_needed_size);
158158
*_free = *_not_free;
159159
_free->alloc_mem -= total_needed_size;
160-
*(_Chunk **)((char *)_free + _free->alloc_mem - MP_CHUNKEND) = _free;
160+
*(_MP_Chunk **)((char *)_free + _free->alloc_mem - MP_CHUNKEND) = _free;
161161

162162
// update free_list
163163
if (!_free->prev) {
@@ -171,7 +171,7 @@ MemoryPool_Alloc(MemoryPool *mp, mem_size_t wantsize)
171171
_not_free->is_free = 0;
172172
_not_free->alloc_mem = total_needed_size;
173173

174-
*(_Chunk **)((char *)_not_free + total_needed_size - MP_CHUNKEND) = _not_free;
174+
*(_MP_Chunk **)((char *)_not_free + total_needed_size - MP_CHUNKEND) = _not_free;
175175
}
176176
// 不够 则整块分配为alloc
177177
else {
@@ -217,11 +217,11 @@ MemoryPool_Free(MemoryPool *mp, void *p)
217217
if (p == NULL || mp == NULL)
218218
return 1;
219219
MP_LOCK(mp->thread_safe, mp);
220-
_Memory *mm = mp->mlist;
220+
_MP_Memory *mm = mp->mlist;
221221
if (mp->auto_extend)
222222
mm = find_memory_list(mp, p);
223223

224-
_Chunk *ck = (_Chunk *)((char *)p - MP_CHUNKHEADER);
224+
_MP_Chunk *ck = (_MP_Chunk *)((char *)p - MP_CHUNKHEADER);
225225

226226
MP_DLINKLIST_DEL(mm->alloc_list, ck);
227227
MP_DLINKLIST_INS_FRT(mm->free_list, ck);
@@ -237,7 +237,7 @@ MemoryPool *
237237
MemoryPool_Clear(MemoryPool *mp) {
238238
if (!mp) return NULL;
239239
MP_LOCK(mp->thread_safe, mp);
240-
_Memory *mm = mp->mlist;
240+
_MP_Memory *mm = mp->mlist;
241241
while (mm) {
242242
MP_INIT_MEMORY_STRUCT(mm, mm->mem_pool_size);
243243
mm = mm->next;
@@ -250,7 +250,7 @@ int
250250
MemoryPool_Destroy(MemoryPool *mp) {
251251
if (mp == NULL) return 1;
252252
MP_LOCK(mp->thread_safe, mp);
253-
_Memory *mm = mp->mlist, *mm1 = NULL;
253+
_MP_Memory *mm = mp->mlist, *mm1 = NULL;
254254
while (mm) {
255255
mm1 = mm;
256256
mm = mm->next;
@@ -264,11 +264,23 @@ MemoryPool_Destroy(MemoryPool *mp) {
264264
return 0;
265265
}
266266

267+
int
268+
MemoryPool_SetThreadSafe(MemoryPool *mp, int thread_safe) {
269+
MP_LOCK(mp->thread_safe, mp);
270+
if (mp->thread_safe) {
271+
mp->thread_safe = thread_safe;
272+
MP_UNLOCK(1, mp);
273+
} else {
274+
mp->thread_safe = thread_safe;
275+
}
276+
return 0;
277+
}
278+
267279
mem_size_t
268280
get_used_memory(MemoryPool *mp) {
269281
MP_LOCK(mp->thread_safe, mp);
270282
mem_size_t total_alloc = 0;
271-
_Memory *mm = mp->mlist;
283+
_MP_Memory *mm = mp->mlist;
272284
while (mm) {
273285
total_alloc += mm->alloc_mem;
274286
mm = mm->next;
@@ -281,7 +293,7 @@ mem_size_t
281293
get_prog_memory(MemoryPool *mp) {
282294
MP_LOCK(mp->thread_safe, mp);
283295
mem_size_t total_alloc_prog = 0;
284-
_Memory *mm = mp->mlist;
296+
_MP_Memory *mm = mp->mlist;
285297
while (mm) {
286298
total_alloc_prog += mm->alloc_prog_mem;
287299
mm = mm->next;

memorypool.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <pthread.h>
55

66
#define mem_size_t unsigned long long
7-
#define MP_CHUNKHEADER sizeof(struct _chunk)
8-
#define MP_CHUNKEND sizeof(struct _chunk *)
7+
#define MP_CHUNKHEADER sizeof(struct _mp_chunk)
8+
#define MP_CHUNKEND sizeof(struct _mp_chunk *)
99

1010
#define KB (mem_size_t)(1 << 10)
1111
#define MB (mem_size_t)(1 << 20)
@@ -24,7 +24,7 @@
2424
mm->mem_pool_size = mem_sz; \
2525
mm->alloc_mem = 0; \
2626
mm->alloc_prog_mem = 0; \
27-
mm->free_list = (_Chunk *)mm->start; \
27+
mm->free_list = (_MP_Chunk *)mm->start; \
2828
mm->free_list->is_free = 1; \
2929
mm->free_list->alloc_mem = mem_sz; \
3030
mm->free_list->prev = NULL; \
@@ -50,44 +50,44 @@
5050
} \
5151
} while(0)
5252

53-
typedef struct _chunk
53+
typedef struct _mp_chunk
5454
{
5555
mem_size_t alloc_mem;
56-
struct _chunk *prev, *next;
56+
struct _mp_chunk *prev, *next;
5757
int is_free;
58-
} _Chunk;
58+
} _MP_Chunk;
5959

60-
typedef struct _mem_pool_list
60+
typedef struct _mp_mem_pool_list
6161
{
6262
char *start;
6363
unsigned int id;
6464
mem_size_t mem_pool_size;
6565
mem_size_t alloc_mem;
6666
mem_size_t alloc_prog_mem;
67-
_Chunk *free_list, *alloc_list;
68-
struct _mem_pool_list *next;
67+
_MP_Chunk *free_list, *alloc_list;
68+
struct _mp_mem_pool_list *next;
6969
// pthread_mutex_t lock;
70-
} _Memory;
70+
} _MP_Memory;
7171

72-
typedef struct _mem_pool
72+
typedef struct _mp_mem_pool
7373
{
7474
unsigned int last_id;
7575
int auto_extend;
7676
int thread_safe;
7777
mem_size_t mem_pool_size, total_mem_pool_size, max_mem_pool_size;
78-
struct _mem_pool_list *mlist;
78+
struct _mp_mem_pool_list *mlist;
7979
pthread_mutex_t lock;
8080
} MemoryPool;
8181

8282
/*
83-
* 内部工具函数
83+
* 内部工具函数(调试用)
8484
*/
8585

8686
// 所有Memory的数量
8787
void get_memory_list_count(MemoryPool *mp, mem_size_t *mlist_len);
8888
// 每个Memory的统计信息
89-
void get_memory_info(MemoryPool *mp, _Memory *mm, mem_size_t *free_list_len, mem_size_t *alloc_list_len);
90-
int get_memory_id(_Memory *mm);
89+
void get_memory_info(MemoryPool *mp, _MP_Memory *mm, mem_size_t *free_list_len, mem_size_t *alloc_list_len);
90+
int get_memory_id(_MP_Memory *mm);
9191

9292
/*
9393
* 内存池API
@@ -98,6 +98,7 @@ void* MemoryPool_Alloc (MemoryPool *mp, mem_size_t wantsize);
9898
int MemoryPool_Free (MemoryPool *mp, void *p);
9999
MemoryPool* MemoryPool_Clear (MemoryPool *mp);
100100
int MemoryPool_Destroy(MemoryPool *mp);
101+
int MemoryPool_SetThreadSafe(MemoryPool *mp, int thread_safe);
101102

102103
/*
103104
* 内存池信息API

test.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
x, get_mempool_usage(mp), get_mempool_prog_usage(mp)); \
3838
get_memory_list_count(mp, &mlist_cnt); \
3939
printf("->> [memorypool_list_count] mlist(%llu)\n", mlist_cnt); \
40-
_Memory *mlist = mp->mlist; \
40+
_MP_Memory *mlist = mp->mlist; \
4141
while (mlist) { \
4242
get_memory_info(mp, mlist, &free_cnt, &alloc_cnt); \
4343
printf("->>> id: %d [list_count] free_list(%llu) alloc_list(%llu)\n", get_memory_id(mlist), free_cnt, alloc_cnt); \
@@ -60,13 +60,15 @@ struct Node
6060
bool operator <(const Node& n) const { return size < n.size; }
6161
};
6262

63-
unsigned int random_uint(unsigned int maxn)
63+
unsigned int
64+
random_uint(unsigned int maxn)
6465
{
6566
unsigned int ret = abs(rand()) % maxn;
6667
return ret > 0 ? ret : 32;
6768
}
6869

69-
void *test_fn(void *arg) {
70+
void *
71+
test_fn(void *arg) {
7072
Node mem[DATA_N];
7173
#ifdef _Z_MEMORYPOOL_H_
7274
MemoryPool *mp = (MemoryPool *)arg;
@@ -177,9 +179,10 @@ int main()
177179
pthread_join(pid2, NULL);
178180
pthread_join(pid3, NULL);
179181

180-
// 第二次执行
182+
// 第二次执行
181183
printf("\n>\n>\n>\n\n");
182184
total_size = 0;
185+
MemoryPool_SetThreadSafe(mp, 0);
183186

184187
#ifdef _Z_MEMORYPOOL_H_
185188
pthread_create(&pid1, &attr, test_fn, mp);
@@ -194,6 +197,7 @@ int main()
194197
// pthread_join(pid2, NULL);
195198
// pthread_join(pid3, NULL);
196199

200+
197201
#ifdef _Z_MEMORYPOOL_H_
198202
MemoryPool_Destroy(mp);
199203
#endif

0 commit comments

Comments
 (0)