#include <strings.h>
#include <assert.h>
#include <ipmi_impl.h>
#include <string.h>
#include <strings.h>
#define IPMI_HASHCROSSOVER 137
#define IPMI_HASHCROSSUNDER 67
#define IPMI_HASHMINSIZE 13
static ulong_t
ipmi_hash_double(ulong_t size)
{
ulong_t nsize;
if (size < IPMI_HASHCROSSOVER) {
nsize = (size * 2) + 5;
return (nsize < IPMI_HASHCROSSOVER ? nsize :
IPMI_HASHCROSSOVER);
}
return ((size * 2) + 33);
}
static ulong_t
ipmi_hash_half(ulong_t size)
{
ulong_t nsize;
if (size > IPMI_HASHCROSSUNDER) {
nsize = (size - 33) / 2;
return (nsize > IPMI_HASHCROSSUNDER ? nsize :
IPMI_HASHCROSSUNDER);
}
nsize = (size - 5) / 2;
return (nsize > IPMI_HASHMINSIZE ? nsize : IPMI_HASHMINSIZE);
}
ipmi_hash_t *
ipmi_hash_create(ipmi_handle_t *hp, size_t linkoffs,
const void *(*convert)(const void *elem),
ulong_t (*compute)(const void *key),
int (*compare)(const void *lkey, const void *rkey))
{
ipmi_hash_t *ihp;
if ((ihp = ipmi_zalloc(hp, sizeof (ipmi_hash_t))) == NULL)
return (NULL);
ihp->ih_handle = hp;
ihp->ih_nbuckets = IPMI_HASHMINSIZE;
ihp->ih_linkoffs = linkoffs;
ihp->ih_convert = convert;
ihp->ih_compute = compute;
ihp->ih_compare = compare;
if ((ihp->ih_buckets = ipmi_zalloc(hp,
ihp->ih_nbuckets * sizeof (void *))) == NULL) {
ipmi_free(hp, ihp);
return (NULL);
}
return (ihp);
}
void
ipmi_hash_destroy(ipmi_hash_t *ihp)
{
if (ihp != NULL) {
ipmi_free(ihp->ih_handle, ihp->ih_buckets);
ipmi_free(ihp->ih_handle, ihp);
}
}
ulong_t
ipmi_hash_strhash(const void *key)
{
ulong_t g, h = 0;
const char *p;
for (p = key; *p != '\0'; p++) {
h = (h << 4) + *p;
if ((g = (h & 0xf0000000)) != 0) {
h ^= (g >> 24);
h ^= g;
}
}
return (h);
}
int
ipmi_hash_strcmp(const void *lhs, const void *rhs)
{
return (strcmp(lhs, rhs));
}
ulong_t
ipmi_hash_ptrhash(const void *key)
{
return (*((const uintptr_t *)key) >> 4);
}
int
ipmi_hash_ptrcmp(const void *lhs, const void *rhs)
{
const uintptr_t *l = lhs, *r = rhs;
return (*l == *r ? 0 : -1);
}
static ulong_t
ipmi_hash_compute(ipmi_hash_t *ihp, const void *elem)
{
return (ihp->ih_compute(ihp->ih_convert(elem)) % ihp->ih_nbuckets);
}
static void
ipmi_hash_resize(ipmi_hash_t *ihp, ulong_t nsize)
{
size_t osize = ihp->ih_nbuckets;
ipmi_handle_t *hp = ihp->ih_handle;
ipmi_hash_link_t *link, **nbuckets;
ulong_t idx, nidx;
assert(nsize >= IPMI_HASHMINSIZE);
if (nsize == osize)
return;
if ((nbuckets = ipmi_zalloc(hp, nsize * sizeof (void *))) == NULL) {
return;
}
ihp->ih_nbuckets = nsize;
for (idx = 0; idx < osize; idx++) {
while ((link = ihp->ih_buckets[idx]) != NULL) {
void *elem;
ihp->ih_buckets[idx] = link->ihl_next;
elem = (void *)((uintptr_t)link - ihp->ih_linkoffs);
nidx = ipmi_hash_compute(ihp, elem);
link->ihl_next = nbuckets[nidx];
nbuckets[nidx] = link;
}
}
ipmi_free(hp, ihp->ih_buckets);
ihp->ih_buckets = nbuckets;
}
void *
ipmi_hash_lookup(ipmi_hash_t *ihp, const void *search)
{
ulong_t idx = ihp->ih_compute(search) % ihp->ih_nbuckets;
ipmi_hash_link_t *hl;
for (hl = ihp->ih_buckets[idx]; hl != NULL; hl = hl->ihl_next) {
void *elem = (void *)((uintptr_t)hl - ihp->ih_linkoffs);
if (ihp->ih_compare(ihp->ih_convert(elem), search) == 0)
return (elem);
}
return (NULL);
}
void *
ipmi_hash_first(ipmi_hash_t *ihp)
{
void *link = ipmi_list_next(&(ihp)->ih_list);
if (link == NULL)
return (NULL);
return ((void *)((uintptr_t)link - ihp->ih_linkoffs));
}
void *
ipmi_hash_next(ipmi_hash_t *ihp, void *elem)
{
void *link = ipmi_list_next((uintptr_t)elem + ihp->ih_linkoffs);
if (link == NULL)
return (NULL);
return ((void *)((uintptr_t)link - ihp->ih_linkoffs));
}
void
ipmi_hash_insert(ipmi_hash_t *ihp, void *elem)
{
ipmi_hash_link_t *link = (void *)((uintptr_t)elem + ihp->ih_linkoffs);
ulong_t idx = ipmi_hash_compute(ihp, elem);
assert(ipmi_hash_lookup(ihp, ihp->ih_convert(elem)) == NULL);
link->ihl_next = ihp->ih_buckets[idx];
ihp->ih_buckets[idx] = link;
ipmi_list_append(&ihp->ih_list, link);
if (++ihp->ih_nelements > ihp->ih_nbuckets / 2)
ipmi_hash_resize(ihp, ipmi_hash_double(ihp->ih_nbuckets));
}
void
ipmi_hash_remove(ipmi_hash_t *ihp, void *elem)
{
ulong_t idx = ipmi_hash_compute(ihp, elem);
ipmi_hash_link_t *link = (void *)((uintptr_t)elem + ihp->ih_linkoffs);
ipmi_hash_link_t **hlp = &ihp->ih_buckets[idx];
for (; *hlp != NULL; hlp = &(*hlp)->ihl_next) {
if (*hlp == link)
break;
}
assert(*hlp != NULL);
*hlp = (*hlp)->ihl_next;
ipmi_list_delete(&ihp->ih_list, link);
assert(ihp->ih_nelements > 0);
if (--ihp->ih_nelements < ihp->ih_nbuckets / 4)
ipmi_hash_resize(ihp, ipmi_hash_half(ihp->ih_nbuckets));
}
size_t
ipmi_hash_count(ipmi_hash_t *ihp)
{
return (ihp->ih_nelements);
}