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