-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAddressTable.hpp
301 lines (271 loc) · 7.78 KB
/
AddressTable.hpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright (C) Tue Jun 25 12:44:21 2013 XiaoWei
*
* handsomexiaowei@gmail.com
*/
#ifndef _ADDRESSTABLE_H_
#define _ADDRESSTABLE_H_
#include <vector>
#include <list>
#include <windows.h>
using namespace std;
template <class T>
class AddressTable
{
public:
typedef struct _space_
{
ULONG_PTR begin;
ULONG_PTR end;
T data;
_space_():begin(0),end(0) { memset(&data,0,sizeof(data)); }
~_space_(){}
}space;
struct space_sort_cmp
{
bool operator () (const typename AddressTable<T>::space &a, const typename AddressTable<T>::space &b)
{
return a.begin < b.begin;
}
};
AddressTable(long _base,unsigned long _size,bool _sign)
:base(_base),size(_size),alignment(_size),sign(_sign)
{
}
virtual ~AddressTable()
{
}
ULONG_PTR base;
unsigned long size;
unsigned long alignment;
bool sign; //正增长还是负增长
list <space> addr_used; //已经使用的空间
ULONG_PTR assign_address(unsigned long _size);
ULONG_PTR assign_address(unsigned long _size,const T &_data);
ULONG_PTR assign_address(unsigned long _size,const T &_data,bool _sign);
void set_sign(bool _sign) { sign = _sign; }
bool get_sign() { return sign; }
void addr_used_sort()
{
addr_used.sort(AddressTable<T>::space_sort_cmp());
}
};
typedef struct _useful_space
{
ULONG_PTR begin;
ULONG_PTR end;
}useful_space;
template <class T>
ULONG_PTR AddressTable<T>::assign_address(unsigned long _size,const T & _data,bool _sign)
{
addr_used_sort();
sign = _sign;
vector <useful_space> useful_vec;
useful_space t;
t.begin = base;
for (typename list<AddressTable<T>::space>::iterator iter = addr_used.begin(); iter != addr_used.end(); ++iter)
{
t.end = 0;
if (t.begin != iter->begin)
{
t.end = iter->begin;
useful_vec.push_back(t);
}
t.begin = iter->end;
}
t.end = base + size;
if (t.begin != t.end)
useful_vec.push_back(t); //筛选出没有使用的空间
long count = 0; //符合要求的有多少个
for (vector<useful_space>::iterator iter = useful_vec.begin(); iter != useful_vec.end(); ++iter)
{
//if (iter->begin - iter->end >= _size)
if (iter->end - iter->begin >= _size)
{
//printf("空闲地址开始:%x\r\n",iter->begin);
//printf("空闲地址结束:%x\r\n",iter->end);
//printf("符合要求的空间:%x-%x,大小为:%d\r\n",iter->begin,iter->end,iter->end-iter->begin);
count++;
}
}
if (!count)
{
size += alignment;
//return assign_address(_size);
return assign_address(_size,_data,_sign);
}
long r = rand()%count; //如果有多个就随机抽取一个
count = 0;
for (vector<useful_space>::iterator iter = useful_vec.begin(); iter != useful_vec.end(); ++iter)
{
if (iter->end - iter->begin >= _size)
{
if (count == r)
{
space p;
////注释掉这句就只会在某些概率下随机了
//不能和0取模
int space_rand = 0;
if (iter->end - iter->begin - _size)
{
space_rand = rand() % (iter->end - iter->begin - _size);
}
p.begin = space_rand + iter->begin;
////
//p.begin = iter->begin;
//p.end = iter->begin + _size;//iter->end;
p.end = p.begin + _size;
p.data = _data;
addr_used.push_back(p);
if (sign)
return p.begin;// iter->begin;
else
return p.end;
}
count++;
}
}
}
template <class T>
ULONG_PTR AddressTable<T>::assign_address(unsigned long _size,const T & _data)
{
addr_used_sort();
vector <useful_space> useful_vec;
useful_space t;
t.begin = base;
for (typename list<AddressTable<T>::space>::iterator iter = addr_used.begin(); iter != addr_used.end(); ++iter)
{
t.end = 0;
if (t.begin != iter->begin)
{
t.end = iter->begin;
useful_vec.push_back(t);
}
t.begin = iter->end;
}
t.end = base + size;
if (t.begin != t.end)
useful_vec.push_back(t); //筛选出没有使用的空间
long count = 0; //符合要求的有多少个
for (vector<useful_space>::iterator iter = useful_vec.begin(); iter != useful_vec.end(); ++iter)
{
//if (iter->begin - iter->end >= _size)
if (iter->end - iter->begin >= _size)
{
//printf("符合要求的空间:%x-%x,大小为:%d\r\n",iter->begin,iter->end,iter->end-iter->begin);
//printf("空闲地址开始:%x\r\n",iter->begin);
//printf("空闲地址结束:%x\r\n",iter->end);
count++;
}
}
if (!count)
{
size += alignment;
//return assign_address(_size);
return assign_address(_size,_data);
}
long r = rand()%count; //如果有多个就随机抽取一个
count = 0;
for (vector<useful_space>::iterator iter = useful_vec.begin(); iter != useful_vec.end(); ++iter)
{
if (iter->end - iter->begin >= _size)
{
if (count == r)
{
space p;
////注释掉这句就只会在某些概率下随机了
//不能和0取模
unsigned long space_rand = 0;
if (iter->end - iter->begin - _size)
{
space_rand = rand() % (unsigned long)(iter->end - iter->begin - _size);
}
p.begin = space_rand + iter->begin;
////
//p.begin = iter->begin;
//p.end = iter->begin + _size;//iter->end;
p.end = p.begin + _size;
p.data = _data;
addr_used.push_back(p);
if (get_sign())
return p.begin;
else
return p.end;
}
count++;
}
}
return 0;
}
template <class T>
ULONG_PTR AddressTable<T>::assign_address(unsigned long _size)
{
addr_used_sort();
vector <useful_space> useful_vec;
useful_space t;
t.begin = base;
for (typename list<AddressTable<T>::space>::iterator iter = addr_used.begin();
iter != addr_used.end();
++iter)
{
t.end = 0;
if (t.begin != iter->begin)
{
t.end = iter->begin;
useful_vec.push_back(t);
}
t.begin = iter->end;
}
t.end = base + size;
if (t.begin != t.end)
useful_vec.push_back(t); //筛选出没有使用的空间
long count = 0; //符合要求的有多少个
for (vector<useful_space>::iterator iter = useful_vec.begin();
iter != useful_vec.end();
++iter)
{
if (iter->end - iter->begin >= _size)
{
printf("符合要求的空间:%x-%x,大小为:%d\n",iter->begin,iter->end,iter->end-iter->begin);
count++;
}
}
if (!count)
{
size += alignment;
return assign_address(_size);
}
long r = rand()%count; //如果有多个就随机抽取一个
count = 0;
for (vector<useful_space>::iterator iter = useful_vec.begin();
iter != useful_vec.end();
++iter)
{
if (iter->end - iter->begin >= _size)
{
if (count == r)
{
space p;
////注释掉这句就只会在某些概率下随机了
//不能和0取模
int space_rand = 0;
if (iter->end - iter->begin - _size)
{
space_rand = rand() % (int)(iter->end - iter->begin - _size);
}
p.begin = space_rand + iter->begin;
p.end = p.begin + _size;
//memset(&(p.data),0,sizeof(p.data));//清空数据
//printf("%s\naddress:%x","正增长" ,&p);
addr_used.push_back(p);
if (get_sign())
return p.begin;
else
return p.end;
}
count++;
}
}
return 0;
}
#endif /* _ADDRESSTABLE_H_ */