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