2004-12-10 19:12:32 +01:00
|
|
|
/** $MirBSD: src/bin/ksh/table.c,v 2.1 2004/12/10 18:09:42 tg Exp $ */
|
2004-09-21 13:57:17 +02:00
|
|
|
/* $OpenBSD: table.c,v 1.5 1999/01/10 17:55:03 millert Exp $ */
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* dynamic hashed associative table for commands and variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sh.h"
|
|
|
|
|
2004-12-10 19:12:32 +01:00
|
|
|
__RCSID("$MirBSD: src/bin/ksh/table.c,v 2.1 2004/12/10 18:09:42 tg Exp $");
|
2004-10-28 13:11:19 +02:00
|
|
|
|
2003-03-22 18:35:03 +01:00
|
|
|
#define INIT_TBLS 8 /* initial table size (power of 2) */
|
|
|
|
|
2004-10-28 13:11:19 +02:00
|
|
|
static void texpand(struct table *tp, int nsize);
|
|
|
|
static int tnamecmp(void *p1, void *p2);
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
unsigned int
|
2004-10-28 13:53:44 +02:00
|
|
|
hash(const char *n)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
2004-10-28 13:03:24 +02:00
|
|
|
unsigned int h = 0;
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
while (*n != '\0')
|
|
|
|
h = 2*h + *n++;
|
|
|
|
return h * 32821; /* scatter bits */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-10-28 13:53:44 +02:00
|
|
|
tinit(struct table *tp, Area *ap, int tsize)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
|
|
|
tp->areap = ap;
|
|
|
|
tp->tbls = NULL;
|
|
|
|
tp->size = tp->nfree = 0;
|
|
|
|
if (tsize)
|
|
|
|
texpand(tp, tsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-10-28 13:53:44 +02:00
|
|
|
texpand(struct table *tp, int nsize)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
2004-10-28 13:03:24 +02:00
|
|
|
int i;
|
|
|
|
struct tbl *tblp, **p;
|
|
|
|
struct tbl **ntblp, **otblp = tp->tbls;
|
2003-03-22 18:35:03 +01:00
|
|
|
int osize = tp->size;
|
|
|
|
|
|
|
|
ntblp = (struct tbl**) alloc(sizeofN(struct tbl *, nsize), tp->areap);
|
|
|
|
for (i = 0; i < nsize; i++)
|
|
|
|
ntblp[i] = NULL;
|
|
|
|
tp->size = nsize;
|
|
|
|
tp->nfree = 8*nsize/10; /* table can get 80% full */
|
|
|
|
tp->tbls = ntblp;
|
|
|
|
if (otblp == NULL)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < osize; i++)
|
|
|
|
if ((tblp = otblp[i]) != NULL) {
|
|
|
|
if ((tblp->flag&DEFINED)) {
|
|
|
|
for (p = &ntblp[hash(tblp->name)
|
|
|
|
& (tp->size-1)];
|
|
|
|
*p != NULL; p--)
|
|
|
|
if (p == ntblp) /* wrap */
|
|
|
|
p += tp->size;
|
|
|
|
*p = tblp;
|
|
|
|
tp->nfree--;
|
|
|
|
} else if (!(tblp->flag & FINUSE)) {
|
|
|
|
afree((void*)tblp, tp->areap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
afree((void*)otblp, tp->areap);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tbl *
|
2004-10-28 13:53:44 +02:00
|
|
|
tsearch(struct table *tp, const char *n, unsigned int h)
|
|
|
|
/* table */
|
|
|
|
/* name to enter */
|
|
|
|
/* hash(n) */
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
2004-10-28 13:03:24 +02:00
|
|
|
struct tbl **pp, *p;
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
if (tp->size == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* search for name in hashed table */
|
|
|
|
for (pp = &tp->tbls[h & (tp->size-1)]; (p = *pp) != NULL; pp--) {
|
|
|
|
if (*p->name == *n && strcmp(p->name, n) == 0
|
|
|
|
&& (p->flag&DEFINED))
|
|
|
|
return p;
|
|
|
|
if (pp == tp->tbls) /* wrap */
|
|
|
|
pp += tp->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tbl *
|
2004-10-28 13:53:44 +02:00
|
|
|
tenter(struct table *tp, const char *n, unsigned int h)
|
|
|
|
/* table */
|
|
|
|
/* name to enter */
|
|
|
|
/* hash(n) */
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
2004-10-28 13:03:24 +02:00
|
|
|
struct tbl **pp, *p;
|
|
|
|
int len;
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
if (tp->size == 0)
|
|
|
|
texpand(tp, INIT_TBLS);
|
|
|
|
Search:
|
|
|
|
/* search for name in hashed table */
|
|
|
|
for (pp = &tp->tbls[h & (tp->size-1)]; (p = *pp) != NULL; pp--) {
|
|
|
|
if (*p->name == *n && strcmp(p->name, n) == 0)
|
|
|
|
return p; /* found */
|
|
|
|
if (pp == tp->tbls) /* wrap */
|
|
|
|
pp += tp->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tp->nfree <= 0) { /* too full */
|
|
|
|
texpand(tp, 2*tp->size);
|
|
|
|
goto Search;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create new tbl entry */
|
|
|
|
len = strlen(n) + 1;
|
|
|
|
p = (struct tbl *) alloc(offsetof(struct tbl, name[0]) + len,
|
|
|
|
tp->areap);
|
|
|
|
p->flag = 0;
|
|
|
|
p->type = 0;
|
|
|
|
p->areap = tp->areap;
|
|
|
|
p->u2.field = 0;
|
|
|
|
p->u.array = (struct tbl *)0;
|
|
|
|
memcpy(p->name, n, len);
|
|
|
|
|
|
|
|
/* enter in tp->tbls */
|
|
|
|
tp->nfree--;
|
|
|
|
*pp = p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-10-28 13:53:44 +02:00
|
|
|
tdelete(struct tbl *p)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
|
|
|
p->flag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-10-28 13:53:44 +02:00
|
|
|
twalk(struct tstate *ts, struct table *tp)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
|
|
|
ts->left = tp->size;
|
|
|
|
ts->next = tp->tbls;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tbl *
|
2004-10-28 13:53:44 +02:00
|
|
|
tnext(struct tstate *ts)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
|
|
|
while (--ts->left >= 0) {
|
|
|
|
struct tbl *p = *ts->next++;
|
|
|
|
if (p != NULL && (p->flag&DEFINED))
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-10-28 13:53:44 +02:00
|
|
|
tnamecmp(void *p1, void *p2)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
|
|
|
return strcmp(((struct tbl *)p1)->name, ((struct tbl *)p2)->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tbl **
|
2004-10-28 13:53:44 +02:00
|
|
|
tsort(struct table *tp)
|
2003-03-22 18:35:03 +01:00
|
|
|
{
|
2004-10-28 13:03:24 +02:00
|
|
|
int i;
|
|
|
|
struct tbl **p, **sp, **dp;
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
p = (struct tbl **)alloc(sizeofN(struct tbl *, tp->size+1), ATEMP);
|
|
|
|
sp = tp->tbls; /* source */
|
|
|
|
dp = p; /* dest */
|
|
|
|
for (i = 0; i < tp->size; i++)
|
|
|
|
if ((*dp = *sp++) != NULL && (((*dp)->flag&DEFINED) ||
|
|
|
|
((*dp)->flag&ARRAY)))
|
|
|
|
dp++;
|
|
|
|
i = dp - p;
|
|
|
|
qsortp((void**)p, (size_t)i, tnamecmp);
|
|
|
|
p[i] = NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PERF_DEBUG /* performance debugging */
|
|
|
|
|
2004-10-28 13:11:19 +02:00
|
|
|
void tprintinfo(struct table *tp);
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
tprintinfo(tp)
|
|
|
|
struct table *tp;
|
|
|
|
{
|
|
|
|
struct tbl *te;
|
|
|
|
char *n;
|
|
|
|
unsigned int h;
|
|
|
|
int ncmp;
|
|
|
|
int totncmp = 0, maxncmp = 0;
|
|
|
|
int nentries = 0;
|
|
|
|
struct tstate ts;
|
|
|
|
|
|
|
|
shellf("table size %d, nfree %d\n", tp->size, tp->nfree);
|
|
|
|
shellf(" Ncmp name\n");
|
|
|
|
twalk(&ts, tp);
|
|
|
|
while ((te = tnext(&ts))) {
|
2004-10-28 13:03:24 +02:00
|
|
|
struct tbl **pp, *p;
|
2003-03-22 18:35:03 +01:00
|
|
|
|
|
|
|
h = hash(n = te->name);
|
|
|
|
ncmp = 0;
|
|
|
|
|
|
|
|
/* taken from tsearch() and added counter */
|
|
|
|
for (pp = &tp->tbls[h & (tp->size-1)]; (p = *pp); pp--) {
|
|
|
|
ncmp++;
|
|
|
|
if (*p->name == *n && strcmp(p->name, n) == 0
|
|
|
|
&& (p->flag&DEFINED))
|
|
|
|
break; /* return p; */
|
|
|
|
if (pp == tp->tbls) /* wrap */
|
|
|
|
pp += tp->size;
|
|
|
|
}
|
|
|
|
shellf(" %4d %s\n", ncmp, n);
|
|
|
|
totncmp += ncmp;
|
|
|
|
nentries++;
|
|
|
|
if (ncmp > maxncmp)
|
|
|
|
maxncmp = ncmp;
|
|
|
|
}
|
|
|
|
if (nentries)
|
|
|
|
shellf(" %d entries, worst ncmp %d, avg ncmp %d.%02d\n",
|
|
|
|
nentries, maxncmp,
|
|
|
|
totncmp / nentries,
|
|
|
|
(totncmp % nentries) * 100 / nentries);
|
|
|
|
}
|
|
|
|
#endif /* PERF_DEBUG */
|