pointer formatting (trunk only)

Hide pointer formatting in alloc.c by eliminating the need for callers
to know how big a buffer is required.  I generally prefer the caller to
pass in its own buffer for this sort of thing, but in this case the usage
is almost entirely for debugging so using static buffers results in less
clutter in the rest of the code.
This commit is contained in:
nethack.rankin
2006-07-08 03:22:40 +00:00
parent 2b530870a3
commit dbc3abb226
8 changed files with 86 additions and 86 deletions
+1 -1
View File
@@ -12,7 +12,7 @@
#if 0 #if 0
E long *FDECL(alloc, (unsigned int)); E long *FDECL(alloc, (unsigned int));
#endif #endif
E char *FDECL(fmt_ptr, (const genericptr,char *)); E char *FDECL(fmt_ptr, (const genericptr));
/* This next pre-processor directive covers almost the entire file, /* This next pre-processor directive covers almost the entire file,
* interrupted only occasionally to pick up specific functions as needed. */ * interrupted only occasionally to pick up specific functions as needed. */
+6 -7
View File
@@ -1,4 +1,4 @@
/* SCCS Id: @(#)tradstdc.h 3.5 2006/06/28 */ /* SCCS Id: @(#)tradstdc.h 3.5 2006/07/07 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -195,16 +195,15 @@ typedef genericptr genericptr_t; /* (void *) or (char *) */
#endif #endif
#if defined(MICRO) || defined(WIN32) #if defined(MICRO) || defined(WIN32)
/* we actually want to know which systems have an ANSI run-time library /* We actually want to know which systems have an ANSI run-time library
* to know which support the new %p format for printing pointers. * to know which support the %p format for printing pointers.
* due to the presence of things like gcc, NHSTDC is not a good test. * Due to the presence of things like gcc, NHSTDC is not a good test.
* so we assume microcomputers have all converted to ANSI and bigger * So we assume microcomputers have all converted to ANSI and bigger
* computers which may have older libraries give reasonable results with * computers which may have older libraries give reasonable results with
* the cast. * casting pointers to unsigned long int (fmt_ptr() in alloc.c).
*/ */
# define HAS_PTR_FMT # define HAS_PTR_FMT
#endif #endif
#define FMT_PTR_BUFSIZ 20 /* buffer size for ptr address as text */
/* /*
* According to ANSI, prototypes for old-style declarations must widen the * According to ANSI, prototypes for old-style declarations must widen the
+25 -14
View File
@@ -1,4 +1,4 @@
/* SCCS Id: @(#)alloc.c 3.5 1995/10/04 */ /* SCCS Id: @(#)alloc.c 3.5 2006/07/07 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -10,9 +10,7 @@
#define EXTERN_H /* comment line for pre-compiled headers */ #define EXTERN_H /* comment line for pre-compiled headers */
#include "config.h" #include "config.h"
#if defined(MONITOR_HEAP) || defined(WIZARD) char *FDECL(fmt_ptr, (const genericptr));
char *FDECL(fmt_ptr, (const genericptr,char *));
#endif
#ifdef MONITOR_HEAP #ifdef MONITOR_HEAP
#undef alloc #undef alloc
@@ -61,12 +59,30 @@ register unsigned int lth;
# define PTR_TYP unsigned long # define PTR_TYP unsigned long
# endif # endif
/* format a pointer for display purposes; caller supplies the result buffer */ /* A small pool of static formatting buffers.
* PTRBUFSIZ: We assume that pointers will be formatted as integers in
* hexadecimal, requring at least 16+1 characters for each buffer to handle
* 64-bit systems, but the standard doesn't mandate that encoding and an
* implementation could do something different for %p, so we make some
* extra room.
* PTRBUFCNT: Number of formatted values which can be in use at the same
* time. To have more, callers need to make copies of them as they go.
*/
#define PTRBUFCNT 4
#define PTRBUFSIZ 32
static char ptrbuf[PTRBUFCNT][PTRBUFSIZ];
static int ptrbufidx = 0;
/* format a pointer for display purposes; returns a static buffer */
char * char *
fmt_ptr(ptr, buf) fmt_ptr(ptr)
const genericptr ptr; const genericptr ptr;
char *buf;
{ {
char *buf;
buf = ptrbuf[ptrbufidx];
if (++ptrbufidx >= PTRBUFCNT) ptrbufidx = 0;
Sprintf(buf, PTR_FMT, (PTR_TYP)ptr); Sprintf(buf, PTR_FMT, (PTR_TYP)ptr);
return buf; return buf;
} }
@@ -92,13 +108,11 @@ const char *file;
int line; int line;
{ {
long *ptr = alloc(lth); long *ptr = alloc(lth);
char ptr_address[FMT_PTR_BUFSIZ];
if (!tried_heaplog) heapmon_init(); if (!tried_heaplog) heapmon_init();
if (heaplog) if (heaplog)
(void) fprintf(heaplog, "+%5u %s %4d %s\n", lth, (void) fprintf(heaplog, "+%5u %s %4d %s\n", lth,
fmt_ptr((genericptr_t)ptr, ptr_address), fmt_ptr((genericptr_t)ptr), line, file);
line, file);
/* potential panic in alloc() was deferred til here */ /* potential panic in alloc() was deferred til here */
if (!ptr) panic("Cannot get %u bytes, line %d of %s", if (!ptr) panic("Cannot get %u bytes, line %d of %s",
lth, line, file); lth, line, file);
@@ -112,13 +126,10 @@ genericptr_t ptr;
const char *file; const char *file;
int line; int line;
{ {
char ptr_address[FMT_PTR_BUFSIZ];
if (!tried_heaplog) heapmon_init(); if (!tried_heaplog) heapmon_init();
if (heaplog) if (heaplog)
(void) fprintf(heaplog, "- %s %4d %s\n", (void) fprintf(heaplog, "- %s %4d %s\n",
fmt_ptr((genericptr_t)ptr, ptr_address), fmt_ptr((genericptr_t)ptr), line, file);
line, file);
free(ptr); free(ptr);
} }
+7 -9
View File
@@ -479,13 +479,11 @@ compare_blstats(bl1, bl2)
struct istat_s *bl1, *bl2; struct istat_s *bl1, *bl2;
{ {
int anytype, result = 0; int anytype, result = 0;
char bl1_address[FMT_PTR_BUFSIZ], bl2_address[FMT_PTR_BUFSIZ];
if (!bl1 || !bl2) { if (!bl1 || !bl2) {
char bl1_address[FMT_PTR_BUFSIZ], bl2_address[FMT_PTR_BUFSIZ];
panic("compare_blstat: bad istat pointer %s, %s", panic("compare_blstat: bad istat pointer %s, %s",
fmt_ptr((genericptr_t)bl1, bl1_address), fmt_ptr((genericptr_t)bl1),
fmt_ptr((genericptr_t)bl2, bl2_address)); fmt_ptr((genericptr_t)bl2));
} }
anytype = bl1->anytype; anytype = bl1->anytype;
@@ -493,8 +491,8 @@ struct istat_s *bl1, *bl2;
(anytype == ANY_IPTR || anytype == ANY_UPTR || (anytype == ANY_IPTR || anytype == ANY_UPTR ||
anytype == ANY_LPTR || anytype == ANY_ULPTR)) { anytype == ANY_LPTR || anytype == ANY_ULPTR)) {
panic("compare_blstat: invalid pointer %s, %s", panic("compare_blstat: invalid pointer %s, %s",
fmt_ptr((genericptr_t)bl1->a.a_void, bl1_address), fmt_ptr((genericptr_t)bl1->a.a_void),
fmt_ptr((genericptr_t)bl2->a.a_void, bl2_address)); fmt_ptr((genericptr_t)bl2->a.a_void));
} }
switch(anytype) { switch(anytype) {
@@ -558,11 +556,11 @@ struct istat_s *bl, *maxbl;
{ {
int result = 0; int result = 0;
int anytype; int anytype;
if (!bl || !maxbl) { if (!bl || !maxbl) {
char bl_address[FMT_PTR_BUFSIZ], maxbl_address[FMT_PTR_BUFSIZ];
impossible("percentage: bad istat pointer %s, %s", impossible("percentage: bad istat pointer %s, %s",
fmt_ptr((genericptr_t)bl, bl_address), fmt_ptr((genericptr_t)bl),
fmt_ptr((genericptr_t)maxbl, maxbl_address)); fmt_ptr((genericptr_t)maxbl));
return 0; return 0;
} }
+11 -12
View File
@@ -1,4 +1,4 @@
/* SCCS Id: @(#)light.c 3.5 1997/04/10 */ /* SCCS Id: @(#)light.c 3.5 2006/07/07 */
/* Copyright (c) Dean Luick, 1994 */ /* Copyright (c) Dean Luick, 1994 */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -91,7 +91,6 @@ del_light_source(type, id)
{ {
light_source *curr, *prev; light_source *curr, *prev;
genericptr_t tmp_id; genericptr_t tmp_id;
char id_address[FMT_PTR_BUFSIZ];
/* need to be prepared for dealing a with light source which /* need to be prepared for dealing a with light source which
has only been partially restored during a level change has only been partially restored during a level change
@@ -119,7 +118,7 @@ del_light_source(type, id)
} }
} }
impossible("del_light_source: not found type=%d, id=%s", impossible("del_light_source: not found type=%d, id=%s",
type, fmt_ptr((genericptr_t)id, id_address)); type, fmt_ptr((genericptr_t)id));
} }
/* Mark locations that are temporarily lit via mobile light sources. */ /* Mark locations that are temporarily lit via mobile light sources. */
@@ -583,7 +582,7 @@ int
wiz_light_sources() wiz_light_sources()
{ {
winid win; winid win;
char buf[BUFSZ], arg_address[FMT_PTR_BUFSIZ]; char buf[BUFSZ];
light_source *ls; light_source *ls;
win = create_nhwindow(NHW_MENU); /* corner text window */ win = create_nhwindow(NHW_MENU); /* corner text window */
@@ -598,14 +597,14 @@ wiz_light_sources()
putstr(win, 0, "-------- ----- ------ ---- -------"); putstr(win, 0, "-------- ----- ------ ---- -------");
for (ls = light_base; ls; ls = ls->next) { for (ls = light_base; ls; ls = ls->next) {
Sprintf(buf, " %2d,%2d %2d 0x%04x %s %s", Sprintf(buf, " %2d,%2d %2d 0x%04x %s %s",
ls->x, ls->y, ls->range, ls->flags, ls->x, ls->y, ls->range, ls->flags,
(ls->type == LS_OBJECT ? "obj" : (ls->type == LS_OBJECT ? "obj" :
ls->type == LS_MONSTER ? ls->type == LS_MONSTER ?
(mon_is_local((struct monst *)ls->id) ? "mon" : (mon_is_local((struct monst *)ls->id) ? "mon" :
((struct monst *)ls->id == &youmonst) ? "you" : ((struct monst *)ls->id == &youmonst) ? "you" :
"<m>") : /* migrating monster */ "<m>") : /* migrating monster */
"???"), "???"),
fmt_ptr(ls->id, arg_address)); fmt_ptr(ls->id));
putstr(win, 0, buf); putstr(win, 0, buf);
} }
} else } else
+25 -30
View File
@@ -1,4 +1,4 @@
/* SCCS Id: @(#)mkobj.c 3.5 2006/06/17 */ /* SCCS Id: @(#)mkobj.c 3.5 2006/07/07 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -1840,16 +1840,14 @@ obj_sanity_check()
struct obj *obj; struct obj *obj;
struct monst *mon; struct monst *mon;
const char *mesg; const char *mesg;
char obj_address[FMT_PTR_BUFSIZ],
mon_address[FMT_PTR_BUFSIZ]; /* room for formatted pointers */
mesg = "fobj sanity"; mesg = "fobj sanity";
for (obj = fobj; obj; obj = obj->nobj) { for (obj = fobj; obj; obj = obj->nobj) {
if (obj->where != OBJ_FLOOR) { if (obj->where != OBJ_FLOOR) {
pline("%s obj %s %s@(%d,%d): %s\n", mesg, pline("%s obj %s %s@(%d,%d): %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where), where_name(obj->where),
obj->ox, obj->oy, doname(obj)); obj->ox, obj->oy, doname(obj));
} }
check_contained(obj, mesg); check_contained(obj, mesg);
} }
@@ -1860,17 +1858,17 @@ obj_sanity_check()
for (obj = level.objects[x][y]; obj; obj = obj->nexthere) for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
if (obj->where != OBJ_FLOOR) { if (obj->where != OBJ_FLOOR) {
pline("%s obj %s %s@(%d,%d): %s\n", mesg, pline("%s obj %s %s@(%d,%d): %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where), where_name(obj->where),
obj->ox, obj->oy, doname(obj)); obj->ox, obj->oy, doname(obj));
} }
mesg = "invent sanity"; mesg = "invent sanity";
for (obj = invent; obj; obj = obj->nobj) { for (obj = invent; obj; obj = obj->nobj) {
if (obj->where != OBJ_INVENT) { if (obj->where != OBJ_INVENT) {
pline("%s obj %s %s: %s\n", mesg, pline("%s obj %s %s: %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where), doname(obj)); where_name(obj->where), doname(obj));
} }
check_contained(obj, mesg); check_contained(obj, mesg);
} }
@@ -1879,8 +1877,8 @@ obj_sanity_check()
for (obj = migrating_objs; obj; obj = obj->nobj) { for (obj = migrating_objs; obj; obj = obj->nobj) {
if (obj->where != OBJ_MIGRATING) { if (obj->where != OBJ_MIGRATING) {
pline("%s obj %s %s: %s\n", mesg, pline("%s obj %s %s: %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where), doname(obj)); where_name(obj->where), doname(obj));
} }
check_contained(obj, mesg); check_contained(obj, mesg);
} }
@@ -1889,8 +1887,8 @@ obj_sanity_check()
for (obj = level.buriedobjlist; obj; obj = obj->nobj) { for (obj = level.buriedobjlist; obj; obj = obj->nobj) {
if (obj->where != OBJ_BURIED) { if (obj->where != OBJ_BURIED) {
pline("%s obj %s %s: %s\n", mesg, pline("%s obj %s %s: %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where), doname(obj)); where_name(obj->where), doname(obj));
} }
check_contained(obj, mesg); check_contained(obj, mesg);
} }
@@ -1899,14 +1897,14 @@ obj_sanity_check()
for (obj = billobjs; obj; obj = obj->nobj) { for (obj = billobjs; obj; obj = obj->nobj) {
if (obj->where != OBJ_ONBILL) { if (obj->where != OBJ_ONBILL) {
pline("%s obj %s %s: %s\n", mesg, pline("%s obj %s %s: %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where), doname(obj)); where_name(obj->where), doname(obj));
} }
/* shouldn't be a full container on the bill */ /* shouldn't be a full container on the bill */
if (obj->cobj) { if (obj->cobj) {
pline("%s obj %s contains %s! %s\n", mesg, pline("%s obj %s contains %s! %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
something, doname(obj)); something, doname(obj));
} }
} }
@@ -1915,15 +1913,13 @@ obj_sanity_check()
for (obj = mon->minvent; obj; obj = obj->nobj) { for (obj = mon->minvent; obj; obj = obj->nobj) {
if (obj->where != OBJ_MINVENT) { if (obj->where != OBJ_MINVENT) {
pline("%s obj %s %s: %s\n", mesg, pline("%s obj %s %s: %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where), doname(obj)); where_name(obj->where), doname(obj));
} }
if (obj->ocarry != mon) { if (obj->ocarry != mon) {
pline("%s obj %s (%s) not held by mon %s (%s)\n", mesg, pline("%s obj %s (%s) not held by mon %s (%s)\n", mesg,
fmt_ptr((genericptr_t)obj, obj_address), fmt_ptr((genericptr_t)obj), doname(obj),
doname(obj), fmt_ptr((genericptr_t)mon), mon_nam(mon));
fmt_ptr((genericptr_t)mon, mon_address),
mon_nam(mon));
} }
check_contained(obj, mesg); check_contained(obj, mesg);
} }
@@ -1949,17 +1945,16 @@ check_contained(container, mesg)
const char *mesg; const char *mesg;
{ {
struct obj *obj; struct obj *obj;
char obj1_address[FMT_PTR_BUFSIZ], obj2_address[FMT_PTR_BUFSIZ];
for (obj = container->cobj; obj; obj = obj->nobj) { for (obj = container->cobj; obj; obj = obj->nobj) {
if (obj->where != OBJ_CONTAINED) if (obj->where != OBJ_CONTAINED)
pline("contained %s obj %s: %s\n", mesg, pline("contained %s obj %s: %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj1_address), fmt_ptr((genericptr_t)obj),
where_name(obj->where)); where_name(obj->where));
else if (obj->ocontainer != container) else if (obj->ocontainer != container)
pline("%s obj %s not in container %s\n", mesg, pline("%s obj %s not in container %s\n", mesg,
fmt_ptr((genericptr_t)obj, obj1_address), fmt_ptr((genericptr_t)obj),
fmt_ptr((genericptr_t)container, obj2_address)); fmt_ptr((genericptr_t)container));
} }
} }
#endif /* WIZARD */ #endif /* WIZARD */
+2 -3
View File
@@ -1,4 +1,4 @@
/* SCCS Id: @(#)mondata.c 3.5 2005/10/05 */ /* SCCS Id: @(#)mondata.c 3.5 2006/07/07 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -482,9 +482,8 @@ monsndx(ptr) /* return an index into the mons array */
i = (int)(ptr - &mons[0]); i = (int)(ptr - &mons[0]);
if (i < LOW_PM || i >= NUMMONS) { if (i < LOW_PM || i >= NUMMONS) {
char ptr_address[FMT_PTR_BUFSIZ];
panic("monsndx - could not index monster (%s)", panic("monsndx - could not index monster (%s)",
fmt_ptr((genericptr_t)ptr, ptr_address)); fmt_ptr((genericptr_t)ptr));
return NON_PM; /* will not get here */ return NON_PM; /* will not get here */
} }
+9 -10
View File
@@ -1,4 +1,4 @@
/* SCCS Id: @(#)timeout.c 3.5 2006/04/14 */ /* SCCS Id: @(#)timeout.c 3.5 2006/07/07 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -1368,7 +1368,7 @@ print_queue(win, base)
timer_element *base; timer_element *base;
{ {
timer_element *curr; timer_element *curr;
char buf[BUFSZ], arg_address[FMT_PTR_BUFSIZ]; char buf[BUFSZ];
if (!base) { if (!base) {
putstr(win, 0, "<empty>"); putstr(win, 0, "<empty>");
@@ -1377,14 +1377,14 @@ print_queue(win, base)
for (curr = base; curr; curr = curr->next) { for (curr = base; curr; curr = curr->next) {
#ifdef VERBOSE_TIMER #ifdef VERBOSE_TIMER
Sprintf(buf, " %4ld %4ld %-6s %s(%s)", Sprintf(buf, " %4ld %4ld %-6s %s(%s)",
curr->timeout, curr->tid, kind_name(curr->kind), curr->timeout, curr->tid, kind_name(curr->kind),
timeout_funcs[curr->func_index].name, timeout_funcs[curr->func_index].name,
fmt_ptr((genericptr_t)curr->arg, arg_address)); fmt_ptr((genericptr_t)curr->arg));
#else #else
Sprintf(buf, " %4ld %4ld %-6s #%d(%s)", Sprintf(buf, " %4ld %4ld %-6s #%d(%s)",
curr->timeout, curr->tid, kind_name(curr->kind), curr->timeout, curr->tid, kind_name(curr->kind),
curr->func_index, curr->func_index,
fmt_ptr((genericptr_t)curr->arg, arg_address)); fmt_ptr((genericptr_t)curr->arg));
#endif #endif
putstr(win, 0, buf); putstr(win, 0, buf);
} }
@@ -1417,7 +1417,6 @@ void
timer_sanity_check() timer_sanity_check()
{ {
timer_element *curr; timer_element *curr;
char obj_address[FMT_PTR_BUFSIZ];
/* this should be much more complete */ /* this should be much more complete */
for (curr = timer_base; curr; curr = curr->next) for (curr = timer_base; curr; curr = curr->next)
@@ -1425,7 +1424,7 @@ timer_sanity_check()
struct obj *obj = (struct obj *) curr->arg; struct obj *obj = (struct obj *) curr->arg;
if (obj->timed == 0) { if (obj->timed == 0) {
pline("timer sanity: untimed obj %s, timer %ld", pline("timer sanity: untimed obj %s, timer %ld",
fmt_ptr((genericptr_t)obj, obj_address), curr->tid); fmt_ptr((genericptr_t)obj), curr->tid);
} }
} }
} }