diff options
author | Michael Forney <mforney@mforney.org> | 2021-10-28 16:37:38 -0700 |
---|---|---|
committer | Michael Forney <mforney@mforney.org> | 2024-03-24 02:22:23 -0700 |
commit | 7585cbb758b7bc2169cba42b7665d46f0c57db93 (patch) | |
tree | 96f76517c06e5f0bc5ff4c310a15872c32520f46 | |
parent | 0ab9f3793fa43afd7d941ff674cc642e20188d45 (diff) |
map: Use separately allocated struct map
-rw-r--r-- | cc.h | 4 | ||||
-rw-r--r-- | decl.c | 8 | ||||
-rw-r--r-- | map.c | 19 | ||||
-rw-r--r-- | pp.c | 10 | ||||
-rw-r--r-- | qbe.c | 8 | ||||
-rw-r--r-- | scope.c | 28 | ||||
-rw-r--r-- | util.h | 22 |
7 files changed, 46 insertions, 53 deletions
@@ -297,8 +297,8 @@ struct decl { }; struct scope { - struct map *tags; - struct map *decls; + struct map tags; + struct map decls; struct block *breaklabel; struct block *continuelabel; struct switchcases *switchcases; @@ -1089,16 +1089,16 @@ decl(struct scope *s, struct func *f) struct decl * stringdecl(struct expr *expr) { - static struct map *strings; + static struct map strings; struct mapkey key; void **entry; struct decl *d; - if (!strings) - strings = mkmap(64); + if (!strings.len) + mapinit(&strings, 64); assert(expr->kind == EXPRSTRING); mapkey(&key, expr->u.string.data, expr->u.string.size); - entry = mapput(strings, &key); + entry = mapput(&strings, &key); d = *entry; if (!d) { d = mkdecl(DECLOBJECT, expr->type, QUALNONE, LINKNONE); @@ -5,12 +5,6 @@ #include <string.h> #include "util.h" -struct map { - size_t len, cap; - struct mapkey *keys; - void **vals; -}; - static uint64_t hash(const void *ptr, size_t len) { @@ -31,31 +25,25 @@ mapkey(struct mapkey *k, const void *s, size_t n) k->hash = hash(s, n); } -struct map * -mkmap(size_t cap) +void +mapinit(struct map *h, size_t cap) { - struct map *h; size_t i; assert(!(cap & cap - 1)); - h = xmalloc(sizeof(*h)); h->len = 0; h->cap = cap; h->keys = xreallocarray(NULL, cap, sizeof(h->keys[0])); h->vals = xreallocarray(NULL, cap, sizeof(h->vals[0])); for (i = 0; i < cap; ++i) h->keys[i].str = NULL; - - return h; } void -delmap(struct map *h, void del(void *)) +mapfree(struct map *h, void del(void *)) { size_t i; - if (!h) - return; if (del) { for (i = 0; i < h->cap; ++i) { if (h->keys[i].str) @@ -64,7 +52,6 @@ delmap(struct map *h, void del(void *)) } free(h->keys); free(h->vals); - free(h); } static bool @@ -51,14 +51,14 @@ struct frame { enum ppflags ppflags; static struct array ctx; -static struct map *macros; +static struct map macros; /* number of macros currently undergoing expansion */ static size_t macrodepth; void ppinit(void) { - macros = mkmap(64); + mapinit(¯os, 64); next(); } @@ -112,7 +112,7 @@ macroget(char *name) struct mapkey k; mapkey(&k, name, strlen(name)); - return mapget(macros, &k); + return mapget(¯os, &k); } static void @@ -272,7 +272,7 @@ define(void) tok = *t; mapkey(&k, m->name, strlen(m->name)); - entry = mapput(macros, &k); + entry = mapput(¯os, &k); if (*entry && !macroequal(m, *entry)) error(&tok.loc, "redefinition of macro '%s'", m->name); *entry = m; @@ -288,7 +288,7 @@ undef(void) name = tokencheck(&tok, TIDENT, "after #undef"); mapkey(&k, name, strlen(name)); - entry = mapput(macros, &k); + entry = mapput(¯os, &k); m = *entry; if (m) { free(name); @@ -89,7 +89,7 @@ struct func { char *name; struct type *type; struct block *start, *end; - struct map *gotos; + struct map gotos; unsigned lastid; }; @@ -481,8 +481,8 @@ mkfunc(struct decl *decl, char *name, struct type *t, struct scope *s) f->name = name; f->type = t; f->start = f->end = mkblock("start"); - f->gotos = mkmap(8); f->lastid = 0; + mapinit(&f->gotos, 8); emittype(t->base); /* allocate space for parameters */ @@ -528,7 +528,7 @@ delfunc(struct func *f) free(b->insts.val); free(b); } - delmap(f->gotos, free); + mapfree(&f->gotos, free); free(f); } @@ -600,7 +600,7 @@ funcgoto(struct func *f, char *name) struct mapkey key; mapkey(&key, name, strlen(name)); - entry = mapput(f->gotos, &key); + entry = mapput(&f->gotos, &key); g = *entry; if (!g) { g = xmalloc(sizeof(*g)); @@ -44,8 +44,8 @@ mkscope(struct scope *parent) struct scope *s; s = xmalloc(sizeof(*s)); - s->decls = NULL; - s->tags = NULL; + s->decls.len = 0; + s->tags.len = 0; s->breaklabel = parent->breaklabel; s->continuelabel = parent->continuelabel; s->switchcases = parent->switchcases; @@ -59,10 +59,10 @@ delscope(struct scope *s) { struct scope *parent = s->parent; - if (s->decls) - delmap(s->decls, NULL); - if (s->tags) - delmap(s->tags, NULL); + if (s->decls.len) + mapfree(&s->decls, NULL); + if (s->tags.len) + mapfree(&s->tags, NULL); free(s); return parent; @@ -76,7 +76,7 @@ scopegetdecl(struct scope *s, const char *name, bool recurse) mapkey(&k, name, strlen(name)); do { - d = s->decls ? mapget(s->decls, &k) : NULL; + d = s->decls.len ? mapget(&s->decls, &k) : NULL; s = s->parent; } while (!d && s && recurse); @@ -91,7 +91,7 @@ scopegettag(struct scope *s, const char *name, bool recurse) mapkey(&k, name, strlen(name)); do { - t = s->tags ? mapget(s->tags, &k) : NULL; + t = s->tags.len ? mapget(&s->tags, &k) : NULL; s = s->parent; } while (!t && s && recurse); @@ -103,10 +103,10 @@ scopeputdecl(struct scope *s, const char *name, struct decl *d) { struct mapkey k; - if (!s->decls) - s->decls = mkmap(32); + if (!s->decls.len) + mapinit(&s->decls, 32); mapkey(&k, name, strlen(name)); - *mapput(s->decls, &k) = d; + *mapput(&s->decls, &k) = d; } void @@ -114,8 +114,8 @@ scopeputtag(struct scope *s, const char *name, struct type *t) { struct mapkey k; - if (!s->tags) - s->tags = mkmap(32); + if (!s->tags.len) + mapinit(&s->tags, 32); mapkey(&k, name, strlen(name)); - *mapput(s->tags, &k) = t; + *mapput(&s->tags, &k) = t; } @@ -7,12 +7,6 @@ struct array { size_t len, cap; }; -struct mapkey { - uint64_t hash; - const void *str; - size_t len; -}; - struct treenode { unsigned long long key; void *child[2]; @@ -47,9 +41,21 @@ void *arraylast(struct array *, size_t); /* map */ +struct map { + size_t len, cap; + struct mapkey *keys; + void **vals; +}; + +struct mapkey { + uint64_t hash; + const void *str; + size_t len; +}; + void mapkey(struct mapkey *, const void *, size_t); -struct map *mkmap(size_t); -void delmap(struct map *, void(void *)); +void mapinit(struct map *, size_t); +void mapfree(struct map *, void(void *)); void **mapput(struct map *, struct mapkey *); void *mapget(struct map *, struct mapkey *); |