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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

⼀个简易内存池(C++)做这个内存池主要是为了完成⼀道⾯试题,题⽬在代码中。

代码
1 #include <iostream>
2 #include<string>
3 #include <list>
4using namespace std;
5
6//⼀个简单的内存池,池中保存3块内存分别为1k,2k,4k
7//实现池⼦的malloc(size)和free(void*)操作
8//不考虑跨块申请内存情况
9
10class node
11 {
12public:
13int offset;//相对于起始地址偏移
14bool flag;//是有效地址还是已经分配地址
15int len;
16 node()
17 {
18 offset=0;
19 flag=true;
20 len=0;
21 }
22 node(int sz)
23 {
24 offset=0;
25 flag=true;
26 len=sz;
27 }
28 };
29
30class memPool
31 {
32public:
33 memPool()
34 {
35 m_First_Count= 1024;
36 m_Second_Count = 2*1024;
37 m_Third_Count = 4*1024;
38 m_First_Address = new char[m_First_Count];
39 m_Second_Address = new char[m_Second_Count];
40 m_Third_Address = new char[m_Third_Count];
41
42 first.insert(first.begin(),new node(1024));
43 second.insert(second.begin(),new node(1024*2));
44 third.insert(third.begin(),new node(1024*4));
45 }
46 ~memPool()
47 {
48delete[]m_First_Address;
49delete[]m_Second_Address;
50delete[]m_Third_Address;
51 m_First_Address = NULL;
52 m_Second_Address = NULL;
53 m_Third_Address = NULL;
54
55 }
56char* myMalloc(const int memSize)
57 {
58//采⽤⾸次适应算法
59 list<node*>::iterator it;
60if (memSize <= m_First_Count)
61 {
62 it = first.begin();
63while(it!=first.end())
64 {
65if (((*it)->len)>= memSize &&(*it)->flag == true)
66 {
67 (*it)->flag = false;
68int temp = (*it)->len;
69 (*it)->len = memSize;
70if (temp - memSize >0)
71 {
72 node *obj = new node;
73 obj->flag=true;
74 obj->len = temp - memSize;
75 obj->offset = (*it)->offset + memSize;
76 it++;
77 first.insert(it,obj);
78 it--;
79 it--;
80 m_First_Count-=memSize;
81 cout << "malloc "<< memSize << " in first memery"<<endl;
82return m_First_Address+(*it)->offset;
83 }
84 }
85 it++;
86 }
87
88 }
89
90if (memSize <= m_Second_Count)
91 {
92 it = second.begin();
93while(it!=second.end())
94 {
95if (((*it)->len)>= memSize&&(*it)->flag == true)
96 {
97 (*it)->flag = false;
98int temp = (*it)->len;
99 (*it)->len = memSize;
100if (temp - memSize >0)
101 {
102 node *obj = new node;
103 obj->flag=true;
104 obj->len = temp - memSize;
105 obj->offset = (*it)->offset + memSize;
106 it++;
107 second.insert(it,obj);
108 it--;
109 it--;
110 m_Second_Count-=memSize;
111 cout << "malloc "<< memSize << "in second memery"<<endl; 112return m_Second_Address+(*it)->offset;
113 }
114 }
115 it++;
116 }
117
118 }
119
120if (memSize <= m_Third_Count)
121 {
122 it = third.begin();
123while(it!=third.end())
124 {
125if (((*it)->len)>= memSize&&(*it)->flag == true)
126 {
127 (*it)->flag = false;
128int temp = (*it)->len;
129 (*it)->len = memSize;
130if (temp - memSize >0)
131 {
132 node *obj=new node;
133 obj->flag=true;
134 obj->len = temp - memSize;
135 obj->offset = (*it)->offset + memSize;
136 it++;
137 third.insert(it,obj);
138 it--;
139 it--;
140 m_Third_Count-=memSize;
141 cout << "malloc "<< memSize << "in third memery"<<endl;
142return m_Third_Address+(*it)->offset;
143 }
144 }
145 it++;
146 }
147
148 }
149
150 cout<<"no memery\n";
151return NULL;
152
153 }
154/************************************************************************/
155/* 算法思路是第⼀步定位这个指针位于哪⼀个内存块,第⼆步在对应内存块中找到*/ 156/*其node,然后判断node前后是否都为有效内存,也就是没有被利⽤的内存块*/ 157/************************************************************************/
158void memFree(void* address_arg)
159 {
160char *freeAddress= static_cast<char*>(address_arg);
161int offset;
162 list<node*>::iterator it;
163if (freeAddress >= m_First_Address && freeAddress < (m_First_Address+1024))//位于第⼀块
164 {
165 offset = freeAddress - m_First_Address;
166 it = first.begin();
167
168while(it!= first.end())//定位offset
169 {
170if (offset == (*it)->offset)
171break;
172
173 it++;
174 }
175if (it == first.end())//没有找到offset
176 {
177 cout << "In first memery,there is no memery of freeaddress"<<endl;
178return;
179 }
180
181if((*it)->flag == true)//找到offset,但是这块内存没有分配
182 {
183 cout << "In first memery,the freeAddress is valid,you can't free it"<<endl;
184return;
185 }
186 (*it)->flag = true;
187
188int len = (*it)->len;
189int count=0;
190if (it!=first.end())//判断it后继节点是否被分配
191 {
192 it++;
193if ((*it)->flag == true)
194 {
195 len+=(*it)->len;
196 count++;
197 }
198 it--;
199 }
200if (it!=first.begin())
201 {
202 it--;
203if ((*it)->flag == true)
204 {
205 len+=(*it)->len;
206 count++;
207 }
208else
209 it++;
210 }
211 (*it)->len = len;
212 it++;
213while(count--)
214 {
215 it = first.erase(it);//erase返回删除节点的后继节点
216
217 }
218
219 cout << "free success"<<endl;
220return;
221
222 }
223else if (freeAddress >= m_Second_Address && freeAddress < (m_Second_Address+1024*2))//位于第⼆块224 {
225 offset = freeAddress - m_Second_Address;
226 it = second.begin();
227
228while(it!= second.end())//定位offset
229 {
230if (offset == (*it)->offset)
231break;
232
233 it++;
234 }
235if (it == second.end())//没有找到offset
236 {
237 cout << "In second memery,there is no memery of freeaddress"<<endl;
238return;
239 }
240
241if((*it)->flag == true)//找到offset,但是这块内存没有分配
242 {
243 cout << "In second memery,the freeAddress is valid,you can't free it"<<endl;
244return;
245 }
246 (*it)->flag = true;
247
248int len = (*it)->len;
249int count=0;
250if (it!=second.end())//判断it后继节点是否被分配
251 {
252 it++;
253if ((*it)->flag == true)
254 {
255 len+=(*it)->len;
256 count++;
257 }
258 it--;
259 }
260if (it!=second.begin())
261 {
262 it--;
263if ((*it)->flag == true)
264 {
265 len+=(*it)->len;
266 count++;
267 }
268else
269 it++;
270 }
271 (*it)->len = len;
272 it++;
273while(count--)
274 {
275 it = second.erase(it);
276 }
277
278 cout << "free success"<<endl;
279return;
280 }
281else if (freeAddress >= m_Third_Address && freeAddress < (m_Third_Address+1024*4))//位于第三块282 {
283 offset = freeAddress - m_Third_Address;
284 it = third.begin();
285
286while(it!= third.end())//定位offset
287 {
288if (offset == (*it)->offset)
289break;
290
291 it++;
292 }
293if (it == third.end())//没有找到offset
294 {
295 cout << "In third memery,there is no memery of freeaddress"<<endl;
296return;
297 }
298
299if((*it)->flag == true)//找到offset,但是这块内存没有分配
300 {
301 cout << "In third memery,the freeAddress is valid,you can't free it"<<endl;
302return;
303 }
304 (*it)->flag = true;
305
306int len = (*it)->len;
307int count=0;
308if (it!=third.end())//判断it后继节点是否被分配
309 {
310 it++;
311if ((*it)->flag == true)
312 {
313 len+=(*it)->len;
314 count++;
315 }
316 it--;
317 }
318if (it!=third.begin())
319 {
320 it--;
321if ((*it)->flag == true)
322 {
323 len+=(*it)->len;
324 count++;
325 }
326else
327 it++;
328 }
329 (*it)->len = len;
330 it++;
331while(count--)
332 {
333 it = third.erase(it);
334 }
335
336 cout << "free success"<<endl;
337return;
338 }
339else
340 {
341 cout << "the memery to be freed is invalid\n";
342 }
343return;
344 }
345private:
346char *m_First_Address;//每⼀块内存起始地址
347char *m_Second_Address;
348char *m_Third_Address;
349int m_First_Count;//剩余有效地址⼤⼩,不⼀定是连续,是第⼀块内存中所有有效之和350int m_Second_Count;
351int m_Third_Count;
352 list<node*> first;
353 list<node*> second;
354 list<node*> third;
355
356 };
357int main()
358 {
359 memPool obj;
360char *ptr1 = obj.myMalloc(10);
361char *ptr2 = obj.myMalloc(100);
362char *ptr3 = obj.myMalloc(500);
363char *ptr4 = obj.myMalloc(4*1024+1);
364 obj.memFree(ptr2);
365 obj.memFree(ptr3);
366
367return0;
368 }。

相关文档
最新文档