4 * Copyright (c) 1997 Ben Harrison
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.
11 #ifndef INCLUDED_Z_VIRT_H
12 #define INCLUDED_Z_VIRT_H
17 * Memory management routines.
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.
23 * These routines work best as a *replacement* for malloc/free.
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.
29 * Note the macros below which simplify the details of allocation,
30 * deallocation, setting, clearing, casting, size extraction, etc.
32 * The macros MAKE/C_MAKE and KILL/C_KILL have a "procedural" metaphor,
33 * and they actually modify their arguments.
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)".
40 * Note that it is assumed that "memset()" will function correctly,
41 * in particular, that it returns its first argument.
46 /**** Available macros ****/
49 /* Size of 'N' things of type 'T' */
51 ((huge)((N)*(sizeof(T))))
53 /* Size of one thing of type 'T' */
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)))
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)))
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)))
71 /* Set every byte in a thing of type T, at location P, to V, and return P */
73 (T*)(memset((char*)(P),(V),SIZE(T)))
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)))
80 /* Wipe a thing of type T, at location P, and return P */
82 (T*)(memset((char*)(P),0,SIZE(T)))
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)))
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)))
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)))
98 /* Free one thing of type T at P, return NULL */
100 (T*)(rnfree(P,SIZE(T)))
103 /* Allocate, and return, an array of type T[N] */
104 #define C_RNEW(N,T) \
105 ((T*)(ralloc(C_SIZE(N,T))))
107 /* Allocate, and return, a thing of type T */
109 ((T*)(ralloc(SIZE(T))))
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)))
116 /* Allocate, wipe, and return a thing of type T */
118 ((T*)(WIPE(RNEW(T),T)))
121 /* Allocate a wiped array of type T[N], assign to pointer P */
122 #define C_MAKE(P,N,T) \
125 /* Allocate a wiped thing of type T, assign to pointer P */
130 /* Free an array of type T[N], at location P, and set P to NULL */
131 #define C_KILL(P,N,T) \
134 /* Free a thing of type T, at location P, and set P to NULL */
140 /**** Available variables ****/
142 /* Replacement hook for "rnfree()" */
143 extern vptr (*rnfree_aux)(vptr, huge);
145 /* Replacement hook for "rpanic()" */
146 extern vptr (*rpanic_aux)(huge);
148 /* Replacement hook for "ralloc()" */
149 extern vptr (*ralloc_aux)(huge);
152 /**** Available functions ****/
154 /* De-allocate a given amount of memory */
155 extern vptr rnfree(vptr p, huge len);
157 /* Panic, attempt to Allocate 'len' bytes */
158 extern vptr rpanic(huge len);
160 /* Allocate (and return) 'len', or dump core */
161 extern vptr ralloc(huge len);
163 /* Create a "dynamic string" */
164 extern cptr string_make(cptr str);
166 /* Free a string allocated with "string_make()" */
167 extern errr string_free(cptr str);