经典的内存池(mempool)技术,是一种用于分配大量大小相同的小对象的技术。通过该技术可以极大加快内存分配/释放过程。 内存池只涉及两个静态常量:Block_Size(mempool的容量大小)、Item_Size(小对象的大小,但不能小于指针的大小,在32位平台也就是不能小于4字节),以及两个指针变量BlockNodeHead、FreeItemNodeHead。开始,这两个指针均为空。 其中指针变量BlockNodeHead是把所有申请的内存块(Block_Node)串成一个链表,以便通过它可以释放所有申请的内存。FreeItemNodeHead变量则是把所有自由内存结点(Item_Node)串成一个链。 详细信息请查看http://www.itgrass.com/a/cjj/C-jc/200706/29-9836.html 下面给出完整实例代码: /* #ifndef _MEMPOOL_H class Mempool ?????????????????? void* Alloc(); ????????? private: ? ?????????????????? struct Item_Node ?????????????????? struct Block_Node ???????????????????Block_Node* BlockNodeHead; /* #include <iostream> Mempool::Mempool():BlockNodeHead(NULL),freeItemNodeHead(NULL) Mempool::~Mempool() void* Mempool::Alloc() ? ???????????????? if(BlockNodeHead == NULL) ?????????????????freeItemNodeHead = &(tmpBlockNode->data[0]); ???????????????? int i = 0; ????????Item_Node* allocItemNode = freeItemNodeHead; ??????? return allocItemNode; void Mempool::Free(void* p) ????????return 0;
?*
?* Filename:?mempool.h
?*
?* Description:?
?*
?* Version:?1.0
?* Created:?03/31/2010 11:03:23 AM
?* Revision:?None
?* Compiler: ?gcc(g+)
?*
?* Author:?Zhenghe Zhang (Programmer),?406550785@qq.com
?* Company:?XX.CO.,LTD
?*
?*/
#define _MEMPOOL_H
{
????????? public:
?????????????????? Mempool();
?????????????????? ~Mempool();
?????????????????? void Free(void* p);
?????????????????? const static int Block_Size = 1024;
?????????????????? const static int Item_Size = 16;
?????????????????? {
?????????????????????????????? struct Item_Node *next;
???????????????????????????????char data[Item_Size - sizeof(Item_Node*)];
?????????????????? };
?????????????????? {
?????????????????????????????? struct Block_Node *next;
?????????????????????????????? Item_Node data[Block_Size / Item_Size];
???????????????????};
?????????????????? Item_Node* freeItemNodeHead;
};
#endif
?*
?* Filename:?mempool.cpp
?*
?* Description:?
?*
?* Version:?1.0
?* Created:?03/31/2010 01:43:20 PM
?* Revision:?None
?* Compiler:?gcc(g++)
?*
?* Author:?Zhenghe Zhang (Programmer),?406550785@qq.com
?* Company:?XX.CO.,LTD
?*
?*/
#include "mempool.h"
using namespace std;
{
}
{
???????? Block_Node* tmp = BlockNodeHead;
?????????while(tmp != NULL)
???????? {
???????????????? BlockNodeHead = BlockNodeHead->next;
???????????????? delete tmp;
???????????????? tmp = BlockNodeHead;
?????????}?
}
{
???????? if(freeItemNodeHead == NULL)
???????? {
???????????????? Block_Node* tmpBlockNode = new Block_Node;
?????????????????if(tmpBlockNode == NULL)
?????????????????{
?????????????????????????????perror("No memory!\n");
???????????????????????????? exit(1);
???????????????? }
???????????????? {
???????????????????????????? BlockNodeHead = tmpBlockNode;
???????????????????????????? BlockNodeHead->next = NULL;
?????????????????}
?????????????????else
???????????????? {
?????????????????????????????tmpBlockNode->next = BlockNodeHead;
???????????????????????????? BlockNodeHead = tmpBlockNode;
?????????????????}
???????????????? while(i < Block_Size/Item_Size - 1)
?????????????????{
???????????????????????????? tmpBlockNode->data[i].next = &(tmpBlockNode->data[i + 1]);
?????????????????????????????++i;
??????????????????}
??????????????????tmpBlockNode->data[i].next = NULL;
??? ??? }
??????? freeItemNodeHead = freeItemNodeHead->next;
}
{
????????Item_Node* tmpItemNode = static_cast<Item_Node*>(p);
??????? tmpItemNode->next = freeItemNodeHead;
??????? freeItemNodeHead = tmpItemNode;
}
int main()
{
??????? Mempool pool;
??????? char* p = NULL;
??????? int i = 0;
??????? while(i < 100000)
??????? {
??????????????????? p = static_cast<char*>(pool.Alloc());
??????????????????? strcpy(p, "Hello");
??????????????????? cout<<i<<" : "<<p<<endl;
??????????????????? ++i;
????????????????????pool.Free(p);
????????}
}