-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.h
More file actions
163 lines (130 loc) · 3.09 KB
/
alloc.h
File metadata and controls
163 lines (130 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once
#ifndef MEMORY_POOL_H
#define MEMORY_POOL_H
#include <memory.h>
#include <stdlib.h>
#include <windows.h>
#include "alloc_list.h"
namespace asr
{
class lock
{
public:
lock(bool threads):_threads(threads)
{
if(_threads)
{
InitializeCriticalSection(&cs);
EnterCriticalSection(&cs);
}
}
~lock()
{
if(_threads)
LeaveCriticalSection(&cs);
}
bool _threads;
CRITICAL_SECTION cs;
};
template <bool threads = 1, size_t BlockNum = 5, size_t BlockSize = 4096>
class MemoryPool
{
private:
CMemoryList* pFreeList;
CMemoryList* pUsedList;
public:
static MemoryPool& getInstance()
{
static MemoryPool<> mp;
return mp;
}
~MemoryPool()
{
if(pFreeList != NULL)
{
pFreeList->printList();
delete pFreeList;
}
if(pUsedList != NULL)
{
pUsedList->printList();
delete pUsedList;
}
}
template<typename T>
T* alloc(size_t n = 1)
{
int nBytes = sizeof(T) * n;
cout<<"分配内存大小为 "<<nBytes<<" 个字节"<<endl;
T* ret = NULL;
lock lock_t(threads);
if(pFreeList->getNodeNum() == 0)
throw exception("NO free memory left");
else
{
//找出第一个大于nBytes的node节点
node* pNode = pFreeList->findNode(nBytes);
cout<<"使用节点"<<pNode<<endl;
if(pNode != NULL)
{
cout<<"将节点 "<<pNode<<" 加入UsedList"<<endl;
pUsedList->appendNode(pNode);
ret = (T*) pNode->pData;
}
else
{
//将第一个结点也即freelist中最大的节点更改大小拿来使用
node* pNode = pFreeList->removeHead();
if(pNode == NULL)
throw exception("NO free memory left");
cout<<"更改节点"<<pNode<<"的大小"<<endl;
pFreeList->resizeNode(pNode, nBytes);
cout<<"节点"<<pNode<<"的大小为"<<pNode->nBytes<<endl;
pUsedList->appendNode(pNode);
ret = (T*) pNode->pData;
}
}
pFreeList->printList();
pUsedList->printList();
return ret;
}
template<typename T>
void dealloc(T* p)
{
void* pd = (void*) p;
pFreeList->printList();
pUsedList->printList();
lock lock_t(threads);
node* pNode = pUsedList->removeNode(pd);
pFreeList->printList();
pUsedList->printList();
cout<<"释放"<<pNode<<"加入到FreeList"<<endl;
if(pNode == NULL)
throw exception("Not find");
pFreeList->printList();
pUsedList->printList();
pFreeList->insertNode(pNode);
pFreeList->printList();
pUsedList->printList();
return;
}
private:
// POD means plain old data
//void fill_n_aux(pointer p, size_type n, const_reference val, __true_type);
//void fill_n_aux(pointer p, size_type n, const_reference val, __false_type);
MemoryPool()
{
pFreeList = new CMemoryList(BlockNum, BlockSize);
pUsedList = new CMemoryList;
pFreeList->printList();
pUsedList->printList();
}
private:
const MemoryPool& operator=(const MemoryPool& c);
bool operator==(const MemoryPool& c) const;
bool operator!=(const MemoryPool& c) const;
MemoryPool(const MemoryPool& memoryPool) throw();
//template <class U> MemoryPool(const MemoryPool<U>& memoryPool);
};
}
#endif