-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanager.cpp
More file actions
426 lines (335 loc) · 8.95 KB
/
Copy pathmanager.cpp
File metadata and controls
426 lines (335 loc) · 8.95 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include<atomic>
#include<exception>
#include"manager.h"
namespace MyTool
{
struct ComMsg
{
uint posi;
int cut;
};
static MVector<MemoryHeadMsg> *mhm_list;
static MVector<MemoryMsgEx> *mme_free_list;
static MVector<MemoryMsgEx> *mme_ufree_list;
static std::atomic_char mhm_list_flag = 1;
static std::atomic_char mme_free_list_flag = 1;
static std::atomic_char mme_ufree_list_flag = 1;
bool initflag = false;
std::atomic_bool quit_flag=false;
#define LOCK(arg) do{arg##_flag--;while(arg##_flag<0){};}while(0);
#define ULOCK(arg) do{arg##_flag++;}while(0);
bool str_equal(const char* str1, const char*str2)
{
if (str1 == str2)
return true;
uint len1 = strlen(str1);
uint len2 = strlen(str2);
if (len1 != len2)
return false;
else if (str1 == nullptr || str2 == nullptr||len1==0)
return false;
for (int i = 0; i < len1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
int M_find_id(const char* name)
{
if (quit_flag)
return -1;
LOCK(mhm_list);
for (int i = 0; i < mhm_list->Length(); i++)
{
if (str_equal(mhm_list->operator[](i).mhm_name, name))
{
ULOCK(mhm_list);
return i;
}
}
ULOCK(mhm_list);
return -1;
}
int M_regis_struct(const char* name, uint szof, ulong count)
{
if (quit_flag)
{
return -1;
}
if (M_find_id(name) != -1)
{
return -1;
}
LOCK(mhm_list)LOCK(mme_free_list)LOCK(mme_ufree_list);
MemoryHeadMsg msg;
msg.mhm_add_count = count;
msg.mhm_szof = szof;
msg.mhm_now_count = count;
strcpy(msg.mhm_name, name);
//注册头
msg.mhm_id = mhm_list->Length();
bit64 tlen = (bit64)szof * count;
byte* temp = nullptr;
try
{
temp = new byte[tlen];
}
catch(std::exception a)
{
return -1;
}
msg.mhm_addr = new MVector<byte*>(10);
msg.mhm_addr->Push_back(temp);
mhm_list->Push_back(msg);
//注册相应的空间碎片信息
MemoryMsgEx mme;
mme.mhm_id = msg.mhm_id;
mme.mme_msgs = new MVector<MemoryMsg>(count);
MemoryMsg mm;
mm.count = msg.mhm_now_count;
mm.mm_addr = temp;
mme.mme_msgs->Push_back(mm);
mme_free_list->Push_back(mme);
//注册相应的未释放内存信息
mme.mhm_id = msg.mhm_id;
mme.mme_msgs= new MVector<MemoryMsg>(count);
mme_ufree_list->Push_back(mme);
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
return msg.mhm_id;
}
byte* M_Malloc(uint id, ulong count)
{
if (quit_flag)
{
return nullptr;
}
LOCK(mhm_list)LOCK(mme_free_list)LOCK(mme_ufree_list);
if (id >= mme_free_list->Length())
{
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
return nullptr;
}
MemoryMsgEx &mme = mme_free_list->operator[](id);//获取该类型空间碎片表
byte* result=nullptr;
ComMsg cm;
ComMsg temp = { -1,-1 };
cm.posi = 0;
cm.cut = mme.mme_msgs->operator[](0).count - count;
for (int i = 1; i < mme.mme_msgs->Length(); i++)
{
if (cm.cut == 0)
break;
MemoryMsg &mm = mme.mme_msgs->operator[](i);
temp.posi = i;
temp.cut = mm.count - count;
if (temp.cut == 0)
{
cm.cut = 0;
cm.posi = i;
break;
}
else if (temp.cut > 0)
{
if (cm.cut < 0)
memcpy(&cm, &temp, sizeof(ComMsg));
else
if(temp.cut<cm.cut)
memcpy(&cm, &temp, sizeof(ComMsg));
}
}
// 如果空间碎片不足长度,向系统申请新的空间
if (cm.cut < 0)
{
MemoryHeadMsg &mhm = mhm_list->operator[](id);
ulong newlen = mhm.mhm_add_count*mhm.mhm_szof;
ulong adlen = newlen;
while (newlen < count*mhm.mhm_szof)
{
newlen += adlen;
}
byte *temp = new byte[newlen];
mhm.mhm_addr->Push_back(temp);
result = temp;
//添加新的空间碎片
MemoryMsg newmm;
newmm.mm_addr = temp + count*mhm.mhm_szof+1;
newmm.count = newlen/mhm.mhm_szof-count;
if(newmm.count)
mme_free_list->operator[](id).mme_msgs->Push_back(newmm);
}
else if (cm.cut == 0)
{
result = mme.mme_msgs->operator[](cm.posi).mm_addr;
//删除碎片信息
mme.mme_msgs->Erase(cm.posi);
}
else
{
result = mme.mme_msgs->operator[](cm.posi).mm_addr;
//修改碎片信息
MemoryMsg&mm = mme.mme_msgs->operator[](cm.posi);
MemoryHeadMsg &mhm = mhm_list->operator[](id);
mm.mm_addr = mm.mm_addr + count*mhm.mhm_szof+1;
mm.count = cm.cut;
}
//result = cm.cut < 0 ? result : mme.mme_msgs->operator[](cm.posi).mm_addr;
//加入当前活跃内存信息
MemoryMsg umm;
umm.count = count;
umm.mm_addr = result;
mme_ufree_list->operator[](id).mme_msgs->Push_back(umm);
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
return result;
}
MemoryMsgEx* find_msg(byte* ptr,int *posi)
{
if (quit_flag)
return nullptr;
LOCK(mhm_list)LOCK(mme_free_list)LOCK(mme_ufree_list);
for (int i = 0; i < mme_ufree_list->Length(); i++)
{
MemoryMsgEx& msg = mme_ufree_list->operator[](i);
for (int j = 0; j < msg.mme_msgs->Length(); j++)
{
MemoryMsg &mm = msg.mme_msgs->operator[](j);
if (mm.mm_addr == ptr)
{
*posi = j;
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
return &msg;
}
}
}
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
*posi = -1;
return nullptr;
}
void M_free(byte* ptr)
{
if (quit_flag)
return;
int posi;
MemoryMsgEx* msgptr = find_msg(ptr, &posi);
LOCK(mhm_list)LOCK(mme_free_list)LOCK(mme_ufree_list);
if (msgptr == nullptr)
{
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
return;
}
MemoryMsg &mm=msgptr->mme_msgs->operator[](posi);
//加入到空间碎片信息 或者 合并碎片空间信息
MemoryMsgEx&mex = mme_free_list->operator[](msgptr->mhm_id);
uint szof = mhm_list->operator[](msgptr->mhm_id).mhm_szof;
bool flag = false;
//判断是否需要整合碎片
for (int i = 0; i < mex.mme_msgs->Length(); i++)
{
MemoryMsg &temp = mex.mme_msgs->operator[](i);
if (temp.mm_addr == (mm.mm_addr + (mm.count*szof))+1)//头尾相接
{
temp.mm_addr = mm.mm_addr;
temp.count += mm.count;
flag = true;
break;
}
else if ((temp.mm_addr + (temp.count*szof))+1 == mm.mm_addr)//尾头相接
{
temp.count += mm.count;
flag = true;
break;
}
}
if (!flag)//不存在整合碎片的情况
{
mex.mme_msgs->Push_back(mm);
}
//从动态的内存信息表移除
bool f=mme_ufree_list->operator[](msgptr->mhm_id).mme_msgs->Erase(posi);
int i = 0;
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
}
void M_get_msg(byte* ptr,MemoryMsgDetailUnit&msg)
{
if (quit_flag)
return;
LOCK(mhm_list)LOCK(mme_free_list)LOCK(mme_ufree_list);
for (int i = 0; i < mme_ufree_list->Length(); i++)
{
MEMORYMSGEX &mex = mme_ufree_list->operator[](i);
for (int j = 0; j < mex.mme_msgs->Length(); j++)
{
MemoryMsg &mm = mex.mme_msgs->operator[](j);
byte* end = mm.mm_addr + mhm_list->operator[](mex.mhm_id).mhm_szof*mm.count;
if (ptr >= mm.mm_addr&&ptr <= end)
{
memcpy(msg.name, mhm_list->operator[](mex.mhm_id).mhm_name, M_MAX_NAME_SIZE);
msg.szof = mhm_list->operator[](mex.mhm_id).mhm_szof;
msg.posi = (ptr - mm.mm_addr) / 4;
msg.count = mm.count;
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
return;
}
}
}
msg.posi = -1;
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
}
void M_quit()
{
if (quit_flag)
return;
LOCK(mhm_list)LOCK(mme_free_list)LOCK(mme_ufree_list);
//删除总空间
for (int i = 0; i < mhm_list->Length(); i++)
{
MemoryHeadMsg &mhm = mhm_list->operator[](i);
for (int j = 0; j < mhm.mhm_addr->Length(); j++)
{
byte* &temp = mhm.mhm_addr->operator[](j);
if (temp)
delete[]temp;
temp = nullptr;
}
delete mhm.mhm_addr;
}
//释放碎片信息
for (int i = 0; i < mme_free_list->Length(); i++)
{
MemoryMsgEx &mme = mme_free_list->operator[](i);
for (int j = 0; j < mme.mme_msgs->Length(); j++)
{
//添加日志信息
}
delete mme.mme_msgs;
}
//释放活跃内存信息
for (int i = 0; i < mme_ufree_list->Length(); i++)
{
MemoryMsgEx &mme = mme_ufree_list->operator[](i);
for (int j = 0; j < mme.mme_msgs->Length(); j++)
{
//添加日志信息
}
delete mme.mme_msgs;
}
delete mhm_list, mhm_list = nullptr;
delete mme_free_list, mme_free_list = nullptr;
delete mme_ufree_list, mme_ufree_list = nullptr;
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
quit_flag = true;
}
void M_init()
{
LOCK(mhm_list)LOCK(mme_free_list)LOCK(mme_ufree_list);
if (!initflag)
{
mhm_list = new MVector<MemoryHeadMsg>();
mme_free_list = new MVector<MemoryMsgEx>();
mme_ufree_list = new MVector<MemoryMsgEx>();
initflag = true;
}
ULOCK(mhm_list)ULOCK(mme_free_list)ULOCK(mme_ufree_list);
}
void M_printf_log()
{
}
}