From: mdw Date: Sat, 17 Jun 2000 10:39:00 +0000 (+0000) Subject: Add support for arena management. X-Git-Tag: 2.0.4~178 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/commitdiff_plain/20eb516fdebd2fb901e6a09ffa7e741cfb8e3a83 Add support for arena management. --- diff --git a/darray.c b/darray.c index 83150f7..9e3eb90 100644 --- a/darray.c +++ b/darray.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: darray.c,v 1.4 1999/11/06 12:40:45 mdw Exp $ + * $Id: darray.c,v 1.5 2000/06/17 10:37:39 mdw Exp $ * * Dynamically growing dense arrays * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: darray.c,v $ + * Revision 1.5 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.4 1999/11/06 12:40:45 mdw * Minor changes to allocation strategy. * @@ -51,6 +54,7 @@ #include #include "alloc.h" +#include "arena.h" #include "darray.h" /*----- Magic numbers -----------------------------------------------------*/ @@ -140,14 +144,14 @@ void *da_ensure(da_base *b, void *v, size_t sz, size_t n) */ if (p && slots == b->off) { - q = xrealloc(p - b->off * sz, nsz * sz); + q = x_realloc(b->a, p - b->off * sz, nsz * sz); q += slots * sz; } else { - q = xmalloc(nsz * sz); + q = x_alloc(b->a, nsz * sz); q += slots * sz; if (p) { memcpy(q, p, b->len * sz); - free(p - b->off * sz); + x_free(b->a, p - b->off * sz); } } @@ -246,11 +250,11 @@ void *da_shunt(da_base *b, void *v, size_t sz, size_t n) * almost all the time -- that's the whole point of this routine! */ - q = xmalloc(nsz * sz); + q = x_alloc(b->a, nsz * sz); q += (nsz - slots) * sz; if (p) { memcpy(q, p, b->len * sz); - free(p - b->off * sz); + x_free(b->a, p - b->off * sz); } /* --- Fill in the other parts of the base structure --- */ @@ -286,13 +290,13 @@ void *da_tidy(da_base *b, void *v, size_t sz) return (p); if (!b->len) { - free(p - b->off * sz); + xfree(p - b->off * sz); return (0); } - q = xmalloc(b->len * sz); + q = x_alloc(b->a, b->len * sz); memcpy(q, p, b->len * sz); - free(p - b->off * sz); + x_free(b->a, p - b->off * sz); b->sz = b->len; b->off = 0; return (q); diff --git a/darray.h b/darray.h index 065c507..b8f17eb 100644 --- a/darray.h +++ b/darray.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: darray.h,v 1.4 1999/12/10 23:42:04 mdw Exp $ + * $Id: darray.h,v 1.5 2000/06/17 10:37:39 mdw Exp $ * * Dynamically growing dense arrays * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: darray.h,v $ + * Revision 1.5 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.4 1999/12/10 23:42:04 mdw * Change header file guard names. * @@ -104,6 +107,7 @@ typedef struct da_base { size_t len; /* Length of useful portion */ size_t off; /* Offset of @v@ into space */ unsigned push, unshift; /* Pushes/unshifts since growth */ + arena *a; /* Pointer to allocation arena */ } da_base; /* --- @DA_DECL@ --- * @@ -119,7 +123,7 @@ typedef struct da_base { /*----- Initialization, creation and destruction --------------------------*/ -#define DA_INIT { { 0, 0, 0, 0, 0 }, 0 } /* Standard initializer */ +#define DA_INIT { { 0, 0, 0, 0, 0, &arena_stdlib }, 0 } /* --- @DA_CREATE@ --- * * @@ -128,11 +132,12 @@ typedef struct da_base { * Use: Initializes an array block. */ -#define DA_CREATE(a) do { \ - (a)->b.sz = (a)->b.len = 0; \ - (a)->b.off = 0; \ - (a)->b.push = (a)->b.unshift = 0; \ - (a)->v = 0; \ +#define DA_CREATE(aa) do { \ + (aa)->b.sz = (aa)->b.len = 0; \ + (aa)->b.off = 0; \ + (aa)->b.push = (aa)->b.unshift = 0; \ + (aa)->b.a = &arena_stdlib; \ + (aa)->v = 0; \ } while (0) /* --- @DA_DESTROY@ --- * @@ -142,10 +147,10 @@ typedef struct da_base { * Use: Destroys an array. The array is left valid but empty. */ -#define DA_DESTROY(a) do { \ - if ((a)->v) \ - free((a)->v - (a)->b.off); \ - DA_CREATE(a); \ +#define DA_DESTROY(aa) do { \ + if ((aa)->v) \ + x_free((aa)->b.a, (aa)->v - (aa)->b.off); \ + DA_CREATE(aa); \ } while (0) /*----- Storage reservation -----------------------------------------------*/ diff --git a/dstr.c b/dstr.c index 4df8f72..d76ccf5 100644 --- a/dstr.c +++ b/dstr.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: dstr.c,v 1.13 1999/12/22 15:39:28 mdw Exp $ + * $Id: dstr.c,v 1.14 2000/06/17 10:37:39 mdw Exp $ * * Handle dynamically growing strings * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: dstr.c,v $ + * Revision 1.14 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.13 1999/12/22 15:39:28 mdw * Fix overflow in dstr_putline. * @@ -156,9 +159,9 @@ void dstr_ensure(dstr *d, size_t sz) do nsz <<= 1; while (nsz < rq); if (d->buf) - d->buf = xrealloc(d->buf, nsz); + d->buf = x_realloc(d->a, d->buf, nsz); else - d->buf = xmalloc(nsz); + d->buf = x_alloc(d->a, nsz); d->sz = nsz; } @@ -237,7 +240,7 @@ void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); } void dstr_tidy(dstr *d) { - d->buf = xrealloc(d->buf, d->len + 1); + d->buf = x_realloc(d->a, d->buf, d->len + 1); d->buf[d->len] = 0; d->sz = d->len + 1; } diff --git a/dstr.h b/dstr.h index 6bc33f8..538dd33 100644 --- a/dstr.h +++ b/dstr.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: dstr.h,v 1.10 1999/12/22 15:39:51 mdw Exp $ + * $Id: dstr.h,v 1.11 2000/06/17 10:37:39 mdw Exp $ * * Handle dynamically growing strings * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: dstr.h,v $ + * Revision 1.11 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.10 1999/12/22 15:39:51 mdw * Fix argument reuse in DPUTS. * @@ -86,15 +89,24 @@ #include #include +#ifndef MLIB_ALLOC_H +# include "alloc.h" +#endif + +#ifndef MLIB_ARENA_H +# include "arena.h" +#endif + /*----- Data structures ---------------------------------------------------*/ typedef struct dstr { char *buf; /* Pointer to string buffer */ size_t sz; /* Size of the buffer */ size_t len; /* Length of the string */ + arena *a; /* Pointer to arena */ } dstr; -#define DSTR_INIT { 0, 0, 0 } /* How to initialize one */ +#define DSTR_INIT { 0, 0, 0, &arena_stdlib } /* How to initialize one */ /*----- Functions provided ------------------------------------------------*/ @@ -114,6 +126,7 @@ extern void dstr_create(dstr */*d*/); _dd->buf = 0; \ _dd->sz = 0; \ _dd->len = 0; \ + _dd->a = &arena_stdlib; \ } while (0) /* --- @dstr_destroy@ --- * @@ -130,7 +143,7 @@ extern void dstr_destroy(dstr */*d*/); #define DDESTROY(d) do { \ dstr *_d = (d); \ if (_d->buf) \ - free(_d->buf); \ + x_free(_d->a, _d->buf); \ DCREATE(_d); \ } while (0) diff --git a/env.c b/env.c index 4a4fbe5..d648b79 100644 --- a/env.c +++ b/env.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: env.c,v 1.1 1999/07/26 23:15:57 mdw Exp $ + * $Id: env.c,v 1.2 2000/06/17 10:39:00 mdw Exp $ * * Fiddling with environment variables * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: env.c,v $ + * Revision 1.2 2000/06/17 10:39:00 mdw + * Add support for arena management. + * * Revision 1.1 1999/07/26 23:15:57 mdw * Fiddling with environment variables. * @@ -97,7 +100,7 @@ void env_put(sym_table *t, const char *name, const char *value) { size_t eq = strcspn(name, "="); if (name[eq] == '=') { - q = xmalloc(eq + 1); + q = x_alloc(t->t.a, eq + 1); memcpy(q, name, eq); q[eq] = 0; value = name + eq + 1; @@ -110,21 +113,21 @@ void env_put(sym_table *t, const char *name, const char *value) if (!value) { var *v; if ((v = sym_find(t, name, -1, 0, 0)) != 0) { - free(v->v); + x_free(t->t.a, v->v); sym_remove(t, v); } } else { unsigned found; var *v = sym_find(t, name, -1, sizeof(*v), &found); if (found) - free(v->v); - v->v = xstrdup(value); + x_free(t->t.a, v->v); + v->v = x_strdup(t->t.a, value); } /* --- Tidying --- */ if (q) - free(q); + xfree(q); } /* --- @env_import@ --- * @@ -209,7 +212,7 @@ void env_destroy(sym_table *t) var *v; for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) - free(v->v); + x_free(t->t.a, v->v); sym_destroy(t); } diff --git a/hash.c b/hash.c index 96c150b..3945755 100644 --- a/hash.c +++ b/hash.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: hash.c,v 1.1 1999/08/02 14:45:48 mdw Exp $ + * $Id: hash.c,v 1.2 2000/06/17 10:37:39 mdw Exp $ * * General hashtable infrastructure * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: hash.c,v $ + * Revision 1.2 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.1 1999/08/02 14:45:48 mdw * Break low-level hashtable code out from sym. * @@ -42,10 +45,10 @@ #include #include "alloc.h" +#include "arena.h" #include "bits.h" #include "exc.h" #include "hash.h" -#include "track.h" /*----- Main code ---------------------------------------------------------*/ @@ -64,13 +67,12 @@ void hash_create(hash_table *t, size_t n) { hash_base **v; - TRACK_CTX("hashtable creation"); - TRACK_PUSH; - t->v = xmalloc(n * sizeof(hash_base *)); + + t->a = arena_global; + t->v = x_alloc(t->a, n * sizeof(hash_base *)); t->mask = n - 1; for (v = t->v; n; v++, n--) *v = 0; - TRACK_POP; } /* --- @hash_destroy@ --- * @@ -85,10 +87,7 @@ void hash_create(hash_table *t, size_t n) void hash_destroy(hash_table *t) { - TRACK_CTX("hashtable destruction"); - TRACK_PUSH; - free(t->v); - TRACK_POP; + x_free(t->a, t->v); } /* --- @hash_bin@ --- * @@ -123,15 +122,9 @@ int hash_extend(hash_table *t) uint32 m = t->mask + 1; size_t i; - /* --- Push in a tracking context --- */ - - TRACK_CTX("hashtable extension"); - TRACK_PUSH; - /* --- Allocate a new hash bin vector --- */ - if ((v = realloc(t->v, m * 2 * sizeof(hash_base *))) == 0) { - TRACK_POP; + if ((v = A_REALLOC(t->a, t->v, m * 2 * sizeof(hash_base *))) == 0) { return (0); } t->v = v; @@ -156,7 +149,6 @@ int hash_extend(hash_table *t) *q = 0; } - TRACK_POP; return (1); } diff --git a/hash.h b/hash.h index a7dd705..c76c6a1 100644 --- a/hash.h +++ b/hash.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: hash.h,v 1.2 1999/12/10 23:42:04 mdw Exp $ + * $Id: hash.h,v 1.3 2000/06/17 10:37:39 mdw Exp $ * * General hashtable infrastructure * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: hash.h,v $ + * Revision 1.3 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.2 1999/12/10 23:42:04 mdw * Change header file guard names. * @@ -56,6 +59,10 @@ #include +#ifndef MLIB_ARENA_H +# include "arena.h" +#endif + #ifndef MLIB_BITS_H # include "bits.h" #endif @@ -72,6 +79,7 @@ typedef struct hash_table { uint32 mask; /* Bit mask of hash bits */ struct hash_base **v; /* Vector of hash bins */ + arena *a; /* Allocation arena */ } hash_table; /* --- A hashtable entry --- * diff --git a/sym.c b/sym.c index a4445de..4e11fba 100644 --- a/sym.c +++ b/sym.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sym.c,v 1.10 1999/12/10 23:42:04 mdw Exp $ + * $Id: sym.c,v 1.11 2000/06/17 10:37:39 mdw Exp $ * * Symbol table management * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: sym.c,v $ + * Revision 1.11 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.10 1999/12/10 23:42:04 mdw * Change header file guard names. * @@ -74,13 +77,13 @@ /* --- Local headers --- */ #include "alloc.h" +#include "arena.h" #include "bits.h" #include "crc32.h" #include "exc.h" #include "hash.h" #include "sub.h" #include "sym.h" -#include "track.h" /*----- Tuning parameters -------------------------------------------------*/ @@ -117,11 +120,9 @@ void sym_create(sym_table *t) { - TRACK_CTX("symbol table creation"); - TRACK_PUSH; hash_create(&t->t, SYM_INITSZ); + t->s = &sub_global; t->load = SYM_LIMIT(SYM_INITSZ); - TRACK_POP; } /* --- @sym_destroy@ --- * @@ -138,9 +139,6 @@ void sym_destroy(sym_table *t) { sym_iter i; - TRACK_CTX("symbol table destruction"); - TRACK_PUSH; - SYM_MKITER(&i, t); for (;;) { sym_base *p; @@ -148,12 +146,10 @@ void sym_destroy(sym_table *t) if (!p) break; if (p->len > SYM_BUFSZ) - sub_free(p->name.p, p->len); - free(p); + subarena_free(t->s, p->name.p, p->len); + x_free(t->t.a, p); } hash_destroy(&t->t); - - TRACK_POP; } /* --- @sym_find@ --- * @@ -230,30 +226,17 @@ void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f) /* --- Create a new symbol block and initialize it --- */ - { - TRACK_CTX("new symbol creation"); - TRACK_PUSH; - - q = xmalloc(sz); - q->b.next = *bin; - q->b.hash = hash; - q->len = len; - if (n) { - if (len <= SYM_BUFSZ) - memcpy(q->name.b, n, len); - else { - TRY { - q->name.p = sub_alloc(len); - memcpy(q->name.p, n, len); - } CATCH { - free(q); - TRACK_POP; - RETHROW; - } END_TRY; - } + q = x_alloc(t->t.a, sz); + q->b.next = *bin; + q->b.hash = hash; + q->len = len; + if (n) { + if (len <= SYM_BUFSZ) + memcpy(q->name.b, n, len); + else { + q->name.p = subarena_alloc(t->s, len); + memcpy(q->name.p, n, len); } - - TRACK_POP; } *bin = &q->b; @@ -287,8 +270,8 @@ void sym_remove(sym_table *t, void *p) sym_base *q = p; hash_remove(&t->t, &q->b); if (q->len > SYM_BUFSZ) - sub_free(q->name.p, q->len); - free(q); + subarena_free(t->s, q->name.p, q->len); + xfree(q); t->load++; } diff --git a/sym.h b/sym.h index 7dd9217..d9d270c 100644 --- a/sym.h +++ b/sym.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sym.h,v 1.10 1999/12/10 23:42:04 mdw Exp $ + * $Id: sym.h,v 1.11 2000/06/17 10:37:39 mdw Exp $ * * Symbol table management * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: sym.h,v $ + * Revision 1.11 2000/06/17 10:37:39 mdw + * Add support for arena management. + * * Revision 1.10 1999/12/10 23:42:04 mdw * Change header file guard names. * @@ -82,6 +85,10 @@ # include "hash.h" #endif +#ifndef MLIB_SUB_H +# include "sub.h" +#endif + /*----- Type definitions --------------------------------------------------*/ /* --- Symbol table --- * @@ -93,6 +100,7 @@ typedef struct sym_table { hash_table t; + subarena *s; size_t load; } sym_table;