简单的内存池实现

 1 #ifndef _MEMPOOL_H_
 2 #define _MEMPOOL_H_
 3 #include<iostream>
 4 template<typename T>
 5 class CMemPool{
 6 private:
 7     CMemPool<T>* m_pFreeList;
 8 public:
 9     enum{EAXPANSION = 32};
10     CMemPool(unsigned int nItemCount = EAXPANSION){
11         ExpandFreeList(nItemCount);
12     }
13     ~CMemPool(){
14         CMemPool<T>* pNext = NULL;
15         for(pNext = m_pFreeList;pNext!=NULL;pNext = m_pFreeList){
16             m_pFreeList = m_pFreeList->m_pFreeList;
17             delete[]  (char*)pNext;
18         }
19     }
20     void* Alloc(size_t){
21         if(m_pFreeList==NULL){
22             ExpandFreeList();
23         }
24         std::cout<<"ALLOC called"<<std::endl;
25         CMemPool<T>* phead = m_pFreeList;
26         m_pFreeList = m_pFreeList->m_pFreeList;
27         return phead;
28     }
29     void Free(void* p){
30         CMemPool<T>* phead = static_cast<CMemPool<T>*>(p);
31         phead->m_pFreeList = m_pFreeList;
32         m_pFreeList = phead;
33         std::cout<<"Free called"<<std::endl;
34     }
35 protected:
36     void ExpandFreeList(unsigned int nItemcount = EAXPANSION){
37         unsigned int nSize = sizeof(T)>sizeof(CMemPool<T>*)?sizeof(T):sizeof(CMemPool<T>*);
38         CMemPool<T>* plastItem = static_cast<CMemPool<T>*>(static_cast<void*> (new char[nSize]));
39         m_pFreeList = plastItem;
40         for(int i=0;i<nItemcount-1;i++){
41             plastItem->m_pFreeList = static_cast<CMemPool<T>*>(static_cast<void*>(new char[nSize]));
42             plastItem = plastItem->m_pFreeList;
43         }
44         plastItem->m_pFreeList = NULL;
45     }
46 };
47 #endif
 1 #include<iostream>
 2 #include<Windows.h>
 3 using namespace std;
 4 #include "mempool.h"
 5
 6
 7 class CTest{
 8 public:
 9     int m;
10     int n;
11     void* operator new(size_t size){
12         void* p = s_pool->Alloc(size);
13         return p;
14     }
15     void operator delete(void* p){
16         s_pool->Free(p);
17         return;
18     }
19     static void NewPool(){
20         s_pool = new CMemPool<CTest>;
21     }
22     static void DelPool(){
23         delete s_pool;
24         return;
25     }
26     static CMemPool<CTest>* s_pool;
27 };
28 CMemPool<CTest>* CTest::s_pool=NULL;
29
30 void TestFun(){
31     int i;
32     const int nLoop = 10;
33     const int nCount = 10;
34
35     for(int j = 0; j<nLoop; ++j)
36     {
37         typedef CTest* LPTest;
38         LPTest arData[nCount];
39         for(i=0;i <nCount; ++i)
40         {
41             arData[i] = new CTest;
42         }
43
44         for(i=0;i <nCount; ++i)
45         {
46             delete arData[i];
47         }
48     }
49 }
50
51 int main(int argc, char* argv[])
52 {
53     {
54         unsigned int dwStartTickCount = GetTickCount();
55
56         CTest::NewPool();
57
58         TestFun();
59
60         CTest::DelPool();
61
62         cout << "total cost" << GetTickCount() - dwStartTickCount << endl;
63     }
64     system("pause");
65
66     return 0;
67 }

转:http://www.2cto.com/kf/201205/130457.html

时间: 2024-08-02 02:46:36

简单的内存池实现的相关文章

简单实现内存池

#include "common.h" #include "pool.h" #include <assert.h> static inline void *objmem_to_obj(void *objmem) { return objmem + sizeof(pool_obj_head_t); } static inline void *obj_to_objmem(void *obj) { return obj - sizeof(pool_obj_he

简单的内存池实现gko_alloc

在用gpreftools优化gko_pool的时候我发现一个问题,malloc竟然成了性能瓶颈 由于在每个连接建立的时候gko_pool默认会为读写各分配2KB的buf备用,这个是比较固定的 每个连接的的生命周期会伴随着4KB大小的内存malloc & free 正好可以写个只能分配固定大小内存的"内存池",基本思路就是每次分配一个大内存bucket(64MB),需要4KB的块的时候就从bucket中取,当bucket没有可用slot就再分配一个新的bucket,当bucket

C++实现的简单的内存池

用空闲链表的方式组织一连串的分配的空间,且在此程序中仅支持内置类型.只是实现了简单的分配和回收. #include<iostream> #include<assert.h> #include<stdlib.h> using namespace std; int const MAX=100; struct block{ block *next; block *addr; explicit block(int _size):next(NULL){addr=(block*)m

重写boost内存池

最近在写游戏服务器网络模块的时候,需要用到内存池.大量玩家通过tcp连接到服务器,通过大量的消息包与服务器进行交互.因此要给每个tcp分配收发两块缓冲区.那么这缓冲区多大呢?通常游戏操作的消息包都很小,大概几十字节.但是在玩家登录时或者卡牌游戏发战报(将整场战斗打完,生成一个消息包),包的大小可能达到30k或者更大,取决于游戏设定.这些缓冲区不可能使用glibc原始的new.delete来分配,这样可能会造成严重的内存碎片,并且效率也不高. 于是我们要使用内存池.并且是等长内存池,即每次分配的内

一个简易内存池(C++)

做这个内存池主要是为了完成一道面试题,题目在代码中. 代码 1 #include <iostream> 2 #include<string> 3 #include <list> 4 using namespace std; 5 6 //一个简单的内存池,池中保存3块内存分别为1k,2k,4k 7 //实现池子的malloc(size)和free(void*)操作 8 //不考虑跨块申请内存情况 9 10 class node 11 { 12 public: 13 int

内存池的实现(一)

1.引言 C/C++下内存管理是让几乎每一个程序员头疼的问题,分配足够的内存.追踪内存的分配.在不需要的时候释放内存——这个任务相当复杂.而直接使用系统调用malloc/free.new/delete进行内存分配和释放,有以下弊端: A.调用malloc/new,系统需要根据“最先匹配”.“最优匹配”或其他算法在内存空闲块表中查找一块空闲内存,调用free/delete,系统可能需要合并空闲内存块,这些会产生额外开销 B.频繁使用时会产生大量内存碎片,从而降低程序运行效率 C.容易造成内存泄漏

简单内存池实现

#ifndef __MEM_POOL_H__ #define __MEM_POOL_H__ typedef struct tagMemItem { int nSize; int nUsed; void* addr; }MEM_ITEM_S; /***************************************************************************************************** * 4M memory pool * +------

【源码剖析】MemoryPool —— 简单高效的内存池 allocator 实现

什么是内存池?什么是 C++ 的 allocator? 内存池简单说,是为了减少频繁使用 malloc/free new/delete 等系统调用而造成的性能损耗而设计的.当我们的程序需要频繁地申请和释放内存时,频繁地使用内存管理的系统调用可能会造成性能的瓶颈,嗯,是可能,毕竟操作系统的设计也不是盖的(麻麻说把话说太满会被打脸的(⊙v⊙)).内存池的思想是申请较大的一块内存(不够时继续申请),之后把内存管理放在应用层执行,减少系统调用的开销. 那么,allocator 呢?它默默的工作在 C++

简单内存池

System Call 先测试系统调用new/delete的用时. #include <iostream> #include <time.h> using namespace std; timespec diff(timespec start, timespec end) { timespec temp; if((end.tv_nsec-start.tv_nsec)<0) { temp.tv_sec = end.tv_sec-start.tv_sec-1; temp.tv_n