void * hash_insert (hash_t h, const void *key, void *data) { struct hash_node *p; unsigned int slot; if (!h || !key || !data) { errno = EINVAL; return (NULL); } lsd_mutex_lock (&h->mutex); assert (h->magic == HASH_MAGIC); slot = h->key_f (key) % h->size; for (p = h->table[slot]; p != NULL; p = p->next) { if (!h->cmp_f (p->hkey, key)) { errno = EEXIST; data = NULL; goto end; } } if (!(p = hash_node_alloc ())) { data = lsd_nomem_error (__FILE__, __LINE__, "hash_insert"); goto end; } p->hkey = key; p->data = data; p->next = h->table[slot]; h->table[slot] = p; h->count++; end: lsd_mutex_unlock (&h->mutex); return (data); }
void * hash_insert (hash_t h, const void *key, void *data) { unsigned int slot; int cmpval; struct hash_node **pp; struct hash_node *p; if (!h || !key || !data) { errno = EINVAL; return (NULL); } lsd_mutex_lock (&h->mutex); slot = h->key_f (key) % h->size; for (pp = &(h->table[slot]); (p = *pp) != NULL; pp = &(p->next)) { cmpval = h->cmp_f (p->hkey, key); if (cmpval < 0) { continue; } if (cmpval == 0) { errno = EEXIST; data = NULL; goto end; } break; } if (!(p = hash_node_alloc ())) { data = NULL; goto end; } p->hkey = key; p->data = data; p->next = *pp; *pp = p; h->count++; end: lsd_mutex_unlock (&h->mutex); return (data); }