OSDN Git Service

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