Mercurial > mm7
annotate Allocator.cpp @ 1548:2ae7139dde11
added a few artifacts to enum ITEM_TYPE
author | Grumpy7 |
---|---|
date | Sat, 07 Sep 2013 08:17:49 +0200 |
parents | 0aeac0b9ca30 |
children | c4ab816fcc5e |
rev | line source |
---|---|
1165
29a8defbad9e
temporary _CRT_SECURE_NO_WARNINGS to remove some warnings
Grumpy7
parents:
1093
diff
changeset
|
1 #ifdef _MSC_VER |
29a8defbad9e
temporary _CRT_SECURE_NO_WARNINGS to remove some warnings
Grumpy7
parents:
1093
diff
changeset
|
2 #define _CRT_SECURE_NO_WARNINGS |
29a8defbad9e
temporary _CRT_SECURE_NO_WARNINGS to remove some warnings
Grumpy7
parents:
1093
diff
changeset
|
3 #endif |
29a8defbad9e
temporary _CRT_SECURE_NO_WARNINGS to remove some warnings
Grumpy7
parents:
1093
diff
changeset
|
4 |
0 | 5 #include <string.h> |
6 #include <stdio.h> | |
7 | |
8 #include "Allocator.h" | |
9 | |
10 #include "mm7_data.h" | |
11 | |
12 | |
13 | |
14 Allocator *pAllocator; // idb | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 void __fastcall CheckMemoryAccessibility(void *pBase, DWORD uSize); | |
21 | |
22 | |
23 | |
24 //----- (00426755) -------------------------------------------------------- | |
25 void *Allocator::AllocNamedChunk(const void *pPrevPtrValue, unsigned int uSize, const char *pName) | |
26 { | |
705 | 27 |
1093 | 28 if ( pPrevPtrValue && !aborting_app ) |
29 AbortWithError(); | |
30 if (!uSize) | |
31 return 0; | |
705 | 32 return malloc(uSize); |
33 /* | |
0 | 34 //Allocator *v4; // esi@1 |
35 unsigned int v5; // eax@7 | |
36 void *result; // eax@8 | |
363 | 37 // unsigned int *pNumBuffersUsed; // ebx@12 |
0 | 38 unsigned int v8; // edi@15 |
39 char v9; // zf@15 | |
40 int v10; // eax@16 | |
41 void **v11; // ebx@19 | |
42 void *v12; // eax@22 | |
43 unsigned int Size; // [sp+14h] [bp+Ch]@16 | |
44 | |
363 | 45 int aligned_size; |
46 | |
47 | |
48 if ( pPrevPtrValue && !aborting_app ) | |
49 AbortWithError(); | |
50 if ( !bBigBufferAllocated && !aborting_app ) | |
51 AbortWithError(); | |
52 | |
53 if (!uSize) | |
54 return 0; | |
55 | |
56 if (((uSize & 0x80000000u) != 0) && !aborting_app) | |
57 AbortWithError(); | |
58 | |
59 if (uNumBuffersUsed == 6000 && !aborting_app) | |
60 AbortWithError(); | |
61 | |
62 ++uNumBuffersUsed; | |
63 | |
64 if (!bUseBigBuffer) | |
65 { | |
66 pMemoryBuffers[uNumBuffersUsed] = malloc(uSize); | |
67 if (pMemoryBuffers[uNumBuffersUsed]) | |
68 uMemoryBuffersSizes[uNumBuffersUsed] = uSize; | |
69 else | |
70 { | |
71 if ( !aborting_app ) | |
72 AbortWithError(); | |
73 } | |
74 } | |
75 else | |
76 { | |
77 | |
78 aligned_size =(uSize&0xFFFFFFFC) + 4; | |
79 | |
80 if ((uNextFreeOffsetInBigBuffer +aligned_size) > uBigBufferSizeAligned) | |
81 { | |
82 printf("Id: %s Size: %i", pName, aligned_size); | |
83 CreateFileDump( "Memory"); | |
84 if ( !aborting_app ) | |
85 AbortWithError(); | |
86 } | |
87 | |
697 | 88 pMemoryBuffers[uNumBuffersUsed] = (char *)pBigMemoryBuffer + uNextFreeOffsetInBigBuffer;//(char *)(pBigBufferAligned) + uNextFreeOffsetInBigBuffer; |
363 | 89 uMemoryBuffersSizes[uNumBuffersUsed] =aligned_size; |
90 | |
91 if (pName) | |
92 strncpy(pMemoryBuffersNames[uNumBuffersUsed], pName, 11); | |
93 uNextFreeOffsetInBigBuffer += aligned_size; | |
94 } | |
95 return pMemoryBuffers[uNumBuffersUsed]; | |
705 | 96 */ |
363 | 97 /* |
0 | 98 //v4 = this; |
99 if ( pPrevPtrValue && !aborting_app ) | |
100 AbortWithError(); | |
101 if ( !bBigBufferAllocated && !aborting_app ) | |
102 AbortWithError(); | |
103 v5 = uSize; | |
104 if ( uSize ) | |
105 { | |
106 if ( (uSize & 0x80000000u) != 0 && !aborting_app ) | |
107 { | |
108 AbortWithError(); | |
109 v5 = uSize; | |
110 } | |
111 pNumBuffersUsed = &uNumBuffersUsed; | |
112 if (uNumBuffersUsed == 6000 && !aborting_app) | |
113 { | |
114 AbortWithError(); | |
115 v5 = uSize; | |
116 } | |
117 v8 = *pNumBuffersUsed; | |
118 v9 = bUseBigBuffer == 0; | |
119 ++*pNumBuffersUsed; | |
120 if ( v9 ) | |
121 { | |
122 v12 = malloc(v5); | |
123 v11 = &pMemoryBuffers[v8]; | |
124 *v11 = v12; | |
125 if ( v12 ) | |
126 { | |
127 uMemoryBuffersSizes[v8] = uSize; | |
128 } | |
129 else | |
130 { | |
131 if ( !aborting_app ) | |
132 AbortWithError(); | |
133 } | |
134 } | |
135 else | |
136 { | |
137 LOBYTE(v5) = v5 & 0xFC; | |
138 v10 = v5 + 4; | |
139 Size = v10; | |
140 if ( v10 + uNextFreeOffsetInBigBuffer > uBigBufferSizeAligned ) | |
141 { | |
142 printf("Id: %s Size: %i\n", pName, v10); | |
143 CreateFileDump("Memory"); | |
144 if ( !aborting_app ) | |
145 AbortWithError(); | |
146 } | |
147 v11 = &pMemoryBuffers[v8]; | |
148 *v11 = (char *)pBigMemoryBuffer + uNextFreeOffsetInBigBuffer; | |
149 uMemoryBuffersSizes[v8] = Size; | |
150 if (pName) | |
151 strncpy(pMemoryBuffersNames[v8], pName, 11); | |
152 uNextFreeOffsetInBigBuffer += Size; | |
153 } | |
154 result = *v11; | |
155 } | |
156 else | |
157 { | |
158 result = 0; | |
159 } | |
363 | 160 return result;*/ |
0 | 161 } |
162 // 720018: using guessed type int aborting_app; | |
163 | |
164 | |
165 | |
166 //----- (004266CD) -------------------------------------------------------- | |
167 bool Allocator::Initialize(unsigned int uNumKBytes) | |
168 { | |
169 if (bBigBufferAllocated) | |
170 return false; | |
171 | |
172 if (!uNumKBytes) | |
173 return false; | |
174 | |
175 pBigMemoryBuffer = malloc(uNumKBytes * 1024); | |
176 if (!pBigMemoryBuffer) | |
177 return false; | |
178 | |
363 | 179 pBigBufferAligned = (void *)((unsigned int)(pBigMemoryBuffer) & 0xFFFF0000 + 4096); |
180 //(char *)pBigMemoryBuffer + -((unsigned __int16)pBigMemoryBuffer & 0xFFF) + 4096; | |
0 | 181 uBigBufferSizeAligned = (uNumKBytes * 1024) - 4096; |
182 | |
183 uNumBuffersUsed = 0; | |
184 uNextFreeOffsetInBigBuffer = 0; | |
185 | |
186 bUseBigBuffer = true; | |
187 return bBigBufferAllocated = true; | |
188 } | |
189 | |
190 | |
191 //----- (00426474) -------------------------------------------------------- | |
192 void Allocator::FreeChunk(void *ptr) | |
193 { | |
705 | 194 free(ptr); |
195 return; | |
196 /* Allocator *v2; // esi@1 | |
0 | 197 unsigned int v3; // eax@5 |
198 signed int i; // edi@5 | |
199 unsigned int v5; // eax@13 | |
200 signed int v6; // ecx@16 | |
201 signed int j; // edx@16 | |
202 char v8; // zf@20 | |
203 | |
363 | 204 |
205 | |
206 unsigned int uBuffindx; | |
207 unsigned int indx; | |
208 | |
209 if (ptr) | |
210 { | |
211 if ((!bBigBufferAllocated)&& (!aborting_app )) | |
212 AbortWithError(); | |
213 for (indx = 0; indx <=uNumBuffersUsed; ++indx) | |
214 if (pMemoryBuffers[indx] == ptr) | |
215 break; | |
702 | 216 //if ((indx>uNumBuffersUsed)&& !aborting_app ) |
217 //AbortWithError(); //to detect memory problems - uncomment | |
363 | 218 if (!bUseBigBuffer) |
219 free(pMemoryBuffers[indx]); | |
220 pMemoryBuffers[indx] = NULL; | |
221 uMemoryBuffersSizes[indx] = 0; | |
222 pMemoryBuffersNames[indx][0] = 0; | |
223 if (indx == (uNumBuffersUsed-1)) | |
224 { | |
225 if (indx>0) | |
226 { | |
227 uBuffindx = 0; | |
228 for (j = 0; j < uNumBuffersUsed-1; ++j) | |
229 { | |
230 if (pMemoryBuffers[j]) | |
231 uBuffindx = j; | |
232 } | |
233 uNumBuffersUsed = uBuffindx + 1; | |
234 if (bUseBigBuffer) | |
235 uNextFreeOffsetInBigBuffer = | |
236 ((long)((char*)pMemoryBuffers[uBuffindx] -(char*)pBigBufferAligned)+ uMemoryBuffersSizes[uBuffindx]); | |
237 } | |
238 else | |
239 { | |
240 uNumBuffersUsed = 0; | |
241 uNextFreeOffsetInBigBuffer = 0; | |
242 } | |
243 } | |
244 } | |
705 | 245 */ |
363 | 246 /* |
0 | 247 v2 = this; |
248 if ( ptr ) | |
249 { | |
250 if ( !this->bBigBufferAllocated && !aborting_app ) | |
251 AbortWithError(); | |
252 v3 = v2->uNumBuffersUsed; | |
253 for ( i = 0; i < (signed int)v3; ++i ) | |
254 { | |
255 if ( v2->pMemoryBuffers[i] == ptr ) | |
256 break; | |
257 } | |
258 if ( i >= (signed int)v3 && !aborting_app ) | |
259 AbortWithError(); | |
260 if ( !v2->bUseBigBuffer ) | |
261 free(v2->pMemoryBuffers[i]); | |
262 v2->pMemoryBuffers[i] = 0; | |
263 v2->uMemoryBuffersSizes[i] = 0; | |
264 LOBYTE(v2->pMemoryBuffers[3 * i + 12000]) = 0; | |
265 v5 = v2->uNumBuffersUsed; | |
266 if ( i == v5 - 1 ) | |
267 { | |
268 if ( i ) | |
269 { | |
270 v6 = 0; | |
271 for ( j = 0; j < (signed int)v5; ++j ) | |
272 { | |
273 if ( v2->pMemoryBuffers[j] ) | |
274 v6 = j; | |
275 } | |
276 v8 = v2->bUseBigBuffer == 0; | |
277 v2->uNumBuffersUsed = v6 + 1; | |
278 if ( !v8 ) | |
279 v2->uNextFreeOffsetInBigBuffer = (char *)v2->pMemoryBuffers[v6] | |
280 + v2->uMemoryBuffersSizes[v6] | |
281 - v2->pBigMemoryBuffer; | |
282 } | |
283 else | |
284 { | |
285 v2->uNextFreeOffsetInBigBuffer = 0; | |
286 v2->uNumBuffersUsed = 0; | |
287 } | |
288 } | |
363 | 289 }*/ |
0 | 290 } |
291 // 720018: using guessed type int aborting_app; | |
292 | |
293 | |
294 | |
295 //----- (00426429) -------------------------------------------------------- | |
296 Allocator::Allocator() | |
297 { | |
298 Allocator *v1; // eax@1 | |
299 signed int v2; // ecx@1 | |
300 int v3; // edx@1 | |
301 | |
302 v1 = this; | |
303 v2 = 0; | |
304 v1->uBigBufferSizeAligned = 0; | |
305 v1->bBigBufferAllocated = 0; | |
306 v3 = (int)v1->pMemoryBuffersNames; | |
307 do | |
308 { | |
309 v1->pMemoryBuffers[v2] = 0; | |
310 v1->uMemoryBuffersSizes[v2] = 0; | |
311 *(char *)v3 = 0; | |
312 ++v2; | |
313 v3 += 12; | |
314 } | |
315 while ( v2 < 6000 ); | |
316 v1->uDumpsCount = 0; | |
317 } | |
318 | |
319 | |
320 //----- (0042654C) -------------------------------------------------------- | |
321 bool Allocator::CreateFileDump(const char *a1) | |
322 { | |
323 Allocator *v2; // esi@1 | |
324 unsigned int v3; // ST44_4@1 | |
325 FILE *v4; // eax@1 | |
326 FILE *v5; // edi@1 | |
327 bool result; // eax@2 | |
328 unsigned int v7; // ecx@5 | |
329 int v8; // edx@5 | |
330 int v9; // eax@6 | |
331 int v10; // eax@8 | |
332 int v11; // ecx@8 | |
333 signed int v12; // ebx@8 | |
334 signed int v13; // ebx@10 | |
335 struct _MEMORYSTATUS memstat; // [sp+8h] [bp-38h]@5 | |
336 char Filename[20]; // [sp+28h] [bp-18h]@1 | |
337 unsigned int *v16; // [sp+3Ch] [bp-4h]@11 | |
338 int a1a; // [sp+48h] [bp+8h]@11 | |
339 | |
340 v2 = this; | |
341 v3 = this->uDumpsCount; | |
342 this->uDumpsCount = v3 + 1; | |
343 sprintf(Filename, "Mem%03i.txt", v3); | |
344 v4 = fopen(Filename, "w"); | |
345 v5 = v4; | |
346 if ( v4 ) | |
347 { | |
348 if ( a1 ) | |
349 fprintf(v4, "%s\n\n", a1); | |
350 fprintf(v5, "Windows Memory Status"); | |
351 GlobalMemoryStatus(&memstat); | |
352 fprintf(v5, "dwLength: %d\n\n", memstat.dwLength); | |
353 fprintf(v5, "dwMemoryLoad: %d\n", memstat.dwMemoryLoad); | |
354 fprintf(v5, "dwTotalPhys: %d\n", memstat.dwTotalPhys); | |
355 fprintf(v5, "dwAvailPhys: %d\n", memstat.dwAvailPhys); | |
356 fprintf(v5, "dwTotalPageFile: %d\n", memstat.dwTotalPageFile); | |
357 fprintf(v5, "dwAvailPageFile: %d\n", memstat.dwAvailPageFile); | |
358 fprintf(v5, "dwTotalVirtual: %d\n", memstat.dwTotalVirtual); | |
359 fprintf(v5, "dwAvailVirtual: %d\n\n\n", memstat.dwAvailVirtual); | |
360 v7 = v2->uNumBuffersUsed; | |
361 v8 = 0; | |
362 if ( (signed int)v7 > 0 ) | |
363 { | |
364 v9 = (int)v2->uMemoryBuffersSizes; | |
365 do | |
366 { | |
367 v8 += *(int *)v9; | |
368 v9 += 4; | |
369 --v7; | |
370 } | |
371 while ( v7 ); | |
372 } | |
373 v10 = 0; | |
374 v11 = (int)v2->uMemoryBuffersSizes; | |
375 v12 = 6000; | |
376 do | |
377 { | |
378 v10 += *(int *)v11; | |
379 v11 += 4; | |
380 --v12; | |
381 } | |
382 while ( v12 ); | |
383 fprintf(v5, "Size: %i %i %i\n\n", v2->uNumBuffersUsed, v8, v10); | |
384 v13 = 0; | |
385 if ( (signed int)v2->uNumBuffersUsed > 0 ) | |
386 { | |
387 v16 = v2->uMemoryBuffersSizes; | |
388 a1a = (int)v2->pMemoryBuffersNames; | |
389 do | |
390 { | |
391 fprintf(v5, "%10i %10i %s\n", v13, *v16, a1a); | |
392 a1a += 12; | |
393 ++v16; | |
394 ++v13; | |
395 } | |
396 while ( v13 < (signed int)v2->uNumBuffersUsed ); | |
397 } | |
398 fclose(v5); | |
399 result = 0; | |
400 } | |
401 else | |
402 { | |
403 result = 1; | |
404 } | |
405 return result; | |
406 } | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | |
413 | |
414 | |
415 //----- (00426892) -------------------------------------------------------- | |
416 static void __fastcall CheckMemoryAccessibility(void *pBase, DWORD uSize) | |
417 { | |
418 DWORD v2; // edi@1 | |
419 DWORD i; // eax@1 | |
420 struct _SYSTEM_INFO SystemInfo; // [sp+8h] [bp-24h]@1 | |
421 | |
422 v2 = uSize; | |
423 GetSystemInfo(&SystemInfo); | |
424 for ( i = 16 * SystemInfo.dwPageSize; i < v2; i += SystemInfo.dwPageSize ) | |
425 ; | |
426 } |