forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.c
More file actions
165 lines (140 loc) · 4.12 KB
/
alloc.c
File metadata and controls
165 lines (140 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* NetHack 3.6 alloc.c $NHDT-Date: 1454376505 2016/02/02 01:28:25 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.16 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
/* to get the malloc() prototype from system.h */
#define ALLOC_C /* comment line for pre-compiled headers */
/* since this file is also used in auxiliary programs, don't include all the
function declarations for all of nethack */
#define EXTERN_H /* comment line for pre-compiled headers */
#include "config.h"
char *FDECL(fmt_ptr, (const genericptr));
#ifdef MONITOR_HEAP
#undef alloc
#undef free
extern void FDECL(free, (genericptr_t));
static void NDECL(heapmon_init);
static FILE *heaplog = 0;
static boolean tried_heaplog = FALSE;
#endif
long *FDECL(alloc, (unsigned int));
extern void VDECL(panic, (const char *, ...)) PRINTF_F(1, 2);
long *
alloc(lth)
register unsigned int lth;
{
#ifdef LINT
/*
* a ridiculous definition, suppressing
* "possible pointer alignment problem" for (long *) malloc()
* from lint
*/
long dummy = ftell(stderr);
if (lth)
dummy = 0; /* make sure arg is used */
return &dummy;
#else
register genericptr_t ptr;
ptr = malloc(lth);
#ifndef MONITOR_HEAP
if (!ptr)
panic("Memory allocation failure; cannot get %u bytes", lth);
#endif
return (long *) ptr;
#endif
}
#ifdef HAS_PTR_FMT
#define PTR_FMT "%p"
#define PTR_TYP genericptr_t
#else
#define PTR_FMT "%06lx"
#define PTR_TYP unsigned long
#endif
/* A small pool of static formatting buffers.
* PTRBUFSIZ: We assume that pointers will be formatted as integers in
* hexadecimal, requiring 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 *
fmt_ptr(ptr)
const genericptr ptr;
{
char *buf;
buf = ptrbuf[ptrbufidx];
if (++ptrbufidx >= PTRBUFCNT)
ptrbufidx = 0;
Sprintf(buf, PTR_FMT, (PTR_TYP) ptr);
return buf;
}
#ifdef MONITOR_HEAP
/* If ${NH_HEAPLOG} is defined and we can create a file by that name,
then we'll log the allocation and release information to that file. */
static void
heapmon_init()
{
char *logname = getenv("NH_HEAPLOG");
if (logname && *logname)
heaplog = fopen(logname, "w");
tried_heaplog = TRUE;
}
long *
nhalloc(lth, file, line)
unsigned int lth;
const char *file;
int line;
{
long *ptr = alloc(lth);
if (!tried_heaplog)
heapmon_init();
if (heaplog)
(void) fprintf(heaplog, "+%5u %s %4d %s\n", lth,
fmt_ptr((genericptr_t) ptr), line, file);
/* potential panic in alloc() was deferred til here */
if (!ptr)
panic("Cannot get %u bytes, line %d of %s", lth, line, file);
return ptr;
}
void
nhfree(ptr, file, line)
genericptr_t ptr;
const char *file;
int line;
{
if (!tried_heaplog)
heapmon_init();
if (heaplog)
(void) fprintf(heaplog, "- %s %4d %s\n",
fmt_ptr((genericptr_t) ptr), line, file);
free(ptr);
}
/* strdup() which uses our alloc() rather than libc's malloc(),
with caller tracking */
char *
nhdupstr(string, file, line)
const char *string;
const char *file;
int line;
{
return strcpy((char *) nhalloc(strlen(string) + 1, file, line), string);
}
#undef dupstr
#endif /* MONITOR_HEAP */
/* strdup() which uses our alloc() rather than libc's malloc();
not used when MONITOR_HEAP is enabled, but included unconditionally
in case utility programs get built using a different setting for that */
char *
dupstr(string)
const char *string;
{
return strcpy((char *) alloc(strlen(string) + 1), string);
}
/*alloc.c*/