1 /* NetHack 3.6 alloc.c $NHDT-Date: 1446975460 2015/11/08 09:37:40 $ $NHDT-Branch: master $:$NHDT-Revision: 1.14 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* to get the malloc() prototype from system.h */
6 #define ALLOC_C /* comment line for pre-compiled headers */
7 /* since this file is also used in auxiliary programs, don't include all the
8 * function declarations for all of nethack
10 #define EXTERN_H /* comment line for pre-compiled headers */
13 char *FDECL(fmt_ptr, (const genericptr));
18 extern void FDECL(free, (genericptr_t));
19 static void NDECL(heapmon_init);
21 static FILE *heaplog = 0;
22 static boolean tried_heaplog = FALSE;
25 long *FDECL(alloc, (unsigned int));
27 VDECL(panic, (const char *, ...))
32 register unsigned int lth;
36 * a ridiculous definition, suppressing
37 * "possible pointer alignment problem" for (long *) malloc()
40 long dummy = ftell(stderr);
43 dummy = 0; /* make sure arg is used */
46 register genericptr_t ptr;
51 panic("Memory allocation failure; cannot get %u bytes", lth);
59 #define PTR_TYP genericptr_t
61 #define PTR_FMT "%06lx"
62 #define PTR_TYP unsigned long
65 /* A small pool of static formatting buffers.
66 * PTRBUFSIZ: We assume that pointers will be formatted as integers in
67 * hexadecimal, requiring at least 16+1 characters for each buffer to handle
68 * 64-bit systems, but the standard doesn't mandate that encoding and an
69 * implementation could do something different for %p, so we make some
71 * PTRBUFCNT: Number of formatted values which can be in use at the same
72 * time. To have more, callers need to make copies of them as they go.
76 static char ptrbuf[PTRBUFCNT][PTRBUFSIZ];
77 static int ptrbufidx = 0;
79 /* format a pointer for display purposes; returns a static buffer */
86 buf = ptrbuf[ptrbufidx];
87 if (++ptrbufidx >= PTRBUFCNT)
90 Sprintf(buf, PTR_FMT, (PTR_TYP) ptr);
96 /* If ${NH_HEAPLOG} is defined and we can create a file by that name,
97 then we'll log the allocation and release information to that file. */
101 char *logname = getenv("NH_HEAPLOG");
103 if (logname && *logname)
104 heaplog = fopen(logname, "w");
105 tried_heaplog = TRUE;
109 nhalloc(lth, file, line)
114 long *ptr = alloc(lth);
119 (void) fprintf(heaplog, "+%5u %s %4d %s\n", lth,
120 fmt_ptr((genericptr_t) ptr), line, file);
121 /* potential panic in alloc() was deferred til here */
123 panic("Cannot get %u bytes, line %d of %s", lth, line, file);
129 nhfree(ptr, file, line)
137 (void) fprintf(heaplog, "- %s %4d %s\n",
138 fmt_ptr((genericptr_t) ptr), line, file);
143 /* strdup() which uses our alloc() rather than libc's malloc(),
144 with caller tracking */
146 nhdupstr(string, file, line)
151 return strcpy((char *) nhalloc(strlen(string) + 1, file, line), string);
155 #endif /* MONITOR_HEAP */
157 /* strdup() which uses our alloc() rather than libc's malloc();
158 not used when MONITOR_HEAP is enabled, but included unconditionally
159 in case utility programs get built using a different setting for that */
164 return strcpy((char *) alloc(strlen(string) + 1), string);