[util] Resurrect Hunk_Print and fix some errors

It turned out that Hunk_HighAlloc was not creating a hunk header
(ancient bug by me), and I cleaned up a bunch of name-size issues, along
the way.
This commit is contained in:
Bill Currie 2021-02-03 13:21:08 +09:00
parent 0bfb60775e
commit 5d1d85f72e
2 changed files with 24 additions and 19 deletions

View file

@ -105,6 +105,7 @@ void Z_SetError (memzone_t *zone, void (*err) (void *data, const char *msg),
void *data);
void Z_CheckPointer (const memzone_t *zone, const void *ptr, int size);
void Hunk_Print (qboolean all);
void *Hunk_Alloc (int size); // returns 0 filled memory
void *Hunk_AllocName (int size, const char *name);
int Hunk_LowMark (void) __attribute__((pure));

View file

@ -438,7 +438,8 @@ Hunk_Check (void)
for (h = (hunk_t *) hunk_base; (byte *) h != hunk_base + hunk_low_used;) {
if (h->sentinal != HUNK_SENTINAL)
Sys_Error ("Hunk_Check: trashed sentinal");
if (h->size < 16 || h->size + (byte *) h - hunk_base > hunk_size)
if (h->size < (int) sizeof (hunk_t)
|| h->size + (byte *) h - hunk_base > hunk_size)
Sys_Error ("Hunk_Check: bad size");
h = (hunk_t *) ((byte *) h + h->size);
}
@ -451,15 +452,13 @@ Hunk_Check (void)
Otherwise, allocations with the same name will be totaled up before
printing.
*/
/*
static void
VISIBLE void
Hunk_Print (qboolean all)
{
char name[9];
hunk_t *h, *next, *endlow, *starthigh, *endhigh;
int count, sum, totalblocks;
name[8] = 0;
count = 0;
sum = 0;
totalblocks = 0;
@ -488,7 +487,8 @@ Hunk_Print (qboolean all)
// run consistancy checks
if (h->sentinal != HUNK_SENTINAL)
Sys_Error ("Hunk_Check: trahsed sentinal");
if (h->size < 16 || h->size + (byte *) h - hunk_base > hunk_size)
if (h->size < (int) sizeof (hunk_t)
|| h->size + (byte *) h - hunk_base > hunk_size)
Sys_Error ("Hunk_Check: bad size");
next = (hunk_t *) ((byte *) h + h->size);
@ -497,15 +497,14 @@ Hunk_Print (qboolean all)
sum += h->size;
// print the single block
memcpy (name, h->name, 8);
if (all)
Sys_Printf ("%8p :%8i %8s\n", h, h->size, name);
Sys_Printf ("%8p :%8i %8.8s\n", h, h->size, h->name);
// print the total
if (next == endlow || next == endhigh ||
strncmp (h->name, next->name, 8)) {
if (!all)
Sys_Printf (" :%8i %8s (TOTAL)\n", sum, name);
Sys_Printf (" :%8i %8.8s (TOTAL)\n", sum, h->name);
count = 0;
sum = 0;
}
@ -516,7 +515,7 @@ Hunk_Print (qboolean all)
Sys_Printf ("-------------------------\n");
Sys_Printf ("%8i total blocks\n", totalblocks);
}
*/
static void
Hunk_FreeToHighMark (int mark)
{
@ -579,8 +578,7 @@ Hunk_AllocName (int size, const char *name)
h->size = size;
h->sentinal = HUNK_SENTINAL;
memcpy (h->name, name, 8);
h->name[7] = 0;
memcpy (h->name, name, sizeof (h->name));
return (void *) (h + 1);
}
@ -609,6 +607,8 @@ Hunk_FreeToLowMark (int mark)
static void *
Hunk_HighAlloc (int size)
{
hunk_t *h;
if (size < 0)
Sys_Error ("Hunk_HighAlloc: bad size: %i", size);
@ -620,7 +620,7 @@ Hunk_HighAlloc (int size)
Hunk_Check ();
#endif
size = ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1));
size = sizeof (hunk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1));
if (hunk_size - hunk_low_used - hunk_high_used < size) {
Sys_Printf ("Hunk_HighAlloc: failed on %i bytes\n", size);
@ -629,7 +629,11 @@ Hunk_HighAlloc (int size)
hunk_high_used += size;
return (void *) (hunk_base + hunk_size - hunk_high_used);
h = (void *) (hunk_base + hunk_size - hunk_high_used);
h->sentinal = HUNK_SENTINAL;
h->size = size;
h->name[0] = 0;
return h + 1;
}
/*
@ -744,7 +748,7 @@ static inline void
Cache_UnlinkLRU (cache_system_t * cs)
{
if (!cs->lru_next || !cs->lru_prev)
Sys_Error ("Cache_UnlinkLRU: NULL link: %s %p %p",
Sys_Error ("Cache_UnlinkLRU: NULL link: %.16s %p %p",
cs->name, cs->lru_next, cs->lru_prev);
cs->lru_next->lru_prev = cs->lru_prev;
@ -757,7 +761,7 @@ static void
Cache_MakeLRU (cache_system_t * cs)
{
if (cs->lru_next || cs->lru_prev)
Sys_Error ("Cache_MakeLRU: active link: %s %p %p",
Sys_Error ("Cache_MakeLRU: active link: %.16s %p %p",
cs->name, cs->lru_next, cs->lru_prev);
cache_head.lru_next->lru_prev = cs;
@ -896,7 +900,7 @@ Cache_Print (void)
cache_system_t *cd;
for (cd = cache_head.next; cd != &cache_head; cd = cd->next) {
Sys_Printf ("%8d : %s\n", (int) cd->size, cd->name);
Sys_Printf ("%8d : %.16s\n", (int) cd->size, cd->name);
}
}
@ -929,7 +933,7 @@ Cache_Flush (void)
while (cache_head.prev != &cache_head) {
if (!cache_head.prev->user->data)
Sys_Error ("Cache_Flush: user/system out of sync for "
"'%s' with %d size",
"'%.16s' with %d size",
cache_head.prev->name, (int) cache_head.prev->size);
Cache_Free (cache_head.prev->user); // reclaim the space
}
@ -970,7 +974,7 @@ Cache_Free (cache_user_t *c)
if (cs->readlock)
Sys_Error ("Cache_Free: attempt to free locked block");
Sys_MaskPrintf (SYS_DEV, "Cache_Free: freeing '%s' %p\n", cs->name, cs);
Sys_MaskPrintf (SYS_DEV, "Cache_Free: freeing '%.16s' %p\n", cs->name, cs);
Cache_UnlinkLRU (cs);