Author | Jakob Wakeling <[email protected]> |
Date | 2022-03-27 00:11:02 |
Commit | fe591f98568959e5f448b6f7fa2fcfb8f7667f41 |
Parent | d38c160b2b031552bcbe4daf190f1e71ba28bed3 |
util: Remove unused libutil/alloc and libutil/map
Diffstat
M | src/symbol.c | | | 8 | +++----- |
D | src/util/alloc.c | | | 21 | --------------------- |
D | src/util/alloc.h | | | 23 | ----------------------- |
D | src/util/map.c | | | 161 | -------------------------------------------------------------------------------- |
D | src/util/map.h | | | 40 | ---------------------------------------- |
5 files changed, 3 insertions, 250 deletions
diff --git a/src/symbol.c b/src/symbol.c index eb04bc5..43ef8e1 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -11,7 +11,6 @@ */ #include "symbol.h" -#include "util/alloc.h" #include "util/util.h" #include <stddef.h> @@ -27,7 +26,7 @@ syt kwt = (syt){ NULL, 0, 0, NULL }; static void syt_resize(syt *st); /* Allocate a symbol table. */ -syt *syt_aloc(void) { return xcalloc(1, sizeof (syt)); } +syt *syt_aloc(void) { return calloc(1, sizeof (syt)); } /* Initialise a symbol table. */ // void syt_init(syt *st) { *st = (syt){ NULL, 0, 0, NULL }; } @@ -180,12 +179,12 @@ static void syt_resize(syt *st) { /* If the map is empty, simply allocate it without rehashing */ if (st->ac == 0) { st->ac = INITIAL_CAPACITY; - st->a = xcalloc(st->ac, sizeof (*st->a)); return; + st->a = calloc(st->ac, sizeof (*st->a)); return; } /* Otherwise rehash every element into a new resized map */ syt old = *st; st->ac *= 2; st->al = 0; - st->a = xcalloc(st->ac, sizeof (*st->a)); + st->a = calloc(st->ac, sizeof (*st->a)); for (UINT i = 0; i < old.ac; i += 1) { if (old.a[i].h == 0) { continue; } diff --git a/src/util/alloc.c b/src/util/alloc.c deleted file mode 100644 index f8d1782..0000000 --- a/src/util/alloc.c +++ /dev/null @@ -1,21 +0,0 @@ -// util/alloc.h, version 1.0.1 -// Memory allocation source file from libutil -// Copyright (C) 2021, Jakob Wakeling -// All rights reserved. - -#include "alloc.h" -#include "error.h" - -#include <stdlib.h> - -void *xmalloc(UINT l) { - void *q = malloc(l); if (!q) { error(1, "%s", SERR); } return q; -} - -void *xcalloc(UINT n, UINT l) { - void *q = calloc(n, l); if (!q) { error(1, "%s", SERR); } return q; -} - -void *xrealloc(void *p, UINT l) { - void *q = realloc(p, l); if (!q) { error(1, "%s", SERR); } return q; -} diff --git a/src/util/alloc.h b/src/util/alloc.h deleted file mode 100644 index 2d56976..0000000 --- a/src/util/alloc.h +++ /dev/null @@ -1,23 +0,0 @@ -// util/alloc.h, version 1.0.1 -// Memory allocation header file from libutil -// Copyright (C) 2021, Jakob Wakeling -// All rights reserved. - -#ifndef UTIL_ALLOC_H_M0UWQ8LT -#define UTIL_ALLOC_H_M0UWQ8LT - -#ifdef __cplusplus -extern "C" { -#endif - -#include "util.h" - -extern void *xmalloc(UINT l); -extern void *xcalloc(UINT n, UINT l); -extern void *xrealloc(void *p, UINT l); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // UTIL_ALLOC_H_M0UWQ8LT diff --git a/src/util/map.c b/src/util/map.c deleted file mode 100644 index b40ae63..0000000 --- a/src/util/map.c +++ /dev/null @@ -1,161 +0,0 @@ -// util/map.c, version 0.1.2 -// Map utility source file from libutil -// Copyright (C) 2021, Jakob Wakeling -// All rights reserved. - -/* - This file uses the currently non-standard 'typeof' operator. Its use is - considered acceptable because it is supported by both GCC and Clang, and - POSIX extensions are used here anyway. Additionally, it is expected that the - 'typeof' operator will become standard in C23. -*/ - -#include "alloc.h" -#include "map.h" -#include "util.h" - -#include <stddef.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#define LOAD_FACTOR 0.90 - -UINT map_initial_capacity = 64; - -static void map_resize(map *m); -static u64 hash(const char *s, UINT l); - -/* Initialise a map. */ -map map_init(void (*free)(void *)) { - return (map){ NULL, 0, 0, free }; -} - -/* Uninitialise a map. */ -void map_free(map *m) { - for (UINT i = 0; i < m->ac; i += 1) { - if (m->a[i].h == 0) { continue; } free(m->a[i].k); - if (m->free != NULL) { m->free(m->a[i].v); } - } - - free(m->a); *m = (map){ NULL, 0, 0, NULL }; -} - -#define SWAP(a, b) { register typeof (a) t = a; a = b; b = t; } -#define DIB(i) ((i + m->ac - (m->a[i].h % m->ac)) % m->ac) - -/* Insert a key-value pair into a map. The key is duplicated. */ -void map_insert(map *m, char *k, void *v) { - if (m->ac == 0 || m->al >= ((f64)m->ac * LOAD_FACTOR)) { map_resize(m); } - UINT h = hash(k, strlen(k)), i = h % m->ac; k = strdup(k); - - for (UINT dist = 0;; i = (i + 1) % m->ac, dist += 1) { - if (m->a[i].h == 0) { - /* If an empty bucket is found, insert here */ - m->a[i] = (typeof (*m->a)){ h, k, v }; - m->al += 1; return; - } - - /* Calculate tsid, the DIB of the item at the current index */ - UINT tsid = (i + m->ac - (m->a[i].h % m->ac)) % m->ac; - - if (dist > tsid) { - SWAP(m->a[i].h, h); - SWAP(m->a[i].k, k); - SWAP(m->a[i].v, v); - - dist = tsid; - } - } -} - -/* Lookup the value associated with a key from a map. */ -void *map_lookup(map *m, char *k) { - UINT h = hash(k, strlen(k)), i = h % m->ac; - - for (UINT dist = 0;; i = (i + 1) % m->ac, dist += 1) { - if (m->a[i].h == 0) { return NULL; } - - if (dist > DIB(i)) { return NULL; } - if ((m->a[i].h == h) && (strcmp(m->a[i].k, k) == 0)) { - return m->a[i].v; - } - } -} - -/* Remove a key-value pair from a map. */ -void *map_remove(map *m, char *k) { - UINT h = hash(k, strlen(k)), i = h % m->ac; - - for (UINT dist = 0;; i = (i + 1) % m->ac, dist += 1) { - if (m->a[i].h == 0) { return NULL; } - - if (dist > DIB(i)) { return NULL; } - if ((m->a[i].h == h) && (strcmp(m->a[i].k, k) == 0)) { - /* If the element to be removed is found, then deallocate it */ - if (m->free != NULL) { m->free(m->a[i].v); } free(m->a[i].k); - m->a[i] = (typeof (*m->a)){ 0, NULL, NULL }; m->al -= 1; - - /* */ - for (UINT j = (i + 1) % m->ac;; i = j, j = (j + 1) % m->ac) { - if (m->a[j].h == 0 || DIB(j) == 0) { break; } - - SWAP(m->a[i].h, m->a[j].h); - SWAP(m->a[i].k, m->a[j].k); - SWAP(m->a[i].v, m->a[j].v); - } - - /* - TODO I am unsure if I want to have this procedure return the - removed value or simply an acknowledgement of its removal - */ - return (void *)true; - } - } -} - -/* Print a basic representation of a map to stdout. */ -void map_print(map *m) { - for (UINT i = 0; i < m->ac; i += 1) if (m->a[i].h != 0) { - printf("%s -> %s\n", m->a[i].k, (char *)m->a[i].v); - } -} - -/* Print a debug representation of a map to stdout. */ -void map_debug(map *m) { - for (UINT i = 0; i < m->ac; i += 1) { - if (m->a[i].h == 0) { printf("[%zu] %lu\n", i, m->a[i].h); } - else printf( - "[%zu] %lu, %s -> %s, DIB: %zu\n", - i, m->a[i].h, m->a[i].k, (char *)m->a[i].v, DIB(i) - ); - } -} - -/* Double the number of buckets in a map. */ -static void map_resize(map *m) { - /* If the map is empty, simply allocate it without rehashing */ - if (m->ac == 0) { - m->ac = map_initial_capacity; - m->a = xcalloc(m->ac, sizeof (*m->a)); return; - } - - /* Otherwise rehash every element into a new resized map */ - map old = *m; m->ac *= 2; m->a = xcalloc(m->ac, sizeof (*m->a)); m->al = 0; - - for (UINT i = 0; i < old.ac; i += 1) { - if (old.a[i].h == 0) { continue; } - - map_insert(m, old.a[i].k, old.a[i].v); - free(old.a[i].k); - } - - free(old.a); -} - -/* Compute the hash of some data. Will not return 0. */ -static u64 hash(const char *dat, UINT len) { - register u64 fnv = 0xCBF29CE484222325; - for (; len; len -= 1, dat += 1) { fnv ^= *dat; fnv *= 0x00000100000001B3; } - fnv |= fnv == 0; return fnv; -} diff --git a/src/util/map.h b/src/util/map.h deleted file mode 100644 index d840b21..0000000 --- a/src/util/map.h +++ /dev/null @@ -1,40 +0,0 @@ -// util/map.h, version 0.1.2 -// Map utility header file from libutil -// Copyright (C) 2021, Jakob Wakeling -// All rights reserved. - -#ifndef UTIL_MAP_H_NB53CJJ8 -#define UTIL_MAP_H_NB53CJJ8 - -#ifdef __cplusplus -extern "C" { -#endif - -#include "util.h" - -typedef struct { - struct { u64 h; char *k; void *v; } *a; - UINT al, ac; void (*free)(void *); -} map; - -// #define MAP_INIT(type, free) map_init(sizeof (type), (free)) -// #define MAP_INSERT(m, k, v) map_insert(m, k, (void *)(UINT)(e)) - -/* This should be used for debugging or testing only. */ -extern UINT map_initial_capacity; - -extern map map_init(void (*free)(void *)); -extern void map_free(map *m); - -extern void map_insert(map *m, char *k, void *v); -extern void *map_lookup(map *m, char *k); -extern void *map_remove(map *m, char *k); - -extern void map_print(map *m); -extern void map_debug(map *m); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // UTIL_MAP_H_NB53CJJ8