https://blog.csdn.net/u010183728/article/details/81531392

-

  1. 内存池设计

1.1 目的

在给定的内存buffer上建立内存管理机制,根据用户需求从该buffer上分配内存或者将已经分配的内存释放回buffer中。

1.2 要求

尽量减少内存碎片,平均效率高于C语言的malloc和free。

1.3 设计思路

将buffer分为四部分,第1部分是mem\_pool结构体;第2部分是内存映射表;第3部分是内存chunk结构体缓冲区;第4部分是实际可分配的内存区。整个buffer结构图如图1所示:


图1 内存buffer结构图

图1 内存buffer结构图

第1部分的作用是可以通过该mem\_pool结构体控制整个内存池。

第2部分的作用是记录第4部分,即实际可分配的内存区的使用情况。表中的每一个单元表示一个固定大小的内存块(block),多个连续的block组成一个chunk,每个block的详细结构如图2所示:

图2 memory block结构图

图2 memory block结构图

其中count表示该block后面的与该block同属于一个chunk的blokc的个数,start表示该block所在的chunk的起始block索引。其实start这个域只有在每个chunk的最后一个block中才会用到(用于从当前chunk寻找前一个chunk的起始位置),而pmem\_chunk则是一个指针,指向一个mem\_chunk结构体。任意一块大小的内存都会被取向上整到block大小的整数倍。

第3部分是一个mem\_chunk pool,其作用是存储整个程序可用的mem\_chunk结构体。mem\_chunk pool中的mem\_chunk被组织成双向链表结构(快速插入和删除)。每个mem\_chunk结构图如图3所示:

图3 memory chunk结构图

图3 memory chunk结构图

其中pmem\_block指向该chunk在内存映射表中的位置,others表示其他一些域,不同的实现对应该域的内容略有不同。

第4部分就是实际可以被分配给用户的内存。

整个内存池管理程序除了这四部分外,还有一个重要的内容就是memory chunk set。虽然其中的每个元素都来自mem\_chunk pool,但是它与mem\_chunk pool的不同之处在于其中的每个memory chunk中记录了当前可用的一块内存的相关信息。而mem\_chunk pool中的memory chunk的内容是无定以的。可以这样理解mem\_chunk pool与memory chunk set:mem\_chunk pool是为memory chunk set分配内存的“内存池”,只是该“内存池”每次分配的内存大小是固定的,为mem\_chunk结构体的大小。内存池程序主要是通过搜索这个memory chunk set来获取可被分配的内存。在memory chunk set上建立不同的数据结构就构成了不同的内存池实现方法,同时也导致了不同的搜索效率,直接影响内存池的性能,本文稍后会介绍两种内存池的实现。

1.4 内存池管理程序运行过程

  • 初始化:内存映射表中只有一块可用的内存信息,大小为内存池中所有可用的内存。从memory chunk pool中分配一个mem\_chunk,使其指向内存映射表中的第一个block,并根据具体的内存池实现方式填充mem\_chunk中的其他域,然后将该mem\_chunk添加到memory chunk set中。
  • 申请内存:当用户申请一块内存时,首先在memory chunk set中查找合适的内存块。如果找到符合要求的内存块,就在内存映射表中找到相应的chunk,并修改chunk中相应block结构体的内容,然后根据修改后的chunk修改memory chunk set中chunk的内容,最后返回分配内存的起始地址;否则返回NULL。
  • 释放内存:当用户释放一块内存时,首先根据这块内存的起始地址找到其在内存映射表中对应的chunk,然后尝试将该chunk和与其相邻的chunk合并,修改chunk中相应block的内容并修改memory chunk set中相应chunk的内容或者向memory chunk set加入新的mem\_chunk(这种情况在不能合并内存是发生)。

1.5 减少内存碎片

本文设计的方法只能在一定程度上减少内存碎片,并不能彻底消除内存碎片。具体方法如下:

在用户释放内存时,尝试将该内存与其相邻的内存合并。如果其相邻内存为未分配内存则合并成功,合并后作为一整块内存使用;如火其相邻内存为已分配内存则不能合并,该释放的内存块作为一个独立的内存块被使用。

2 内存池实现-链表结构

2.1 性能分析

链表结构的内存池实现是指将memory chunk set实现为双链表结构。这种方法的优缺点如下:

优点:释放内存很快,O(1)复杂度。

缺点:分配内存较慢,O(n)复杂度。

2.2 内存池运行状态转移图

绿色表示未使用的内存,红色表示已经使用的内存。其中每个block表示64B,这个值可以根据具体需要设定。

  • 初始化

图4 内存池初始化状态

图4 内存池初始化状态

  • 申请内存

图5 第1次申请128B内存后

图5 第1次申请128B内存后

图6 第n次申请、释放内存后

图6 第n次申请、释放内存后

  • 释放内存

图7 释放64B内存前后

图7 释放64B内存前后

3 内存池实现-大顶堆结构

3.1 性能分析

大顶堆结构的内存池实现是指将memory chunk set实现为大顶堆结构。这种方法的优缺点如下:

优点:降低了分配内存的时间复杂度,O(log(n))。

缺点:增加了释放内存的时间复杂度,O(log(n))。

3.2 内存池运行状态转移图

绿色表示未使用的内存,红色表示已经使用的内存。其中每个block表示64B,这个值可以根据具体需要设定。

  • 初始化

图8 内存池初始化状态

图8 内存池初始化状态

  • 申请内存

图9 第1次申请128B内存后

图9 第1次申请128B内存后

图10 第n次申请、释放内存后

图10 第n次申请、释放内存后

  • 释放内存

图11 释放64B内存前后

图11 释放64B内存前后

4 性能测试

  • 测试对象:C语言的malloc、free和本文的两种内存池(大小为500M,实际可分配内存为310M)。
  • 测试指标:执行n=2000次随机分配、释放随机大小内存(范围为64B~1024B)的时间比。
  • 测试方法1:

(1) 生成n个随机数,大小在64~1024之间,用于表示n个要分配的内存大小;

(2) 生成n个随机数,取值 为0或者1,表示每次分配内存后紧接着是否释放内存;

(3) 测量C语言的malloc、free和本文两种内存池执行n次随机分配、释放随机大小内存的时间比ratio;

(4) 重复(3)m=200次,记录每次活动的ratio,并绘制相应的曲线。

  • 测试方法2:

(1) 生成n个随机数,大小在a~b之间(初始值a=64,b=1024),用于表示n个要分配的内存大小;

(2) 测量C语言的malloc、free和本文两种内存池执行n次分配、释放随机大小内存的时间比ratio;

(3) 重复(2)m=512次,每次分配的内存容量的范围比前一次大1024B,记录每次获得的ratio,并绘制相应曲线。

4.1 性能测试结果-链表结果内存池

图12 链表结构内存池性能测试结果1

图12 链表结构内存池性能测试结果1

链表结构内存池性能测试结果2

图13 链表结构内存池性能测试结果2

4.2 性能测试结果-大顶堆结构内存池

图14 大顶堆内存池性能测试结果1

图14 大顶堆内存池性能测试结果1

图15 大顶堆内存池性能测试结果2

图15 大顶堆内存池性能测试结果2

4.3 性能比较

图16 两种内存池性能测试结果比较1

图16 两种内存池性能测试结果比较1

图17 两种内存池性能测试结果比较2

图17 两种内存池性能测试结果比较2

5 结论

从上面的内存池性能测试结果中可以看出,相比C语言的malloc和free,内存池使得用户分配内存和释放内存的效率有了较大的提高,这一优势尤其分配较大快的内存时体现的尤为突出。

同时也可以看出大顶堆结够的内存池的性能并不比链表结构的内存池性能高,反而低于链表结构内存池的性能。这再一次表明O(log(n))优于O(n)是有条件的。当然,本文的测试具有一定的局限性,也许在其他的测试案例中大顶堆结构的内存池性能会超越链表结构的内存池。

附:源代码

链表结构内存池:

MemoryPool.h

 
  1. #ifndef _MEMORYPOOL_H
  2. #define _MEMORYPOOL_H
  3. #include <stdlib.h>
  4. #define MINUNITSIZE 64
  5. #define ADDR_ALIGN 8
  6. #define SIZE_ALIGN MINUNITSIZE
  7. struct memory_chunk;
  8. typedef struct memory_block
  9. {
  10. size_t count;
  11. size_t start;
  12. memory_chunk* pmem_chunk;
  13. }memory_block;
  14. // 可用的内存块结构体
  15. typedef struct memory_chunk
  16. {
  17. memory_block* pfree_mem_addr;
  18. memory_chunk* pre;
  19. memory_chunk* next;
  20. }memory_chunk;
  21. // 内存池结构体
  22. typedef struct MEMORYPOOL
  23. {
  24. void *memory;
  25. size_t size;
  26. memory_block* pmem_map;
  27. memory_chunk* pfree_mem_chunk;
  28. memory_chunk* pfree_mem_chunk_pool;
  29. size_t mem_used_size; // 记录内存池中已经分配给用户的内存的大小
  30. size_t mem_map_pool_count; // 记录链表单元缓冲池中剩余的单元的个数,个数为0时不能分配单元给pfree_mem_chunk
  31. size_t free_mem_chunk_count; // 记录 pfree_mem_chunk链表中的单元个数
  32. size_t mem_map_unit_count; //
  33. size_t mem_block_count; // 一个 mem_unit 大小为 MINUNITSIZE
  34. }MEMORYPOOL, *PMEMORYPOOL;
  35. PMEMORYPOOL CreateMemoryPool(void* pBuf, size_t sBufSize);
  36. void ReleaseMemoryPool(PMEMORYPOOL* ppMem) ;
  37. void* GetMemory(size_t sMemorySize, PMEMORYPOOL pMem) ;
  38. void FreeMemory(void *ptrMemoryBlock, PMEMORYPOOL pMem) ;
  39. #endif //_MEMORYPOOL_H

MemoryPool.cpp

 
  1. #include "stdafx.h"
  2. #include <memory.h>
  3. #include "MemoryPool.h"
  4. size_t check_align_addr(void*& pBuf)
  5. {
  6. size_t align = 0;
  7. size_t addr = (int)pBuf;
  8. align = (ADDR_ALIGN - addr % ADDR_ALIGN) % ADDR_ALIGN;
  9. pBuf = (char*)pBuf + align;
  10. return align;
  11. }
  12. size_t check_align_block(size_t size)
  13. {
  14. size_t align = size % MINUNITSIZE;
  15. return size - align;
  16. }
  17. size_t check_align_size(size_t size)
  18. {
  19. size = (size + SIZE_ALIGN - 1) / SIZE_ALIGN * SIZE_ALIGN;
  20. return size;
  21. }
  22. memory_chunk* create_list(memory_chunk* pool, size_t count)
  23. {
  24. if (!pool)
  25. {
  26. return NULL;
  27. }
  28. memory_chunk* head = NULL;
  29. for (size_t i = 0; i < count; i++)
  30. {
  31. pool->pre = NULL;
  32. pool->next = head;
  33. if (head != NULL)
  34. {
  35. head->pre = pool;
  36. }
  37. head = pool;
  38. pool++;
  39. }
  40. return head;
  41. }
  42. memory_chunk* front_pop(memory_chunk*& pool)
  43. {
  44. if (!pool)
  45. {
  46. return NULL;
  47. }
  48. memory_chunk* tmp = pool;
  49. pool = tmp->next;
  50. pool->pre = NULL;
  51. return tmp;
  52. }
  53. void push_back(memory_chunk*& head, memory_chunk* element)
  54. {
  55. if (head == NULL)
  56. {
  57. head = element;
  58. head->pre = element;
  59. head->next = element;
  60. return;
  61. }
  62. head->pre->next = element;
  63. element->pre = head->pre;
  64. head->pre = element;
  65. element->next = head;
  66. }
  67. void push_front(memory_chunk*& head, memory_chunk* element)
  68. {
  69. element->pre = NULL;
  70. element->next = head;
  71. if (head != NULL)
  72. {
  73. head->pre = element;
  74. }
  75. head = element;
  76. }
  77. void delete_chunk(memory_chunk*& head, memory_chunk* element)
  78. {
  79. // 在双循环链表中删除元素
  80. if (element == NULL)
  81. {
  82. return;
  83. }
  84. // element为链表头
  85. else if (element == head)
  86. {
  87. // 链表只有一个元素
  88. if (head->pre == head)
  89. {
  90. head = NULL;
  91. }
  92. else
  93. {
  94. head = element->next;
  95. head->pre = element->pre;
  96. head->pre->next = head;
  97. }
  98. }
  99. // element为链表尾
  100. else if (element->next == head)
  101. {
  102. head->pre = element->pre;
  103. element->pre->next = head;
  104. }
  105. else
  106. {
  107. element->pre->next = element->next;
  108. element->next->pre = element->pre;
  109. }
  110. element->pre = NULL;
  111. element->next = NULL;
  112. }
  113. void* index2addr(PMEMORYPOOL mem_pool, size_t index)
  114. {
  115. char* p = (char*)(mem_pool->memory);
  116. void* ret = (void*)(p + index *MINUNITSIZE);
  117. return ret;
  118. }
  119. size_t addr2index(PMEMORYPOOL mem_pool, void* addr)
  120. {
  121. char* start = (char*)(mem_pool->memory);
  122. char* p = (char*)addr;
  123. size_t index = (p - start) / MINUNITSIZE;
  124. return index;
  125. }
  126. PMEMORYPOOL CreateMemoryPool(void* pBuf, size_t sBufSize)
  127. {
  128. memset(pBuf, 0, sBufSize);
  129. PMEMORYPOOL mem_pool = (PMEMORYPOOL)pBuf;
  130. // 计算需要多少memory map单元格
  131. size_t mem_pool_struct_size = sizeof(MEMORYPOOL);
  132. mem_pool->mem_map_pool_count = (sBufSize - mem_pool_struct_size + MINUNITSIZE - 1) / MINUNITSIZE;
  133. mem_pool->mem_map_unit_count = (sBufSize - mem_pool_struct_size + MINUNITSIZE - 1) / MINUNITSIZE;
  134. mem_pool->pmem_map = (memory_block*)((char*)pBuf + mem_pool_struct_size);
  135. mem_pool->pfree_mem_chunk_pool = (memory_chunk*)((char*)pBuf + mem_pool_struct_size + sizeof(memory_block) * mem_pool->mem_map_unit_count);
  136. mem_pool->memory = (char*)pBuf + mem_pool_struct_size+ sizeof(memory_block) * mem_pool->mem_map_unit_count + sizeof(memory_chunk) * mem_pool->mem_map_pool_count;
  137. mem_pool->size = sBufSize - mem_pool_struct_size - sizeof(memory_block) * mem_pool->mem_map_unit_count - sizeof(memory_chunk) * mem_pool->mem_map_pool_count;
  138. size_t align = check_align_addr(mem_pool->memory);
  139. mem_pool->size -= align;
  140. mem_pool->size = check_align_block(mem_pool->size);
  141. mem_pool->mem_block_count = mem_pool->size / MINUNITSIZE;
  142. // 链表化
  143. mem_pool->pfree_mem_chunk_pool = create_list(mem_pool->pfree_mem_chunk_pool, mem_pool->mem_map_pool_count);
  144. // 初始化 pfree_mem_chunk,双向循环链表
  145. memory_chunk* tmp = front_pop(mem_pool->pfree_mem_chunk_pool);
  146. tmp->pre = tmp;
  147. tmp->next = tmp;
  148. tmp->pfree_mem_addr = NULL;
  149. mem_pool->mem_map_pool_count--;
  150. // 初始化 pmem_map
  151. mem_pool->pmem_map[0].count = mem_pool->mem_block_count;
  152. mem_pool->pmem_map[0].pmem_chunk = tmp;
  153. mem_pool->pmem_map[mem_pool->mem_block_count-1].start = 0;
  154. tmp->pfree_mem_addr = mem_pool->pmem_map;
  155. push_back(mem_pool->pfree_mem_chunk, tmp);
  156. mem_pool->free_mem_chunk_count = 1;
  157. mem_pool->mem_used_size = 0;
  158. return mem_pool;
  159. }
  160. void ReleaseMemoryPool(PMEMORYPOOL* ppMem)
  161. {
  162. }
  163. void* GetMemory(size_t sMemorySize, PMEMORYPOOL pMem)
  164. {
  165. sMemorySize = check_align_size(sMemorySize);
  166. size_t index = 0;
  167. memory_chunk* tmp = pMem->pfree_mem_chunk;
  168. for (index = 0; index < pMem->free_mem_chunk_count; index++)
  169. {
  170. if (tmp->pfree_mem_addr->count * MINUNITSIZE >= sMemorySize)
  171. {
  172. break;
  173. }
  174. tmp = tmp->next;
  175. }
  176. if (index == pMem->free_mem_chunk_count)
  177. {
  178. return NULL;
  179. }
  180. pMem->mem_used_size += sMemorySize;
  181. if (tmp->pfree_mem_addr->count * MINUNITSIZE == sMemorySize)
  182. {
  183. // 当要分配的内存大小与当前chunk中的内存大小相同时,从pfree_mem_chunk链表中删除此chunk
  184. size_t current_index = (tmp->pfree_mem_addr - pMem->pmem_map);
  185. delete_chunk(pMem->pfree_mem_chunk, tmp);
  186. tmp->pfree_mem_addr->pmem_chunk = NULL;
  187. push_front(pMem->pfree_mem_chunk_pool, tmp);
  188. pMem->free_mem_chunk_count--;
  189. pMem->mem_map_pool_count++;
  190. return index2addr(pMem, current_index);
  191. }
  192. else
  193. {
  194. // 当要分配的内存小于当前chunk中的内存时,更改pfree_mem_chunk中相应chunk的pfree_mem_addr
  195. // 复制当前mem_map_unit
  196. memory_block copy;
  197. copy.count = tmp->pfree_mem_addr->count;
  198. copy.pmem_chunk = tmp;
  199. // 记录该block的起始和结束索引
  200. memory_block* current_block = tmp->pfree_mem_addr;
  201. current_block->count = sMemorySize / MINUNITSIZE;
  202. size_t current_index = (current_block - pMem->pmem_map);
  203. pMem->pmem_map[current_index+current_block->count-1].start = current_index;
  204. current_block->pmem_chunk = NULL; // NULL表示当前内存块已被分配
  205. // 当前block被一分为二,更新第二个block中的内容
  206. pMem->pmem_map[current_index+current_block->count].count = copy.count - current_block->count;
  207. pMem->pmem_map[current_index+current_block->count].pmem_chunk = copy.pmem_chunk;
  208. // 更新原来的pfree_mem_addr
  209. tmp->pfree_mem_addr = &(pMem->pmem_map[current_index+current_block->count]);
  210. size_t end_index = current_index + copy.count - 1;
  211. pMem->pmem_map[end_index].start = current_index + current_block->count;
  212. return index2addr(pMem, current_index);
  213. }
  214. }
  215. void FreeMemory(void *ptrMemoryBlock, PMEMORYPOOL pMem)
  216. {
  217. size_t current_index = addr2index(pMem, ptrMemoryBlock);
  218. size_t size = pMem->pmem_map[current_index].count * MINUNITSIZE;
  219. // 判断与当前释放的内存块相邻的内存块是否可以与当前释放的内存块合并
  220. memory_block* pre_block = NULL;
  221. memory_block* next_block = NULL;
  222. memory_block* current_block = &(pMem->pmem_map[current_index]);
  223. // 第一个
  224. if (current_index == 0)
  225. {
  226. if (current_block->count < pMem->mem_block_count)
  227. {
  228. next_block = &(pMem->pmem_map[current_index+current_block->count]);
  229. // 如果后一个内存块是空闲的,合并
  230. if (next_block->pmem_chunk != NULL)
  231. {
  232. next_block->pmem_chunk->pfree_mem_addr = current_block;
  233. pMem->pmem_map[current_index+current_block->count+next_block->count-1].start = current_index;
  234. current_block->count += next_block->count;
  235. current_block->pmem_chunk = next_block->pmem_chunk;
  236. next_block->pmem_chunk = NULL;
  237. }
  238. // 如果后一块内存不是空闲的,在pfree_mem_chunk中增加一个chunk
  239. else
  240. {
  241. memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
  242. new_chunk->pfree_mem_addr = current_block;
  243. current_block->pmem_chunk = new_chunk;
  244. push_back(pMem->pfree_mem_chunk, new_chunk);
  245. pMem->mem_map_pool_count--;
  246. pMem->free_mem_chunk_count++;
  247. }
  248. }
  249. else
  250. {
  251. memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
  252. new_chunk->pfree_mem_addr = current_block;
  253. current_block->pmem_chunk = new_chunk;
  254. push_back(pMem->pfree_mem_chunk, new_chunk);
  255. pMem->mem_map_pool_count--;
  256. pMem->free_mem_chunk_count++;
  257. }
  258. }
  259. // 最后一个
  260. else if (current_index == pMem->mem_block_count-1)
  261. {
  262. if (current_block->count < pMem->mem_block_count)
  263. {
  264. pre_block = &(pMem->pmem_map[current_index-1]);
  265. size_t index = pre_block->count;
  266. pre_block = &(pMem->pmem_map[index]);
  267. // 如果前一个内存块是空闲的,合并
  268. if (pre_block->pmem_chunk != NULL)
  269. {
  270. pMem->pmem_map[current_index+current_block->count-1].start = current_index - pre_block->count;
  271. pre_block->count += current_block->count;
  272. current_block->pmem_chunk = NULL;
  273. }
  274. // 如果前一块内存不是空闲的,在pfree_mem_chunk中增加一个chunk
  275. else
  276. {
  277. memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
  278. new_chunk->pfree_mem_addr = current_block;
  279. current_block->pmem_chunk = new_chunk;
  280. push_back(pMem->pfree_mem_chunk, new_chunk);
  281. pMem->mem_map_pool_count--;
  282. pMem->free_mem_chunk_count++;
  283. }
  284. }
  285. else
  286. {
  287. memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
  288. new_chunk->pfree_mem_addr = current_block;
  289. current_block->pmem_chunk = new_chunk;
  290. push_back(pMem->pfree_mem_chunk, new_chunk);
  291. pMem->mem_map_pool_count--;
  292. pMem->free_mem_chunk_count++;
  293. }
  294. }
  295. else
  296. {
  297. next_block = &(pMem->pmem_map[current_index+current_block->count]);
  298. pre_block = &(pMem->pmem_map[current_index-1]);
  299. size_t index = pre_block->start;
  300. pre_block = &(pMem->pmem_map[index]);
  301. bool is_back_merge = false;
  302. if (next_block->pmem_chunk == NULL && pre_block->pmem_chunk == NULL)
  303. {
  304. memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
  305. new_chunk->pfree_mem_addr = current_block;
  306. current_block->pmem_chunk = new_chunk;
  307. push_back(pMem->pfree_mem_chunk, new_chunk);
  308. pMem->mem_map_pool_count--;
  309. pMem->free_mem_chunk_count++;
  310. }
  311. // 后一个内存块
  312. if (next_block->pmem_chunk != NULL)
  313. {
  314. next_block->pmem_chunk->pfree_mem_addr = current_block;
  315. pMem->pmem_map[current_index+current_block->count+next_block->count-1].start = current_index;
  316. current_block->count += next_block->count;
  317. current_block->pmem_chunk = next_block->pmem_chunk;
  318. next_block->pmem_chunk = NULL;
  319. is_back_merge = true;
  320. }
  321. // 前一个内存块
  322. if (pre_block->pmem_chunk != NULL)
  323. {
  324. pMem->pmem_map[current_index+current_block->count-1].start = current_index - pre_block->count;
  325. pre_block->count += current_block->count;
  326. if (is_back_merge)
  327. {
  328. delete_chunk(pMem->pfree_mem_chunk, current_block->pmem_chunk);
  329. push_front(pMem->pfree_mem_chunk_pool, current_block->pmem_chunk);
  330. pMem->free_mem_chunk_count--;
  331. pMem->mem_map_pool_count++;
  332. }
  333. current_block->pmem_chunk = NULL;
  334. }
  335. }
  336. pMem->mem_used_size -= size;
  337. }

MemoryPoolTest.cpp

 
  1. // memory pool test.cpp : Defines the entry point for the console application.
  2. #include <tchar.h>
  3. #include "MemoryPool.h"
  4. #include <iostream>
  5. #include <windows.h>
  6. #include <vector>
  7. #include <time.h>
  8. #include <math.h>
  9. #include <fstream>
  10. using namespace std;
  11. int break_time = 0;
  12. // 检测内存池相关参数
  13. void check_mem_pool(int& max_chunk_size, int& free_chunk_count, int& min_chunk_size, int& total_free_mem, MEMORYPOOL* mem_pool)
  14. {
  15. memory_chunk* head = mem_pool->pfree_mem_chunk;
  16. memory_chunk* tmp = head;
  17. free_chunk_count = 0;
  18. total_free_mem = 0;
  19. max_chunk_size = 0;
  20. min_chunk_size = 500*1024*1024;
  21. if (head == NULL)
  22. {
  23. min_chunk_size = 0;
  24. return;
  25. }
  26. while (tmp->next != head)
  27. {
  28. free_chunk_count++;
  29. total_free_mem += tmp->pfree_mem_addr->count * MINUNITSIZE;
  30. if (tmp->pfree_mem_addr->count * MINUNITSIZE > max_chunk_size )
  31. {
  32. max_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
  33. }
  34. if (tmp->pfree_mem_addr->count * MINUNITSIZE < min_chunk_size)
  35. {
  36. min_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
  37. }
  38. tmp = tmp->next;
  39. }
  40. free_chunk_count++;
  41. total_free_mem += tmp->pfree_mem_addr->count * MINUNITSIZE;
  42. if (tmp->pfree_mem_addr->count * MINUNITSIZE > max_chunk_size )
  43. {
  44. max_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
  45. }
  46. if (tmp->pfree_mem_addr->count * MINUNITSIZE < min_chunk_size)
  47. {
  48. min_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
  49. }
  50. }
  51. // 申请后紧接着释放
  52. double test_mem_pool_perf_1(PMEMORYPOOL mem_pool, int iter, int* sizes)
  53. {
  54. cout << "*********************test_mem_pool_perf_1*********************" << endl;
  55. LARGE_INTEGER litmp;
  56. LONGLONG QPart1, QPart2;
  57. double t;
  58. double dfMinus, dfFreq;
  59. QueryPerformanceFrequency(&litmp);
  60. dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
  61. QueryPerformanceCounter(&litmp);
  62. QPart1 = litmp.QuadPart;// 获得初始值
  63. for (int i = 0; i < iter; i++)
  64. {
  65. void *p = GetMemory(sizes[i], mem_pool);
  66. if (p == NULL)
  67. {
  68. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << sizes[i] << " Byte" << endl;
  69. cout << "total memory is: " << mem_pool->size << " Byte" << endl;
  70. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
  71. cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
  72. int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
  73. check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
  74. cout << "check memory pool result:" << endl;
  75. cout << "free_chunk_count: " << free_chunk_count << endl
  76. << "total_free_mem: " << total_free_mem << endl
  77. << "max_chunk_size: " << max_chunk_size << endl
  78. << "min_chunk_size: " << min_chunk_size << endl;
  79. break;
  80. }
  81. FreeMemory(p, mem_pool);
  82. }
  83. QueryPerformanceCounter(&litmp);
  84. QPart2 = litmp.QuadPart;//获得中止值
  85. dfMinus = (double)(QPart2-QPart1);
  86. t = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
  87. cout << "test_mem_pool_perf_1: iter = " << iter << endl;
  88. cout << "time: " << t << endl;
  89. cout << "*********************test_mem_pool_perf_1*********************" << endl << endl << endl;
  90. return t;
  91. }
  92. double test_std_perf_1(int iter, int* sizes)
  93. {
  94. cout << "*********************test_std_perf_1*********************" << endl;
  95. LARGE_INTEGER litmp;
  96. LONGLONG QPart1, QPart2;
  97. double t;
  98. double dfMinus, dfFreq;
  99. QueryPerformanceFrequency(&litmp);
  100. dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
  101. QueryPerformanceCounter(&litmp);
  102. QPart1 = litmp.QuadPart;// 获得初始值
  103. for (int i = 0; i < iter; i++)
  104. {
  105. void *p = malloc(sizes[i]);
  106. if (p == NULL)
  107. {
  108. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << sizes[i] << " Byte" << endl;
  109. break;
  110. }
  111. free(p);
  112. }
  113. QueryPerformanceCounter(&litmp);
  114. QPart2 = litmp.QuadPart;//获得中止值
  115. dfMinus = (double)(QPart2-QPart1);
  116. t = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
  117. cout << "test_std_perf_1: iter = " << iter << endl;
  118. cout << "time: " << t << endl;
  119. cout << "*********************test_std_perf_1*********************" << endl << endl << endl;
  120. return t;
  121. }
  122. // 连续申请iter/2次,然后释放所有申请内存;再重复一次
  123. double test_mem_pool_perf_2(PMEMORYPOOL mem_pool, int iter, int size)
  124. {
  125. cout << "*********************test_mem_pool_perf_2*********************" << endl;
  126. LARGE_INTEGER litmp;
  127. LONGLONG QPart1, QPart2;
  128. double t;
  129. double dfMinus, dfFreq;
  130. QueryPerformanceFrequency(&litmp);
  131. dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
  132. QueryPerformanceCounter(&litmp);
  133. QPart1 = litmp.QuadPart;// 获得初始值
  134. void **p = new void*[iter];
  135. if (p == NULL)
  136. {
  137. cout << "new faild" << endl;
  138. return -1;
  139. }
  140. int count = 0;
  141. for (int i = 0; i < iter/2; i++)
  142. {
  143. p[i] = GetMemory(size, mem_pool);
  144. if (p[i] == NULL)
  145. {
  146. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
  147. cout << "total memory is: " << mem_pool->size << " Byte" << endl;
  148. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
  149. cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
  150. int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
  151. check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
  152. cout << "check memory pool result:" << endl;
  153. cout << "free_chunk_count: " << free_chunk_count << endl
  154. << "total_free_mem: " << total_free_mem << endl
  155. << "max_chunk_size: " << max_chunk_size << endl
  156. << "min_chunk_size: " << min_chunk_size << endl;
  157. break;
  158. }
  159. count++;
  160. }
  161. for (int i = 0; i < count; i++)
  162. {
  163. FreeMemory(p[i], mem_pool);
  164. }
  165. count = 0;
  166. for (int i = 0; i < iter/2; i++)
  167. {
  168. p[i] = GetMemory(size, mem_pool);
  169. if (p[i] == NULL)
  170. {
  171. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
  172. cout << "total memory is: " << mem_pool->size << " Byte" << endl;
  173. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
  174. cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
  175. int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
  176. check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
  177. cout << "check memory pool result:" << endl;
  178. cout << "free_chunk_count: " << free_chunk_count << endl
  179. << "total_free_mem: " << total_free_mem << endl
  180. << "max_chunk_size: " << max_chunk_size << endl
  181. << "min_chunk_size: " << min_chunk_size << endl;
  182. break;
  183. }
  184. count++;
  185. }
  186. for (int i = 0; i < count; i++)
  187. {
  188. if (p[i] == NULL)
  189. {
  190. cout << i << endl;
  191. break;
  192. }
  193. FreeMemory(p[i], mem_pool);
  194. }
  195. QueryPerformanceCounter(&litmp);
  196. QPart2 = litmp.QuadPart;//获得中止值
  197. dfMinus = (double)(QPart2-QPart1);
  198. t = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
  199. cout << "test_mem_pool_perf_2: iter = " << iter << endl;
  200. cout << "time: " << t << endl;
  201. delete []p;
  202. cout << "*********************test_mem_pool_perf_2*********************" << endl << endl << endl;
  203. return t;
  204. }
  205. // 连续申请inner_iter次,释放;重复iter/inner_iter次
  206. double test_mem_pool_perf_3(PMEMORYPOOL mem_pool, int iter, int size)
  207. {
  208. cout << "*********************test_mem_pool_perf_3*********************" << endl;
  209. int inner_iter = 10;
  210. void **p = new void*[inner_iter];
  211. if (p == NULL)
  212. {
  213. cout << "new faild" << endl;
  214. return -1;
  215. }
  216. LARGE_INTEGER litmp;
  217. LONGLONG QPart1, QPart2, start, finish;
  218. double t;
  219. double dfMinus, dfFreq;
  220. QueryPerformanceFrequency(&litmp);
  221. dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
  222. QueryPerformanceCounter(&litmp);
  223. QPart1 = litmp.QuadPart;// 获得初始值
  224. for (int k = 0; k < iter / inner_iter; k++)
  225. {
  226. int j = 0;
  227. for (j = 0; j < inner_iter; j++)
  228. {
  229. p[j] = GetMemory(size, mem_pool);
  230. if (p[j] == NULL)
  231. {
  232. cout << "break @ iterator = " << j << " / " << iter << ", need memory " << size << " Byte" << endl;
  233. cout << "total memory is: " << mem_pool->size << " Byte" << endl;
  234. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
  235. cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
  236. int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
  237. check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
  238. cout << "check memory pool result:" << endl;
  239. cout << "free_chunk_count: " << free_chunk_count << endl
  240. << "total_free_mem: " << total_free_mem << endl
  241. << "max_chunk_size: " << max_chunk_size << endl
  242. << "min_chunk_size: " << min_chunk_size << endl;
  243. break;
  244. }
  245. }
  246. for (int i = 0; i < j; i++)
  247. {
  248. FreeMemory(p[i], mem_pool);
  249. }
  250. }
  251. QueryPerformanceCounter(&litmp);
  252. QPart2 = litmp.QuadPart;//获得中止值
  253. dfMinus = (double)(QPart2-QPart1);
  254. t = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
  255. cout << "test_mem_pool_perf_3: iter = " << iter << endl;
  256. cout << "time: " << t << endl;
  257. cout << "*********************test_mem_pool_perf_3*********************" << endl << endl << endl;
  258. return t;
  259. }
  260. // 随机内存大小,随机释放操作
  261. double test_mem_pool_perf_rand(PMEMORYPOOL mem_pool, int iter, int* sizes, int* instruction)
  262. {
  263. cout << "-----------------------test_mem_pool_perf_rand----------------------- "<< endl;
  264. void** p = new void*[iter];
  265. if (p == NULL)
  266. {
  267. cout << "new failed" << endl;
  268. return -1;
  269. }
  270. LARGE_INTEGER litmp, gftime;
  271. LONGLONG QPart1, QPart2, start, finish;
  272. double t, GetMemory_time, FreeMemory_time;
  273. double dfMinus, dfFreq;
  274. QueryPerformanceFrequency(&litmp);
  275. dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
  276. QueryPerformanceCounter(&litmp);
  277. QPart1 = litmp.QuadPart;// 获得初始值
  278. int index = 0;
  279. int size;
  280. int free_tmp = 0;
  281. double seach_time;
  282. for (int i = 0; i < iter; i++)
  283. {
  284. size = sizes[i];
  285. p[index++] = GetMemory(size, mem_pool);
  286. if (p[index-1] == NULL)
  287. {
  288. break_time++;
  289. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
  290. cout << "total memory is: " << mem_pool->size << " Byte" << endl;
  291. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
  292. cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
  293. int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
  294. check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
  295. cout << "check memory pool result:" << endl;
  296. cout << "free_chunk_count: " << free_chunk_count << endl
  297. << "total_free_mem: " << total_free_mem << endl
  298. << "max_chunk_size: " << max_chunk_size << endl
  299. << "min_chunk_size: " << min_chunk_size << endl;
  300. break;
  301. }
  302. if (instruction[i] == 1)
  303. {
  304. FreeMemory(p[--index], mem_pool);
  305. }
  306. }
  307. QueryPerformanceCounter(&litmp);
  308. QPart2 = litmp.QuadPart;//获得中止值
  309. dfMinus = (double)(QPart2-QPart1);
  310. t = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
  311. cout << "test_mem_pool_perf_rand: iter = " << iter << endl;
  312. cout << "time: " << t << endl << endl;
  313. delete []p;
  314. return t;
  315. }
  316. double test_std_perf(int iter, int* sizes, int* instruction)
  317. {
  318. cout << "test_std_perf" << endl;
  319. void** p =new void*[iter];
  320. if (p == NULL)
  321. {
  322. cout << "new failed" << endl;
  323. return -1;
  324. }
  325. LARGE_INTEGER litmp;
  326. LONGLONG QPart1, QPart2;
  327. double t;
  328. double dfMinus, dfFreq;
  329. QueryPerformanceFrequency(&litmp);
  330. dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
  331. QueryPerformanceCounter(&litmp);
  332. QPart1 = litmp.QuadPart;// 获得初始值
  333. // cout << "test start" << endl;
  334. int index = 0;
  335. int size;
  336. for (int i = 0; i < iter; i++)
  337. {
  338. size = sizes[i];
  339. p[index++] = malloc(size);
  340. if (p[index-1] == NULL)
  341. {
  342. cout << i << endl;
  343. break;
  344. }
  345. if (instruction[i] == 1)
  346. {
  347. free(p[--index]);
  348. }
  349. }
  350. QueryPerformanceCounter(&litmp);
  351. QPart2 = litmp.QuadPart;//获得中止值
  352. dfMinus = (double)(QPart2-QPart1);
  353. t = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
  354. cout << "test_std_perf: iter = " << iter << endl;
  355. cout << "time: " << t << endl << endl;
  356. for (int k = 0; k < index; k++)
  357. {
  358. free(p[k]);
  359. }
  360. return t;
  361. }
  362. double test_std_perf_fix_size(int iter, int size)
  363. {
  364. cout << "******************* test_std_perf_fix_size *******************" << endl;
  365. LARGE_INTEGER litmp;
  366. LONGLONG QPart1, QPart2;
  367. double t;
  368. double dfMinus, dfFreq;
  369. QueryPerformanceFrequency(&litmp);
  370. dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
  371. QueryPerformanceCounter(&litmp);
  372. QPart1 = litmp.QuadPart;// 获得初始值
  373. int index = 0;
  374. for (int i = 0; i < iter; i++)
  375. {
  376. void *p = malloc(size);
  377. if (p == NULL)
  378. {
  379. cout << i << endl;
  380. break;
  381. }
  382. free(p);
  383. }
  384. QueryPerformanceCounter(&litmp);
  385. QPart2 = litmp.QuadPart;//获得中止值
  386. dfMinus = (double)(QPart2-QPart1);
  387. t = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
  388. cout << "test_std_perf: iter = " << iter << endl;
  389. cout << "time: " << t << endl;
  390. cout << "******************* test_std_perf_fix_size *******************" << endl << endl << endl;
  391. return t;
  392. }
  393. void test_correct_1(PMEMORYPOOL mem_pool, int iter, int size)
  394. {
  395. vector<void*>vec;
  396. vector<void*>::iterator vec_iter;
  397. int i = 0;
  398. cout << "**************************** Get Memory Test Start ****************************"<< endl << endl;
  399. for (i = 0; i < iter; i++)
  400. {
  401. void *p = GetMemory(size, mem_pool);
  402. if (p == NULL)
  403. {
  404. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
  405. cout << "memory left is: " << mem_pool->size << " Byte" << endl;
  406. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl << endl;
  407. break;
  408. }
  409. vec.push_back(p);
  410. }
  411. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
  412. cout << "memory left is: " << mem_pool->size << " Byte" << endl;
  413. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl << endl;
  414. cout << "verify memory size" << endl;
  415. memory_chunk* tmp = mem_pool->pfree_mem_chunk;
  416. int free_size = 0;
  417. for (int k = 0; k < mem_pool->free_mem_chunk_count; k++)
  418. {
  419. free_size += tmp->pfree_mem_addr->count * MINUNITSIZE;
  420. tmp = tmp->next;
  421. }
  422. cout << "memory free size is " << free_size << " Byte" << endl;
  423. cout << "memory used size is " << mem_pool->mem_used_size << " Byte" << endl;
  424. cout << "*************************** Get Memory Test Finish ***************************"<< endl << endl;
  425. cout << "*************************** Free Memory Test Start ***************************"<< endl << endl;
  426. int index = 0;
  427. for (vec_iter = vec.begin(); vec_iter != vec.end(); vec_iter++)
  428. {
  429. index++;
  430. FreeMemory(*vec_iter, mem_pool);
  431. }
  432. cout << "memory left is: " << mem_pool->size << " Byte" << endl;
  433. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl << endl;
  434. cout << "*************************** Free Memory Test Finish ***************************"<< endl << endl;
  435. cout << "********************* Get Memory Test (after Free) Start *********************"<< endl << endl;
  436. for (i = 0; i < iter; i++)
  437. {
  438. void *p = GetMemory(size, mem_pool);
  439. if (p == NULL)
  440. {
  441. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
  442. cout << "memory left is: " << mem_pool->size << " Byte" << endl;
  443. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl << endl;
  444. int max_size = 0;
  445. memory_chunk* tmp = mem_pool->pfree_mem_chunk;
  446. for (int k = 0; k < mem_pool->free_mem_chunk_count; k++)
  447. {
  448. if (tmp->pfree_mem_addr->count * MINUNITSIZE > max_size)
  449. {
  450. max_size = tmp->pfree_mem_addr->count * MINUNITSIZE > max_size;
  451. }
  452. }
  453. cout << "max chunk size is: " << max_size << " Byte" << endl;
  454. break;
  455. }
  456. vec.push_back(p);
  457. }
  458. cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
  459. cout << "memory left is: " << mem_pool->size << " Byte" << endl;
  460. cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl << endl;
  461. cout << "verify memory size" << endl;
  462. tmp = mem_pool->pfree_mem_chunk;
  463. free_size = 0;
  464. for (int k = 0; k < mem_pool->free_mem_chunk_count; k++)
  465. {
  466. free_size += tmp->pfree_mem_addr->count * MINUNITSIZE;
  467. tmp = tmp->next;
  468. }
  469. cout << "memory free size is " << free_size << " Byte" << endl;
  470. cout << "memory used size is " << mem_pool->mem_used_size << " Byte" << endl;
  471. cout << "********************* Get Memory Test (after Free) Finish *********************"<< endl << endl;
  472. }
  473. void rand_test()
  474. {
  475. size_t sBufSize = 500* 1024*1024;
  476. void*pBuf = malloc(sBufSize);
  477. if (pBuf == NULL)
  478. {
  479. cout << "malloc failed" << endl;
  480. return;
  481. }
  482. PMEMORYPOOL mem_pool = CreateMemoryPool(pBuf, sBufSize);
  483. ofstream out("rand_test.txt");
  484. int iter = 2000;
  485. int* instruction = new int[iter];
  486. int* sizes = new int[iter];
  487. if (instruction == NULL || sizes == NULL)
  488. {
  489. cout << "new memory failed" << endl;
  490. return;
  491. }
  492. srand(time(NULL));
  493. cout << "generate rand number" << endl;
  494. // instruction 中元素为1时表示在GetMemory后执行FreeMemory,0表示不执行FreeMemory
  495. // sizes中是每次分配内存的大小,范围从64B~1024B
  496. for (int i = 0; i < iter; i++)
  497. {
  498. instruction[i] = rand() % 2;
  499. sizes[i] = (rand() % 16 + 1) * 64;
  500. }
  501. int test_count = 200;
  502. double t1, t2;
  503. double* ratio = new double[test_count];
  504. int count = 0;
  505. for (int k = 0; k < test_count; k++)
  506. {
  507. if (break_time != 0)
  508. {
  509. cout << "break @ " << k << " / " << test_count << endl;
  510. break;
  511. }
  512. count++;
  513. cout << "******************************************test " << k+1 << " *************************************************" << endl;
  514. t1 = test_std_perf(iter, sizes, instruction);
  515. t2 = test_mem_pool_perf_rand(mem_pool, iter, sizes, instruction);
  516. cout << "total memory: " << mem_pool->size << ", memory used: " << mem_pool->mem_used_size
  517. << ", memory left: " << mem_pool->size - mem_pool->mem_used_size << endl;
  518. ratio[k] = t1 / t2;
  519. }
  520. if(break_time == 0)
  521. break_time = test_count;
  522. break_time = count - 1;
  523. cout << "*************************** ratio (system time / mem_pool time) ***************************" << endl;
  524. for (int k = 0; k < break_time; k++)
  525. {
  526. out << ratio[k] << ",";
  527. if (k % 10 == 0 && k != 0)
  528. {
  529. cout << endl;
  530. }
  531. cout << ratio[k] << " ";
  532. }
  533. cout << endl;
  534. delete []ratio;
  535. delete []instruction;
  536. delete []sizes;
  537. free(pBuf);
  538. }
  539. // 申请紧接着释放
  540. void rand_test_2()
  541. {
  542. size_t sBufSize = 500* 1024*1024;
  543. void*pBuf = malloc(sBufSize);
  544. if (pBuf == NULL)
  545. {
  546. cout << "malloc failed" << endl;
  547. return;
  548. }
  549. PMEMORYPOOL mem_pool = CreateMemoryPool(pBuf, sBufSize);
  550. int iter = 2000;
  551. int test_count = 511;
  552. int* sizes = new int[iter];
  553. double* ratio = new double[test_count];
  554. if (sizes == NULL || ratio == NULL)
  555. {
  556. cout << "new memory failed" << endl;
  557. return;
  558. }
  559. srand(time(NULL));
  560. cout << "generate rand number" << endl;
  561. ofstream out("rand_test_2.txt");
  562. for (int k = 0; k < test_count; k++)
  563. {
  564. for (int i = 0; i < iter; i++)
  565. {
  566. sizes[i] = (rand() % 16 + 1) * 64 + 1024 * k;
  567. }
  568. double mem_pool_t = test_mem_pool_perf_1(mem_pool, iter, sizes);
  569. double std_t = test_std_perf_1(iter, sizes);
  570. ratio[k] = std_t / mem_pool_t;
  571. }
  572. cout << "*************************** ratio (system time / mem_pool time) ***************************" << endl;
  573. for (int k = 0; k < test_count; k++)
  574. {
  575. out << ratio[k] << ",";
  576. if (k % 10 == 0 && k != 0)
  577. {
  578. cout << endl;
  579. }
  580. cout << ratio[k] << " ";
  581. }
  582. cout << endl;
  583. delete []sizes;
  584. delete ratio;
  585. free(pBuf);
  586. }
  587. int _tmain(int argc, _TCHAR* argv[])
  588. {
  589. rand_test();
  590. // rand_test_2();
  591. return 0;
  592. }

转自https://blog.csdn.net/shawngucas/article/details/6574863

标签: C++, 内存, count, size, pool, mem, 实现, chunk

相关文章推荐

添加新评论,含*的栏目为必填