#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
struct hashent {
struct hashent *h_next;
const char *h_name;
u_int h_hash;
void *h_value;
};
struct hashtab {
size_t ht_size;
u_int ht_mask;
u_int ht_used;
u_int ht_lim;
struct hashent **ht_tab;
};
static struct hashtab strings;
#define HASHFRACTION(sz) ((sz) * 3 / 2)
#define ROUND(x, y) (((x) + (y) - 1) & ~((y) - 1))
static void ht_init(struct hashtab *, size_t);
static void ht_expand(struct hashtab *);
static void
ht_init(struct hashtab *ht, size_t sz)
{
struct hashent **h;
u_int n;
h = ereallocarray(NULL, sz, sizeof *h);
ht->ht_tab = h;
ht->ht_size = sz;
ht->ht_mask = sz - 1;
for (n = 0; n < sz; n++)
*h++ = NULL;
ht->ht_used = 0;
ht->ht_lim = HASHFRACTION(sz);
}
static void
ht_expand(struct hashtab *ht)
{
struct hashent *p, **h, **oldh, *q;
u_int n, i;
n = ht->ht_size * 2;
h = ecalloc(n, sizeof *h);
oldh = ht->ht_tab;
n--;
for (i = ht->ht_size; i != 0; i--) {
for (p = *oldh++; p != NULL; p = q) {
q = p->h_next;
p->h_next = h[p->h_hash & n];
h[p->h_hash & n] = p;
}
}
free(ht->ht_tab);
ht->ht_tab = h;
ht->ht_mask = n;
ht->ht_size = ++n;
ht->ht_lim = HASHFRACTION(n);
}
static __inline struct hashent *
newhashent(const char *name, u_int h)
{
struct hashent *hp;
hp = emalloc(sizeof(*hp));
hp->h_name = name;
hp->h_hash = h;
hp->h_next = NULL;
return (hp);
}
static __inline u_int
hash(const char *str)
{
u_int h;
for (h = 0; *str;)
h = (h << 5) + h + *str++;
return (h);
}
void
initintern(void)
{
ht_init(&strings, 128);
}
const char *
intern(const char *s)
{
struct hashtab *ht;
struct hashent *hp, **hpp;
u_int h;
char *p;
size_t l;
ht = &strings;
h = hash(s);
hpp = &ht->ht_tab[h & ht->ht_mask];
for (; (hp = *hpp) != NULL; hpp = &hp->h_next)
if (hp->h_hash == h && strcmp(hp->h_name, s) == 0)
return (hp->h_name);
l = strlen(s) + 1;
p = malloc(l);
bcopy(s, p, l);
*hpp = newhashent(p, h);
if (++ht->ht_used > ht->ht_lim)
ht_expand(ht);
return (p);
}
struct hashtab *
ht_new(void)
{
struct hashtab *ht;
ht = emalloc(sizeof *ht);
ht_init(ht, 8);
return (ht);
}
int
ht_remove(struct hashtab *ht, const char *nam)
{
struct hashent *hp, *thp;
u_int h;
h = hash(nam);
hp = ht->ht_tab[h & ht->ht_mask];
while (hp && hp->h_name == nam) {
ht->ht_tab[h & ht->ht_mask] = hp->h_next;
hp = ht->ht_tab[h & ht->ht_mask];
}
if ((hp = ht->ht_tab[h & ht->ht_mask]) == NULL)
return (0);
for (thp = hp->h_next; thp != NULL; thp = hp->h_next) {
if (thp->h_name == nam) {
hp->h_next = thp->h_next;
} else
hp = thp;
}
return (0);
}
int
ht_insrep(struct hashtab *ht, const char *nam, void *val, int replace)
{
struct hashent *hp, **hpp;
u_int h;
h = hash(nam);
hpp = &ht->ht_tab[h & ht->ht_mask];
for (; (hp = *hpp) != NULL; hpp = &hp->h_next) {
if (hp->h_name == nam) {
if (replace)
hp->h_value = val;
return (1);
}
}
*hpp = hp = newhashent(nam, h);
hp->h_value = val;
if (++ht->ht_used > ht->ht_lim)
ht_expand(ht);
return (0);
}
void *
ht_lookup(struct hashtab *ht, const char *nam)
{
struct hashent *hp, **hpp;
u_int h;
h = hash(nam);
hpp = &ht->ht_tab[h & ht->ht_mask];
for (; (hp = *hpp) != NULL; hpp = &hp->h_next)
if (hp->h_name == nam)
return (hp->h_value);
return (NULL);
}