From: mdw Date: Sun, 16 Jul 2000 12:31:50 +0000 (+0000) Subject: Change to arena `realloc' interface, to fix a design bug. X-Git-Tag: 2.0.4~169 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/commitdiff_plain/b5ea4de3ac1c61ef84c21f4e3ceff2181d23e456 Change to arena `realloc' interface, to fix a design bug. --- diff --git a/alloc.c b/alloc.c index d425e97..9bc536e 100644 --- a/alloc.c +++ b/alloc.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: alloc.c,v 1.4 2000/06/17 10:35:51 mdw Exp $ + * $Id: alloc.c,v 1.5 2000/07/16 12:29:16 mdw Exp $ * * Memory allocation functions * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: alloc.c,v $ + * Revision 1.5 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * * Revision 1.4 2000/06/17 10:35:51 mdw * Major overhaul for arena support. * @@ -104,6 +107,7 @@ char *x_strdup(arena *a, const char *s) * 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). @@ -112,9 +116,9 @@ char *x_strdup(arena *a, const char *s) * exception @EXC_NOMEM@ is thrown. */ -void *x_realloc(arena *a, void *p, size_t sz) +void *x_realloc(arena *a, void *p, size_t sz, size_t osz) { - p = A_REALLOC(a, p, sz); + p = A_REALLOC(a, p, sz, osz); if (!p) THROW(EXC_NOMEM); return (p); @@ -163,6 +167,7 @@ char *(xstrdup)(const char *s) { return xstrdup(s); } * * 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). @@ -171,7 +176,8 @@ char *(xstrdup)(const char *s) { return xstrdup(s); } * exception @EXC_NOMEM@ is thrown. */ -void *(xrealloc)(void *p, size_t sz) { return xrealloc(p, sz); } +void *(xrealloc)(void *p, size_t sz, size_t osz) +{ return xrealloc(p, sz, osz); } /* --- @xfree@ --- * * diff --git a/alloc.h b/alloc.h index edf555a..feed0bf 100644 --- a/alloc.h +++ b/alloc.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: alloc.h,v 1.5 2000/06/17 10:35:51 mdw Exp $ + * $Id: alloc.h,v 1.6 2000/07/16 12:29:16 mdw Exp $ * * Memory allocation functions * @@ -30,6 +30,9 @@ /*----- 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. * @@ -96,6 +99,7 @@ extern char *x_strdup(arena */*a*/, const char */*s*/); * 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). @@ -104,7 +108,8 @@ extern char *x_strdup(arena */*a*/, const char */*s*/); * exception @EXC_NOMEM@ is thrown. */ -extern void *x_realloc(arena */*a*/, void */*p*/, size_t /*sz*/); +extern void *x_realloc(arena */*a*/, void */*p*/, + size_t /*sz*/, size_t /*osz*/); /* --- @x_free@ --- * * @@ -152,6 +157,7 @@ extern char *xstrdup(const char */*s*/); * * 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). @@ -160,8 +166,8 @@ extern char *xstrdup(const char */*s*/); * exception @EXC_NOMEM@ is thrown. */ -extern void *xrealloc(void */*p*/, size_t /*sz*/); -#define xrealloc(p, sz) x_realloc(arena_global, (p), (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@ --- * * diff --git a/arena.c b/arena.c index 20d286c..3e13bf2 100644 --- a/arena.c +++ b/arena.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: arena.c,v 1.1 2000/06/17 10:37:53 mdw Exp $ + * $Id: arena.c,v 1.2 2000/07/16 12:29:16 mdw Exp $ * * Abstraction for memory allocation arenas * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: arena.c,v $ + * Revision 1.2 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * * Revision 1.1 2000/06/17 10:37:53 mdw * Basic arena management. * @@ -44,7 +47,8 @@ /*----- The standard arena ------------------------------------------------*/ static void *_alloc(arena *a, size_t sz) { return malloc(sz); } -static void *_realloc(arena *a, void *p, size_t sz){ return realloc(p, sz); } +static void *_realloc(arena *a, void *p, size_t sz, size_t osz) + { return realloc(p, sz); } static void _free(arena *a, void *p) { free (p); } static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 }; @@ -61,6 +65,7 @@ arena *arena_global = &arena_stdlib; * Arguments: @arena *a@ = pointer to arena block * @void *p@ = pointer to memory block to resize * @size_t sz@ = size desired for the block + * @size_t osz@ = size of the old block * * Returns: --- * @@ -68,12 +73,12 @@ arena *arena_global = &arena_stdlib; * support @realloc@ properly. */ -void *arena_fakerealloc(arena *a, void *p, size_t sz) +void *arena_fakerealloc(arena *a, void *p, size_t sz, size_t osz) { void *q = A_ALLOC(a, sz); if (!q) return (0); - memcpy(q, p, sz); + memcpy(q, p, sz > osz ? osz : sz); A_FREE(a, p); return (q); } @@ -81,7 +86,8 @@ void *arena_fakerealloc(arena *a, void *p, size_t sz) /* --- Function equivalents of the macros --- */ void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); } -void *a_realloc(arena *a, void *p, size_t sz) { return A_REALLOC(a, p, sz); } +void *a_realloc(arena *a, void *p, size_t sz, size_t osz) +{ return A_REALLOC(a, p, sz, osz); } void a_free(arena *a, void *p) { A_FREE(a, p); } /*----- That's all, folks -------------------------------------------------*/ diff --git a/arena.h b/arena.h index 6ceef09..4b1dffe 100644 --- a/arena.h +++ b/arena.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: arena.h,v 1.1 2000/06/17 10:37:53 mdw Exp $ + * $Id: arena.h,v 1.2 2000/07/16 12:29:16 mdw Exp $ * * Abstraction for memory allocation arenas * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: arena.h,v $ + * Revision 1.2 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * * Revision 1.1 2000/06/17 10:37:53 mdw * Basic arena management. * @@ -56,7 +59,7 @@ typedef struct arena { typedef struct arena_ops { void *(*alloc)(arena */*a*/, size_t /*sz*/); - void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/); + void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/, size_t /*osz*/); void (*free)(arena */*a*/, void */*p*/); void (*purge)(arena */*a*/); } arena_ops; @@ -73,6 +76,7 @@ extern arena arena_stdlib; /* Arena based on @malloc@/@free@ */ * Arguments: @arena *a@ = pointer to arena block * @void *p@ = pointer to memory block to resize * @size_t sz@ = size desired for the block + * @size_t osz@ = size of the old block * * Returns: --- * @@ -80,18 +84,20 @@ extern arena arena_stdlib; /* Arena based on @malloc@/@free@ */ * support @realloc@ properly. */ -extern void *arena_fakerealloc(arena */*a*/, void */*p*/, size_t /*sz*/); +extern void *arena_fakerealloc(arena */*a*/, void */*p*/, + size_t /*sz*/, size_t /*osz*/); /* --- Useful macros --- */ #define A_ALLOC(a, sz) (((a)->ops->alloc)((a), (sz))) -#define A_REALLOC(a, p, sz) (((a)->ops->realloc)((a), (p), (sz))) +#define A_REALLOC(a, p, sz, osz) (((a)->ops->realloc)((a), (p), (sz), (osz))) #define A_FREE(a, p) (((a)->ops->free)((a), (p))) /* --- Simple function equivalents --- */ extern void *a_alloc(arena */*a*/, size_t /*sz*/); -extern void *a_realloc(arena */*a*/, void */*p*/, size_t /*sz*/); +extern void *a_realloc(arena */*a*/, void */*p*/, + size_t /*sz*/, size_t /*osz*/); extern void a_free(arena */*a*/, void */*p*/); /*----- That's all, folks -------------------------------------------------*/ diff --git a/darray.c b/darray.c index 9e3eb90..c1ee606 100644 --- a/darray.c +++ b/darray.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: darray.c,v 1.5 2000/06/17 10:37:39 mdw Exp $ + * $Id: darray.c,v 1.6 2000/07/16 12:29:16 mdw Exp $ * * Dynamically growing dense arrays * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: darray.c,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:37:39 mdw * Add support for arena management. * @@ -144,7 +147,7 @@ void *da_ensure(da_base *b, void *v, size_t sz, size_t n) */ if (p && slots == b->off) { - q = x_realloc(b->a, p - b->off * sz, nsz * sz); + q = x_realloc(b->a, p - b->off * sz, nsz * sz, b->sz + b->off); q += slots * sz; } else { q = x_alloc(b->a, nsz * sz); diff --git a/dstr.c b/dstr.c index d76ccf5..273c53a 100644 --- a/dstr.c +++ b/dstr.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: dstr.c,v 1.14 2000/06/17 10:37:39 mdw Exp $ + * $Id: dstr.c,v 1.15 2000/07/16 12:29:16 mdw Exp $ * * Handle dynamically growing strings * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: dstr.c,v $ + * Revision 1.15 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * * Revision 1.14 2000/06/17 10:37:39 mdw * Add support for arena management. * @@ -159,7 +162,7 @@ void dstr_ensure(dstr *d, size_t sz) do nsz <<= 1; while (nsz < rq); if (d->buf) - d->buf = x_realloc(d->a, d->buf, nsz); + d->buf = x_realloc(d->a, d->buf, nsz, d->sz); else d->buf = x_alloc(d->a, nsz); d->sz = nsz; @@ -240,7 +243,7 @@ void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); } void dstr_tidy(dstr *d) { - d->buf = x_realloc(d->a, d->buf, d->len + 1); + d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz); d->buf[d->len] = 0; d->sz = d->len + 1; } diff --git a/hash.c b/hash.c index 3945755..9483e10 100644 --- a/hash.c +++ b/hash.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: hash.c,v 1.2 2000/06/17 10:37:39 mdw Exp $ + * $Id: hash.c,v 1.3 2000/07/16 12:29:16 mdw Exp $ * * General hashtable infrastructure * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: hash.c,v $ + * Revision 1.3 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * * Revision 1.2 2000/06/17 10:37:39 mdw * Add support for arena management. * @@ -124,7 +127,9 @@ int hash_extend(hash_table *t) /* --- Allocate a new hash bin vector --- */ - if ((v = A_REALLOC(t->a, t->v, m * 2 * sizeof(hash_base *))) == 0) { + if ((v = A_REALLOC(t->a, t->v, + 2 * m * sizeof(hash_base *), + m * sizeof(hash_base *))) == 0) { return (0); } t->v = v; diff --git a/man/alloc.3 b/man/alloc.3 index ed3a036..295ae2c 100644 --- a/man/alloc.3 +++ b/man/alloc.3 @@ -9,13 +9,13 @@ alloc \- mLib low-level memory allocation .nf .B "#include " -.BI "void *x_alloc(size_t " sz ); -.BI "char *x_strdup(const char *" s ); -.BI "void *x_realloc(void *" p ", size_t " sz ); -.BI "void x_free(void *" p ); +.BI "void *x_alloc(arena *" a ", size_t " sz ); +.BI "char *x_strdup(arena *" a ", const char *" s ); +.BI "void *x_realloc(arena *" a ", void *" p ", size_t " sz ", size_t " osz ); +.BI "void x_free(arena *" a ", void *" p ); .BI "void *xmalloc(size_t " sz ); -.BI "void *xrealloc(void *" p ", size_t " sz ); +.BI "void *xrealloc(void *" p ", size_t " sz ", size_t " osz ); .BI "char *xstrdup(const char *" s ); .BI "void xfree(void *" p ); .fi diff --git a/man/arena.3 b/man/arena.3 index d7631c2..5b45099 100644 --- a/man/arena.3 +++ b/man/arena.3 @@ -18,14 +18,15 @@ arena \- control of memory allocation .BI "arena *arena_global;" .BI "arena arena_stdlib;" -.BI "void *arena_fakerealloc(arena *" a ", void *" p ", size_t " sz ); +.BI "void *arena_fakerealloc(arena *" a ", void *" p , +.BI " size_t " sz ", size_t " osz ); .BI "void *a_alloc(arena *" a ", size_t " sz ); -.BI "void *a_realloc(arena *" a ", void *" p ", size_t " sz ); +.BI "void *a_realloc(arena *" a ", void *" p ", size_t " sz ", size_t " osz ); .BI "void a_free(arena *" a ); .BI "void *A_ALLOC(arena *" a ", size_t " sz ); -.BI "void *A_REALLOC(arena *" a ", void *" p ", size_t " sz ); +.BI "void *A_REALLOC(arena *" a ", void *" p ", size_t " sz ", size_t " osz ); .BI "void A_FREE(arena *" a ); .fi .SH "DESCRIPTION" @@ -60,6 +61,14 @@ function-call equivalents with lower-case names too. For a more convenient interface to memory allocation, see .BR alloc (3). .PP +.B Note: +The +.B realloc +function has an extra argument +.I osz +specifying the old size of the block. This is for the benefit of arena +handlers which can't easily find the old block's size. +.PP .SS "Defining new arenas" An .B arena @@ -81,10 +90,15 @@ memory allocation tasks: Allocates a block of memory, of at least .I sz bytes in size, appropriately aligned, and returns its address. +.nf .TP -.BI "void *(*" realloc ")(arena *" a ", void *" p ", size_t " sz ); +.BI "void *(*" realloc ")(arena *" a ", void *" p ", size_t " sz ", size_t " osz ); +.fi Resizes the block pointed to by .IR p , +with +.I osz +interesting bytes in it, so that it is at least .I sz bytes long. You can use diff --git a/pkbuf.c b/pkbuf.c index 73e2ca5..31830e0 100644 --- a/pkbuf.c +++ b/pkbuf.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: pkbuf.c,v 1.1 2000/06/17 10:39:19 mdw Exp $ + * $Id: pkbuf.c,v 1.2 2000/07/16 12:29:16 mdw Exp $ * * Simple packet buffering * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: pkbuf.c,v $ + * Revision 1.2 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * * Revision 1.1 2000/06/17 10:39:19 mdw * Experimental new support for packet buffering. * @@ -206,7 +209,7 @@ void pkbuf_want(pkbuf *pk, size_t want) do pk->sz <<= 1; while (want < pk->sz); if (pk->buf) { if (pk->len) - pk->buf = x_realloc(pk->a, pk->buf, pk->sz); + pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len); else { x_free(pk->a, pk->buf); pk->buf = 0;