OSDN Git Service

Merge remote-tracking branch 'remotes/origin/feature/Refactoring-Hourier' into For2...
[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         return ((vptr)(NULL));
83 }
84
85
86 /*
87  * Optional auxiliary "ralloc" function
88  */
89 vptr (*ralloc_aux)(huge) = NULL;
90
91
92 /*
93  * Allocate some memory
94  */
95 vptr ralloc(huge len)
96 {
97         vptr mem;
98
99         /* Allow allocation of "zero bytes" */
100         if (len == 0) return ((vptr)(NULL));
101
102 #ifdef VERBOSE_RALLOC
103
104         /* Count allocated memory */
105         virt_make += len;
106
107         /* Log important allocations */
108         if (len > virt_size)
109         {
110                 char buf[80];
111                 sprintf(buf, "Make (%ld): %ld - %ld = %ld.",
112                         len, virt_make, virt_kill, virt_make - virt_kill);
113                 plog(buf);
114         }
115
116 #endif
117
118         /* Use the aux function if set */
119         if (ralloc_aux) mem = (*ralloc_aux)(len);
120
121         /* Use malloc() to allocate some memory */
122         else mem = ((vptr)(malloc((size_t)(len))));
123
124         /* We were able to acquire memory */
125         if (!mem) mem = rpanic(len);
126
127         /* Return the memory, if any */
128         return (mem);
129 }
130
131
132
133
134 /*
135  * Allocate a constant string, containing the same thing as 'str'
136  */
137 concptr string_make(concptr str)
138 {
139         huge len = 0;
140         concptr t = str;
141         char *s, *res;
142
143         /* Simple sillyness */
144         if (!str) return (str);
145
146         /* Get the number of chars in the string, including terminator */
147         while (str[len++]) /* loop */;
148
149         /* Allocate space for the string */
150         s = res = (char*)(ralloc(len));
151
152         /* Copy the string (with terminator) */
153         while ((*s++ = *t++) != 0) /* loop */;
154
155         /* Return the allocated, initialized, string */
156         return (res);
157 }
158
159
160 /*
161  * Un-allocate a string allocated above.
162  * Depends on no changes being made to the string.
163  */
164 errr string_free(concptr str)
165 {
166         huge len = 0;
167
168         /* Succeed on non-strings */
169         if (!str) return 0;
170
171         /* Count the number of chars in 'str' plus the terminator */
172         while (str[len++]) /* loop */;
173
174         /* Kill the buffer of chars we must have allocated above */
175         (void)rnfree((vptr)(str), len);
176
177         /* Success */
178         return 0;
179 }
180
181