From 715c07930dfa721b106c6db54db779de1d021c6c Mon Sep 17 00:00:00 2001 From: Christoph Mallon Date: Sat, 6 Aug 2011 11:12:02 +0200 Subject: CRUISE: Replace casts and offset calculations for memory debugger by a simple struct. --- engines/cruise/cruise.h | 10 +++++++++- engines/cruise/cruise_main.cpp | 32 +++++++++++++++++--------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/engines/cruise/cruise.h b/engines/cruise/cruise.h index 900f677975..94f8759d01 100644 --- a/engines/cruise/cruise.h +++ b/engines/cruise/cruise.h @@ -108,7 +108,15 @@ public: Common::RandomSource _rnd; - Common::List _memList; + struct MemInfo { + int32 lineNum; + char fname[64]; + uint32 magic; + + static uint32 const cookie = 0x41424344; + }; + + Common::List _memList; typedef Common::List RectList; diff --git a/engines/cruise/cruise_main.cpp b/engines/cruise/cruise_main.cpp index 031c53b96a..ff4669607d 100644 --- a/engines/cruise/cruise_main.cpp +++ b/engines/cruise/cruise_main.cpp @@ -40,19 +40,21 @@ unsigned int timer = 0; gfxEntryStruct* linkedMsgList = NULL; +typedef CruiseEngine::MemInfo MemInfo; + void MemoryList() { if (!_vm->_memList.empty()) { debug("Current list of un-freed memory blocks:"); - Common::List::iterator i; + Common::List::iterator i; for (i = _vm->_memList.begin(); i != _vm->_memList.end(); ++i) { - byte *v = *i; - debug("%s - %d", (const char *)(v - 68), *((int32 *)(v - 72))); + MemInfo const *const v = *i; + debug("%s - %d", v->fname, v->lineNum); } } } void *MemoryAlloc(uint32 size, bool clearFlag, int32 lineNum, const char *fname) { - byte *result; + void *result; if (gDebugLevel > 0) { // Find the point after the final slash @@ -61,17 +63,17 @@ void *MemoryAlloc(uint32 size, bool clearFlag, int32 lineNum, const char *fname) --fnameP; // Create the new memory block and add it to the memory list - byte *v = (byte *)malloc(size + 64 + 8); - *((int32 *) v) = lineNum; - strncpy((char *)v + 4, fnameP, 63); - *((char *)v + 4 + 63) = '\0'; - *((uint32 *) (v + 68)) = 0x41424344; + MemInfo *const v = (MemInfo *)malloc(sizeof(MemInfo) + size); + v->lineNum = lineNum; + strncpy(v->fname, fnameP, sizeof(v->fname)); + v->fname[ARRAYSIZE(v->fname) - 1] = '\0'; + v->magic = MemInfo::cookie; // Add the block to the memory list - result = v + 64 + 8; - _vm->_memList.push_back(result); + _vm->_memList.push_back(v); + result = v + 1; } else - result = (byte *)malloc(size); + result = malloc(size); if (clearFlag) memset(result, 0, size); @@ -84,11 +86,11 @@ void MemoryFree(void *v) { return; if (gDebugLevel > 0) { - byte *p = (byte *)v; - assert(*((uint32 *) (p - 4)) == 0x41424344); + MemInfo *const p = (MemInfo *)v - 1; + assert(p->magic == MemInfo::cookie); _vm->_memList.remove(p); - free(p - 8 - 64); + free(p); } else free(v); } -- cgit v1.2.3