diff options
| author | 2025-12-15 21:46:24 +0100 | |
|---|---|---|
| committer | 2025-12-15 21:46:24 +0100 | |
| commit | c6c0f2ef35175075e91169113cfe856f29b3eb9a (patch) | |
| tree | 65dc85d7b2845a4e1145ac9d6ffd26fbb2482e8e /c/lex.c | |
| parent | bf0f2805b5aec7f4fa5fb4ff1a4da081a0112e4e (diff) | |
move intern() to mem.c
Being in lex.c was vestigial, since it was being used all over the
frontend and backend.
Diffstat (limited to 'c/lex.c')
| -rw-r--r-- | c/lex.c | 45 |
1 files changed, 0 insertions, 45 deletions
@@ -1,51 +1,6 @@ #include "lex.h" #include <string.h> -const char * -intern(const char *s) -{ - static uint N, n; - static struct ht { - const char *s; - size_t h; - } *ht; - static struct { char m[sizeof(struct arena) + (2<<10)]; struct arena *_a; } amem; - static struct arena *arena; - - if (!N) { - ht = xcalloc((sizeof *ht) * (N = 1<<10)); - arena = (void *)amem.m, arena->cap = sizeof amem.m - sizeof(struct arena); - } - - for (size_t h = hashs(0, s), i = h;;) { - i &= N - 1; - if (!ht[i].s) { /* insert */ - if (n < N/4*3 /*load factor 75%*/) { - ++n; - ht[i].h = h; - return ht[i].s = alloccopy(&arena, s, strlen(s)+1, 1); - } - /* resize */ - size_t nnew = N * 2; - struct ht *new = xcalloc(sizeof *new * nnew); - for (uint i = 0; i < N; ++i) { /* rehash */ - if (!ht[i].s) continue; - uint j = ht[i].h; - while (new[j &= nnew-1].s) ++j; - new[j] = ht[i]; - } - free(ht); - ht = new; - N = nnew; - i = h; - continue; - } else if (h == ht[i].h && !strcmp(s, ht[i].s)) { - return ht[i].s; - } - ++i; - } -} - static void identkeyword(struct token *tk, const char *s, int len) { |