-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathMemManager.cpp
218 lines (165 loc) · 5.23 KB
/
MemManager.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
#include "global.h"
CAllocater::CAllocater(char *strName)
{
//copy the name
strcpy(strAllocName,strName);
nAllocSize=0;
lpMemList=NULL;
}
CAllocater::~CAllocater()
{
}
void CAllocater::SetMemListFree(MEMLIST* lpList)
{
memset(lpList,0,sizeof(MEMLIST)*100);
}
void CAllocater::ResetAllocater()
{
nAllocSize=0;
nAllocNumber=0;
nMemListSize=100;
bFree=true;
lpMemList=(MEMLIST*)malloc(sizeof(MEMLIST)*nMemListSize);
SetMemListFree(lpMemList);
}
void CAllocater::ShowMemUsage()
{
PrintMessage("---Memory usage of %s allocater",strAllocName);
PrintMessage("---Allocated memory %i in bytes",nAllocSize);
PrintMessage("---Allocated number of Memory %i",nAllocNumber);
// PrintMessage("////////////////////////////////////");
PrintMessage("");
}
//Allocate memory and save its address in a list
void* CAllocater::Alloc(size_t nSize,int nMemInit)
{
if(bFree)
bFree=false;
//if we have more alloaction as we can hold in ouer list then grow ouer list
if(nAllocNumber > nMemListSize-1)
{
//grow the list by 100
nMemListSize+=100;
lpMemList=(MEMLIST*)realloc(lpMemList,sizeof(MEMLIST)*nMemListSize);
//set memlist empty
SetMemListFree(&lpMemList[nMemListSize-100]);
}
u32 i;
//search the list for a free place to hold the address
for (i=0;i<nMemListSize;i++)
if(lpMemList[i].nType==FREE) break;
if(i > nMemListSize)
PrintMessage("CAllocater::no free block found");
lpMemList[i].nType=ALLOC;
lpMemList[i].adrress=malloc(nSize);
if(lpMemList[i].adrress==0)
PrintMessage("CAllocater::malloc failed");
memset(lpMemList[i].adrress,0,nSize);
lpMemList[i].nSize=nSize;
nAllocNumber++;
nAllocSize+=nSize;
return lpMemList[i].adrress;
}
//reallocate the given memory block and save the new address in the list
void* CAllocater::Realloc(void *CurMem,size_t nSize)
{
u32 i;
bool bFound=false;
for(i=0;i<nMemListSize;i++)
{
if(CurMem==lpMemList[i].adrress && lpMemList[i].nType == ALLOC )
{
bFound=true;
break;
}
}
if(!bFound)
PrintMessage("Realloc failed %x was never allocated",CurMem);
nAllocSize-=lpMemList[i].nSize;
lpMemList[i].adrress=realloc(lpMemList[i].adrress,nSize);
lpMemList[i].nSize=nSize;
nAllocSize+=nSize;
return lpMemList[i].adrress;
}
//seacrh the list for the allocated block and free it from memory
void CAllocater::Free(void *CurMem)
{
u32 i;
bool bFound=false;
for(i=0;i<nMemListSize;i++)
{
if(CurMem==lpMemList[i].adrress && lpMemList[i].nType == ALLOC )
{
bFound=true;
break;
}
}
if(!bFound)
PrintMessage("free failed %x was never allocated",CurMem);
free(lpMemList[i].adrress);
lpMemList[i].adrress=0;
lpMemList[i].nType=FREE;
nAllocSize-=lpMemList[i].nSize;
nAllocNumber--;
}
//Free all allocated memory
void CAllocater::FreeAllocater()
{
if(!bFree)
{
for(u32 i=0;i<nMemListSize;i++)
{
if(lpMemList[i].nType==ALLOC) Free(lpMemList[i].adrress);
}
}
// PrintMessage("CAllocater::%s unfreed %i bytes",strAllocName,nAllocSize);
//clear ouer memlist
if(lpMemList!=NULL){
free(lpMemList);
}
}
//the MemManager with the included allocaters
CMemManager::CMemManager()
{
}
CMemManager::~CMemManager()
{
}
void CMemManager::InitManager()
{
lpAlloc[MAINMENU] =new CAllocater("Main Menu");
lpAlloc[STAGE] =new CAllocater("Stage");
lpAlloc[ENGINE] =new CAllocater("Engine");
lpAlloc[P1] =new CAllocater("Player 1");
lpAlloc[P2] =new CAllocater("Player 2");
lpAlloc[P3] =new CAllocater("Player 3");
lpAlloc[P4] =new CAllocater("Player 4");
PrintMessage("Init Memory Manager");
}
CAllocater* CMemManager::GetAllocater(int nAllocater)
{
return lpAlloc[nAllocater];
}
//free all the allocaters from mem
void CMemManager::FreeManager()
{
for(int i=0;i<7;i++)
{
lpAlloc[i]->FreeAllocater();
delete lpAlloc[i];
}
}
//show and return the memusage
size_t CMemManager::GetMemUsage()
{
size_t nMem=0;
PrintMessage("======================Memory Manager============================");
PrintMessage("|Name: |Allocated bytes |");
PrintMessage("----------------------------------------------------------------");
for(int i=0;i<7;i++)
{
PrintMessage("|%20s |%20i | ",lpAlloc[i]->GetName(),lpAlloc[i]->GetMemUsage());
}
PrintMessage("================================================================");
return nMem;
}