c++ 内存池的实现
2015-11-02 17:14
375 查看
1、使用内存池的原因
①c/c++的内存分配(malloc或new)可能会花费时间比较多
②随着时间的流逝, 会形成大量的内存碎片
2、代码:
#pragma once
#include <iostream>
#include <assert.h>
#include <typeinfo>
#define GrowValue 16
struct SingleLinkNode
{
SingleLinkNode *next;
void *check;
};
static size_t node_size = sizeof(SingleLinkNode);
static void InsertNode(SingleLinkNode *head, SingleLinkNode *node)
{
assert(head);
node->next = head->next;
head->next = node;
}
static SingleLinkNode *PopNode(SingleLinkNode *head)
{
assert(head->next);
SingleLinkNode *p = head->next;
head->next = p->next;
p->next = nullptr;
return p;
}
template <typename T>
class CObjPool
{
public:
CObjPool(size_t l = 0)
{
head_.next = nullptr;
head_.check = nullptr;
pool_size_ = 0;
Init(l);
}
bool Init(size_t l)
{
if (head_.next != nullptr)
return false;
return Grow(l);
}
void CleanMemory(void)
{
SingleLinkNode *node = head_.next;
while (node)
{
SingleLinkNode *temp = node->next;
delete node;
--pool_size_;
node = temp;
}
head_.next = 0;
}
T *AllocObj(void)
{
if (!head_.next)
{
if (!Grow(GrowValue))
return 0;
}
SingleLinkNode *p = PopNode(&head_);
return (T *)((char *)p + node_size);
}
void FreeObj(T *t)
{
SingleLinkNode *p = (SingleLinkNode *)((char *)t - node_size);
assert(p->check == this);
InsertNode(&head_, p);
}
size_t PoolSize(void)
{
return pool_size_;
}
bool IsAllocByPool(T *t)
{
if (!t)
return;
SingleLinkNode *p = (SingleLinkNode *)((char *)t - node_size);
return p->check == this;
}
private:
bool Grow(size_t l)
{
if (!l)
return true;
for (int i = 0; i < l; ++i)
{
char *ptr = new(std::nothrow) char[node_size + sizeof(T)];
if (!ptr)
return false;
SingleLinkNode *node = (SingleLinkNode *)ptr;
InsertNode(&head_, node);
++pool_size_;
new(ptr + node_size) T;
node->check = this;
}
return true;
}
private:
SingleLinkNode head_;
size_t pool_size_;
};
①c/c++的内存分配(malloc或new)可能会花费时间比较多
②随着时间的流逝, 会形成大量的内存碎片
2、代码:
#pragma once
#include <iostream>
#include <assert.h>
#include <typeinfo>
#define GrowValue 16
struct SingleLinkNode
{
SingleLinkNode *next;
void *check;
};
static size_t node_size = sizeof(SingleLinkNode);
static void InsertNode(SingleLinkNode *head, SingleLinkNode *node)
{
assert(head);
node->next = head->next;
head->next = node;
}
static SingleLinkNode *PopNode(SingleLinkNode *head)
{
assert(head->next);
SingleLinkNode *p = head->next;
head->next = p->next;
p->next = nullptr;
return p;
}
template <typename T>
class CObjPool
{
public:
CObjPool(size_t l = 0)
{
head_.next = nullptr;
head_.check = nullptr;
pool_size_ = 0;
Init(l);
}
bool Init(size_t l)
{
if (head_.next != nullptr)
return false;
return Grow(l);
}
void CleanMemory(void)
{
SingleLinkNode *node = head_.next;
while (node)
{
SingleLinkNode *temp = node->next;
delete node;
--pool_size_;
node = temp;
}
head_.next = 0;
}
T *AllocObj(void)
{
if (!head_.next)
{
if (!Grow(GrowValue))
return 0;
}
SingleLinkNode *p = PopNode(&head_);
return (T *)((char *)p + node_size);
}
void FreeObj(T *t)
{
SingleLinkNode *p = (SingleLinkNode *)((char *)t - node_size);
assert(p->check == this);
InsertNode(&head_, p);
}
size_t PoolSize(void)
{
return pool_size_;
}
bool IsAllocByPool(T *t)
{
if (!t)
return;
SingleLinkNode *p = (SingleLinkNode *)((char *)t - node_size);
return p->check == this;
}
private:
bool Grow(size_t l)
{
if (!l)
return true;
for (int i = 0; i < l; ++i)
{
char *ptr = new(std::nothrow) char[node_size + sizeof(T)];
if (!ptr)
return false;
SingleLinkNode *node = (SingleLinkNode *)ptr;
InsertNode(&head_, node);
++pool_size_;
new(ptr + node_size) T;
node->check = this;
}
return true;
}
private:
SingleLinkNode head_;
size_t pool_size_;
};
相关文章推荐
- C++基础——有关FILE的那些函数
- C语言类型转换详解
- C语言之malloc函数
- 第8周项目4 字符串加密
- C语言预处理指令
- 基于视频的车辆检测(c语言)
- C语言enum枚举类型解析
- 黑马程序员———OC语言 copy
- 黑马程序员———OC语言 NSDictionary和 NSMutableDictionary的介绍
- VC++ 6.0实用技巧汇总
- 欢迎使用CSDN-markdown编辑器
- 黑马程序员———OC语言 类的声明和实现
- C语言8章函数
- 黑马程序员———C语言 结构体
- 浅谈C++类--隐式类类型转换
- 黑马程序员———C语言 一维数组
- C++基础——类模板
- Student Information System
- 495个C语言问题(摘录)
- 日志库EasyLogging++学习系列(9)—— 性能跟踪功能