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