OSDN Git Service

[Refactor] #37353 spells.hにスペルに関する共通定義を追加。。 / Add common definition to spells.h.
[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         return (NULL);
61 }
62
63
64 /*
65  * Optional auxiliary "rpanic" function
66  */
67 vptr (*rpanic_aux)(huge) = NULL;
68
69 /*
70  * The system is out of memory, so panic.  If "rpanic_aux" is set,
71  * it can be used to free up some memory and do a new "ralloc()",
72  * or if not, it can be used to save things, clean up, and exit.
73  * By default, this function simply crashes the computer.
74  */
75 vptr rpanic(huge len)
76 {
77         /* Hopefully, we have a real "panic" function */
78         if (rpanic_aux) return ((*rpanic_aux)(len));
79
80         /* Attempt to crash before icky things happen */
81         core("Out of Memory!");
82
83         /* Paranoia */
84         return ((vptr)(NULL));
85 }
86
87
88 /*
89  * Optional auxiliary "ralloc" function
90  */
91 vptr (*ralloc_aux)(huge) = NULL;
92
93
94 /*
95  * Allocate some memory
96  */
97 vptr ralloc(huge len)
98 {
99         vptr mem;
100
101         /* Allow allocation of "zero bytes" */
102         if (len == 0) return ((vptr)(NULL));
103
104 #ifdef VERBOSE_RALLOC
105
106         /* Count allocated memory */
107         virt_make += len;
108
109         /* Log important allocations */
110         if (len > virt_size)
111         {
112                 char buf[80];
113                 sprintf(buf, "Make (%ld): %ld - %ld = %ld.",
114                         len, virt_make, virt_kill, virt_make - virt_kill);
115                 plog(buf);
116         }
117
118 #endif
119
120         /* Use the aux function if set */
121         if (ralloc_aux) mem = (*ralloc_aux)(len);
122
123         /* Use malloc() to allocate some memory */
124         else mem = ((vptr)(malloc((size_t)(len))));
125
126         /* We were able to acquire memory */
127         if (!mem) mem = rpanic(len);
128
129         /* Return the memory, if any */
130         return (mem);
131 }
132
133
134
135
136 /*
137  * Allocate a constant string, containing the same thing as 'str'
138  */
139 concptr string_make(concptr str)
140 {
141         huge len = 0;
142         concptr t = str;
143         char *s, *res;
144
145         /* Simple sillyness */
146         if (!str) return (str);
147
148         /* Get the number of chars in the string, including terminator */
149         while (str[len++]) /* loop */;
150
151         /* Allocate space for the string */
152         s = res = (char*)(ralloc(len));
153
154         /* Copy the string (with terminator) */
155         while ((*s++ = *t++) != 0) /* loop */;
156
157         /* Return the allocated, initialized, string */
158         return (res);
159 }
160
161
162 /*
163  * Un-allocate a string allocated above.
164  * Depends on no changes being made to the string.
165  */
166 errr string_free(concptr str)
167 {
168         huge len = 0;
169
170         /* Succeed on non-strings */
171         if (!str) return (0);
172
173         /* Count the number of chars in 'str' plus the terminator */
174         while (str[len++]) /* loop */;
175
176         /* Kill the buffer of chars we must have allocated above */
177         (void)rnfree((vptr)(str), len);
178
179         /* Success */
180         return (0);
181 }
182
183