aboutsummaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorAndrew Chambers <[email protected]>2021-10-13 11:40:59 +1300
committerAndrew Chambers <[email protected]>2021-10-13 11:40:59 +1300
commite6326541162130dd4c2121db14c298cd5ad2009c (patch)
tree36693b45d78df0b2551511bdd08f7ab847c035a0 /util.c
parent4fc6bfe524c3f708111e4659627d474d73f3352d (diff)
Simple string interning.
Diffstat (limited to 'util.c')
-rw-r--r--util.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/util.c b/util.c
index 9ed1c50..5cede75 100644
--- a/util.c
+++ b/util.c
@@ -76,6 +76,22 @@ char *xmemdup(const char *s, size_t n) {
char *xstrdup(const char *s) { return xmemdup(s, strlen(s) + 1); }
+const char *internstring(const char *s) {
+ size_t idx, len;
+ const char *interned;
+ static const char *cache[4096] = {0};
+
+ len = strlen(s);
+ idx = murmurhash64a(s, len) % sizeof(cache)/sizeof(cache[0]);
+ interned = cache[idx];
+ if (interned && strcmp(s, cache[idx]) == 0) {
+ return interned;
+ }
+ interned = xstrdup(s);
+ cache[idx] = interned;
+ return interned;
+}
+
void htabkey(struct hashtablekey *k, const char *s, size_t n) {
k->str = s;
k->len = n;