88## Example
99
1010~~~ c
11- #include < stdio.h>
1211#include " memorypool.h"
12+ #include < stdio.h>
1313
14- struct TAT
15- {
14+ struct TAT {
1615 int T_T;
1716};
18-
19- int max_mem = 2 *GB + 1000 *MB + 1000 *KB;
20- int mem_pool_size = 1 *GB + 500 *MB + 500 *KB;
17+ mem_size_t max_mem = 2 *GB + 500 *MB + 500 *KB;
18+ mem_size_t mem_pool_size = 1 *GB + 500 *MB + 500 *KB;
2119
2220int main ()
2321{
24- MemoryPool *mp = MemoryPool_Init(max_mem, mem_pool_size, 1 );
22+ MemoryPool *mp = MemoryPool_Init(max_mem, mem_pool_size, 0 );
2523 struct TAT *tat = (struct TAT *)MemoryPool_Alloc(mp, sizeof(struct TAT));
2624 tat->T_T = 2333;
2725 printf ("%d\n", tat->T_T);
@@ -42,26 +40,24 @@ int main()
4240
4341` mem_size_t ` => ` unsigned long long `
4442
45- ` MemoryPool_Init ` 参数(` mem_size_t max_mem_pool_size ` , ` mem_size_t mempoolsize ` , ` int auto_extend ` )
43+ ` MemoryPool_Init ` 参数(` mem_size_t maxmempoolsize ` , ` mem_size_t mempoolsize ` , ` int thread_safe ` )
4644
47- > ` mempoolsize ` : 内存池字节数
45+ > ` maxmempoolsize ` : 内存池字节数上限
46+ >
47+ > ` mempoolsize ` : 内存池字节数 (maxmempoolsize与mempoolsize不相等时会自动扩展, 直到上限)
48+ >
49+ > ` thread_safe ` : 是否线程安全 (1->线程安全)
4850
49- > ` auto_extend ` : 是否自动扩展 (1->开启自动扩展 0->不开启 )
51+ ` MemoryPool_Alloc ` 行为与系统malloc一致(参数多了一个 )
5052
51- ` MemoryPool_Alloc ` 行为与系统malloc一致(唔 参数多了一个)
52-
53- ` MemoryPool_Free ` 行为与系统free一致(唔 多了个返回值)
53+ ` MemoryPool_Free ` 行为与系统free一致(返回值0为正常)
5454
5555~~~ c
56- MemoryPool* MemoryPool_Init (mem_size_t max_mem_pool_size, mem_size_t mempoolsize, int auto_extend);
57-
58- void* MemoryPool_Alloc(MemoryPool * mp, mem_size_t wantsize);
59-
60- int MemoryPool_Free(MemoryPool * mp, void * p);
61-
62- MemoryPool* MemoryPool_Clear(MemoryPool * mp);
63-
64- int MemoryPool_Destroy(MemoryPool * mp);
56+ MemoryPool* MemoryPool_Init (mem_size_t maxmempoolsize, mem_size_t mempoolsize, int thread_safe);
57+ void* MemoryPool_Alloc (MemoryPool * mp, mem_size_t wantsize);
58+ int MemoryPool_Free (MemoryPool * mp, void * p);
59+ MemoryPool* MemoryPool_Clear (MemoryPool * mp);
60+ int MemoryPool_Destroy(MemoryPool * mp);
6561~~~
6662
6763- 获取内存池信息
@@ -71,27 +67,26 @@ int MemoryPool_Destroy(MemoryPool *mp);
7167`get_mempool_prog_usage` 获取内存池中真实分配内存比例(除去了内存池管理结构占用的内存)
7268
7369~~~c
74- double get_mempool_usage(MemoryPool *mp);
75-
76- double get_mempool_prog_usage(MemoryPool *mp);
70+ // 实际分配空间
71+ mem_size_t get_used_memory (MemoryPool *mp);
72+ float get_mempool_usage(MemoryPool *mp);
73+ // 数据占用空间
74+ mem_size_t get_prog_memory (MemoryPool *mp);
75+ float get_mempool_prog_usage(MemoryPool *mp);
7776~~~
7877
7978## Update
8079
8180- 18-1-7 12.53 增加了自动扩展 (内存池耗尽时自动新扩展一个mempoolsize大小的内存)
8281- 18-5-27 1.10 改进输出信息 增强测试程序(详见main.cpp)
8382- 19-3-18 11.05 改进格式, 修复潜在bug
83+ - 19-4-1 20:46 增加线程安全选项, 修改了自动扩展逻辑.
8484
8585## Tips
8686
8787- 可通过注释` test.c ` 里的` #include "memorypool.h" ` 来切换对比系统` malloc ` ` free ` 和内存池
88-
89- - 暂不支持多线程
90-
91- - 多食用` MemoryPool_Clear `
92-
88+ - 线程安全(如无必要, 关闭线程安全以改进性能)
89+ - 多食用` MemoryPool_Clear ` (多线程情况下慎用)
9390- 在 ** 2GB** 数据量 ** 顺序分配释放** 的情况下比系统` malloc ` ` free ` 平均快 ** 30%-50%** (食用` MemoryPool_Clear ` 效果更明显)
94-
95- - ` mem_size_t ` 使用` unsigned long long ` 以支持4GB以上内存管理(头文件中` MAX_MEM_SIZE ` 宏定义了最大可管理内存)
96-
97- - 大量小块内存分配会有 ** 20%-30%** 内存空间损失(用于存储管理结构体 emmmmm)
91+ - ` mem_size_t ` 使用` unsigned long long ` 以支持4GB以上内存管理
92+ - 大量小块内存分配会有 ** 20%-30%** 内存空间损失(用于存储管理结构体)
0 commit comments