OSDN Git Service

add translation
[jnethack/source.git] / src / alloc.c
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. */
4
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  */
10 #define EXTERN_H /* comment line for pre-compiled headers */
11 #include "config.h"
12
13 char *FDECL(fmt_ptr, (const genericptr));
14
15 #ifdef MONITOR_HEAP
16 #undef alloc
17 #undef free
18 extern void FDECL(free, (genericptr_t));
19 static void NDECL(heapmon_init);
20
21 static FILE *heaplog = 0;
22 static boolean tried_heaplog = FALSE;
23 #endif
24
25 long *FDECL(alloc, (unsigned int));
26 extern void
27 VDECL(panic, (const char *, ...))
28 PRINTF_F(1, 2);
29
30 long *
31 alloc(lth)
32 register unsigned int lth;
33 {
34 #ifdef LINT
35     /*
36      * a ridiculous definition, suppressing
37      *  "possible pointer alignment problem" for (long *) malloc()
38      * from lint
39      */
40     long dummy = ftell(stderr);
41
42     if (lth)
43         dummy = 0; /* make sure arg is used */
44     return &dummy;
45 #else
46     register genericptr_t ptr;
47
48     ptr = malloc(lth);
49 #ifndef MONITOR_HEAP
50     if (!ptr)
51         panic("Memory allocation failure; cannot get %u bytes", lth);
52 #endif
53     return (long *) ptr;
54 #endif
55 }
56
57 #ifdef HAS_PTR_FMT
58 #define PTR_FMT "%p"
59 #define PTR_TYP genericptr_t
60 #else
61 #define PTR_FMT "%06lx"
62 #define PTR_TYP unsigned long
63 #endif
64
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
70  * extra room.
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.
73  */
74 #define PTRBUFCNT 4
75 #define PTRBUFSIZ 32
76 static char ptrbuf[PTRBUFCNT][PTRBUFSIZ];
77 static int ptrbufidx = 0;
78
79 /* format a pointer for display purposes; returns a static buffer */
80 char *
81 fmt_ptr(ptr)
82 const genericptr ptr;
83 {
84     char *buf;
85
86     buf = ptrbuf[ptrbufidx];
87     if (++ptrbufidx >= PTRBUFCNT)
88         ptrbufidx = 0;
89
90     Sprintf(buf, PTR_FMT, (PTR_TYP) ptr);
91     return buf;
92 }
93
94 #ifdef MONITOR_HEAP
95
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. */
98 static void
99 heapmon_init()
100 {
101     char *logname = getenv("NH_HEAPLOG");
102
103     if (logname && *logname)
104         heaplog = fopen(logname, "w");
105     tried_heaplog = TRUE;
106 }
107
108 long *
109 nhalloc(lth, file, line)
110 unsigned int lth;
111 const char *file;
112 int line;
113 {
114     long *ptr = alloc(lth);
115
116     if (!tried_heaplog)
117         heapmon_init();
118     if (heaplog)
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 */
122     if (!ptr)
123         panic("Cannot get %u bytes, line %d of %s", lth, line, file);
124
125     return ptr;
126 }
127
128 void
129 nhfree(ptr, file, line)
130 genericptr_t ptr;
131 const char *file;
132 int line;
133 {
134     if (!tried_heaplog)
135         heapmon_init();
136     if (heaplog)
137         (void) fprintf(heaplog, "-      %s %4d %s\n",
138                        fmt_ptr((genericptr_t) ptr), line, file);
139
140     free(ptr);
141 }
142
143 /* strdup() which uses our alloc() rather than libc's malloc(),
144    with caller tracking */
145 char *
146 nhdupstr(string, file, line)
147 const char *string;
148 const char *file;
149 int line;
150 {
151     return strcpy((char *) nhalloc(strlen(string) + 1, file, line), string);
152 }
153 #undef dupstr
154
155 #endif /* MONITOR_HEAP */
156
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 */
160 char *
161 dupstr(string)
162 const char *string;
163 {
164     return strcpy((char *) alloc(strlen(string) + 1), string);
165 }
166
167 /*alloc.c*/