OSDN Git Service

v3.0.0 Alpha5 OSDN最終版
[hengband/hengband.git] / src / z-virt.h
1 /* File: z-virt.h */
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 #ifndef INCLUDED_Z_VIRT_H
12 #define INCLUDED_Z_VIRT_H
13
14 #include "h-basic.h"
15
16 /*
17  * Memory management routines.
18  *
19  * Set ralloc_aux to modify the memory allocation routine.
20  * Set rnfree_aux to modify the memory de-allocation routine.
21  * Set rpanic_aux to let the program react to memory failures.
22  *
23  * These routines work best as a *replacement* for malloc/free.
24  *
25  * The string_make() and string_free() routines handle dynamic strings.
26  * A dynamic string is a string allocated at run-time, which should not
27  * be modified once it has been created.
28  *
29  * Note the macros below which simplify the details of allocation,
30  * deallocation, setting, clearing, casting, size extraction, etc.
31  *
32  * The macros MAKE/C_MAKE and KILL/C_KILL have a "procedural" metaphor,
33  * and they actually modify their arguments.
34  *
35  * Note that, for some reason, some allocation macros may disallow
36  * "stars" in type names, but you can use typedefs to circumvent
37  * this.  For example, instead of "type **p; MAKE(p,type*);" you
38  * can use "typedef type *type_ptr; type_ptr *p; MAKE(p,type_ptr)".
39  *
40  * Note that it is assumed that "memset()" will function correctly,
41  * in particular, that it returns its first argument.
42  */
43
44
45
46 /**** Available macros ****/
47
48
49 /* Size of 'N' things of type 'T' */
50 #define C_SIZE(N,T) \
51         ((huge)((N)*(sizeof(T))))
52
53 /* Size of one thing of type 'T' */
54 #define SIZE(T) \
55         ((huge)(sizeof(T)))
56
57
58 /* Compare two arrays of type T[N], at locations P1 and P2 */
59 #define C_DIFF(P1,P2,N,T) \
60         (memcmp((char*)(P1),(char*)(P2),C_SIZE(N,T)))
61
62 /* Compare two things of type T, at locations P1 and P2 */
63 #define DIFF(P1,P2,T) \
64         (memcmp((char*)(P1),(char*)(P2),SIZE(T)))
65
66
67 /* Set every byte in an array of type T[N], at location P, to V, and return P */
68 #define C_BSET(P,V,N,T) \
69         (T*)(memset((char*)(P),(V),C_SIZE(N,T)))
70
71 /* Set every byte in a thing of type T, at location P, to V, and return P */
72 #define BSET(P,V,T) \
73         (T*)(memset((char*)(P),(V),SIZE(T)))
74
75
76 /* Wipe an array of type T[N], at location P, and return P */
77 #define C_WIPE(P,N,T) \
78         (T*)(memset((char*)(P),0,C_SIZE(N,T)))
79
80 /* Wipe a thing of type T, at location P, and return P */
81 #define WIPE(P,T) \
82         (T*)(memset((char*)(P),0,SIZE(T)))
83
84
85 /* Load an array of type T[N], at location P1, from another, at location P2 */
86 #define C_COPY(P1,P2,N,T) \
87         (T*)(memcpy((char*)(P1),(char*)(P2),C_SIZE(N,T)))
88
89 /* Load a thing of type T, at location P1, from another, at location P2 */
90 #define COPY(P1,P2,T) \
91         (T*)(memcpy((char*)(P1),(char*)(P2),SIZE(T)))
92
93
94 /* Free an array of N things of type T at P, return NULL */
95 #define C_FREE(P,N,T) \
96         (T*)(rnfree(P,C_SIZE(N,T)))
97
98 /* Free one thing of type T at P, return NULL */
99 #define FREE(P,T) \
100         (T*)(rnfree(P,SIZE(T)))
101
102
103 /* Allocate, and return, an array of type T[N] */
104 #define C_RNEW(N,T) \
105         ((T*)(ralloc(C_SIZE(N,T))))
106
107 /* Allocate, and return, a thing of type T */
108 #define RNEW(T) \
109         ((T*)(ralloc(SIZE(T))))
110
111
112 /* Allocate, wipe, and return an array of type T[N] */
113 #define C_ZNEW(N,T) \
114         ((T*)(C_WIPE(C_RNEW(N,T),N,T)))
115
116 /* Allocate, wipe, and return a thing of type T */
117 #define ZNEW(T) \
118         ((T*)(WIPE(RNEW(T),T)))
119
120
121 /* Allocate a wiped array of type T[N], assign to pointer P */
122 #define C_MAKE(P,N,T) \
123         ((P)=C_ZNEW(N,T))
124
125 /* Allocate a wiped thing of type T, assign to pointer P */
126 #define MAKE(P,T) \
127         ((P)=ZNEW(T))
128
129
130 /* Free an array of type T[N], at location P, and set P to NULL */
131 #define C_KILL(P,N,T) \
132         ((P)=C_FREE(P,N,T))
133
134 /* Free a thing of type T, at location P, and set P to NULL */
135 #define KILL(P,T) \
136         ((P)=FREE(P,T))
137
138
139
140 /**** Available variables ****/
141
142 /* Replacement hook for "rnfree()" */
143 extern vptr (*rnfree_aux)(vptr, huge);
144
145 /* Replacement hook for "rpanic()" */
146 extern vptr (*rpanic_aux)(huge);
147
148 /* Replacement hook for "ralloc()" */
149 extern vptr (*ralloc_aux)(huge);
150
151
152 /**** Available functions ****/
153
154 /* De-allocate a given amount of memory */
155 extern vptr rnfree(vptr p, huge len);
156
157 /* Panic, attempt to Allocate 'len' bytes */
158 extern vptr rpanic(huge len);
159
160 /* Allocate (and return) 'len', or dump core */
161 extern vptr ralloc(huge len);
162
163 /* Create a "dynamic string" */
164 extern concptr string_make(concptr str);
165
166 /* Free a string allocated with "string_make()" */
167 extern errr string_free(concptr str);
168
169
170
171
172 #endif
173
174
175