date.c vs MONITOR_HEAP

I tried building with MONITOR_HEAP defined for the first time in a
while.  It wasn't pretty.

date.c was calling libc's strdup() instead of our dupstr() so alloc.c
wasn't tracking those allocations when/if MONITOR_HEAP is enabled.
Then it called free() which is actually a call to nhfree() in that
situation.  If the file of allocations and releases was subsequently
fed to heaputil, it would complain about freeing pointers that hadn't
been allocated.

Worse, makedefs and tilemap wouldn't link.  For MONITOR_HEAP,
makedefs was undefining free() in order to avoid nhfree() but it now
links with date.o so got nhfree() calls anyway.  And it wouldn't link
because that routine isn't available without alloc.o.  tilemap
doesn't link with date.o but it does call malloc() and free() and it
wasn't undefining free(), so looked for nhfree() when linking and got
the same no-such-routine failure.
This commit is contained in:
PatR
2021-12-15 18:39:29 -08:00
parent d6a1835f0d
commit 0a0ee2d1e6
3 changed files with 69 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
/* NetHack 3.7 tilemap.c $NHDT-Date: 1596498340 2020/08/03 23:45:40 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.47 $ */
/* NetHack 3.7 tilemap.c $NHDT-Date: 1639622363 2021/12/16 02:39:23 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.63 $ */
/* Copyright (c) 2016 by Michael Allison */
/* NetHack may be freely redistributed. See license for details. */
@@ -22,6 +22,13 @@
#include <stdarg.h>
#endif
#ifdef MONITOR_HEAP
/* with heap monitoring enabled, free(ptr) is a macro which expands to
nhfree(ptr,__FILE__,__LINE__); since tilemap doesn't link with
src/alloc.o it doesn't have access to nhfree(); use actual free */
#undef free
#endif
#define Fprintf (void) fprintf
#define Snprintf(str, size, ...) \
nh_snprintf(__func__, __LINE__, str, size, __VA_ARGS__)
@@ -1423,8 +1430,13 @@ precheck(int offset, const char *glyphtype)
glyphtype);
}
void add_tileref(int n, int glyphref, enum tilesrc src, int entrynum,
const char *nam, const char *prefix)
void add_tileref(
int n,
int glyphref,
enum tilesrc src,
int entrynum,
const char *nam,
const char *prefix)
{
struct tiles_used temp = { 0 };
static const char ellipsis[] UNUSED = "...";
@@ -1476,8 +1488,7 @@ free_tilerefs(void)
for (i = 0; i < SIZE(tilelist); i++) {
if (tilelist[i])
free(tilelist[i]);
tilelist[i] = (struct tiles_used *) 0;
free((genericptr_t) tilelist[i]), tilelist[i] = 0;
}
}