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