OSDN Git Service

[Refactor] #37353 型の置換。 / Type replacement.
[hengband/hengband.git] / src / z-virt.c
1 /* File: z-virt.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.
9  */
10
11 /* Purpose: Memory management routines -BEN- */
12
13 #include "z-virt.h"
14 #include "z-util.h"
15
16 /*
17  * Allow debugging messages to track memory usage.
18  */
19 #ifdef VERBOSE_RALLOC
20 static long virt_make = 0;
21 static long virt_kill = 0;
22 static long virt_size = 0;
23 #endif
24
25
26 /*
27  * Optional auxiliary "rnfree" function
28  */
29 vptr (*rnfree_aux)(vptr, huge) = NULL;
30
31 /*
32  * Free some memory (allocated by ralloc), return NULL
33  */
34 vptr rnfree(vptr p, huge len)
35 {
36         /* Easy to free zero bytes */
37         if (len == 0) return (NULL);
38
39 #ifdef VERBOSE_RALLOC
40
41         /* Decrease memory count */
42         virt_kill += len;
43
44         if (len > virt_size)
45         {
46                 char buf[80];
47                 sprintf(buf, "Kill (%ld): %ld - %ld = %ld.",
48                         len, virt_make, virt_kill, virt_make - virt_kill);
49                 plog(buf);
50         }
51
52 #endif
53
54         /* Use the "aux" function */
55         if (rnfree_aux) return ((*rnfree_aux)(p, len));
56
57         /* Use "free" */
58         free ((char*)(p));
59
60         /* Done */
61         return (NULL);
62 }
63
64
65 /*
66  * Optional auxiliary "rpanic" function
67  */
68 vptr (*rpanic_aux)(huge) = NULL;
69
70 /*
71  * The system is out of memory, so panic.  If "rpanic_aux" is set,
72  * it can be used to free up some memory and do a new "ralloc()",
73  * or if not, it can be used to save things, clean up, and exit.
74  * By default, this function simply crashes the computer.
75  */
76 vptr rpanic(huge len)
77 {
78         /* Hopefully, we have a real "panic" function */
79         if (rpanic_aux) return ((*rpanic_aux)(len));
80
81         /* Attempt to crash before icky things happen */
82         core("Out of Memory!");
83
84         /* Paranoia */
85         return ((vptr)(NULL));
86 }
87
88
89 /*
90  * Optional auxiliary "ralloc" function
91  */
92 vptr (*ralloc_aux)(huge) = NULL;
93
94
95 /*
96  * Allocate some memory
97  */
98 vptr ralloc(huge len)
99 {
100         vptr mem;
101
102         /* Allow allocation of "zero bytes" */
103         if (len == 0) return ((vptr)(NULL));
104
105 #ifdef VERBOSE_RALLOC
106
107         /* Count allocated memory */
108         virt_make += len;
109
110         /* Log important allocations */
111         if (len > virt_size)
112         {
113                 char buf[80];
114                 sprintf(buf, "Make (%ld): %ld - %ld = %ld.",
115                         len, virt_make, virt_kill, virt_make - virt_kill);
116                 plog(buf);
117         }
118
119 #endif
120
121         /* Use the aux function if set */
122         if (ralloc_aux) mem = (*ralloc_aux)(len);
123
124         /* Use malloc() to allocate some memory */
125         else mem = ((vptr)(malloc((size_t)(len))));
126
127         /* We were able to acquire memory */
128         if (!mem) mem = rpanic(len);
129
130         /* Return the memory, if any */
131         return (mem);
132 }
133
134
135
136
137 /*
138  * Allocate a constant string, containing the same thing as 'str'
139  */
140 cptr string_make(cptr str)
141 {
142         huge len = 0;
143         cptr t = str;
144         char *s, *res;
145
146         /* Simple sillyness */
147         if (!str) return (str);
148
149         /* Get the number of chars in the string, including terminator */
150         while (str[len++]) /* loop */;
151
152         /* Allocate space for the string */
153         s = res = (char*)(ralloc(len));
154
155         /* Copy the string (with terminator) */
156         while ((*s++ = *t++) != 0) /* loop */;
157
158         /* Return the allocated, initialized, string */
159         return (res);
160 }
161
162
163 /*
164  * Un-allocate a string allocated above.
165  * Depends on no changes being made to the string.
166  */
167 errr string_free(cptr str)
168 {
169         huge len = 0;
170
171         /* Succeed on non-strings */
172         if (!str) return (0);
173
174         /* Count the number of chars in 'str' plus the terminator */
175         while (str[len++]) /* loop */;
176
177         /* Kill the buffer of chars we must have allocated above */
178         (void)rnfree((vptr)(str), len);
179
180         /* Success */
181         return (0);
182 }
183
184