From 4432bc0beff9118f962fd0f01bfd284ebd3decc7 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 8 Mar 2011 22:44:56 +0900 Subject: [PATCH] Make the offset in zone dumps more useful. --- include/QF/zone.h | 2 +- libs/gamecode/engine/pr_load.c | 4 ++-- libs/gamecode/engine/pr_zone.c | 3 ++- libs/util/zone.c | 41 ++++++++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/include/QF/zone.h b/include/QF/zone.h index 713a10203..3d77ff68f 100644 --- a/include/QF/zone.h +++ b/include/QF/zone.h @@ -95,7 +95,7 @@ typedef struct memzone_s memzone_t; void Memory_Init (void *buf, int size); -void Z_ClearZone (memzone_t *zone, int size); +void Z_ClearZone (memzone_t *zone, int size, int zone_offset, int ele_size); void Z_Free (memzone_t *zone, void *ptr); void *Z_Malloc (memzone_t *zone, int size); // returns 0 filled memory void *Z_TagMalloc (memzone_t *zone, int size, int tag); diff --git a/libs/gamecode/engine/pr_load.c b/libs/gamecode/engine/pr_load.c index 58042e70e..89128f037 100644 --- a/libs/gamecode/engine/pr_load.c +++ b/libs/gamecode/engine/pr_load.c @@ -203,8 +203,6 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size, int edicts, int zone) *pr->edicts = (edict_t *)((byte *) pr->progs + pr->progs_size); pr->zone = (memzone_t *)((byte *) pr->progs + pr->progs_size + pr->pr_edictareasize); - if (pr->zone_size) - PR_Zone_Init (pr); pr->pr_functions = (dfunction_t *) (base + pr->progs->ofs_functions); @@ -218,6 +216,8 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size, int edicts, int zone) pr->globals_size = (pr_type_t*)((byte *) pr->zone + pr->zone_size) - pr->pr_globals; + if (pr->zone_size) + PR_Zone_Init (pr); if (pr->function_hash) { Hash_FlushTable (pr->function_hash); diff --git a/libs/gamecode/engine/pr_zone.c b/libs/gamecode/engine/pr_zone.c index 5f8a9c6eb..8fe97cc92 100644 --- a/libs/gamecode/engine/pr_zone.c +++ b/libs/gamecode/engine/pr_zone.c @@ -51,7 +51,8 @@ static __attribute__ ((used)) const char rcsid[] = void PR_Zone_Init (progs_t *pr) { - Z_ClearZone (pr->zone, pr->zone_size); + Z_ClearZone (pr->zone, pr->zone_size, + (pr_type_t *)pr->zone - pr->pr_globals, sizeof (pr_type_t)); } VISIBLE void diff --git a/libs/util/zone.c b/libs/util/zone.c index ada54172b..4a7a6436b 100644 --- a/libs/util/zone.c +++ b/libs/util/zone.c @@ -74,22 +74,24 @@ typedef struct memblock_s { int size; // including the header and possibly tiny fragments int tag; // a tag of 0 is a free block - int id; // should be ZONEID struct memblock_s *next, *prev; - int pad; // pad to 64 bit boundary + int id; // should be ZONEID + int id2; // pad to 64 bit boundary } memblock_t; struct memzone_s { int size; // total bytes malloced, including header int used; // ammount used, including header + int offset; + int ele_size; memblock_t blocklist; // start / end cap for linked list memblock_t *rover; }; VISIBLE void -Z_ClearZone (memzone_t *zone, int size) +Z_ClearZone (memzone_t *zone, int size, int zone_offset, int ele_size) { memblock_t *block; @@ -101,6 +103,8 @@ Z_ClearZone (memzone_t *zone, int size) zone->blocklist.tag = 1; // in use block zone->blocklist.id = 0; zone->blocklist.size = 0; + zone->offset = zone_offset; + zone->ele_size = ele_size; zone->rover = block; zone->size = size; zone->used = sizeof (memzone_t); @@ -108,6 +112,7 @@ Z_ClearZone (memzone_t *zone, int size) block->prev = block->next = &zone->blocklist; block->tag = 0; // free block block->id = ZONEID; + block->id2 = ZONEID; block->size = size - sizeof (memzone_t); } @@ -115,12 +120,12 @@ VISIBLE void Z_Free (memzone_t *zone, void *ptr) { memblock_t *block, *other; - + if (!ptr) Sys_Error ("Z_Free: NULL pointer"); block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t)); - if (block->id != ZONEID) + if (block->id != ZONEID || block->id2 != ZONEID) Sys_Error ("Z_Free: freed a pointer without ZONEID"); if (block->tag == 0) Sys_Error ("Z_Free: freed a freed pointer"); @@ -201,6 +206,7 @@ Z_TagMalloc (memzone_t *zone, int size, int tag) new->tag = 0; // free block new->prev = base; new->id = ZONEID; + new->id2 = ZONEID; new->next = base->next; new->next->prev = new; base->next = new; @@ -212,6 +218,7 @@ Z_TagMalloc (memzone_t *zone, int size, int tag) zone->rover = base->next; // next allocation will start looking here base->id = ZONEID; + base->id2 = ZONEID; zone->used += base->size; @@ -232,7 +239,7 @@ Z_Realloc (memzone_t *zone, void *ptr, int size) return Z_Malloc (zone, size); block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t)); - if (block->id != ZONEID) + if (block->id != ZONEID || block->id2 != ZONEID) Sys_Error ("Z_Realloc: realloced a pointer without ZONEID"); if (block->tag == 0) Sys_Error ("Z_Realloc: realloced a freed pointer"); @@ -253,6 +260,20 @@ Z_Realloc (memzone_t *zone, void *ptr, int size) return ptr; } +static int +z_block_size (memblock_t *block) +{ + return block->size - sizeof (memblock_t) - 4; +} + +static int +z_offset (memzone_t *zone, memblock_t *block) +{ + int offset = ((byte *) (block + 1) - (byte *) zone); + + return offset / zone->ele_size + zone->offset; +} + void Z_Print (memzone_t *zone) { @@ -262,12 +283,14 @@ Z_Print (memzone_t *zone) zone->size, zone, zone->used); for (block = zone->blocklist.next ; ; block = block->next) { - Sys_Printf ("block:%p size:%7i tag:%3i ofs:%d\n", - block, block->size - (int) sizeof (memblock_t) - 4, - block->tag, (int) ((byte *) block - (byte *) zone)); + Sys_Printf ("block:%p size:%7i tag:%3i ofs:%x\n", + block, z_block_size (block), + block->tag, z_offset (zone, block)); if (block->next == &zone->blocklist) break; // all blocks have been hit + if (block->id != ZONEID || block->id2 != ZONEID) + Sys_Printf ("ERROR: block ids incorrect\n"); if ( (byte *)block + block->size != (byte *)block->next) Sys_Printf ("ERROR: block size does not touch the next block\n"); if ( block->next->prev != block)