posts - 11,comments - 13,trackbacks - 0

 

 1#ifndef __MEMPOOL_H__
 2#define __MEMPOOL_H__
 3#include "stdio.h"
 4#include "stdlib.h"
 5#include <string.h>
 6
 7class CMemPool
 8{
 9protected:
10    CMemPool()
11    {
12
13    }

14public:
15    ~CMemPool()
16    {
17
18    }

19    virtual int MallocNode(int= 0;
20    virtual int FreeNode(int ,int= 0;
21    virtual int Init() = 0;
22}
;
23
24#define GET(x,y)    inline x Get_##y()    {\
25    return _##y;\
26}
27
28#define SET(x,y)    inline x Set_##y(x t)    {\
29    _##y = t;\
30    return _##y;\
31}
32
33#define SET_GET(x,y)    SET(x,y)    GET(x,y)
34
35#define FREE(x)        free(x);x=NULL;
36
37class CMemPoolChar:public CMemPool
38{
39protected:
40    int _maxsize;
41    int _curpos;
42public:
43    char * _data;
44public:
45    SET_GET(int,maxsize);
46    SET_GET(int,curpos);
47public:
48    CMemPoolChar(int size):_maxsize(size),_data(NULL),_curpos(0)    {}
49    ~CMemPoolChar()
50    {
51        if(_data != NULL)
52        {
53            FREE(_data)
54        }

55    }

56    int Init();
57    int MallocNode(int );
58    int FreeNode(int ,int);
59}
;
60
61typedef char *(CMemPoolChar::*mempointer);
62
63typedef struct _MEM_STRUCT_
64{
65    int _value;
66    int _flag;
67    _MEM_STRUCT_ * prev;
68    _MEM_STRUCT_ * next;
69}
MEMSTRUCT,*LPMEMSTRUCT;
70
71class CSMemPool:public CMemPool
72{
73public:
74    LPMEMSTRUCT _data;
75    LPMEMSTRUCT _free;
76protected:
77    int            _maxcell;
78public:
79    SET_GET(int,maxcell);
80    CSMemPool(int num):_maxcell(num)    {_data = _free = NULL;}
81    ~CSMemPool();
82    int Init();
83    int MallocNode(int  = 0);
84    int FreeNode(int,int = 0);
85}
;
86
87#endif
88


 

  1#include "mempool.h"
  2
  3int CMemPoolChar::Init()
  4{
  5    int i_ret = -1;
  6    if(Get_maxsize() <= 0)
  7    {
  8        return i_ret;
  9    }

 10    i_ret = -2;
 11    _data = (char *)malloc(sizeof(char)*Get_maxsize());
 12    if(_data == NULL)
 13    {
 14        return i_ret;
 15    }

 16    i_ret = 0;
 17    memset(_data,0,Get_maxsize());
 18    return i_ret;
 19}

 20
 21int CMemPoolChar::MallocNode(int len)
 22{
 23    int i_ret = -1;
 24    if(Get_curpos() + len >= Get_maxsize())
 25    {
 26        return i_ret;
 27    }

 28    char * tmp = _data+Get_curpos();
 29    i_ret = Get_curpos();
 30    Set_curpos(Get_curpos()+len);
 31    return i_ret;
 32}

 33
 34int CMemPoolChar::FreeNode(int data,int len)
 35{
 36    int i_ret = 0;
 37    memset((void *)(_data + data),0,len);
 38    memmove(_data+data,_data+data+len,Get_curpos() - data - len);
 39    Set_curpos(Get_curpos() - len);
 40    memset((void *)(_data + Get_curpos()),0,Get_maxsize() - Get_curpos());
 41    i_ret = Get_curpos();
 42    return i_ret;
 43}

 44
 45
 46CSMemPool::~CSMemPool()
 47{
 48    if(_data != NULL)
 49    {
 50        FREE(_data);
 51    }

 52    _free = NULL;
 53    Set_maxcell(0);
 54}

 55
 56int CSMemPool::Init()
 57{
 58    int i_ret = 0;
 59    if(Get_maxcell() <= 0)
 60    {
 61        Set_maxcell(10);
 62    }

 63    _data = (LPMEMSTRUCT)malloc(sizeof(MEMSTRUCT)*Get_maxcell());
 64    if(_data == NULL)
 65    {
 66        return i_ret;
 67    }

 68    memset(_data,0,sizeof(MEMSTRUCT)*Get_maxcell());
 69
 70    i_ret = -2;
 71    _free = (LPMEMSTRUCT)malloc(sizeof(MEMSTRUCT));
 72    if(_data == NULL)
 73    {
 74        return i_ret;
 75    }

 76    memset(_free,0,sizeof(MEMSTRUCT));
 77
 78    i_ret = 0;
 79    LPMEMSTRUCT p = _data;
 80    LPMEMSTRUCT q = _free;
 81    
 82    q->prev = NULL;
 83    int i_len = Get_maxcell();
 84    while(i_len--)
 85    {
 86        q->next = p;
 87        p->prev = q;
 88        q = p++;
 89    }

 90    (p-1)->next = _free->next;
 91    _free->next->prev = q;
 92    return i_ret;
 93}

 94
 95
 96
 97
 98int CSMemPool::MallocNode(int invalid)
 99{
100    if(_free->next == NULL)
101    {
102        return /*(LPMEMSTRUCT)NULL*/0;
103    }

104
105    //Last Node
106    LPMEMSTRUCT p = _free->next->prev;
107    if(p == _free->next)
108    {
109        _free->next = _free->prev = NULL;
110        return (int)p;
111    }

112
113    p->prev->next = _free->next;
114    _free->next->prev = p->prev;
115    return (int)p;
116}

117
118
119int CSMemPool::FreeNode(/*LPMEMSTRUCT data*/int datapos,int invalid)
120{
121    if(datapos <= 0)
122    {
123        return 0;
124    }

125    LPMEMSTRUCT data = (LPMEMSTRUCT)datapos;
126
127    memset(data,0,sizeof(MEMSTRUCT));
128    if(_free->next != NULL)
129    {
130        LPMEMSTRUCT q = _free->next;
131        data->prev = q->prev;
132        q->prev = data;
133        data->next = q;
134        _free->next = data;
135    }

136    else
137    {
138        _free->next = data;
139        data->next = data;
140        data->prev = data;
141    }

142}

143
144/*test*/
145int  main(int argc,char * argv[])
146{
147    CSMemPool c(3);
148    c.Init();
149    LPMEMSTRUCT c3 = (LPMEMSTRUCT)c.MallocNode();
150    c.FreeNode((int)c3);
151    CMemPoolChar ab(100);
152    
153    mempointer lppoint = &CMemPoolChar::_data;
154    ab.Init();
155    char * a1 = ab.*lppoint+ab.MallocNode(15);
156    memcpy(a1,"Hello World!",12);
157    char * a2 = ab.*lppoint+ab.MallocNode(25);
158    memcpy(a2,"echo -e \"Hello World!\"",25);
159    char * a3 = ab.*lppoint+ab.MallocNode(25);
160    memcpy(a3,"ps -ef |fgrep \"Hello\"",25);
161    ab.FreeNode(a2 - ab.*lppoint,25);
162    
163}

164


为了减少系统申请,释放内存的开销(有时申请释放频繁,可能会导致系统异常)和程序的有效运行;
系统初始化时,将需要申请的内存统一申请下来,如果程序中需要申请空间,可以在预留空间中申请,如果空间已满,可以通过写文件或其它方式处理。

posted on 2009-07-13 10:48 Super- 阅读(2601) 评论(6)  编辑 收藏 引用

FeedBack:
# re: c++内存池
2009-07-13 11:56 | 99读书人
不错  回复  更多评论
  
# re: c++内存池
2009-07-13 14:06 | zdhsoft
建议参考一下FastMM  回复  更多评论
  
# re: c++内存池[未登录]
2009-07-13 21:04 | dd
世界最好的 <a href="http://www.bamody.com/" title="handbags">Handbags</a> - <a href="http://www.dunksfad.com/" title="nike dunks">Nike Dunks</a> - <a href="http://www.dearway.com/jeans-c7/" title="Ed Hardy Jeans">Ed Hardy Jeans</a>
<a href="http://www.shoesdone.com/" title="nike dunk">nike dunk</a>- <a href="http://www.grabjersey.com/" title="wholesale nfl jerseys">nfl jerseys</a>- <a href="http://www.wholesale-jordan-shoes.com/" title="wholesale jordan shoes">Wholesale Jordan Shoes</a>  回复  更多评论
  
# re: c++内存池[未登录]
2009-07-13 21:05 | dd
[url=http://www.dunksfad.com/]Nike Dunks[/url] | [url=http://www.bamody.com/]Wholesale Handbags[/url] | [url=http://www.wholesale-jordan-shoes.com/]Wholesale Jordan Shoes[/url] | [url=http://www.grabjersey.com/]NFL Jerseys[/url]  回复  更多评论
  
# re: c++内存池
2009-07-14 12:36 | 凡客诚品
大家一起分享!  回复  更多评论
  
# re: c++内存池
2011-09-30 10:18 | Original essay
Some high school students think that custom written term papers are not good for their academic progress. It is not true. I have got a positive reputation because I get Essays to buy. All essays I purchase are written of high quality.   回复  更多评论
  
# re: c++内存池
2011-09-30 11:00 | dissertation writing
Whatever course you decide upon, there is every time someone to tell you that you are faulty. There are every time difficulties arising which termt you to consider that your critics are proper. But our service will facilitate you at anywhen to write your article. I notify youdissertation writing that will help you in your college life. We will help you to arise and become a useful classman!  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理