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- 阅读(2671)
评论(6) 编辑 收藏 引用