1
#ifndef __MEMPOOL_H__
2
#define __MEMPOOL_H__
3
#include "stdio.h"
4
#include "stdlib.h"
5
#include <string.h>
6![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
7
class CMemPool
8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
protected:
10
CMemPool()
11![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
13
}
14
public:
15
~CMemPool()
16![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
18
}
19
virtual int MallocNode(int) = 0;
20
virtual int FreeNode(int ,int) = 0;
21
virtual int Init() = 0;
22
};
23![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
24
#define GET(x,y) inline x Get_##y() {\
25
return _##y;\
26
}
27![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
28
#define SET(x,y) inline x Set_##y(x t) {\
29
_##y = t;\
30
return _##y;\
31
}
32![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
33
#define SET_GET(x,y) SET(x,y) GET(x,y)
34![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
35
#define FREE(x) free(x);x=NULL;
36![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
37
class CMemPoolChar:public CMemPool
38![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
39
protected:
40
int _maxsize;
41
int _curpos;
42
public:
43
char * _data;
44
public:
45
SET_GET(int,maxsize);
46
SET_GET(int,curpos);
47
public:
48![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
CMemPoolChar(int size):_maxsize(size),_data(NULL),_curpos(0)
{}
49
~CMemPoolChar()
50![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
if(_data != NULL)
52![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
FREE(_data)
54
}
55
}
56
int Init();
57
int MallocNode(int );
58
int FreeNode(int ,int);
59
};
60![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
61
typedef char *(CMemPoolChar::*mempointer);
62![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
63
typedef struct _MEM_STRUCT_
64![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
65
int _value;
66
int _flag;
67
_MEM_STRUCT_ * prev;
68
_MEM_STRUCT_ * next;
69
}MEMSTRUCT,*LPMEMSTRUCT;
70![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
71
class CSMemPool:public CMemPool
72![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
73
public:
74
LPMEMSTRUCT _data;
75
LPMEMSTRUCT _free;
76
protected:
77
int _maxcell;
78
public:
79
SET_GET(int,maxcell);
80![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
87
#endif
88![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
1
#include "mempool.h"
2![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
3
int CMemPoolChar::Init()
4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
5
int i_ret = -1;
6
if(Get_maxsize() <= 0)
7![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
return i_ret;
9
}
10
i_ret = -2;
11
_data = (char *)malloc(sizeof(char)*Get_maxsize());
12
if(_data == NULL)
13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
return i_ret;
15
}
16
i_ret = 0;
17
memset(_data,0,Get_maxsize());
18
return i_ret;
19
}
20![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
21
int CMemPoolChar::MallocNode(int len)
22![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
23
int i_ret = -1;
24
if(Get_curpos() + len >= Get_maxsize())
25![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
34
int CMemPoolChar::FreeNode(int data,int len)
35![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
45![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
46
CSMemPool::~CSMemPool()
47![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
48
if(_data != NULL)
49![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
FREE(_data);
51
}
52
_free = NULL;
53
Set_maxcell(0);
54
}
55![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
56
int CSMemPool::Init()
57![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
58
int i_ret = 0;
59
if(Get_maxcell() <= 0)
60![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
Set_maxcell(10);
62
}
63
_data = (LPMEMSTRUCT)malloc(sizeof(MEMSTRUCT)*Get_maxcell());
64
if(_data == NULL)
65![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
return i_ret;
67
}
68
memset(_data,0,sizeof(MEMSTRUCT)*Get_maxcell());
69![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
70
i_ret = -2;
71
_free = (LPMEMSTRUCT)malloc(sizeof(MEMSTRUCT));
72
if(_data == NULL)
73![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
return i_ret;
75
}
76
memset(_free,0,sizeof(MEMSTRUCT));
77![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
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![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
95![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
96![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
97![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
98
int CSMemPool::MallocNode(int invalid)
99![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
100
if(_free->next == NULL)
101![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
102![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return /**//*(LPMEMSTRUCT)NULL*/0;
103
}
104![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
105
//Last Node
106
LPMEMSTRUCT p = _free->next->prev;
107
if(p == _free->next)
108![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109
_free->next = _free->prev = NULL;
110
return (int)p;
111
}
112![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
113
p->prev->next = _free->next;
114
_free->next->prev = p->prev;
115
return (int)p;
116
}
117![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
118![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
119![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int CSMemPool::FreeNode(/**//*LPMEMSTRUCT data*/int datapos,int invalid)
120![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
121
if(datapos <= 0)
122![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
return 0;
124
}
125
LPMEMSTRUCT data = (LPMEMSTRUCT)datapos;
126![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
127
memset(data,0,sizeof(MEMSTRUCT));
128
if(_free->next != NULL)
129![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
138
_free->next = data;
139
data->next = data;
140
data->prev = data;
141
}
142
}
143![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
144![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*test*/
145
int main(int argc,char * argv[])
146![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
为了减少系统申请,释放内存的开销(有时申请释放频繁,可能会导致系统异常)和程序的有效运行;
系统初始化时,将需要申请的内存统一申请下来,如果程序中需要申请空间,可以在预留空间中申请,如果空间已满,可以通过写文件或其它方式处理。
posted on 2009-07-13 10:48
Super- 阅读(2633)
评论(6) 编辑 收藏 引用