Angband 3.0.9b.
[angband.git] / src / z-virt.h
blob491be02d4352bac8aefd92eeb2c3e895ce917270
1 /* File: z-virt.h */
3 /*
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.
9 */
11 #ifndef INCLUDED_Z_VIRT_H
12 #define INCLUDED_Z_VIRT_H
14 #include "h-basic.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 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 /* Set every byte in an array of type T[N], at location P, to V, and return P */
50 #define C_BSET(P, V, N, T) \
51 (memset((P), (V), (N) * sizeof(T)))
53 /* Set every byte in a thing of type T, at location P, to V, and return P */
54 #define BSET(P, V, T) \
55 (memset((P), (V), sizeof(T)))
58 /* Wipe an array of type T[N], at location P, and return P */
59 #define C_WIPE(P, N, T) \
60 (memset((P), 0, (N) * sizeof(T)))
62 /* Wipe a thing of type T, at location P, and return P */
63 #define WIPE(P, T) \
64 (memset((P), 0, sizeof(T)))
67 /* Load an array of type T[N], at location P1, from another, at location P2 */
68 #define C_COPY(P1, P2, N, T) \
69 (memcpy((P1), (P2), (N) * sizeof(T)))
71 /* Load a thing of type T, at location P1, from another, at location P2 */
72 #define COPY(P1, P2, T) \
73 (memcpy((P1), (P2), sizeof(T)))
76 /* Allocate, and return, an array of type T[N] */
77 #define C_RNEW(N, T) \
78 (ralloc((N) * sizeof(T)))
80 /* Allocate, and return, a thing of type T */
81 #define RNEW(T) \
82 (ralloc(sizeof(T)))
85 /* Allocate, wipe, and return an array of type T[N] */
86 #define C_ZNEW(N, T) \
87 (C_WIPE(C_RNEW(N, T), N, T))
89 /* Allocate, wipe, and return a thing of type T */
90 #define ZNEW(T) \
91 (WIPE(RNEW(T), T))
94 /* Allocate a wiped array of type T[N], assign to pointer P */
95 #define C_MAKE(P, N, T) \
96 ((P) = C_ZNEW(N, T))
98 /* Allocate a wiped thing of type T, assign to pointer P */
99 #define MAKE(P, T) \
100 ((P) = ZNEW(T))
103 /* Free one thing at P, return NULL */
104 #define FREE(P) \
105 (rnfree(P))
107 /* Free a thing at location P and set P to NULL */
108 #define KILL(P) \
109 ((P)=FREE(P))
113 /**** Available variables ****/
115 /* Replacement hook for "rnfree()" */
116 extern void* (*rnfree_aux)(void*);
118 /* Replacement hook for "rpanic()" */
119 extern void* (*rpanic_aux)(size_t);
121 /* Replacement hook for "ralloc()" */
122 extern void* (*ralloc_aux)(size_t);
125 /**** Available functions ****/
127 /* De-allocate memory */
128 extern void* rnfree(void *p);
130 /* Panic, attempt to allocate 'len' bytes */
131 extern void* rpanic(size_t len);
133 /* Allocate (and return) 'len', or quit */
134 extern void* ralloc(size_t len);
136 /* Create a "dynamic string" */
137 extern cptr string_make(cptr str);
139 /* Free a string allocated with "string_make()" */
140 extern errr string_free(cptr str);
142 #endif /* INCLUDED_Z_VIRT_H */