C++實現記憶體池
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部分的作用是可以通過該mem_pool結構體控制整個記憶體池。
第2部分的作用是記錄第4部分,即實際可分配的記憶體區的使用情況。表中的每一個單元表示一個固定大小的記憶體塊(block),多個連續的block組成一個chunk,每個block的詳細結構如圖2所示:
圖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結構圖
其中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 記憶體池初始化狀態
- 申請記憶體
圖5 第1次申請128B記憶體後
圖6 第n次申請、釋放記憶體後
- 釋放記憶體
圖7 釋放64B記憶體前後
3 記憶體池實現-大頂堆結構
3.1 效能分析
大頂堆結構的記憶體池實現是指將memory chunk set實現為大頂堆結構。這種方法的優缺點如下:
優點:降低了分配記憶體的時間複雜度,O(log(n))。
缺點:增加了釋放記憶體的時間複雜度,O(log(n))。
3.2 記憶體池執行狀態轉移圖
綠色表示未使用的記憶體,紅色表示已經使用的記憶體。其中每個block表示64B,這個值可以根據具體需要設定。
- 初始化
圖8 記憶體池初始化狀態
- 申請記憶體
圖9 第1次申請128B記憶體後
圖10 第n次申請、釋放記憶體後
- 釋放記憶體
圖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
圖13 連結串列結構記憶體池效能測試結果2
4.2 效能測試結果-大頂堆結構記憶體池
圖14 大頂堆記憶體池效能測試結果1
圖15 大頂堆記憶體池效能測試結果2
4.3 效能比較
圖16 兩種記憶體池效能測試結果比較1
圖17 兩種記憶體池效能測試結果比較2
5 結論
從上面的記憶體池效能測試結果中可以看出,相比C語言的malloc和free,記憶體池使得使用者分配記憶體和釋放記憶體的效率有了較大的提高,這一優勢尤其分配較大快的記憶體時體現的尤為突出。
同時也可以看出大頂堆結夠的記憶體池的效能並不比連結串列結構的記憶體池效能高,反而低於連結串列結構記憶體池的效能。這再一次表明O(log(n))優於O(n)是有條件的。當然,本文的測試具有一定的侷限性,也許在其他的測試案例中大頂堆結構的記憶體池效能會超越連結串列結構的記憶體池。
附:原始碼
連結串列結構記憶體池:
MemoryPool.h
-
#ifndef _MEMORYPOOL_H
-
#define _MEMORYPOOL_H
-
#include <stdlib.h>
-
#define MINUNITSIZE 64
-
#define ADDR_ALIGN 8
-
#define SIZE_ALIGN MINUNITSIZE
-
struct memory_chunk;
-
typedef struct memory_block
-
{
-
size_t count;
-
size_t start;
-
memory_chunk* pmem_chunk;
-
}memory_block;
-
// 可用的記憶體塊結構體
-
typedef struct memory_chunk
-
{
-
memory_block* pfree_mem_addr;
-
memory_chunk* pre;
-
memory_chunk* next;
-
}memory_chunk;
-
// 記憶體池結構體
-
typedef struct MEMORYPOOL
-
{
-
void *memory;
-
size_t size;
-
memory_block* pmem_map;
-
memory_chunk* pfree_mem_chunk;
-
memory_chunk* pfree_mem_chunk_pool;
-
size_t mem_used_size; // 記錄記憶體池中已經分配給使用者的記憶體的大小
-
size_t mem_map_pool_count; // 記錄連結串列單元緩衝池中剩餘的單元的個數,個數為0時不能分配單元給pfree_mem_chunk
-
size_t free_mem_chunk_count; // 記錄 pfree_mem_chunk連結串列中的單元個數
-
size_t mem_map_unit_count; //
-
size_t mem_block_count; // 一個 mem_unit 大小為 MINUNITSIZE
-
}MEMORYPOOL, *PMEMORYPOOL;
-
/************************************************************************/
-
/* 生成記憶體池
-
* pBuf: 給定的記憶體buffer起始地址
-
* sBufSize: 給定的記憶體buffer大小
-
* 返回生成的記憶體池指標
-
/************************************************************************/
-
PMEMORYPOOL CreateMemoryPool(void* pBuf, size_t sBufSize);
-
/************************************************************************/
-
/* 暫時沒用
-
/************************************************************************/
-
void ReleaseMemoryPool(PMEMORYPOOL* ppMem) ;
-
/************************************************************************/
-
/* 從記憶體池中分配指定大小的記憶體
-
* pMem: 記憶體池 指標
-
* sMemorySize: 要分配的記憶體大小
-
* 成功時返回分配的記憶體起始地址,失敗返回NULL
-
/************************************************************************/
-
void* GetMemory(size_t sMemorySize, PMEMORYPOOL pMem) ;
-
/************************************************************************/
-
/* 從記憶體池中釋放申請到的記憶體
-
* pMem:記憶體池指標
-
* ptrMemoryBlock:申請到的記憶體起始地址
-
/************************************************************************/
-
void FreeMemory(void *ptrMemoryBlock, PMEMORYPOOL pMem) ;
-
#endif //_MEMORYPOOL_H
MemoryPool.cpp
-
#include "stdafx.h"
-
#include <memory.h>
-
#include "MemoryPool.h"
-
/************************************************************************/
-
/* 記憶體池起始地址對齊到ADDR_ALIGN位元組
-
/************************************************************************/
-
size_t check_align_addr(void*& pBuf)
-
{
-
size_t align = 0;
-
size_t addr = (int)pBuf;
-
align = (ADDR_ALIGN - addr % ADDR_ALIGN) % ADDR_ALIGN;
-
pBuf = (char*)pBuf + align;
-
return align;
-
}
-
/************************************************************************/
-
/* 記憶體block大小對齊到MINUNITSIZE位元組
-
/************************************************************************/
-
size_t check_align_block(size_t size)
-
{
-
size_t align = size % MINUNITSIZE;
-
return size - align;
-
}
-
/************************************************************************/
-
/* 分配記憶體大小對齊到SIZE_ALIGN位元組
-
/************************************************************************/
-
size_t check_align_size(size_t size)
-
{
-
size = (size + SIZE_ALIGN - 1) / SIZE_ALIGN * SIZE_ALIGN;
-
return size;
-
}
-
/************************************************************************/
-
/* 以下是連結串列相關操作
-
/************************************************************************/
-
memory_chunk* create_list(memory_chunk* pool, size_t count)
-
{
-
if (!pool)
-
{
-
return NULL;
-
}
-
memory_chunk* head = NULL;
-
for (size_t i = 0; i < count; i++)
-
{
-
pool->pre = NULL;
-
pool->next = head;
-
if (head != NULL)
-
{
-
head->pre = pool;
-
}
-
head = pool;
-
pool++;
-
}
-
return head;
-
}
-
memory_chunk* front_pop(memory_chunk*& pool)
-
{
-
if (!pool)
-
{
-
return NULL;
-
}
-
memory_chunk* tmp = pool;
-
pool = tmp->next;
-
pool->pre = NULL;
-
return tmp;
-
}
-
void push_back(memory_chunk*& head, memory_chunk* element)
-
{
-
if (head == NULL)
-
{
-
head = element;
-
head->pre = element;
-
head->next = element;
-
return;
-
}
-
head->pre->next = element;
-
element->pre = head->pre;
-
head->pre = element;
-
element->next = head;
-
}
-
void push_front(memory_chunk*& head, memory_chunk* element)
-
{
-
element->pre = NULL;
-
element->next = head;
-
if (head != NULL)
-
{
-
head->pre = element;
-
}
-
head = element;
-
}
-
void delete_chunk(memory_chunk*& head, memory_chunk* element)
-
{
-
// 在雙迴圈連結串列中刪除元素
-
if (element == NULL)
-
{
-
return;
-
}
-
// element為連結串列頭
-
else if (element == head)
-
{
-
// 連結串列只有一個元素
-
if (head->pre == head)
-
{
-
head = NULL;
-
}
-
else
-
{
-
head = element->next;
-
head->pre = element->pre;
-
head->pre->next = head;
-
}
-
}
-
// element為連結串列尾
-
else if (element->next == head)
-
{
-
head->pre = element->pre;
-
element->pre->next = head;
-
}
-
else
-
{
-
element->pre->next = element->next;
-
element->next->pre = element->pre;
-
}
-
element->pre = NULL;
-
element->next = NULL;
-
}
-
/************************************************************************/
-
/* 記憶體對映表中的索引轉化為記憶體起始地址
-
/************************************************************************/
-
void* index2addr(PMEMORYPOOL mem_pool, size_t index)
-
{
-
char* p = (char*)(mem_pool->memory);
-
void* ret = (void*)(p + index *MINUNITSIZE);
-
return ret;
-
}
-
/************************************************************************/
-
/* 記憶體起始地址轉化為記憶體對映表中的索引
-
/************************************************************************/
-
size_t addr2index(PMEMORYPOOL mem_pool, void* addr)
-
{
-
char* start = (char*)(mem_pool->memory);
-
char* p = (char*)addr;
-
size_t index = (p - start) / MINUNITSIZE;
-
return index;
-
}
-
/************************************************************************/
-
/* 生成記憶體池
-
* pBuf: 給定的記憶體buffer起始地址
-
* sBufSize: 給定的記憶體buffer大小
-
* 返回生成的記憶體池指標
-
/************************************************************************/
-
PMEMORYPOOL CreateMemoryPool(void* pBuf, size_t sBufSize)
-
{
-
memset(pBuf, 0, sBufSize);
-
PMEMORYPOOL mem_pool = (PMEMORYPOOL)pBuf;
-
// 計算需要多少memory map單元格
-
size_t mem_pool_struct_size = sizeof(MEMORYPOOL);
-
mem_pool->mem_map_pool_count = (sBufSize - mem_pool_struct_size + MINUNITSIZE - 1) / MINUNITSIZE;
-
mem_pool->mem_map_unit_count = (sBufSize - mem_pool_struct_size + MINUNITSIZE - 1) / MINUNITSIZE;
-
mem_pool->pmem_map = (memory_block*)((char*)pBuf + mem_pool_struct_size);
-
mem_pool->pfree_mem_chunk_pool = (memory_chunk*)((char*)pBuf + mem_pool_struct_size + sizeof(memory_block) * mem_pool->mem_map_unit_count);
-
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;
-
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;
-
size_t align = check_align_addr(mem_pool->memory);
-
mem_pool->size -= align;
-
mem_pool->size = check_align_block(mem_pool->size);
-
mem_pool->mem_block_count = mem_pool->size / MINUNITSIZE;
-
// 連結串列化
-
mem_pool->pfree_mem_chunk_pool = create_list(mem_pool->pfree_mem_chunk_pool, mem_pool->mem_map_pool_count);
-
// 初始化 pfree_mem_chunk,雙向迴圈連結串列
-
memory_chunk* tmp = front_pop(mem_pool->pfree_mem_chunk_pool);
-
tmp->pre = tmp;
-
tmp->next = tmp;
-
tmp->pfree_mem_addr = NULL;
-
mem_pool->mem_map_pool_count--;
-
// 初始化 pmem_map
-
mem_pool->pmem_map[0].count = mem_pool->mem_block_count;
-
mem_pool->pmem_map[0].pmem_chunk = tmp;
-
mem_pool->pmem_map[mem_pool->mem_block_count-1].start = 0;
-
tmp->pfree_mem_addr = mem_pool->pmem_map;
-
push_back(mem_pool->pfree_mem_chunk, tmp);
-
mem_pool->free_mem_chunk_count = 1;
-
mem_pool->mem_used_size = 0;
-
return mem_pool;
-
}
-
/************************************************************************/
-
/* 暫時沒用
-
/************************************************************************/
-
void ReleaseMemoryPool(PMEMORYPOOL* ppMem)
-
{
-
}
-
/************************************************************************/
-
/* 從記憶體池中分配指定大小的記憶體
-
* pMem: 記憶體池 指標
-
* sMemorySize: 要分配的記憶體大小
-
* 成功時返回分配的記憶體起始地址,失敗返回NULL
-
/************************************************************************/
-
void* GetMemory(size_t sMemorySize, PMEMORYPOOL pMem)
-
{
-
sMemorySize = check_align_size(sMemorySize);
-
size_t index = 0;
-
memory_chunk* tmp = pMem->pfree_mem_chunk;
-
for (index = 0; index < pMem->free_mem_chunk_count; index++)
-
{
-
if (tmp->pfree_mem_addr->count * MINUNITSIZE >= sMemorySize)
-
{
-
break;
-
}
-
tmp = tmp->next;
-
}
-
if (index == pMem->free_mem_chunk_count)
-
{
-
return NULL;
-
}
-
pMem->mem_used_size += sMemorySize;
-
if (tmp->pfree_mem_addr->count * MINUNITSIZE == sMemorySize)
-
{
-
// 當要分配的記憶體大小與當前chunk中的記憶體大小相同時,從pfree_mem_chunk連結串列中刪除此chunk
-
size_t current_index = (tmp->pfree_mem_addr - pMem->pmem_map);
-
delete_chunk(pMem->pfree_mem_chunk, tmp);
-
tmp->pfree_mem_addr->pmem_chunk = NULL;
-
push_front(pMem->pfree_mem_chunk_pool, tmp);
-
pMem->free_mem_chunk_count--;
-
pMem->mem_map_pool_count++;
-
return index2addr(pMem, current_index);
-
}
-
else
-
{
-
// 當要分配的記憶體小於當前chunk中的記憶體時,更改pfree_mem_chunk中相應chunk的pfree_mem_addr
-
// 複製當前mem_map_unit
-
memory_block copy;
-
copy.count = tmp->pfree_mem_addr->count;
-
copy.pmem_chunk = tmp;
-
// 記錄該block的起始和結束索引
-
memory_block* current_block = tmp->pfree_mem_addr;
-
current_block->count = sMemorySize / MINUNITSIZE;
-
size_t current_index = (current_block - pMem->pmem_map);
-
pMem->pmem_map[current_index+current_block->count-1].start = current_index;
-
current_block->pmem_chunk = NULL; // NULL表示當前記憶體塊已被分配
-
// 當前block被一分為二,更新第二個block中的內容
-
pMem->pmem_map[current_index+current_block->count].count = copy.count - current_block->count;
-
pMem->pmem_map[current_index+current_block->count].pmem_chunk = copy.pmem_chunk;
-
// 更新原來的pfree_mem_addr
-
tmp->pfree_mem_addr = &(pMem->pmem_map[current_index+current_block->count]);
-
size_t end_index = current_index + copy.count - 1;
-
pMem->pmem_map[end_index].start = current_index + current_block->count;
-
return index2addr(pMem, current_index);
-
}
-
}
-
/************************************************************************/
-
/* 從記憶體池中釋放申請到的記憶體
-
* pMem:記憶體池指標
-
* ptrMemoryBlock:申請到的記憶體起始地址
-
/************************************************************************/
-
void FreeMemory(void *ptrMemoryBlock, PMEMORYPOOL pMem)
-
{
-
size_t current_index = addr2index(pMem, ptrMemoryBlock);
-
size_t size = pMem->pmem_map[current_index].count * MINUNITSIZE;
-
// 判斷與當前釋放的記憶體塊相鄰的記憶體塊是否可以與當前釋放的記憶體塊合併
-
memory_block* pre_block = NULL;
-
memory_block* next_block = NULL;
-
memory_block* current_block = &(pMem->pmem_map[current_index]);
-
// 第一個
-
if (current_index == 0)
-
{
-
if (current_block->count < pMem->mem_block_count)
-
{
-
next_block = &(pMem->pmem_map[current_index+current_block->count]);
-
// 如果後一個記憶體塊是空閒的,合併
-
if (next_block->pmem_chunk != NULL)
-
{
-
next_block->pmem_chunk->pfree_mem_addr = current_block;
-
pMem->pmem_map[current_index+current_block->count+next_block->count-1].start = current_index;
-
current_block->count += next_block->count;
-
current_block->pmem_chunk = next_block->pmem_chunk;
-
next_block->pmem_chunk = NULL;
-
}
-
// 如果後一塊記憶體不是空閒的,在pfree_mem_chunk中增加一個chunk
-
else
-
{
-
memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
-
new_chunk->pfree_mem_addr = current_block;
-
current_block->pmem_chunk = new_chunk;
-
push_back(pMem->pfree_mem_chunk, new_chunk);
-
pMem->mem_map_pool_count--;
-
pMem->free_mem_chunk_count++;
-
}
-
}
-
else
-
{
-
memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
-
new_chunk->pfree_mem_addr = current_block;
-
current_block->pmem_chunk = new_chunk;
-
push_back(pMem->pfree_mem_chunk, new_chunk);
-
pMem->mem_map_pool_count--;
-
pMem->free_mem_chunk_count++;
-
}
-
}
-
// 最後一個
-
else if (current_index == pMem->mem_block_count-1)
-
{
-
if (current_block->count < pMem->mem_block_count)
-
{
-
pre_block = &(pMem->pmem_map[current_index-1]);
-
size_t index = pre_block->count;
-
pre_block = &(pMem->pmem_map[index]);
-
// 如果前一個記憶體塊是空閒的,合併
-
if (pre_block->pmem_chunk != NULL)
-
{
-
pMem->pmem_map[current_index+current_block->count-1].start = current_index - pre_block->count;
-
pre_block->count += current_block->count;
-
current_block->pmem_chunk = NULL;
-
}
-
// 如果前一塊記憶體不是空閒的,在pfree_mem_chunk中增加一個chunk
-
else
-
{
-
memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
-
new_chunk->pfree_mem_addr = current_block;
-
current_block->pmem_chunk = new_chunk;
-
push_back(pMem->pfree_mem_chunk, new_chunk);
-
pMem->mem_map_pool_count--;
-
pMem->free_mem_chunk_count++;
-
}
-
}
-
else
-
{
-
memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
-
new_chunk->pfree_mem_addr = current_block;
-
current_block->pmem_chunk = new_chunk;
-
push_back(pMem->pfree_mem_chunk, new_chunk);
-
pMem->mem_map_pool_count--;
-
pMem->free_mem_chunk_count++;
-
}
-
}
-
else
-
{
-
next_block = &(pMem->pmem_map[current_index+current_block->count]);
-
pre_block = &(pMem->pmem_map[current_index-1]);
-
size_t index = pre_block->start;
-
pre_block = &(pMem->pmem_map[index]);
-
bool is_back_merge = false;
-
if (next_block->pmem_chunk == NULL && pre_block->pmem_chunk == NULL)
-
{
-
memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);
-
new_chunk->pfree_mem_addr = current_block;
-
current_block->pmem_chunk = new_chunk;
-
push_back(pMem->pfree_mem_chunk, new_chunk);
-
pMem->mem_map_pool_count--;
-
pMem->free_mem_chunk_count++;
-
}
-
// 後一個記憶體塊
-
if (next_block->pmem_chunk != NULL)
-
{
-
next_block->pmem_chunk->pfree_mem_addr = current_block;
-
pMem->pmem_map[current_index+current_block->count+next_block->count-1].start = current_index;
-
current_block->count += next_block->count;
-
current_block->pmem_chunk = next_block->pmem_chunk;
-
next_block->pmem_chunk = NULL;
-
is_back_merge = true;
-
}
-
// 前一個記憶體塊
-
if (pre_block->pmem_chunk != NULL)
-
{
-
pMem->pmem_map[current_index+current_block->count-1].start = current_index - pre_block->count;
-
pre_block->count += current_block->count;
-
if (is_back_merge)
-
{
-
delete_chunk(pMem->pfree_mem_chunk, current_block->pmem_chunk);
-
push_front(pMem->pfree_mem_chunk_pool, current_block->pmem_chunk);
-
pMem->free_mem_chunk_count--;
-
pMem->mem_map_pool_count++;
-
}
-
current_block->pmem_chunk = NULL;
-
}
-
}
-
pMem->mem_used_size -= size;
-
}
MemoryPoolTest.cpp
-
// memory pool test.cpp : Defines the entry point for the console application.
-
#include <tchar.h>
-
#include "MemoryPool.h"
-
#include <iostream>
-
#include <windows.h>
-
#include <vector>
-
#include <time.h>
-
#include <math.h>
-
#include <fstream>
-
using namespace std;
-
int break_time = 0;
-
// 檢測記憶體池相關引數
-
void check_mem_pool(int& max_chunk_size, int& free_chunk_count, int& min_chunk_size, int& total_free_mem, MEMORYPOOL* mem_pool)
-
{
-
memory_chunk* head = mem_pool->pfree_mem_chunk;
-
memory_chunk* tmp = head;
-
free_chunk_count = 0;
-
total_free_mem = 0;
-
max_chunk_size = 0;
-
min_chunk_size = 500*1024*1024;
-
if (head == NULL)
-
{
-
min_chunk_size = 0;
-
return;
-
}
-
while (tmp->next != head)
-
{
-
free_chunk_count++;
-
total_free_mem += tmp->pfree_mem_addr->count * MINUNITSIZE;
-
if (tmp->pfree_mem_addr->count * MINUNITSIZE > max_chunk_size )
-
{
-
max_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
-
}
-
if (tmp->pfree_mem_addr->count * MINUNITSIZE < min_chunk_size)
-
{
-
min_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
-
}
-
tmp = tmp->next;
-
}
-
free_chunk_count++;
-
total_free_mem += tmp->pfree_mem_addr->count * MINUNITSIZE;
-
if (tmp->pfree_mem_addr->count * MINUNITSIZE > max_chunk_size )
-
{
-
max_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
-
}
-
if (tmp->pfree_mem_addr->count * MINUNITSIZE < min_chunk_size)
-
{
-
min_chunk_size = tmp->pfree_mem_addr->count * MINUNITSIZE;
-
}
-
}
-
// 申請後緊接著釋放
-
double test_mem_pool_perf_1(PMEMORYPOOL mem_pool, int iter, int* sizes)
-
{
-
cout << "*********************test_mem_pool_perf_1*********************" << endl;
-
LARGE_INTEGER litmp;
-
LONGLONG QPart1, QPart2;
-
double t;
-
double dfMinus, dfFreq;
-
QueryPerformanceFrequency(&litmp);
-
dfFreq = (double)litmp.QuadPart;// 獲得計數器的時鐘頻率
-
QueryPerformanceCounter(&litmp);
-
QPart1 = litmp.QuadPart;// 獲得初始值
-
for (int i = 0; i < iter; i++)
-
{
-
void *p = GetMemory(sizes[i], mem_pool);
-
if (p == NULL)
-
{
-
cout << "break @ iterator = " << i << " / " << iter << ", need memory " << sizes[i] << " Byte" << endl;
-
cout << "total memory is: " << mem_pool->size << " Byte" << endl;
-
cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
-
cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
-
int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
-
check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
-
cout << "check memory pool result:" << endl;
-
cout << "free_chunk_count: " << free_chunk_count << endl
-
<< "total_free_mem: " << total_free_mem << endl
-
<< "max_chunk_size: " << max_chunk_size << endl
-
<< "min_chunk_size: " << min_chunk_size << endl;
-
break;
-
}
-
FreeMemory(p, mem_pool);
-
}
-
QueryPerformanceCounter(&litmp);
-
QPart2 = litmp.QuadPart;//獲得中止值
-
dfMinus = (double)(QPart2-QPart1);
-
t = dfMinus / dfFreq;// 獲得對應的時間值,單位為秒
-
cout << "test_mem_pool_perf_1: iter = " << iter << endl;
-
cout << "time: " << t << endl;
-
cout << "*********************test_mem_pool_perf_1*********************" << endl << endl << endl;
-
return t;
-
}
-
double test_std_perf_1(int iter, int* sizes)
-
{
-
cout << "*********************test_std_perf_1*********************" << endl;
-
LARGE_INTEGER litmp;
-
LONGLONG QPart1, QPart2;
-
double t;
-
double dfMinus, dfFreq;
-
QueryPerformanceFrequency(&litmp);
-
dfFreq = (double)litmp.QuadPart;// 獲得計數器的時鐘頻率
-
QueryPerformanceCounter(&litmp);
-
QPart1 = litmp.QuadPart;// 獲得初始值
-
for (int i = 0; i < iter; i++)
-
{
-
void *p = malloc(sizes[i]);
-
if (p == NULL)
-
{
-
cout << "break @ iterator = " << i << " / " << iter << ", need memory " << sizes[i] << " Byte" << endl;
-
break;
-
}
-
free(p);
-
}
-
QueryPerformanceCounter(&litmp);
-
QPart2 = litmp.QuadPart;//獲得中止值
-
dfMinus = (double)(QPart2-QPart1);
-
t = dfMinus / dfFreq;// 獲得對應的時間值,單位為秒
-
cout << "test_std_perf_1: iter = " << iter << endl;
-
cout << "time: " << t << endl;
-
cout << "*********************test_std_perf_1*********************" << endl << endl << endl;
-
return t;
-
}
-
// 連續申請iter/2次,然後釋放所有申請記憶體;再重複一次
-
double test_mem_pool_perf_2(PMEMORYPOOL mem_pool, int iter, int size)
-
{
-
cout << "*********************test_mem_pool_perf_2*********************" << endl;
-
LARGE_INTEGER litmp;
-
LONGLONG QPart1, QPart2;
-
double t;
-
double dfMinus, dfFreq;
-
QueryPerformanceFrequency(&litmp);
-
dfFreq = (double)litmp.QuadPart;// 獲得計數器的時鐘頻率
-
QueryPerformanceCounter(&litmp);
-
QPart1 = litmp.QuadPart;// 獲得初始值
-
void **p = new void*[iter];
-
if (p == NULL)
-
{
-
cout << "new faild" << endl;
-
return -1;
-
}
-
int count = 0;
-
for (int i = 0; i < iter/2; i++)
-
{
-
p[i] = GetMemory(size, mem_pool);
-
if (p[i] == NULL)
-
{
-
cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
-
cout << "total memory is: " << mem_pool->size << " Byte" << endl;
-
cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
-
cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
-
int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
-
check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
-
cout << "check memory pool result:" << endl;
-
cout << "free_chunk_count: " << free_chunk_count << endl
-
<< "total_free_mem: " << total_free_mem << endl
-
<< "max_chunk_size: " << max_chunk_size << endl
-
<< "min_chunk_size: " << min_chunk_size << endl;
-
break;
-
}
-
count++;
-
}
-
for (int i = 0; i < count; i++)
-
{
-
FreeMemory(p[i], mem_pool);
-
}
-
count = 0;
-
for (int i = 0; i < iter/2; i++)
-
{
-
p[i] = GetMemory(size, mem_pool);
-
if (p[i] == NULL)
-
{
-
cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
-
cout << "total memory is: " << mem_pool->size << " Byte" << endl;
-
cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
-
cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
-
int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
-
check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
-
cout << "check memory pool result:" << endl;
-
cout << "free_chunk_count: " << free_chunk_count << endl
-
<< "total_free_mem: " << total_free_mem << endl
-
<< "max_chunk_size: " << max_chunk_size << endl
-
<< "min_chunk_size: " << min_chunk_size << endl;
-
break;
-
}
-
count++;
-
}
-
for (int i = 0; i < count; i++)
-
{
-
if (p[i] == NULL)
-
{
-
cout << i << endl;
-
break;
-
}
-
FreeMemory(p[i], mem_pool);
-
}
-
QueryPerformanceCounter(&litmp);
-
QPart2 = litmp.QuadPart;//獲得中止值
-
dfMinus = (double)(QPart2-QPart1);
-
t = dfMinus / dfFreq;// 獲得對應的時間值,單位為秒
-
cout << "test_mem_pool_perf_2: iter = " << iter << endl;
-
cout << "time: " << t << endl;
-
delete []p;
-
cout << "*********************test_mem_pool_perf_2*********************" << endl << endl << endl;
-
return t;
-
}
-
// 連續申請inner_iter次,釋放;重複iter/inner_iter次
-
double test_mem_pool_perf_3(PMEMORYPOOL mem_pool, int iter, int size)
-
{
-
cout << "*********************test_mem_pool_perf_3*********************" << endl;
-
int inner_iter = 10;
-
void **p = new void*[inner_iter];
-
if (p == NULL)
-
{
-
cout << "new faild" << endl;
-
return -1;
-
}
-
LARGE_INTEGER litmp;
-
LONGLONG QPart1, QPart2, start, finish;
-
double t;
-
double dfMinus, dfFreq;
-
QueryPerformanceFrequency(&litmp);
-
dfFreq = (double)litmp.QuadPart;// 獲得計數器的時鐘頻率
-
QueryPerformanceCounter(&litmp);
-
QPart1 = litmp.QuadPart;// 獲得初始值
-
for (int k = 0; k < iter / inner_iter; k++)
-
{
-
int j = 0;
-
for (j = 0; j < inner_iter; j++)
-
{
-
p[j] = GetMemory(size, mem_pool);
-
if (p[j] == NULL)
-
{
-
cout << "break @ iterator = " << j << " / " << iter << ", need memory " << size << " Byte" << endl;
-
cout << "total memory is: " << mem_pool->size << " Byte" << endl;
-
cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
-
cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
-
int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
-
check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
-
cout << "check memory pool result:" << endl;
-
cout << "free_chunk_count: " << free_chunk_count << endl
-
<< "total_free_mem: " << total_free_mem << endl
-
<< "max_chunk_size: " << max_chunk_size << endl
-
<< "min_chunk_size: " << min_chunk_size << endl;
-
break;
-
}
-
}
-
for (int i = 0; i < j; i++)
-
{
-
FreeMemory(p[i], mem_pool);
-
}
-
}
-
QueryPerformanceCounter(&litmp);
-
QPart2 = litmp.QuadPart;//獲得中止值
-
dfMinus = (double)(QPart2-QPart1);
-
t = dfMinus / dfFreq;// 獲得對應的時間值,單位為秒
-
cout << "test_mem_pool_perf_3: iter = " << iter << endl;
-
cout << "time: " << t << endl;
-
cout << "*********************test_mem_pool_perf_3*********************" << endl << endl << endl;
-
return t;
-
}
-
// 隨機記憶體大小,隨機釋放操作
-
double test_mem_pool_perf_rand(PMEMORYPOOL mem_pool, int iter, int* sizes, int* instruction)
-
{
-
cout << "-----------------------test_mem_pool_perf_rand----------------------- "<< endl;
-
void** p = new void*[iter];
-
if (p == NULL)
-
{
-
cout << "new failed" << endl;
-
return -1;
-
}
-
LARGE_INTEGER litmp, gftime;
-
LONGLONG QPart1, QPart2, start, finish;
-
double t, GetMemory_time, FreeMemory_time;
-
double dfMinus, dfFreq;
-
QueryPerformanceFrequency(&litmp);
-
dfFreq = (double)litmp.QuadPart;// 獲得計數器的時鐘頻率
-
QueryPerformanceCounter(&litmp);
-
QPart1 = litmp.QuadPart;// 獲得初始值
-
int index = 0;
-
int size;
-
int free_tmp = 0;
-
double seach_time;
-
for (int i = 0; i < iter; i++)
-
{
-
size = sizes[i];
-
p[index++] = GetMemory(size, mem_pool);
-
if (p[index-1] == NULL)
-
{
-
break_time++;
-
cout << "break @ iterator = " << i << " / " << iter << ", need memory " << size << " Byte" << endl;
-
cout << "total memory is: " << mem_pool->size << " Byte" << endl;
-
cout << "memory used is: " << mem_pool->mem_used_size << " Byte" << endl;
-
cout << "memory left is: " << mem_pool->size - mem_pool->mem_used_size << endl << endl;
-
int max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem;
-
check_mem_pool(max_chunk_size, free_chunk_count, min_chunk_size, total_free_mem, mem_pool);
-
cout << "check memory pool result:" << endl;
-
cout << "free_chunk_count: " << free_chunk_count << endl
-
<< "total_free_mem: " << total_free_mem << endl
-
<< "max_chunk_size: " << max_chunk_size << endl
-
<< "min_chunk_size: " << min_chunk_size << endl;
-
break;
-
}
-
if (instruction[i] == 1)
-
{
-
FreeMemory(p[--index], mem_pool);
-
}
-
}