Give feedback just before timed levitation runs out
[aNetHack.git] / src / alloc.c
blob18a4d533af97283f65a7a126dda17ac9e013fa3f
1 /* NetHack 3.6 alloc.c $NHDT-Date: 1454376505 2016/02/02 01:28:25 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.16 $ */
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 */
9 #define EXTERN_H /* comment line for pre-compiled headers */
10 #include "config.h"
12 char *FDECL(fmt_ptr, (const genericptr));
14 #ifdef MONITOR_HEAP
15 #undef alloc
16 #undef free
17 extern void FDECL(free, (genericptr_t));
18 static void NDECL(heapmon_init);
20 static FILE *heaplog = 0;
21 static boolean tried_heaplog = FALSE;
22 #endif
24 long *FDECL(alloc, (unsigned int));
25 extern void VDECL(panic, (const char *, ...)) PRINTF_F(1, 2);
27 long *
28 alloc(lth)
29 register unsigned int lth;
31 #ifdef LINT
33 * a ridiculous definition, suppressing
34 * "possible pointer alignment problem" for (long *) malloc()
35 * from lint
37 long dummy = ftell(stderr);
39 if (lth)
40 dummy = 0; /* make sure arg is used */
41 return &dummy;
42 #else
43 register genericptr_t ptr;
45 ptr = malloc(lth);
46 #ifndef MONITOR_HEAP
47 if (!ptr)
48 panic("Memory allocation failure; cannot get %u bytes", lth);
49 #endif
50 return (long *) ptr;
51 #endif
54 #ifdef HAS_PTR_FMT
55 #define PTR_FMT "%p"
56 #define PTR_TYP genericptr_t
57 #else
58 #define PTR_FMT "%06lx"
59 #define PTR_TYP unsigned long
60 #endif
62 /* A small pool of static formatting buffers.
63 * PTRBUFSIZ: We assume that pointers will be formatted as integers in
64 * hexadecimal, requiring at least 16+1 characters for each buffer to handle
65 * 64-bit systems, but the standard doesn't mandate that encoding and an
66 * implementation could do something different for %p, so we make some
67 * extra room.
68 * PTRBUFCNT: Number of formatted values which can be in use at the same
69 * time. To have more, callers need to make copies of them as they go.
71 #define PTRBUFCNT 4
72 #define PTRBUFSIZ 32
73 static char ptrbuf[PTRBUFCNT][PTRBUFSIZ];
74 static int ptrbufidx = 0;
76 /* format a pointer for display purposes; returns a static buffer */
77 char *
78 fmt_ptr(ptr)
79 const genericptr ptr;
81 char *buf;
83 buf = ptrbuf[ptrbufidx];
84 if (++ptrbufidx >= PTRBUFCNT)
85 ptrbufidx = 0;
87 Sprintf(buf, PTR_FMT, (PTR_TYP) ptr);
88 return buf;
91 #ifdef MONITOR_HEAP
93 /* If ${NH_HEAPLOG} is defined and we can create a file by that name,
94 then we'll log the allocation and release information to that file. */
95 static void
96 heapmon_init()
98 char *logname = getenv("NH_HEAPLOG");
100 if (logname && *logname)
101 heaplog = fopen(logname, "w");
102 tried_heaplog = TRUE;
105 long *
106 nhalloc(lth, file, line)
107 unsigned int lth;
108 const char *file;
109 int line;
111 long *ptr = alloc(lth);
113 if (!tried_heaplog)
114 heapmon_init();
115 if (heaplog)
116 (void) fprintf(heaplog, "+%5u %s %4d %s\n", lth,
117 fmt_ptr((genericptr_t) ptr), line, file);
118 /* potential panic in alloc() was deferred til here */
119 if (!ptr)
120 panic("Cannot get %u bytes, line %d of %s", lth, line, file);
122 return ptr;
125 void
126 nhfree(ptr, file, line)
127 genericptr_t ptr;
128 const char *file;
129 int line;
131 if (!tried_heaplog)
132 heapmon_init();
133 if (heaplog)
134 (void) fprintf(heaplog, "- %s %4d %s\n",
135 fmt_ptr((genericptr_t) ptr), line, file);
137 free(ptr);
140 /* strdup() which uses our alloc() rather than libc's malloc(),
141 with caller tracking */
142 char *
143 nhdupstr(string, file, line)
144 const char *string;
145 const char *file;
146 int line;
148 return strcpy((char *) nhalloc(strlen(string) + 1, file, line), string);
150 #undef dupstr
152 #endif /* MONITOR_HEAP */
154 /* strdup() which uses our alloc() rather than libc's malloc();
155 not used when MONITOR_HEAP is enabled, but included unconditionally
156 in case utility programs get built using a different setting for that */
157 char *
158 dupstr(string)
159 const char *string;
161 return strcpy((char *) alloc(strlen(string) + 1), string);
164 /*alloc.c*/