implement hcreate_r, hdestroy_r and hsearch_r

the size and alignment of struct hsearch_data are matched to the glibc
definition for binary compatibility. the members of the structure do
not match, which should not be a problem as long as applications
correctly treat the structure as opaque.

unlike the glibc implementation, this version of hcreate_r does not
require the caller to zero-fill the structure before use.
This commit is contained in:
sin 2014-03-25 16:37:51 +00:00 committed by Rich Felker
parent 5446303328
commit fe1ba7dbf1
2 changed files with 86 additions and 31 deletions

View File

@ -22,6 +22,18 @@ int hcreate(size_t);
void hdestroy(void); void hdestroy(void);
ENTRY *hsearch(ENTRY, ACTION); ENTRY *hsearch(ENTRY, ACTION);
#ifdef _GNU_SOURCE
struct hsearch_data {
struct __tab *__tab;
unsigned int __unused1;
unsigned int __unused2;
};
int hcreate_r(size_t, struct hsearch_data *);
void hdestroy_r(struct hsearch_data *);
int hsearch_r(ENTRY, ACTION, ENTRY **, struct hsearch_data *);
#endif
void insque(void *, void *); void insque(void *, void *);
void remque(void *); void remque(void *);

View File

@ -1,6 +1,8 @@
#define _GNU_SOURCE
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <search.h> #include <search.h>
#include "libc.h"
/* /*
open addressing hash table with 2^n table size open addressing hash table with 2^n table size
@ -19,9 +21,17 @@ struct elem {
size_t hash; size_t hash;
}; };
static size_t mask; struct __tab {
static size_t used; struct elem *elems;
static struct elem *tab; size_t mask;
size_t used;
};
static struct hsearch_data htab;
int __hcreate_r(size_t, struct hsearch_data *);
void __hdestroy_r(struct hsearch_data *);
int __hsearch_r(ENTRY, ACTION, ENTRY **, struct hsearch_data *);
static size_t keyhash(char *k) static size_t keyhash(char *k)
{ {
@ -33,29 +43,29 @@ static size_t keyhash(char *k)
return h; return h;
} }
static int resize(size_t nel) static int resize(size_t nel, struct hsearch_data *htab)
{ {
size_t newsize; size_t newsize;
size_t i, j; size_t i, j;
struct elem *e, *newe; struct elem *e, *newe;
struct elem *oldtab = tab; struct elem *oldtab = htab->__tab->elems;
struct elem *oldend = tab + mask + 1; struct elem *oldend = htab->__tab->elems + htab->__tab->mask + 1;
if (nel > MAXSIZE) if (nel > MAXSIZE)
nel = MAXSIZE; nel = MAXSIZE;
for (newsize = MINSIZE; newsize < nel; newsize *= 2); for (newsize = MINSIZE; newsize < nel; newsize *= 2);
tab = calloc(newsize, sizeof *tab); htab->__tab->elems = calloc(newsize, sizeof *htab->__tab->elems);
if (!tab) { if (!htab->__tab->elems) {
tab = oldtab; htab->__tab->elems = oldtab;
return 0; return 0;
} }
mask = newsize - 1; htab->__tab->mask = newsize - 1;
if (!oldtab) if (!oldtab)
return 1; return 1;
for (e = oldtab; e < oldend; e++) for (e = oldtab; e < oldend; e++)
if (e->item.key) { if (e->item.key) {
for (i=e->hash,j=1; ; i+=j++) { for (i=e->hash,j=1; ; i+=j++) {
newe = tab + (i & mask); newe = htab->__tab->elems + (i & htab->__tab->mask);
if (!newe->item.key) if (!newe->item.key)
break; break;
} }
@ -67,27 +77,21 @@ static int resize(size_t nel)
int hcreate(size_t nel) int hcreate(size_t nel)
{ {
mask = 0; return __hcreate_r(nel, &htab);
used = 0;
tab = 0;
return resize(nel);
} }
void hdestroy(void) void hdestroy(void)
{ {
free(tab); __hdestroy_r(&htab);
tab = 0;
mask = 0;
used = 0;
} }
static struct elem *lookup(char *key, size_t hash) static struct elem *lookup(char *key, size_t hash, struct hsearch_data *htab)
{ {
size_t i, j; size_t i, j;
struct elem *e; struct elem *e;
for (i=hash,j=1; ; i+=j++) { for (i=hash,j=1; ; i+=j++) {
e = tab + (i & mask); e = htab->__tab->elems + (i & htab->__tab->mask);
if (!e->item.key || if (!e->item.key ||
(e->hash==hash && strcmp(e->item.key, key)==0)) (e->hash==hash && strcmp(e->item.key, key)==0))
break; break;
@ -97,22 +101,61 @@ static struct elem *lookup(char *key, size_t hash)
ENTRY *hsearch(ENTRY item, ACTION action) ENTRY *hsearch(ENTRY item, ACTION action)
{ {
size_t hash = keyhash(item.key); ENTRY *e;
struct elem *e = lookup(item.key, hash);
if (e->item.key) __hsearch_r(item, action, &e, &htab);
return &e->item; return e;
if (action == FIND) }
int __hcreate_r(size_t nel, struct hsearch_data *htab)
{
int r;
htab->__tab = calloc(1, sizeof *htab->__tab);
if (!htab->__tab)
return 0; return 0;
r = resize(nel, htab);
if (r == 0) {
free(htab->__tab);
htab->__tab = 0;
}
return r;
}
weak_alias(__hcreate_r, hcreate_r);
void __hdestroy_r(struct hsearch_data *htab)
{
if (htab->__tab) free(htab->__tab->elems);
free(htab->__tab);
htab->__tab = 0;
}
weak_alias(__hdestroy_r, hdestroy_r);
int __hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)
{
size_t hash = keyhash(item.key);
struct elem *e = lookup(item.key, hash, htab);
if (e->item.key) {
*retval = &e->item;
return 1;
}
if (action == FIND) {
*retval = 0;
return 0;
}
e->item = item; e->item = item;
e->hash = hash; e->hash = hash;
if (++used > mask - mask/4) { if (++htab->__tab->used > htab->__tab->mask - htab->__tab->mask/4) {
if (!resize(2*used)) { if (!resize(2*htab->__tab->used, htab)) {
used--; htab->__tab->used--;
e->item.key = 0; e->item.key = 0;
*retval = 0;
return 0; return 0;
} }
e = lookup(item.key, hash); e = lookup(item.key, hash, htab);
} }
return &e->item; *retval = &e->item;
return 1;
} }
weak_alias(__hsearch_r, hsearch_r);