annotate Allocator.cpp @ 191:4813b57396ba

spell init in progress
author Gloval
date Sat, 16 Feb 2013 02:26:01 +0400
parents 9c0607679772
children 2bc1c8d68f41
rev   line source
0
Ritor1
parents:
diff changeset
1 #include <string.h>
Ritor1
parents:
diff changeset
2 #include <stdio.h>
Ritor1
parents:
diff changeset
3 #include "OSAPI.h"
Ritor1
parents:
diff changeset
4
Ritor1
parents:
diff changeset
5 #include "Allocator.h"
Ritor1
parents:
diff changeset
6
Ritor1
parents:
diff changeset
7 #include "mm7_data.h"
Ritor1
parents:
diff changeset
8
Ritor1
parents:
diff changeset
9
Ritor1
parents:
diff changeset
10
Ritor1
parents:
diff changeset
11 Allocator *pAllocator; // idb
Ritor1
parents:
diff changeset
12
Ritor1
parents:
diff changeset
13
Ritor1
parents:
diff changeset
14
Ritor1
parents:
diff changeset
15
Ritor1
parents:
diff changeset
16
Ritor1
parents:
diff changeset
17 void __fastcall CheckMemoryAccessibility(void *pBase, DWORD uSize);
Ritor1
parents:
diff changeset
18
Ritor1
parents:
diff changeset
19
Ritor1
parents:
diff changeset
20
Ritor1
parents:
diff changeset
21 //----- (00426755) --------------------------------------------------------
Ritor1
parents:
diff changeset
22 void *Allocator::AllocNamedChunk(const void *pPrevPtrValue, unsigned int uSize, const char *pName)
Ritor1
parents:
diff changeset
23 {
Ritor1
parents:
diff changeset
24 //Allocator *v4; // esi@1
Ritor1
parents:
diff changeset
25 unsigned int v5; // eax@7
Ritor1
parents:
diff changeset
26 void *result; // eax@8
Ritor1
parents:
diff changeset
27 unsigned int *pNumBuffersUsed; // ebx@12
Ritor1
parents:
diff changeset
28 unsigned int v8; // edi@15
Ritor1
parents:
diff changeset
29 char v9; // zf@15
Ritor1
parents:
diff changeset
30 int v10; // eax@16
Ritor1
parents:
diff changeset
31 void **v11; // ebx@19
Ritor1
parents:
diff changeset
32 void *v12; // eax@22
Ritor1
parents:
diff changeset
33 unsigned int Size; // [sp+14h] [bp+Ch]@16
Ritor1
parents:
diff changeset
34
Ritor1
parents:
diff changeset
35 //v4 = this;
Ritor1
parents:
diff changeset
36 if ( pPrevPtrValue && !aborting_app )
Ritor1
parents:
diff changeset
37 AbortWithError();
Ritor1
parents:
diff changeset
38 if ( !bBigBufferAllocated && !aborting_app )
Ritor1
parents:
diff changeset
39 AbortWithError();
Ritor1
parents:
diff changeset
40 v5 = uSize;
Ritor1
parents:
diff changeset
41 if ( uSize )
Ritor1
parents:
diff changeset
42 {
Ritor1
parents:
diff changeset
43 if ( (uSize & 0x80000000u) != 0 && !aborting_app )
Ritor1
parents:
diff changeset
44 {
Ritor1
parents:
diff changeset
45 AbortWithError();
Ritor1
parents:
diff changeset
46 v5 = uSize;
Ritor1
parents:
diff changeset
47 }
Ritor1
parents:
diff changeset
48 pNumBuffersUsed = &uNumBuffersUsed;
Ritor1
parents:
diff changeset
49 if (uNumBuffersUsed == 6000 && !aborting_app)
Ritor1
parents:
diff changeset
50 {
Ritor1
parents:
diff changeset
51 AbortWithError();
Ritor1
parents:
diff changeset
52 v5 = uSize;
Ritor1
parents:
diff changeset
53 }
Ritor1
parents:
diff changeset
54 v8 = *pNumBuffersUsed;
Ritor1
parents:
diff changeset
55 v9 = bUseBigBuffer == 0;
Ritor1
parents:
diff changeset
56 ++*pNumBuffersUsed;
Ritor1
parents:
diff changeset
57 if ( v9 )
Ritor1
parents:
diff changeset
58 {
Ritor1
parents:
diff changeset
59 v12 = malloc(v5);
Ritor1
parents:
diff changeset
60 v11 = &pMemoryBuffers[v8];
Ritor1
parents:
diff changeset
61 *v11 = v12;
Ritor1
parents:
diff changeset
62 if ( v12 )
Ritor1
parents:
diff changeset
63 {
Ritor1
parents:
diff changeset
64 uMemoryBuffersSizes[v8] = uSize;
Ritor1
parents:
diff changeset
65 }
Ritor1
parents:
diff changeset
66 else
Ritor1
parents:
diff changeset
67 {
Ritor1
parents:
diff changeset
68 if ( !aborting_app )
Ritor1
parents:
diff changeset
69 AbortWithError();
Ritor1
parents:
diff changeset
70 }
Ritor1
parents:
diff changeset
71 }
Ritor1
parents:
diff changeset
72 else
Ritor1
parents:
diff changeset
73 {
Ritor1
parents:
diff changeset
74 LOBYTE(v5) = v5 & 0xFC;
Ritor1
parents:
diff changeset
75 v10 = v5 + 4;
Ritor1
parents:
diff changeset
76 Size = v10;
Ritor1
parents:
diff changeset
77 if ( v10 + uNextFreeOffsetInBigBuffer > uBigBufferSizeAligned )
Ritor1
parents:
diff changeset
78 {
Ritor1
parents:
diff changeset
79 printf("Id: %s Size: %i\n", pName, v10);
Ritor1
parents:
diff changeset
80 CreateFileDump("Memory");
Ritor1
parents:
diff changeset
81 if ( !aborting_app )
Ritor1
parents:
diff changeset
82 AbortWithError();
Ritor1
parents:
diff changeset
83 }
Ritor1
parents:
diff changeset
84 v11 = &pMemoryBuffers[v8];
Ritor1
parents:
diff changeset
85 *v11 = (char *)pBigMemoryBuffer + uNextFreeOffsetInBigBuffer;
Ritor1
parents:
diff changeset
86 uMemoryBuffersSizes[v8] = Size;
Ritor1
parents:
diff changeset
87 if (pName)
Ritor1
parents:
diff changeset
88 strncpy(pMemoryBuffersNames[v8], pName, 11);
Ritor1
parents:
diff changeset
89 uNextFreeOffsetInBigBuffer += Size;
Ritor1
parents:
diff changeset
90 }
Ritor1
parents:
diff changeset
91 result = *v11;
Ritor1
parents:
diff changeset
92 }
Ritor1
parents:
diff changeset
93 else
Ritor1
parents:
diff changeset
94 {
Ritor1
parents:
diff changeset
95 result = 0;
Ritor1
parents:
diff changeset
96 }
Ritor1
parents:
diff changeset
97 return result;
Ritor1
parents:
diff changeset
98 }
Ritor1
parents:
diff changeset
99 // 720018: using guessed type int aborting_app;
Ritor1
parents:
diff changeset
100
Ritor1
parents:
diff changeset
101
Ritor1
parents:
diff changeset
102
Ritor1
parents:
diff changeset
103 //----- (004266CD) --------------------------------------------------------
Ritor1
parents:
diff changeset
104 bool Allocator::Initialize(unsigned int uNumKBytes)
Ritor1
parents:
diff changeset
105 {
Ritor1
parents:
diff changeset
106 if (bBigBufferAllocated)
Ritor1
parents:
diff changeset
107 return false;
Ritor1
parents:
diff changeset
108
Ritor1
parents:
diff changeset
109 if (!uNumKBytes)
Ritor1
parents:
diff changeset
110 return false;
Ritor1
parents:
diff changeset
111
Ritor1
parents:
diff changeset
112 pBigMemoryBuffer = malloc(uNumKBytes * 1024);
Ritor1
parents:
diff changeset
113 if (!pBigMemoryBuffer)
Ritor1
parents:
diff changeset
114 return false;
Ritor1
parents:
diff changeset
115
Ritor1
parents:
diff changeset
116 pBigBufferAligned = (char *)pBigMemoryBuffer + -((unsigned __int16)pBigMemoryBuffer & 0xFFF) + 4096;
Ritor1
parents:
diff changeset
117 uBigBufferSizeAligned = (uNumKBytes * 1024) - 4096;
Ritor1
parents:
diff changeset
118
Ritor1
parents:
diff changeset
119 uNumBuffersUsed = 0;
Ritor1
parents:
diff changeset
120 uNextFreeOffsetInBigBuffer = 0;
Ritor1
parents:
diff changeset
121
Ritor1
parents:
diff changeset
122 bUseBigBuffer = true;
Ritor1
parents:
diff changeset
123 return bBigBufferAllocated = true;
Ritor1
parents:
diff changeset
124 }
Ritor1
parents:
diff changeset
125
Ritor1
parents:
diff changeset
126
Ritor1
parents:
diff changeset
127 //----- (00426474) --------------------------------------------------------
Ritor1
parents:
diff changeset
128 void Allocator::FreeChunk(void *ptr)
Ritor1
parents:
diff changeset
129 {
Ritor1
parents:
diff changeset
130 Allocator *v2; // esi@1
Ritor1
parents:
diff changeset
131 unsigned int v3; // eax@5
Ritor1
parents:
diff changeset
132 signed int i; // edi@5
Ritor1
parents:
diff changeset
133 unsigned int v5; // eax@13
Ritor1
parents:
diff changeset
134 signed int v6; // ecx@16
Ritor1
parents:
diff changeset
135 signed int j; // edx@16
Ritor1
parents:
diff changeset
136 char v8; // zf@20
Ritor1
parents:
diff changeset
137
Ritor1
parents:
diff changeset
138 v2 = this;
Ritor1
parents:
diff changeset
139 if ( ptr )
Ritor1
parents:
diff changeset
140 {
Ritor1
parents:
diff changeset
141 if ( !this->bBigBufferAllocated && !aborting_app )
Ritor1
parents:
diff changeset
142 AbortWithError();
Ritor1
parents:
diff changeset
143 v3 = v2->uNumBuffersUsed;
Ritor1
parents:
diff changeset
144 for ( i = 0; i < (signed int)v3; ++i )
Ritor1
parents:
diff changeset
145 {
Ritor1
parents:
diff changeset
146 if ( v2->pMemoryBuffers[i] == ptr )
Ritor1
parents:
diff changeset
147 break;
Ritor1
parents:
diff changeset
148 }
Ritor1
parents:
diff changeset
149 if ( i >= (signed int)v3 && !aborting_app )
Ritor1
parents:
diff changeset
150 AbortWithError();
Ritor1
parents:
diff changeset
151 if ( !v2->bUseBigBuffer )
Ritor1
parents:
diff changeset
152 free(v2->pMemoryBuffers[i]);
Ritor1
parents:
diff changeset
153 v2->pMemoryBuffers[i] = 0;
Ritor1
parents:
diff changeset
154 v2->uMemoryBuffersSizes[i] = 0;
Ritor1
parents:
diff changeset
155 LOBYTE(v2->pMemoryBuffers[3 * i + 12000]) = 0;
Ritor1
parents:
diff changeset
156 v5 = v2->uNumBuffersUsed;
Ritor1
parents:
diff changeset
157 if ( i == v5 - 1 )
Ritor1
parents:
diff changeset
158 {
Ritor1
parents:
diff changeset
159 if ( i )
Ritor1
parents:
diff changeset
160 {
Ritor1
parents:
diff changeset
161 v6 = 0;
Ritor1
parents:
diff changeset
162 for ( j = 0; j < (signed int)v5; ++j )
Ritor1
parents:
diff changeset
163 {
Ritor1
parents:
diff changeset
164 if ( v2->pMemoryBuffers[j] )
Ritor1
parents:
diff changeset
165 v6 = j;
Ritor1
parents:
diff changeset
166 }
Ritor1
parents:
diff changeset
167 v8 = v2->bUseBigBuffer == 0;
Ritor1
parents:
diff changeset
168 v2->uNumBuffersUsed = v6 + 1;
Ritor1
parents:
diff changeset
169 if ( !v8 )
Ritor1
parents:
diff changeset
170 v2->uNextFreeOffsetInBigBuffer = (char *)v2->pMemoryBuffers[v6]
Ritor1
parents:
diff changeset
171 + v2->uMemoryBuffersSizes[v6]
Ritor1
parents:
diff changeset
172 - v2->pBigMemoryBuffer;
Ritor1
parents:
diff changeset
173 }
Ritor1
parents:
diff changeset
174 else
Ritor1
parents:
diff changeset
175 {
Ritor1
parents:
diff changeset
176 v2->uNextFreeOffsetInBigBuffer = 0;
Ritor1
parents:
diff changeset
177 v2->uNumBuffersUsed = 0;
Ritor1
parents:
diff changeset
178 }
Ritor1
parents:
diff changeset
179 }
Ritor1
parents:
diff changeset
180 }
Ritor1
parents:
diff changeset
181 }
Ritor1
parents:
diff changeset
182 // 720018: using guessed type int aborting_app;
Ritor1
parents:
diff changeset
183
Ritor1
parents:
diff changeset
184
Ritor1
parents:
diff changeset
185
Ritor1
parents:
diff changeset
186 //----- (00426429) --------------------------------------------------------
Ritor1
parents:
diff changeset
187 Allocator::Allocator()
Ritor1
parents:
diff changeset
188 {
Ritor1
parents:
diff changeset
189 Allocator *v1; // eax@1
Ritor1
parents:
diff changeset
190 signed int v2; // ecx@1
Ritor1
parents:
diff changeset
191 int v3; // edx@1
Ritor1
parents:
diff changeset
192
Ritor1
parents:
diff changeset
193 v1 = this;
Ritor1
parents:
diff changeset
194 v2 = 0;
Ritor1
parents:
diff changeset
195 v1->uBigBufferSizeAligned = 0;
Ritor1
parents:
diff changeset
196 v1->bBigBufferAllocated = 0;
Ritor1
parents:
diff changeset
197 v3 = (int)v1->pMemoryBuffersNames;
Ritor1
parents:
diff changeset
198 do
Ritor1
parents:
diff changeset
199 {
Ritor1
parents:
diff changeset
200 v1->pMemoryBuffers[v2] = 0;
Ritor1
parents:
diff changeset
201 v1->uMemoryBuffersSizes[v2] = 0;
Ritor1
parents:
diff changeset
202 *(char *)v3 = 0;
Ritor1
parents:
diff changeset
203 ++v2;
Ritor1
parents:
diff changeset
204 v3 += 12;
Ritor1
parents:
diff changeset
205 }
Ritor1
parents:
diff changeset
206 while ( v2 < 6000 );
Ritor1
parents:
diff changeset
207 v1->uDumpsCount = 0;
Ritor1
parents:
diff changeset
208 }
Ritor1
parents:
diff changeset
209
Ritor1
parents:
diff changeset
210
Ritor1
parents:
diff changeset
211 //----- (0042654C) --------------------------------------------------------
Ritor1
parents:
diff changeset
212 bool Allocator::CreateFileDump(const char *a1)
Ritor1
parents:
diff changeset
213 {
Ritor1
parents:
diff changeset
214 Allocator *v2; // esi@1
Ritor1
parents:
diff changeset
215 unsigned int v3; // ST44_4@1
Ritor1
parents:
diff changeset
216 FILE *v4; // eax@1
Ritor1
parents:
diff changeset
217 FILE *v5; // edi@1
Ritor1
parents:
diff changeset
218 bool result; // eax@2
Ritor1
parents:
diff changeset
219 unsigned int v7; // ecx@5
Ritor1
parents:
diff changeset
220 int v8; // edx@5
Ritor1
parents:
diff changeset
221 int v9; // eax@6
Ritor1
parents:
diff changeset
222 int v10; // eax@8
Ritor1
parents:
diff changeset
223 int v11; // ecx@8
Ritor1
parents:
diff changeset
224 signed int v12; // ebx@8
Ritor1
parents:
diff changeset
225 signed int v13; // ebx@10
Ritor1
parents:
diff changeset
226 struct _MEMORYSTATUS memstat; // [sp+8h] [bp-38h]@5
Ritor1
parents:
diff changeset
227 char Filename[20]; // [sp+28h] [bp-18h]@1
Ritor1
parents:
diff changeset
228 unsigned int *v16; // [sp+3Ch] [bp-4h]@11
Ritor1
parents:
diff changeset
229 int a1a; // [sp+48h] [bp+8h]@11
Ritor1
parents:
diff changeset
230
Ritor1
parents:
diff changeset
231 v2 = this;
Ritor1
parents:
diff changeset
232 v3 = this->uDumpsCount;
Ritor1
parents:
diff changeset
233 this->uDumpsCount = v3 + 1;
Ritor1
parents:
diff changeset
234 sprintf(Filename, "Mem%03i.txt", v3);
Ritor1
parents:
diff changeset
235 v4 = fopen(Filename, "w");
Ritor1
parents:
diff changeset
236 v5 = v4;
Ritor1
parents:
diff changeset
237 if ( v4 )
Ritor1
parents:
diff changeset
238 {
Ritor1
parents:
diff changeset
239 if ( a1 )
Ritor1
parents:
diff changeset
240 fprintf(v4, "%s\n\n", a1);
Ritor1
parents:
diff changeset
241 fprintf(v5, "Windows Memory Status");
Ritor1
parents:
diff changeset
242 GlobalMemoryStatus(&memstat);
Ritor1
parents:
diff changeset
243 fprintf(v5, "dwLength: %d\n\n", memstat.dwLength);
Ritor1
parents:
diff changeset
244 fprintf(v5, "dwMemoryLoad: %d\n", memstat.dwMemoryLoad);
Ritor1
parents:
diff changeset
245 fprintf(v5, "dwTotalPhys: %d\n", memstat.dwTotalPhys);
Ritor1
parents:
diff changeset
246 fprintf(v5, "dwAvailPhys: %d\n", memstat.dwAvailPhys);
Ritor1
parents:
diff changeset
247 fprintf(v5, "dwTotalPageFile: %d\n", memstat.dwTotalPageFile);
Ritor1
parents:
diff changeset
248 fprintf(v5, "dwAvailPageFile: %d\n", memstat.dwAvailPageFile);
Ritor1
parents:
diff changeset
249 fprintf(v5, "dwTotalVirtual: %d\n", memstat.dwTotalVirtual);
Ritor1
parents:
diff changeset
250 fprintf(v5, "dwAvailVirtual: %d\n\n\n", memstat.dwAvailVirtual);
Ritor1
parents:
diff changeset
251 v7 = v2->uNumBuffersUsed;
Ritor1
parents:
diff changeset
252 v8 = 0;
Ritor1
parents:
diff changeset
253 if ( (signed int)v7 > 0 )
Ritor1
parents:
diff changeset
254 {
Ritor1
parents:
diff changeset
255 v9 = (int)v2->uMemoryBuffersSizes;
Ritor1
parents:
diff changeset
256 do
Ritor1
parents:
diff changeset
257 {
Ritor1
parents:
diff changeset
258 v8 += *(int *)v9;
Ritor1
parents:
diff changeset
259 v9 += 4;
Ritor1
parents:
diff changeset
260 --v7;
Ritor1
parents:
diff changeset
261 }
Ritor1
parents:
diff changeset
262 while ( v7 );
Ritor1
parents:
diff changeset
263 }
Ritor1
parents:
diff changeset
264 v10 = 0;
Ritor1
parents:
diff changeset
265 v11 = (int)v2->uMemoryBuffersSizes;
Ritor1
parents:
diff changeset
266 v12 = 6000;
Ritor1
parents:
diff changeset
267 do
Ritor1
parents:
diff changeset
268 {
Ritor1
parents:
diff changeset
269 v10 += *(int *)v11;
Ritor1
parents:
diff changeset
270 v11 += 4;
Ritor1
parents:
diff changeset
271 --v12;
Ritor1
parents:
diff changeset
272 }
Ritor1
parents:
diff changeset
273 while ( v12 );
Ritor1
parents:
diff changeset
274 fprintf(v5, "Size: %i %i %i\n\n", v2->uNumBuffersUsed, v8, v10);
Ritor1
parents:
diff changeset
275 v13 = 0;
Ritor1
parents:
diff changeset
276 if ( (signed int)v2->uNumBuffersUsed > 0 )
Ritor1
parents:
diff changeset
277 {
Ritor1
parents:
diff changeset
278 v16 = v2->uMemoryBuffersSizes;
Ritor1
parents:
diff changeset
279 a1a = (int)v2->pMemoryBuffersNames;
Ritor1
parents:
diff changeset
280 do
Ritor1
parents:
diff changeset
281 {
Ritor1
parents:
diff changeset
282 fprintf(v5, "%10i %10i %s\n", v13, *v16, a1a);
Ritor1
parents:
diff changeset
283 a1a += 12;
Ritor1
parents:
diff changeset
284 ++v16;
Ritor1
parents:
diff changeset
285 ++v13;
Ritor1
parents:
diff changeset
286 }
Ritor1
parents:
diff changeset
287 while ( v13 < (signed int)v2->uNumBuffersUsed );
Ritor1
parents:
diff changeset
288 }
Ritor1
parents:
diff changeset
289 fclose(v5);
Ritor1
parents:
diff changeset
290 result = 0;
Ritor1
parents:
diff changeset
291 }
Ritor1
parents:
diff changeset
292 else
Ritor1
parents:
diff changeset
293 {
Ritor1
parents:
diff changeset
294 result = 1;
Ritor1
parents:
diff changeset
295 }
Ritor1
parents:
diff changeset
296 return result;
Ritor1
parents:
diff changeset
297 }
Ritor1
parents:
diff changeset
298
Ritor1
parents:
diff changeset
299
Ritor1
parents:
diff changeset
300
Ritor1
parents:
diff changeset
301
Ritor1
parents:
diff changeset
302
Ritor1
parents:
diff changeset
303
Ritor1
parents:
diff changeset
304
Ritor1
parents:
diff changeset
305
Ritor1
parents:
diff changeset
306 //----- (00426892) --------------------------------------------------------
Ritor1
parents:
diff changeset
307 static void __fastcall CheckMemoryAccessibility(void *pBase, DWORD uSize)
Ritor1
parents:
diff changeset
308 {
Ritor1
parents:
diff changeset
309 DWORD v2; // edi@1
Ritor1
parents:
diff changeset
310 DWORD i; // eax@1
Ritor1
parents:
diff changeset
311 struct _SYSTEM_INFO SystemInfo; // [sp+8h] [bp-24h]@1
Ritor1
parents:
diff changeset
312
Ritor1
parents:
diff changeset
313 v2 = uSize;
Ritor1
parents:
diff changeset
314 GetSystemInfo(&SystemInfo);
Ritor1
parents:
diff changeset
315 for ( i = 16 * SystemInfo.dwPageSize; i < v2; i += SystemInfo.dwPageSize )
Ritor1
parents:
diff changeset
316 ;
Ritor1
parents:
diff changeset
317 }