X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/c6e0eaf00cd09a4e5237e70d8351049ec91d7653..76a7638e4b77ace1b96e1399f8a11e09fb3afede:/alloc.h diff --git a/alloc.h b/alloc.h index 49e66d7..e06fb00 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.7 2004/04/08 01:36:11 mdw Exp $ * * Memory allocation functions * @@ -27,33 +27,81 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* +#ifndef MLIB_ALLOC_H +#define MLIB_ALLOC_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Required header files ---------------------------------------------*/ + +#include + +#ifndef MLIB_ARENA_H +# include "arena.h" +#endif + +/*----- Functions and macros ----------------------------------------------*/ + +/* --- @x_alloc@ --- * * - * $Log: alloc.h,v $ - * Revision 1.4 1999/12/10 23:42:04 mdw - * Change header file guard names. + * Arguments: @arena *a@ = pointer to underlying arena + * @size_t sz@ = size of block to allocate * - * Revision 1.3 1999/05/06 19:51:35 mdw - * Reformatted the LGPL notice a little bit. + * Returns: Pointer to allocated block. * - * Revision 1.2 1999/05/05 18:50:31 mdw - * Change licensing conditions to LGPL. + * 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@ --- * * - * Revision 1.1.1.1 1998/06/17 23:44:42 mdw - * Initial version of mLib + * 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. */ -#ifndef MLIB_ALLOC_H -#define MLIB_ALLOC_H +extern char *x_strdup(arena */*a*/, const char */*s*/); -#ifdef __cplusplus - extern "C" { -#endif +/* --- @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. + */ -#include +extern void *x_realloc(arena */*a*/, void */*p*/, + size_t /*sz*/, size_t /*osz*/); -/*----- Functions provided ------------------------------------------------*/ +/* --- @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 +114,7 @@ */ extern void *xmalloc(size_t /*sz*/); +#define xmalloc(sz) x_alloc(arena_global, (sz)) /* --- @xstrdup@ --- * * @@ -79,11 +128,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 +143,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 -------------------------------------------------*/