Mercurial > might-and-magic-trilogy
view Allocator.cpp @ 83:2d5d13376e91
Merge
author | Nomad |
---|---|
date | Mon, 29 Oct 2012 00:49:33 +0200 |
parents | a84fb3752681 |
children |
line wrap: on
line source
#include <string.h> #include <stdio.h> #include "OSAPI.h" #include "Allocator.h" #include "mm7_data.h" Allocator *pAllocator; // idb void __fastcall CheckMemoryAccessibility(void *pBase, DWORD uSize); //----- (00426755) -------------------------------------------------------- void *Allocator::AllocNamedChunk(const void *pPrevPtrValue, unsigned int uSize, const char *pName) { //Allocator *v4; // esi@1 unsigned int v5; // eax@7 void *result; // eax@8 unsigned int *pNumBuffersUsed; // ebx@12 unsigned int v8; // edi@15 char v9; // zf@15 int v10; // eax@16 void **v11; // ebx@19 void *v12; // eax@22 unsigned int Size; // [sp+14h] [bp+Ch]@16 //v4 = this; if ( pPrevPtrValue && !aborting_app ) AbortWithError(); if ( !bBigBufferAllocated && !aborting_app ) AbortWithError(); v5 = uSize; if ( uSize ) { if ( (uSize & 0x80000000u) != 0 && !aborting_app ) { AbortWithError(); v5 = uSize; } pNumBuffersUsed = &uNumBuffersUsed; if (uNumBuffersUsed == 6000 && !aborting_app) { AbortWithError(); v5 = uSize; } v8 = *pNumBuffersUsed; v9 = bUseBigBuffer == 0; ++*pNumBuffersUsed; if ( v9 ) { v12 = malloc(v5); v11 = &pMemoryBuffers[v8]; *v11 = v12; if ( v12 ) { uMemoryBuffersSizes[v8] = uSize; } else { if ( !aborting_app ) AbortWithError(); } } else { LOBYTE(v5) = v5 & 0xFC; v10 = v5 + 4; Size = v10; if ( v10 + uNextFreeOffsetInBigBuffer > uBigBufferSizeAligned ) { printf("Id: %s Size: %i\n", pName, v10); CreateFileDump("Memory"); if ( !aborting_app ) AbortWithError(); } v11 = &pMemoryBuffers[v8]; *v11 = (char *)pBigMemoryBuffer + uNextFreeOffsetInBigBuffer; uMemoryBuffersSizes[v8] = Size; if (pName) strncpy(pMemoryBuffersNames[v8], pName, 11); uNextFreeOffsetInBigBuffer += Size; } result = *v11; } else { result = 0; } return result; } // 720018: using guessed type int aborting_app; //----- (004266CD) -------------------------------------------------------- bool Allocator::Initialize(unsigned int uNumKBytes) { if (bBigBufferAllocated) return false; if (!uNumKBytes) return false; pBigMemoryBuffer = malloc(uNumKBytes * 1024); if (!pBigMemoryBuffer) return false; pBigBufferAligned = (char *)pBigMemoryBuffer + -((unsigned __int16)pBigMemoryBuffer & 0xFFF) + 4096; uBigBufferSizeAligned = (uNumKBytes * 1024) - 4096; uNumBuffersUsed = 0; uNextFreeOffsetInBigBuffer = 0; bUseBigBuffer = true; return bBigBufferAllocated = true; } //----- (00426474) -------------------------------------------------------- void Allocator::FreeChunk(void *ptr) { Allocator *v2; // esi@1 unsigned int v3; // eax@5 signed int i; // edi@5 unsigned int v5; // eax@13 signed int v6; // ecx@16 signed int j; // edx@16 char v8; // zf@20 v2 = this; if ( ptr ) { if ( !this->bBigBufferAllocated && !aborting_app ) AbortWithError(); v3 = v2->uNumBuffersUsed; for ( i = 0; i < (signed int)v3; ++i ) { if ( v2->pMemoryBuffers[i] == ptr ) break; } if ( i >= (signed int)v3 && !aborting_app ) AbortWithError(); if ( !v2->bUseBigBuffer ) free(v2->pMemoryBuffers[i]); v2->pMemoryBuffers[i] = 0; v2->uMemoryBuffersSizes[i] = 0; LOBYTE(v2->pMemoryBuffers[3 * i + 12000]) = 0; v5 = v2->uNumBuffersUsed; if ( i == v5 - 1 ) { if ( i ) { v6 = 0; for ( j = 0; j < (signed int)v5; ++j ) { if ( v2->pMemoryBuffers[j] ) v6 = j; } v8 = v2->bUseBigBuffer == 0; v2->uNumBuffersUsed = v6 + 1; if ( !v8 ) v2->uNextFreeOffsetInBigBuffer = (char *)v2->pMemoryBuffers[v6] + v2->uMemoryBuffersSizes[v6] - v2->pBigMemoryBuffer; } else { v2->uNextFreeOffsetInBigBuffer = 0; v2->uNumBuffersUsed = 0; } } } } // 720018: using guessed type int aborting_app; //----- (00426429) -------------------------------------------------------- Allocator::Allocator() { Allocator *v1; // eax@1 signed int v2; // ecx@1 int v3; // edx@1 v1 = this; v2 = 0; v1->uBigBufferSizeAligned = 0; v1->bBigBufferAllocated = 0; v3 = (int)v1->pMemoryBuffersNames; do { v1->pMemoryBuffers[v2] = 0; v1->uMemoryBuffersSizes[v2] = 0; *(char *)v3 = 0; ++v2; v3 += 12; } while ( v2 < 6000 ); v1->uDumpsCount = 0; } //----- (0042654C) -------------------------------------------------------- bool Allocator::CreateFileDump(const char *a1) { Allocator *v2; // esi@1 unsigned int v3; // ST44_4@1 FILE *v4; // eax@1 FILE *v5; // edi@1 bool result; // eax@2 unsigned int v7; // ecx@5 int v8; // edx@5 int v9; // eax@6 int v10; // eax@8 int v11; // ecx@8 signed int v12; // ebx@8 signed int v13; // ebx@10 struct _MEMORYSTATUS memstat; // [sp+8h] [bp-38h]@5 char Filename[20]; // [sp+28h] [bp-18h]@1 unsigned int *v16; // [sp+3Ch] [bp-4h]@11 int a1a; // [sp+48h] [bp+8h]@11 v2 = this; v3 = this->uDumpsCount; this->uDumpsCount = v3 + 1; sprintf(Filename, "Mem%03i.txt", v3); v4 = fopen(Filename, "w"); v5 = v4; if ( v4 ) { if ( a1 ) fprintf(v4, "%s\n\n", a1); fprintf(v5, "Windows Memory Status"); GlobalMemoryStatus(&memstat); fprintf(v5, "dwLength: %d\n\n", memstat.dwLength); fprintf(v5, "dwMemoryLoad: %d\n", memstat.dwMemoryLoad); fprintf(v5, "dwTotalPhys: %d\n", memstat.dwTotalPhys); fprintf(v5, "dwAvailPhys: %d\n", memstat.dwAvailPhys); fprintf(v5, "dwTotalPageFile: %d\n", memstat.dwTotalPageFile); fprintf(v5, "dwAvailPageFile: %d\n", memstat.dwAvailPageFile); fprintf(v5, "dwTotalVirtual: %d\n", memstat.dwTotalVirtual); fprintf(v5, "dwAvailVirtual: %d\n\n\n", memstat.dwAvailVirtual); v7 = v2->uNumBuffersUsed; v8 = 0; if ( (signed int)v7 > 0 ) { v9 = (int)v2->uMemoryBuffersSizes; do { v8 += *(int *)v9; v9 += 4; --v7; } while ( v7 ); } v10 = 0; v11 = (int)v2->uMemoryBuffersSizes; v12 = 6000; do { v10 += *(int *)v11; v11 += 4; --v12; } while ( v12 ); fprintf(v5, "Size: %i %i %i\n\n", v2->uNumBuffersUsed, v8, v10); v13 = 0; if ( (signed int)v2->uNumBuffersUsed > 0 ) { v16 = v2->uMemoryBuffersSizes; a1a = (int)v2->pMemoryBuffersNames; do { fprintf(v5, "%10i %10i %s\n", v13, *v16, a1a); a1a += 12; ++v16; ++v13; } while ( v13 < (signed int)v2->uNumBuffersUsed ); } fclose(v5); result = 0; } else { result = 1; } return result; } //----- (00426892) -------------------------------------------------------- static void __fastcall CheckMemoryAccessibility(void *pBase, DWORD uSize) { DWORD v2; // edi@1 DWORD i; // eax@1 struct _SYSTEM_INFO SystemInfo; // [sp+8h] [bp-24h]@1 v2 = uSize; GetSystemInfo(&SystemInfo); for ( i = 16 * SystemInfo.dwPageSize; i < v2; i += SystemInfo.dwPageSize ) ; }