X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/c6e0eaf00cd09a4e5237e70d8351049ec91d7653..a0751c36f04d55cc74c050b692a67701fe210262:/alloc.h diff --git a/alloc.h b/alloc.h index 49e66d7..feed0bf 100644 --- a/alloc.h +++ b/alloc.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: alloc.h,v 1.4 1999/12/10 23:42:04 mdw Exp $ + * $Id: alloc.h,v 1.6 2000/07/16 12:29:16 mdw Exp $ * * Memory allocation functions * @@ -30,6 +30,12 @@ /*----- Revision history --------------------------------------------------* * * $Log: alloc.h,v $ + * Revision 1.6 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * + * Revision 1.5 2000/06/17 10:35:51 mdw + * Major overhaul for arena support. + * * Revision 1.4 1999/12/10 23:42:04 mdw * Change header file guard names. * @@ -51,9 +57,74 @@ extern "C" { #endif +/*----- Required header files ---------------------------------------------*/ + #include -/*----- Functions provided ------------------------------------------------*/ +#ifndef MLIB_ARENA_H +# include "arena.h" +#endif + +/*----- Functions and macros ----------------------------------------------*/ + +/* --- @x_alloc@ --- * + * + * Arguments: @arena *a@ = pointer to underlying arena + * @size_t sz@ = size of block to allocate + * + * Returns: Pointer to allocated block. + * + * Use: Allocates memory. If there's not enough memory, the + * exception @EXC_NOMEM@ is thrown. + */ + +extern void *x_alloc(arena */*a*/, size_t /*sz*/); + +/* --- @x_strdup@ --- * + * + * Arguments: @arena *a@ = pointer to underlying arena + * @const char *s@ = pointer to a string + * + * Returns: Pointer to a copy of the string. + * + * Use: Copies a string (like @strdup@ would, if it existed). If + * there's not enough memory, the exception @EXC_NOMEM@ is + * thrown. + */ + +extern char *x_strdup(arena */*a*/, const char */*s*/); + +/* --- @x_realloc@ --- * + * + * Arguments: @arena *a@ = pointer to underlying arena + * @void *p@ = pointer to a block of memory + * @size_t sz@ = new size desired for the block + * @size_t osz@ = size of the old block + * + * Returns: Pointer to the resized memory block (which is almost + * certainly not in the same place any more). + * + * Use: Resizes a memory block. If there's not enough memory, the + * exception @EXC_NOMEM@ is thrown. + */ + +extern void *x_realloc(arena */*a*/, void */*p*/, + size_t /*sz*/, size_t /*osz*/); + +/* --- @x_free@ --- * + * + * Arguments: @arena *a@ = pointer to underlying arena + * @void *p@ = pointer to a block of memory. + * + * Returns: --- + * + * Use: Frees a block of memory. + */ + +extern void x_free(arena */*a*/, void */*p*/); +#define x_free(a, p) A_FREE(a, p) + +/*----- Old functions for the standard arena ------------------------------*/ /* --- @xmalloc@ --- * * @@ -66,6 +137,7 @@ */ extern void *xmalloc(size_t /*sz*/); +#define xmalloc(sz) x_alloc(arena_global, (sz)) /* --- @xstrdup@ --- * * @@ -79,11 +151,13 @@ extern void *xmalloc(size_t /*sz*/); */ extern char *xstrdup(const char */*s*/); +#define xstrdup(p) x_strdup(arena_global, (p)) /* --- @xrealloc@ --- * * * Arguments: @void *p@ = pointer to a block of memory * @size_t sz@ = new size desired for the block + * @size_t osz@ = size of the old block * * Returns: Pointer to the resized memory block (which is almost * certainly not in the same place any more). @@ -92,7 +166,20 @@ extern char *xstrdup(const char */*s*/); * exception @EXC_NOMEM@ is thrown. */ -extern void *xrealloc(void */*p*/, size_t /*sz*/); +extern void *xrealloc(void */*p*/, size_t /*sz*/, size_t /*osz*/); +#define xrealloc(p, sz, osz) x_realloc(arena_global, (p), (sz), (osz)) + +/* --- @xfree@ --- * + * + * Arguments: @void *p@ = pointer to a block of memory. + * + * Returns: --- + * + * Use: Frees a block of memory. + */ + +extern void xfree(void */*p*/); +#define xfree(p) x_free(arena_global, (p)) /*----- That's all, folks -------------------------------------------------*/