-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryManagerClass.cpp
401 lines (378 loc) · 9.94 KB
/
MemoryManagerClass.cpp
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
#include "MemoryManagerClass.h"
#pragma warning(disable:4996)
MemoryManagerClass::MemoryManagerClass(unsigned int SegmentSize, bool AutomaticReleaseSegment, bool InitializeMems)
{
InitVariables();
_UseSelfMemoryManagement = true;
_Automatic_Release_Segment = AutomaticReleaseSegment;
_Max_Segment_Count = 100;
_Segment_Size = SegmentSize;
__int64 t1 = TimerClass::GetTime_in_MiliSec();
_Mem = (void**)malloc(sizeof(void*)*_Max_Segment_Count);
if (!_Mem)
{
WriteErrorToLogFile("MemManager Cant Alloc Memory Segments");
}
memset(_Mem, 0, sizeof(void*)*_Max_Segment_Count);
_Mem[_Current_Segment] = malloc(_Segment_Size);
if (!_Mem[_Current_Segment])
{
WriteErrorToLogFile("MemManager Cant Alloc Memory Segment 1");
}
memset(_Mem[_Current_Segment], 0, _Segment_Size);
__int64 t2 = TimerClass::GetTime_in_MiliSec();
__int64 t3 = t2 - t1;
_Total_Allocation_Time += t3;
if (_Automatic_Release_Segment)
{
vector<TMemoryArea> List;
_In_Use_Memory_Locations.push_back(List);
}
_InitializeMems = InitializeMems;
}
MemoryManagerClass::MemoryManagerClass(bool InitializeMems)
{
InitVariables();
_UseSelfMemoryManagement = false;
_Automatic_Release_Segment = false;
_Max_Segment_Count = 0;
_Segment_Size = 0;
_InitializeMems = InitializeMems;
}
void MemoryManagerClass::InitVariables()
{
sprintf(LogFileName, "%s", "MemManagerLog.txt");
_Total_Allocation_Time = 0;
_Current_Segment = 0;
_Start = 0;
_Mem = NULL;
_Total_Allocation_Size = 0;
_Allocation_Count = 0;
_Release_Count = 0;
_UnReleased_Count = 0;
_Max_Allocation_Size = 0;
}
void* MemoryManagerClass::AllocVoid(int ElementNumber, int ElementSize)
{
void* temp = MyAlloc(ElementSize*ElementNumber, ElementSize);
return temp;
}
short* MemoryManagerClass::AllocShort(int ElementNumber)
{
int ElementSize = sizeof(short);
short* temp = (short*)MyAlloc(sizeof(short)*ElementNumber, ElementSize);
return temp;
}
char* MemoryManagerClass::AllocChar(int ElementNumber)
{
int ElementSize = sizeof(char);
char* temp = (char*)MyAlloc(sizeof(char)*ElementNumber, ElementSize);
return temp;
}
int* MemoryManagerClass::AllocInt(int ElementNumber)
{
int ElementSize = sizeof(int);
int* temp = (int*)MyAlloc(sizeof(int)*ElementNumber, ElementSize);
return temp;
}
double* MemoryManagerClass::AllocDouble(int ElementNumber)
{
int ElementSize = sizeof(double);
double* temp = (double*)MyAlloc(sizeof(double)*ElementNumber, ElementSize);
return temp;
}
float* MemoryManagerClass::AllocFloat(int ElementNumber)
{
int ElementSize = sizeof(float);
float* temp = (float*)MyAlloc(sizeof(float)*ElementNumber, ElementSize);
return temp;
}
Complex64* MemoryManagerClass::AllocComplex64(int ElementNumber)
{
int ElementSize = sizeof(Complex64);
Complex64* temp = (Complex64*)MyAlloc(sizeof(Complex64)*ElementNumber, ElementSize);
return temp;
}
Complex32* MemoryManagerClass::AllocComplex32(int ElementNumber)
{
int ElementSize = sizeof(Complex32);
Complex32* temp = (Complex32*)MyAlloc(sizeof(Complex32)*ElementNumber, ElementSize);
return temp;
}
ComplexInt* MemoryManagerClass::AllocComplexInt(int ElementNumber)
{
int ElementSize = sizeof(ComplexInt);
ComplexInt* temp = (ComplexInt*)MyAlloc(sizeof(ComplexInt)*ElementNumber, ElementSize);
return temp;
}
ComplexShort* MemoryManagerClass::AllocComplexShort(int ElementNumber)
{
int ElementSize = sizeof(ComplexShort);
ComplexShort* temp = (ComplexShort*)MyAlloc(sizeof(ComplexShort)*ElementNumber, ElementSize);
return temp;
}
void MemoryManagerClass::FREEMEM(void** MemAdd)
{
__int64 t1 = TimerClass::GetTime_in_MiliSec();
if (!_UseSelfMemoryManagement)
{
if (*MemAdd)
{
//delete[] *_Mem;
free(*MemAdd);
*MemAdd = NULL;
}
}
else
{
if (_Automatic_Release_Segment)
{
RemoveMemAreaFromList(*MemAdd);
}
}
_Release_Count++;
_UnReleased_Count--;
__int64 t2 = TimerClass::GetTime_in_MiliSec();
__int64 t3 = t2 - t1;
_Total_Allocation_Time += t3;
}
void MemoryManagerClass::Distroy()
{
if (_Mem)
{
for (int i = 0; i <= _Current_Segment; i++)
{
FreeSegment(i);
}
free(_Mem);
_Mem = NULL;
}
_In_Use_Memory_Locations.clear();
_Total_Allocation_Time = 0;
_Total_Allocation_Size = 0;
_Start = 0;
_Segment_Size = 0;
}
__int64 MemoryManagerClass::GetTotalAllocationTime()
{
return _Total_Allocation_Time;
}
__int64 MemoryManagerClass::GetTotalAllocationSize()
{
return _Total_Allocation_Size;
}
void MemoryManagerClass::ResetManager(int KeepSegmentCount )
{
if (KeepSegmentCount-1>_Current_Segment)
{
KeepSegmentCount = _Current_Segment;
}
for (int i = KeepSegmentCount; i <= _Current_Segment; i++)
{
FreeSegment(i);
}
_Current_Segment = 0;
if (_UseSelfMemoryManagement == true && _InitializeMems == true)
{
memset(_Mem[0], 0, _Segment_Size);
}
if (_Automatic_Release_Segment)
{
_In_Use_Memory_Locations[0].clear();
}
_Total_Allocation_Time = 0;
_Start = 0;
_Total_Allocation_Size = 0;
_Allocation_Count = 0;
_Release_Count = 0;
_UnReleased_Count = 0;
_Max_Allocation_Size = 0;
}
int MemoryManagerClass::GetAllocationCount()
{
return _Allocation_Count;
}
int MemoryManagerClass::GetReleaseCount()
{
return _Release_Count;
}
int MemoryManagerClass::GetUnReleaseCount()
{
return _UnReleased_Count;
}
int MemoryManagerClass::GetMaxAllocationSize()
{
return _Max_Allocation_Size;
}
void* MemoryManagerClass::AssignAddressFromIntrnalMem(unsigned int ByteSize, int ElementSize)
{
while (_Start%ElementSize != 0)
{
_Start++;// move forward _Start for alignment issues
// move forward until the address being multiply of elementsize
}
void* temp = (char *)(_Mem[_Current_Segment]) + _Start;
_Total_Allocation_Size += ByteSize;
if (_Automatic_Release_Segment)
{
TMemoryArea MA;
MA.Start = _Start;
MA.Length = ByteSize;
MA.Address = temp;
_In_Use_Memory_Locations[_Current_Segment].push_back(MA);
}
_Start += ByteSize;
return temp;
}
void MemoryManagerClass::RemoveMemAreaFromList(void* MemAddress)
{
for (int i = 0; i <= _Current_Segment; i++)
{
int size = _In_Use_Memory_Locations[i].size();
for (int j = 0; j<size; j++)
{
if (_In_Use_Memory_Locations[i][j].Address == MemAddress)
{
_In_Use_Memory_Locations[i].erase(_In_Use_Memory_Locations[i].begin() + j);
if (j == 0 && i != _Current_Segment)
{
FreeSegment(i);
}
break;
}
}
}
}
void* MemoryManagerClass::MyAlloc(unsigned int ByteSize, int ElementSize)
{
if (ByteSize > _Max_Allocation_Size)
{
_Max_Allocation_Size = ByteSize;
}
__int64 t1 = TimerClass::GetTime_in_MiliSec();
void* temp = NULL;
if (_UseSelfMemoryManagement)
{
int remain_space_byte = (int)_Segment_Size - (int)_Start;
if (remain_space_byte>ByteSize && remain_space_byte>0)
{
temp = AssignAddressFromIntrnalMem(ByteSize, ElementSize);
}
else
{
int SegmentSizetmp = _Segment_Size;
//go to next segment
if (ByteSize>_Segment_Size)
{
SegmentSizetmp = ByteSize;
}
if (++_Current_Segment<_Max_Segment_Count)
{
_Mem[_Current_Segment] = malloc(SegmentSizetmp);// alloc next segment
if (!_Mem[_Current_Segment])
{
WriteErrorToLogFile("MemManager Cant Alloc Memory Next Segment");
}
memset(_Mem[_Current_Segment], 0, SegmentSizetmp);
_Start = 0;
if (_Automatic_Release_Segment)
{
vector<TMemoryArea> List;
_In_Use_Memory_Locations.push_back(List);
}
if (SegmentSizetmp - _Start >= ByteSize)
{
temp = AssignAddressFromIntrnalMem(ByteSize, ElementSize);
}
else
{
// Byte size is greater than _Segment_Size
return NULL;
}
}
else
{
// reached to max segment count
return NULL;
}
}
}
else
{
temp = malloc(ByteSize);
if (!temp)
{
WriteErrorToLogFile("MemManager Cant Alloc Memory");
}
if (_InitializeMems)
{
memset(temp, 0, ByteSize);
}
_Total_Allocation_Size += ByteSize;
}
_Allocation_Count++;
_UnReleased_Count++;
__int64 t2 = TimerClass::GetTime_in_MiliSec();
__int64 t3 = t2 - t1;
_Total_Allocation_Time += t3;
return temp;
}
void MemoryManagerClass::FreeSegment(int SegmentIndex)
{
__int64 t1 = TimerClass::GetTime_in_MiliSec();
if (_Mem[SegmentIndex])
{
free(_Mem[SegmentIndex]);
_Mem[SegmentIndex] = NULL;
}
if (_Automatic_Release_Segment)
{
_In_Use_Memory_Locations[SegmentIndex].clear();
}
__int64 t2 = TimerClass::GetTime_in_MiliSec();
__int64 t3 = t2 - t1;
_Total_Allocation_Time += t3;
}
void MemoryManagerClass::WriteErrorToLogFile(char* txt)
{
// I comment this function because it's instruction is incompatible with gcc compiler
//time_t now = time(0);
//// char* dt = ctime(&now);
//char dt[26];
//ctime_s(dt, sizeof(dt), &now);
//char month[5] = { 0 };
//char daystr[5] = { 0 };
//int day = 0;
//int hour = 0;
//int min = 0;
//int sec = 0;
//int year = 0;
////sscanf(dt, "%s %s %d %d:%d:%d %d", daystr, month, &day, &hour, &min, &sec, &year);
//sscanf_s(dt, "%s %s %d %d:%d:%d %d", daystr, month, &day, &hour, &min, &sec, &year);
//char timestamp[200] = { 0 };
//// sprintf(timestamp, "%d_%s_%d___%d_%d_%d", day, month, year, hour, min, sec);
//sprintf_s(timestamp, "%d_%s_%d___%d_%d_%d", day, month, year, hour, min, sec);
///*String^ now=DateTime::Now.ToString();
//for(int i = 0;i<now->Length;i++)
//{
//nows += now[i];
//}*/
//char etxt[500] = { 0 };
//// sprintf(etxt, "%s %s", txt, timestamp);
//sprintf_s(etxt, "%s %s", txt, timestamp);
//// FILE* logfile = fopen(LogFileName, "at");
//FILE* logfile;
//errno_t err = fopen_s(&logfile, LogFileName, "at");
//if (logfile)
//{
// //fprintf(logfile, "%s\n", etxt);
// fprintf_s(logfile, "%s\n", etxt);
// fclose(logfile);
//}
/*FILE* f=fopen(LogFileName,"at");
if(f)
{
fprintf(f,"\n%s","MemManager Cant Alloc Memory Segments");
fclose(f);
}*/
}