/* -*-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
*
/*----- 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.
*
#include <stdlib.h>
#include "alloc.h"
+#include "arena.h"
#include "darray.h"
/*----- Magic numbers -----------------------------------------------------*/
*/
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);
}
}
* 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 --- */
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);
/* -*-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
*
/*----- 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.
*
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@ --- *
/*----- 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@ --- *
*
* 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@ --- *
* 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 -----------------------------------------------*/
/* -*-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
*
/*----- 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.
*
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;
}
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;
}
/* -*-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
*
/*----- 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.
*
#include <stdio.h>
#include <stdlib.h>
+#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 ------------------------------------------------*/
_dd->buf = 0; \
_dd->sz = 0; \
_dd->len = 0; \
+ _dd->a = &arena_stdlib; \
} while (0)
/* --- @dstr_destroy@ --- *
#define DDESTROY(d) do { \
dstr *_d = (d); \
if (_d->buf) \
- free(_d->buf); \
+ x_free(_d->a, _d->buf); \
DCREATE(_d); \
} while (0)
/* -*-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
*
/*----- 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.
*
{
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;
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@ --- *
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);
}
/* -*-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
*
/*----- 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.
*
#include <string.h>
#include "alloc.h"
+#include "arena.h"
#include "bits.h"
#include "exc.h"
#include "hash.h"
-#include "track.h"
/*----- Main code ---------------------------------------------------------*/
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@ --- *
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@ --- *
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;
*q = 0;
}
- TRACK_POP;
return (1);
}
/* -*-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
*
/*----- 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.
*
#include <stddef.h>
+#ifndef MLIB_ARENA_H
+# include "arena.h"
+#endif
+
#ifndef MLIB_BITS_H
# include "bits.h"
#endif
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 --- *
/* -*-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
*
/*----- 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.
*
/* --- 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 -------------------------------------------------*/
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@ --- *
{
sym_iter i;
- TRACK_CTX("symbol table destruction");
- TRACK_PUSH;
-
SYM_MKITER(&i, t);
for (;;) {
sym_base *p;
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@ --- *
/* --- 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;
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++;
}
/* -*-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
*
/*----- 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.
*
# include "hash.h"
#endif
+#ifndef MLIB_SUB_H
+# include "sub.h"
+#endif
+
/*----- Type definitions --------------------------------------------------*/
/* --- Symbol table --- *
typedef struct sym_table {
hash_table t;
+ subarena *s;
size_t load;
} sym_table;