From d296bf4d8d017e615b5030f422b99053c4e382e0 Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Wed, 13 Dec 1995 13:44:23 +0000 Subject: [PATCH] Modified Data1-system to use nmem --- include/data1.h | 16 +++++++---- retrieval/d1_map.c | 18 +++++++----- retrieval/d1_read.c | 78 ++++++++++++++++++--------------------------------- util/nmem.c | 19 ++++++++++--- 4 files changed, 64 insertions(+), 67 deletions(-) diff --git a/include/data1.h b/include/data1.h index 6fe7938..f1b2982 100644 --- a/include/data1.h +++ b/include/data1.h @@ -24,7 +24,10 @@ * OF THIS SOFTWARE. * * $Log: data1.h,v $ - * Revision 1.7 1995-12-12 16:37:05 quinn + * Revision 1.8 1995-12-13 13:44:23 quinn + * Modified Data1-system to use nmem + * + * Revision 1.7 1995/12/12 16:37:05 quinn * Added destroy element to data1_node. * * Revision 1.6 1995/12/11 15:22:12 quinn @@ -92,6 +95,7 @@ #include +#include #include #include @@ -310,8 +314,8 @@ typedef struct data1_node data1_node *get_parent_tag(data1_node *n); data1_node *data1_read_node(char **buf, data1_node *parent, int *line, - data1_absyn *absyn); -data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd); + data1_absyn *absyn, NMEM m); +data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd, NMEM m); data1_absyn *data1_read_absyn(char *file); data1_tag *data1_gettagbynum(data1_tagset *s, int type, int value); data1_tagset *data1_read_tagset(char *file); @@ -323,7 +327,7 @@ data1_tag *data1_gettagbyname(data1_tagset *s, char *name); void data1_free_tree(data1_node *t); char *data1_nodetobuf(data1_node *n, int select, int *len); data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, - char *tagname); + char *tagname, NMEM m); data1_datatype data1_maptype(char *t); data1_varset *data1_read_varset(char *file); data1_vartype *data1_getvartypebyct(data1_varset *set, char *class, char *type); @@ -331,10 +335,10 @@ Z_Espec1 *data1_read_espec1(char *file, ODR o); int data1_doespec1(data1_node *n, Z_Espec1 *e); data1_esetname *data1_getesetbyname(data1_absyn *a, char *name); data1_element *data1_getelementbyname(data1_absyn *absyn, char *name); -data1_node *data1_mk_node(void); +data1_node *data1_mk_node(NMEM m); data1_absyn *data1_get_absyn(char *name); data1_maptab *data1_read_maptab(char *file); -data1_node *data1_map_record(data1_node *n, data1_maptab *map); +data1_node *data1_map_record(data1_node *n, data1_maptab *map, NMEM m); data1_marctab *data1_read_marctab(char *file); char *data1_nodetomarc(data1_marctab *p, data1_node *n, int selected, int *len); diff --git a/retrieval/d1_map.c b/retrieval/d1_map.c index 2fcdd66..9a04303 100644 --- a/retrieval/d1_map.c +++ b/retrieval/d1_map.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: d1_map.c,v $ - * Revision 1.6 1995-12-12 16:37:08 quinn + * Revision 1.7 1995-12-13 13:44:31 quinn + * Modified Data1-system to use nmem + * + * Revision 1.6 1995/12/12 16:37:08 quinn * Added destroy element to data1_node. * * Revision 1.5 1995/12/12 14:11:31 quinn @@ -229,7 +232,8 @@ static int tagmatch(data1_node *n, data1_maptag *t) return 1; } -static int map_children(data1_node *n, data1_maptab *map, data1_node *res) +static int map_children(data1_node *n, data1_maptab *map, data1_node *res, + NMEM mem) { data1_node *c; data1_mapunit *m; @@ -255,7 +259,7 @@ static int map_children(data1_node *n, data1_maptab *map, data1_node *res) { if (!cur || mt->new_field || !tagmatch(cur, mt)) { - cur = data1_mk_node(); + cur = data1_mk_node(mem); cur->which = DATA1N_tag; cur->u.tag.element = 0; cur->u.tag.tag = mt->value.string; @@ -285,7 +289,7 @@ static int map_children(data1_node *n, data1_maptab *map, data1_node *res) break; } } - if (map_children(c, map, res) < 0) + if (map_children(c, map, res, mem) < 0) return -1; } return 0; @@ -405,9 +409,9 @@ static int map_record(data1_node *res, data1_node *n, data1_maptab *map) * table. The new copy will refer back to the data of the original record, * which should not be discarded during the lifetime of the copy. */ -data1_node *data1_map_record(data1_node *n, data1_maptab *map) +data1_node *data1_map_record(data1_node *n, data1_maptab *map, NMEM m) { - data1_node *res = data1_mk_node(); + data1_node *res = data1_mk_node(m); res->which = DATA1N_root; res->u.root.type = map->target_absyn_name; @@ -419,7 +423,7 @@ data1_node *data1_map_record(data1_node *n, data1_maptab *map) res->parent = 0; res->root = res; - if (map_children(n, map, res) < 0) + if (map_children(n, map, res, m) < 0) { data1_free_tree(res); return 0; diff --git a/retrieval/d1_read.c b/retrieval/d1_read.c index e164202..2e989bf 100644 --- a/retrieval/d1_read.c +++ b/retrieval/d1_read.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: d1_read.c,v $ - * Revision 1.6 1995-12-12 16:37:08 quinn + * Revision 1.7 1995-12-13 13:44:32 quinn + * Modified Data1-system to use nmem + * + * Revision 1.6 1995/12/12 16:37:08 quinn * Added destroy element to data1_node. * * Revision 1.5 1995/12/11 15:22:37 quinn @@ -81,7 +84,9 @@ char *data1_tabpath = 0; /* global path for tables */ void data1_set_tabpath(char *p) { data1_tabpath = p; } +#if 0 static data1_node *freelist = 0; +#endif /* * get the tag which is the immediate parent of this node (this may mean @@ -95,26 +100,32 @@ data1_node *get_parent_tag(data1_node *n) return 0; } -data1_node *data1_mk_node(void) +data1_node *data1_mk_node(NMEM m) { data1_node *r; +#if 0 if ((r = freelist)) freelist = r->next; else if (!(r = xmalloc(sizeof(*r)))) abort(); +#else + r = nmem_malloc(m, sizeof(*r)); +#endif r->next = r->child = r->last_child = r->parent = 0; r->num_children = 0; r->destroy = 0; return r; } +#if 0 static void fr_node(data1_node *n) { n->next = freelist; freelist = n; } +#endif void data1_free_tree(data1_node *t) { @@ -128,7 +139,9 @@ void data1_free_tree(data1_node *t) } if (t->destroy) (*t->destroy)(t); +#if 0 fr_node(t); +#endif } /* @@ -137,9 +150,9 @@ void data1_free_tree(data1_node *t) * which can then be modified. */ data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, - char *tagname) + char *tagname, NMEM m) { - data1_node *tagn = data1_mk_node(); + data1_node *tagn = data1_mk_node(m); data1_node *datn; tagn->which = DATA1N_tag; @@ -150,11 +163,8 @@ data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, tagn->u.tag.no_data_requested = 0; if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn, 0, tagname))) - { - fr_node(tagn); return 0; - } - tagn->child = datn = data1_mk_node(); + tagn->child = datn = data1_mk_node(m); tagn->num_children = 1; datn->parent = tagn; datn->root = root; @@ -171,7 +181,7 @@ data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, * 'node' and its children. */ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, - data1_absyn *absyn) + data1_absyn *absyn, NMEM m) { data1_node *res; @@ -246,7 +256,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, tag); return 0; } - res = data1_mk_node(); + res = data1_mk_node(m); res->which = DATA1N_root; res->u.root.type = tag; res->u.root.absyn = absyn; @@ -275,7 +285,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, */ if (parent->which != DATA1N_variant) { - res = data1_mk_node(); + res = data1_mk_node(m); res->which = DATA1N_variant; res->u.variant.type = 0; res->u.variant.value = 0; @@ -298,7 +308,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, return 0; } - res = data1_mk_node(); + res = data1_mk_node(m); res->which = DATA1N_variant; res->root = parent->root; res->u.variant.type = tp; @@ -333,7 +343,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, #else elem = data1_getelementbytagname(absyn, e, tag); #endif - res = data1_mk_node(); + res = data1_mk_node(m); res->which = DATA1N_tag; res->u.tag.element = elem; res->u.tag.tag = tag; @@ -351,7 +361,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, /* * Read child nodes. */ - while ((*pp = data1_read_node(buf, res, line, absyn))) + while ((*pp = data1_read_node(buf, res, line, absyn, m))) { res->last_child = *pp; res->num_children++; @@ -384,46 +394,13 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, } while (isspace(data[len-1])) len--; - res = data1_mk_node(); + res = data1_mk_node(m); res->parent = parent; res->which = DATA1N_data; res->u.data.what = DATA1I_text; res->u.data.len = len; res->u.data.data = data; res->root = parent->root; - - /* - * if the parent is structured, we'll insert a 'wellKnown' marker - * in front of the data. - */ -#if 0 - if (partag->u.tag.element && partag->u.tag.element->tag->kind == - DATA1K_structured) - { - data1_node *wk = mk_node(); - static data1_element wk_element = { 0, 0, 0, 0, 0}; - - wk->parent = partag; - wk->root = partag->root; - wk->which = DATA1N_tag; - wk->u.tag.tag = 0; - /* - * get well-known tagdef if required. - */ - if (!wk_element.tag && !(wk_element.tag = - data1_gettagbynum(wk->root->u.root.absyn->tagset, 1, 19))) - { - logf(LOG_WARN, - "Failed to initialize 'wellknown' tag from tagsetM"); - return 0; - } - wk->u.tag.element = &wk_element; - wk->child = partag->child; - if (wk->child) - wk->child->parent = wk; - partag->child = wk; - } -#endif } return res; } @@ -431,7 +408,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, /* * Read a record in the native syntax. */ -data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd) +data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd, NMEM m) { static char *buf = 0; char *bp; @@ -441,6 +418,7 @@ data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd) if (!buf && !(buf = xmalloc(size = 4096))) abort(); + for (;;) { if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2))) @@ -450,7 +428,7 @@ data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd) if (!res) { bp = buf; - return data1_read_node(&bp, 0, &line, 0); + return data1_read_node(&bp, 0, &line, 0, m); } else return 0; diff --git a/util/nmem.c b/util/nmem.c index 8fbfd84..ebd7de2 100644 --- a/util/nmem.c +++ b/util/nmem.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: nmem.c,v $ - * Revision 1.1 1995-11-13 09:27:52 quinn + * Revision 1.2 1995-12-13 13:44:37 quinn + * Modified Data1-system to use nmem + * + * Revision 1.1 1995/11/13 09:27:52 quinn * Fiddling with the variant stuff. * * @@ -32,9 +35,11 @@ typedef struct nmem_control { int total; nmem_block *blocks; + struct nmem_control *next; } nmem_control; -static nmem_block *freelist = 0; /* global freelist */ +static nmem_block *freelist = 0; /* global freelists */ +static nmem_control *cfreelist = 0; static void free_block(nmem_block *p) { @@ -113,10 +118,15 @@ int nmem_total(NMEM n) NMEM nmem_create(void) { - NMEM r = xmalloc(sizeof(*r)); + NMEM r = cfreelist; + if (r) + cfreelist = cfreelist->next; + else + r = xmalloc(sizeof(*r)); r->blocks = 0; r->total = 0; + r->next = 0; return r; } @@ -125,5 +135,6 @@ void nmem_destroy(NMEM n) if (!n) return; nmem_reset(n); - xfree(n); + n->next = cfreelist; + cfreelist = n; } -- 1.7.10.4