Z-virt.h

/* File: z-virt.h */ /* * Copyright (c) 1997 Ben Harrison * * This software may be copied and distributed for educational, research, * and not for profit purposes provided that this copyright and statement * are included in all such copies. */ /* * Memory management routines. * * Set ralloc_aux to modify the memory allocation routine. * Set rnfree_aux to modify the memory de-allocation routine. * Set rpanic_aux to let the program react to memory failures. * * These routines work best as a *replacement* for malloc/free. * * The string_make and string_free routines handle dynamic strings. * A dynamic string is a string allocated at run-time, which should not * be modified once it has been created. * * Note the macros below which simplify the details of allocation, * deallocation, setting, clearing, casting, size extraction, etc. * * The macros MAKE/C_MAKE and KILL have a "procedural" metaphor, * and they actually modify their arguments. * * Note that, for some reason, some allocation macros may disallow * "stars" in type names, but you can use typedefs to circumvent * this. For example, instead of "type **p; MAKE(p,type*);" you * can use "typedef type *type_ptr; type_ptr *p; MAKE(p,type_ptr)". * * Note that it is assumed that "memset" will function correctly, * in particular, that it returns its first argument. */ /**** Available macros ****/ /* Set every byte in an array of type T[N], at location P, to V, and return P */ (memset((P), (V), (N) * sizeof(T))) /* Set every byte in a thing of type T, at location P, to V, and return P */ (memset((P), (V), sizeof(T))) /* Wipe an array of type T[N], at location P, and return P */ (memset((P), 0, (N) * sizeof(T))) /* Wipe a thing of type T, at location P, and return P */ (memset((P), 0, sizeof(T))) /* Load an array of type T[N], at location P1, from another, at location P2 */ (memcpy((P1), (P2), (N) * sizeof(T))) /* Load a thing of type T, at location P1, from another, at location P2 */ (memcpy((P1), (P2), sizeof(T))) /* Allocate, and return, an array of type T[N] */ (ralloc((N) * sizeof(T))) /* Allocate, and return, a thing of type T */ (ralloc(sizeof(T))) /* Allocate, wipe, and return an array of type T[N] */ (C_WIPE(C_RNEW(N, T), N, T)) /* Allocate, wipe, and return a thing of type T */ (WIPE(RNEW(T), T)) /* Allocate a wiped array of type T[N], assign to pointer P */ ((P) = C_ZNEW(N, T)) /* Allocate a wiped thing of type T, assign to pointer P */ ((P) = ZNEW(T)) /* Free one thing at P, return NULL */ (rnfree(P)) /* Free a thing at location P and set P to NULL */ ((P)=FREE(P)) /**** Available variables ****/ /* Replacement hook for "rnfree" */ extern void* (*rnfree_aux)(void*); /* Replacement hook for "rpanic" */ extern void* (*rpanic_aux)(size_t); /* Replacement hook for "ralloc" */ extern void* (*ralloc_aux)(size_t); /**** Available functions ****/ /* De-allocate memory */ extern void* rnfree(void *p); /* Panic, attempt to allocate 'len' bytes */ extern void* rpanic(size_t len); /* Allocate (and return) 'len', or quit */ extern void* ralloc(size_t len); /* Create a "dynamic string" */ extern cptr string_make(cptr str); /* Free a string allocated with "string_make" */ extern errr string_free(cptr str);
 * 1) ifndef INCLUDED_Z_VIRT_H
 * 2) define INCLUDED_Z_VIRT_H
 * 1) include "h-basic.h"
 * 1) define C_BSET(P, V, N, T) \
 * 1) define BSET(P, V, T) \
 * 1) define C_WIPE(P, N, T) \
 * 1) define WIPE(P, T) \
 * 1) define C_COPY(P1, P2, N, T) \
 * 1) define COPY(P1, P2, T) \
 * 1) define C_RNEW(N, T) \
 * 1) define RNEW(T) \
 * 1) define C_ZNEW(N, T) \
 * 1) define ZNEW(T) \
 * 1) define C_MAKE(P, N, T) \
 * 1) define MAKE(P, T)]] \
 * 1) define FREE(P) \
 * 1) define KILL(P) \
 * 1) endif /* INCLUDED_Z_VIRT_H */