2002-06-05 14:56:17 +04:00
|
|
|
/* $NetBSD: hash.c,v 1.10 2002/06/05 10:56:18 lukem Exp $ */
|
1996-03-03 20:21:25 +03:00
|
|
|
|
1995-04-28 10:54:58 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by the Computer Systems Engineering group
|
|
|
|
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
|
|
|
* contributed to Berkeley.
|
|
|
|
*
|
|
|
|
* All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Lawrence Berkeley Laboratories.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* from: @(#)hash.c 8.1 (Berkeley) 6/6/93
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-01-29 13:20:28 +03:00
|
|
|
#include "defs.h"
|
1995-04-28 10:54:58 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interned strings are kept in a hash table. By making each string
|
|
|
|
* unique, the program can compare strings by comparing pointers.
|
|
|
|
*/
|
|
|
|
struct hashent {
|
2002-06-05 14:56:17 +04:00
|
|
|
// XXXLUKEM: a SIMPLEQ might be more appropriate
|
|
|
|
TAILQ_ENTRY(hashent) h_next;
|
1995-04-28 10:54:58 +04:00
|
|
|
const char *h_name; /* the string */
|
|
|
|
u_int h_hash; /* its hash value */
|
|
|
|
void *h_value; /* other values (for name=value) */
|
|
|
|
};
|
|
|
|
struct hashtab {
|
|
|
|
size_t ht_size; /* size (power of 2) */
|
|
|
|
u_int ht_mask; /* == ht_size - 1 */
|
|
|
|
u_int ht_used; /* number of entries used */
|
|
|
|
u_int ht_lim; /* when to expand */
|
2002-06-05 14:56:17 +04:00
|
|
|
TAILQ_HEAD(hashenthead, hashent) *ht_tab;
|
1995-04-28 10:54:58 +04:00
|
|
|
};
|
2002-06-05 14:56:17 +04:00
|
|
|
|
1995-04-28 10:54:58 +04:00
|
|
|
static struct hashtab strings;
|
|
|
|
|
2002-06-05 14:56:17 +04:00
|
|
|
static struct hashenthead hefreelist = TAILQ_HEAD_INITIALIZER(hefreelist);
|
|
|
|
|
1995-04-28 10:54:58 +04:00
|
|
|
/*
|
|
|
|
* HASHFRACTION controls ht_lim, which in turn controls the average chain
|
|
|
|
* length. We allow a few entries, on average, as comparing them is usually
|
|
|
|
* cheap (the h_hash values prevent a strcmp).
|
|
|
|
*/
|
|
|
|
#define HASHFRACTION(sz) ((sz) * 3 / 2)
|
|
|
|
|
2000-10-02 23:48:34 +04:00
|
|
|
static void ht_expand(struct hashtab *);
|
|
|
|
static void ht_init(struct hashtab *, size_t);
|
|
|
|
static inline u_int hash(const char *);
|
|
|
|
static inline struct hashent *newhashent(const char *, u_int);
|
1997-10-18 11:58:56 +04:00
|
|
|
|
1995-04-28 10:54:58 +04:00
|
|
|
/*
|
|
|
|
* Initialize a new hash table. The size must be a power of 2.
|
|
|
|
*/
|
|
|
|
static void
|
2000-10-02 23:48:34 +04:00
|
|
|
ht_init(struct hashtab *ht, size_t sz)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
1997-10-18 11:58:56 +04:00
|
|
|
u_int n;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
2002-06-05 14:56:17 +04:00
|
|
|
ht->ht_tab = emalloc(sz * sizeof (ht->ht_tab[0]));
|
1995-04-28 10:54:58 +04:00
|
|
|
ht->ht_size = sz;
|
|
|
|
ht->ht_mask = sz - 1;
|
|
|
|
for (n = 0; n < sz; n++)
|
2002-06-05 14:56:17 +04:00
|
|
|
TAILQ_INIT(&ht->ht_tab[n]);
|
1995-04-28 10:54:58 +04:00
|
|
|
ht->ht_used = 0;
|
|
|
|
ht->ht_lim = HASHFRACTION(sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand an existing hash table.
|
|
|
|
*/
|
|
|
|
static void
|
2000-10-02 23:48:34 +04:00
|
|
|
ht_expand(struct hashtab *ht)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
2002-06-05 14:56:17 +04:00
|
|
|
struct hashenthead *h, *oldh;
|
|
|
|
struct hashent *p;
|
1997-10-18 11:58:56 +04:00
|
|
|
u_int n, i;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
|
|
|
n = ht->ht_size * 2;
|
|
|
|
h = emalloc(n * sizeof *h);
|
|
|
|
for (i = 0; i < n; i++)
|
2002-06-05 14:56:17 +04:00
|
|
|
TAILQ_INIT(&h[i]);
|
1995-04-28 10:54:58 +04:00
|
|
|
oldh = ht->ht_tab;
|
|
|
|
n--;
|
2002-06-05 14:56:17 +04:00
|
|
|
for (i = 0; i < ht->ht_size; i++) {
|
|
|
|
while ((p = TAILQ_FIRST(&oldh[i])) != NULL) {
|
|
|
|
TAILQ_REMOVE(&oldh[i], p, h_next);
|
|
|
|
// XXXLUKEM: really should be TAILQ_INSERT_TAIL
|
|
|
|
TAILQ_INSERT_HEAD(&h[p->h_hash & n], p, h_next);
|
1995-04-28 10:54:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(ht->ht_tab);
|
|
|
|
ht->ht_tab = h;
|
|
|
|
ht->ht_mask = n;
|
|
|
|
ht->ht_size = ++n;
|
|
|
|
ht->ht_lim = HASHFRACTION(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a new hash entry, setting its h_next to NULL.
|
2002-06-05 14:56:17 +04:00
|
|
|
* If the free list is not empty, use the first entry from there,
|
|
|
|
* otherwise allocate a new entry.
|
1995-04-28 10:54:58 +04:00
|
|
|
*/
|
|
|
|
static inline struct hashent *
|
2000-10-02 23:48:34 +04:00
|
|
|
newhashent(const char *name, u_int h)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
1997-10-18 11:58:56 +04:00
|
|
|
struct hashent *hp;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
2002-06-05 14:56:17 +04:00
|
|
|
if (TAILQ_EMPTY(&hefreelist))
|
|
|
|
hp = emalloc(sizeof(*hp));
|
|
|
|
else {
|
|
|
|
hp = TAILQ_FIRST(&hefreelist);
|
|
|
|
TAILQ_REMOVE(&hefreelist, hp, h_next);
|
|
|
|
}
|
|
|
|
|
1995-04-28 10:54:58 +04:00
|
|
|
hp->h_name = name;
|
|
|
|
hp->h_hash = h;
|
|
|
|
return (hp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash a string.
|
|
|
|
*/
|
|
|
|
static inline u_int
|
2000-10-02 23:48:34 +04:00
|
|
|
hash(const char *str)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
1997-10-18 11:58:56 +04:00
|
|
|
u_int h;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
|
|
|
for (h = 0; *str;)
|
|
|
|
h = (h << 5) + h + *str++;
|
|
|
|
return (h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-10-02 23:48:34 +04:00
|
|
|
initintern(void)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
ht_init(&strings, 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a single unique copy of the given string. We expect this
|
|
|
|
* function to be used frequently, so it should be fast.
|
|
|
|
*/
|
|
|
|
const char *
|
2000-10-02 23:48:34 +04:00
|
|
|
intern(const char *s)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
1997-10-18 11:58:56 +04:00
|
|
|
struct hashtab *ht;
|
2002-06-05 14:56:17 +04:00
|
|
|
struct hashent *hp;
|
|
|
|
struct hashenthead *hpp;
|
1997-10-18 11:58:56 +04:00
|
|
|
u_int h;
|
|
|
|
char *p;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
|
|
|
ht = &strings;
|
|
|
|
h = hash(s);
|
|
|
|
hpp = &ht->ht_tab[h & ht->ht_mask];
|
2002-06-05 14:56:17 +04:00
|
|
|
TAILQ_FOREACH(hp, hpp, h_next) {
|
1995-04-28 10:54:58 +04:00
|
|
|
if (hp->h_hash == h && strcmp(hp->h_name, s) == 0)
|
|
|
|
return (hp->h_name);
|
2002-06-05 14:56:17 +04:00
|
|
|
}
|
|
|
|
p = estrdup(s);
|
|
|
|
hp = newhashent(p, h);
|
|
|
|
TAILQ_INSERT_TAIL(hpp, hp, h_next);
|
1995-04-28 10:54:58 +04:00
|
|
|
if (++ht->ht_used > ht->ht_lim)
|
|
|
|
ht_expand(ht);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hashtab *
|
2000-10-02 23:48:34 +04:00
|
|
|
ht_new(void)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
1997-10-18 11:58:56 +04:00
|
|
|
struct hashtab *ht;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
|
|
|
ht = emalloc(sizeof *ht);
|
|
|
|
ht_init(ht, 8);
|
|
|
|
return (ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert and/or replace.
|
|
|
|
*/
|
|
|
|
int
|
2000-10-02 23:48:34 +04:00
|
|
|
ht_insrep(struct hashtab *ht, const char *nam, void *val, int replace)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
2002-06-05 14:56:17 +04:00
|
|
|
struct hashent *hp;
|
|
|
|
struct hashenthead *hpp;
|
1997-10-18 11:58:56 +04:00
|
|
|
u_int h;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
|
|
|
h = hash(nam);
|
|
|
|
hpp = &ht->ht_tab[h & ht->ht_mask];
|
2002-06-05 14:56:17 +04:00
|
|
|
TAILQ_FOREACH(hp, hpp, h_next) {
|
1995-04-28 10:54:58 +04:00
|
|
|
if (hp->h_name == nam) {
|
|
|
|
if (replace)
|
|
|
|
hp->h_value = val;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
2002-06-05 14:56:17 +04:00
|
|
|
hp = newhashent(nam, h);
|
|
|
|
TAILQ_INSERT_TAIL(hpp, hp, h_next);
|
1995-04-28 10:54:58 +04:00
|
|
|
hp->h_value = val;
|
1996-03-17 16:18:15 +03:00
|
|
|
if (++ht->ht_used > ht->ht_lim)
|
|
|
|
ht_expand(ht);
|
1995-04-28 10:54:58 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-06-05 14:56:17 +04:00
|
|
|
/*
|
|
|
|
* Remove.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ht_remove(struct hashtab *ht, const char *name)
|
|
|
|
{
|
|
|
|
struct hashent *hp;
|
|
|
|
struct hashenthead *hpp;
|
|
|
|
u_int h;
|
|
|
|
|
|
|
|
h = hash(name);
|
|
|
|
hpp = &ht->ht_tab[h & ht->ht_mask];
|
|
|
|
|
|
|
|
TAILQ_FOREACH(hp, hpp, h_next) {
|
|
|
|
if (hp->h_name != name)
|
|
|
|
continue;
|
|
|
|
TAILQ_REMOVE(hpp, hp, h_next);
|
|
|
|
TAILQ_INSERT_TAIL(&hefreelist, hp, h_next);
|
|
|
|
ht->ht_used--;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1995-04-28 10:54:58 +04:00
|
|
|
void *
|
2000-10-02 23:48:34 +04:00
|
|
|
ht_lookup(struct hashtab *ht, const char *nam)
|
1995-04-28 10:54:58 +04:00
|
|
|
{
|
2002-06-05 14:56:17 +04:00
|
|
|
struct hashent *hp;
|
|
|
|
struct hashenthead *hpp;
|
1997-10-18 11:58:56 +04:00
|
|
|
u_int h;
|
1995-04-28 10:54:58 +04:00
|
|
|
|
|
|
|
h = hash(nam);
|
|
|
|
hpp = &ht->ht_tab[h & ht->ht_mask];
|
2002-06-05 14:56:17 +04:00
|
|
|
TAILQ_FOREACH(hp, hpp, h_next)
|
1995-04-28 10:54:58 +04:00
|
|
|
if (hp->h_name == nam)
|
|
|
|
return (hp->h_value);
|
|
|
|
return (NULL);
|
|
|
|
}
|
1997-03-14 03:14:09 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* first parameter to callback is the entry name from the hash table
|
|
|
|
* second parameter is the value from the hash table
|
|
|
|
* third argument is passed through from the "arg" parameter to ht_enumerate()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2000-10-02 23:48:34 +04:00
|
|
|
ht_enumerate(struct hashtab *ht, ht_callback cbfunc, void *arg)
|
1997-03-14 03:14:09 +03:00
|
|
|
{
|
2002-06-05 14:56:17 +04:00
|
|
|
struct hashent *hp;
|
|
|
|
struct hashenthead *hpp;
|
1997-10-18 11:58:56 +04:00
|
|
|
u_int i;
|
1997-03-14 03:14:09 +03:00
|
|
|
int rval = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ht->ht_size; i++) {
|
|
|
|
hpp = &ht->ht_tab[i];
|
2002-06-05 14:56:17 +04:00
|
|
|
TAILQ_FOREACH(hp, hpp, h_next)
|
1997-03-14 03:14:09 +03:00
|
|
|
rval += (*cbfunc)(hp->h_name, hp->h_value, arg);
|
|
|
|
}
|
|
|
|
return rval;
|
|
|
|
}
|