OSDN Git Service

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