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