12882Svi117747 /*
22882Svi117747 * CDDL HEADER START
32882Svi117747 *
42882Svi117747 * The contents of this file are subject to the terms of the
52882Svi117747 * Common Development and Distribution License (the "License").
62882Svi117747 * You may not use this file except in compliance with the License.
72882Svi117747 *
82882Svi117747 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92882Svi117747 * or http://www.opensolaris.org/os/licensing.
102882Svi117747 * See the License for the specific language governing permissions
112882Svi117747 * and limitations under the License.
122882Svi117747 *
132882Svi117747 * When distributing Covered Code, include this CDDL HEADER in each
142882Svi117747 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152882Svi117747 * If applicable, add the following below this CDDL HEADER, with the
162882Svi117747 * fields enclosed by brackets "[]" replaced with your own identifying
172882Svi117747 * information: Portions Copyright [yyyy] [name of copyright owner]
182882Svi117747 *
192882Svi117747 * CDDL HEADER END
202882Svi117747 */
212882Svi117747
222882Svi117747 /*
23*3439Svi117747 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
242882Svi117747 * Use is subject to license terms.
252882Svi117747 */
262882Svi117747
272882Svi117747 #pragma ident "%Z%%M% %I% %E% SMI"
282882Svi117747
29*3439Svi117747 #include <stdlib.h>
30*3439Svi117747 #include <assert.h>
312882Svi117747 #include <pthread.h>
322882Svi117747
332882Svi117747 #include "sip_hash.h"
342882Svi117747
352882Svi117747 /*
362882Svi117747 * This file implements functions that add, search or remove an object
372882Svi117747 * from the hash table. The object is opaque to the hash functions. To add
382882Svi117747 * an object to the hash table, the caller provides the hash table,
392882Svi117747 * the object and the index into the hash table. To search an object,
402882Svi117747 * the caller provides the hash table, the digest (opaque), the index into
412882Svi117747 * the hash table and the function that does the actual match. Similarly,
422882Svi117747 * for removing an object, the caller provides the hash table, the digest
432882Svi117747 * (opaque), the index into the hash table and the function that does
442882Svi117747 * the acutal deletion of the object - if the deletion is successful,
452882Svi117747 * the object is taken off of the hash table.
462882Svi117747 */
472882Svi117747
482882Svi117747 /*
492882Svi117747 * Given an object and the hash index, add it to the given hash table
502882Svi117747 */
512882Svi117747 int
sip_hash_add(sip_hash_t * sip_hash,void * obj,int hindex)522882Svi117747 sip_hash_add(sip_hash_t *sip_hash, void *obj, int hindex)
532882Svi117747 {
542882Svi117747 sip_hash_obj_t *new_obj;
552882Svi117747 sip_hash_t *hash_entry;
562882Svi117747
572882Svi117747 assert(obj != NULL);
582882Svi117747
592882Svi117747 new_obj = (sip_hash_obj_t *)malloc(sizeof (*new_obj));
602882Svi117747 if (new_obj == NULL)
612882Svi117747 return (-1);
622882Svi117747 new_obj->sip_obj = obj;
632882Svi117747 new_obj->next_obj = NULL;
642882Svi117747 new_obj->prev_obj = NULL;
652882Svi117747 hash_entry = &sip_hash[hindex];
662882Svi117747 (void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
672882Svi117747 if (hash_entry->hash_count == 0) {
682882Svi117747 assert(hash_entry->hash_head == NULL);
692882Svi117747 assert(hash_entry->hash_tail == NULL);
702882Svi117747 hash_entry->hash_head = new_obj;
712882Svi117747 } else {
722882Svi117747 hash_entry->hash_tail->next_obj = new_obj;
732882Svi117747 new_obj->prev_obj = hash_entry->hash_tail;
742882Svi117747 }
752882Svi117747 hash_entry->hash_tail = new_obj;
762882Svi117747 hash_entry->hash_count++;
772882Svi117747 (void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
782882Svi117747 return (0);
792882Svi117747 }
802882Svi117747
812882Svi117747 /*
822882Svi117747 * Given the hash table, the digest to be searched for, index into the hash
832882Svi117747 * table and the function to do the actual matching, return the object,
842882Svi117747 * if found.
852882Svi117747 */
862882Svi117747 void *
sip_hash_find(sip_hash_t * sip_hash,void * digest,int hindex,boolean_t (* match_func)(void *,void *))872882Svi117747 sip_hash_find(sip_hash_t *sip_hash, void *digest, int hindex,
882882Svi117747 boolean_t (*match_func)(void *, void *))
892882Svi117747 {
902882Svi117747 int count;
912882Svi117747 sip_hash_obj_t *tmp;
922882Svi117747 sip_hash_t *hash_entry;
932882Svi117747
942882Svi117747 hash_entry = &sip_hash[hindex];
952882Svi117747 (void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
962882Svi117747 tmp = hash_entry->hash_head;
972882Svi117747 for (count = 0; count < hash_entry->hash_count; count++) {
982882Svi117747 if (match_func(tmp->sip_obj, digest)) {
992882Svi117747 (void) pthread_mutex_unlock(
1002882Svi117747 &hash_entry->sip_hash_mutex);
1012882Svi117747 return (tmp->sip_obj);
1022882Svi117747 }
1032882Svi117747 tmp = tmp->next_obj;
1042882Svi117747 }
1052882Svi117747 (void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
1062882Svi117747 return (NULL);
1072882Svi117747 }
1082882Svi117747
1092882Svi117747 /*
1102882Svi117747 * Walk the hash table and invoke func on each object. 'arg' is passed
1112882Svi117747 * to 'func'
1122882Svi117747 */
1132882Svi117747 void
sip_walk_hash(sip_hash_t * sip_hash,void (* func)(void *,void *),void * arg)1142882Svi117747 sip_walk_hash(sip_hash_t *sip_hash, void (*func)(void *, void *), void *arg)
1152882Svi117747 {
1162882Svi117747 sip_hash_t *hash_entry;
1172882Svi117747 int count;
1182882Svi117747 int hcount;
1192882Svi117747 sip_hash_obj_t *tmp;
1202882Svi117747
1212882Svi117747 for (count = 0; count < SIP_HASH_SZ; count++) {
1222882Svi117747 hash_entry = &sip_hash[count];
1232882Svi117747 (void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
1242882Svi117747 tmp = hash_entry->hash_head;
1252882Svi117747 for (hcount = 0; hcount < hash_entry->hash_count; hcount++) {
1262882Svi117747 assert(tmp->sip_obj != NULL);
1272882Svi117747 func(tmp->sip_obj, arg);
1282882Svi117747 tmp = tmp->next_obj;
1292882Svi117747 }
1302882Svi117747 (void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
1312882Svi117747 }
1322882Svi117747 }
1332882Svi117747
1342882Svi117747 /*
1352882Svi117747 * Given the hash table, the digest to be searched for, the index into the
1362882Svi117747 * hash table and the delete function provided to do the actual deletion,
1372882Svi117747 * remove the object from the hash table (i.e. only if the object is deleted).
1382882Svi117747 */
1392882Svi117747 void
sip_hash_delete(sip_hash_t * sip_hash,void * digest,int hindex,boolean_t (* del_func)(void *,void *,int *))1402882Svi117747 sip_hash_delete(sip_hash_t *sip_hash, void *digest, int hindex,
1412882Svi117747 boolean_t (*del_func)(void *, void *, int *))
1422882Svi117747 {
1432882Svi117747 sip_hash_t *hash_entry;
1442882Svi117747 int count;
1452882Svi117747 sip_hash_obj_t *tmp;
1462882Svi117747 int found;
1472882Svi117747
1482882Svi117747 hash_entry = &sip_hash[hindex];
1492882Svi117747 (void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
1502882Svi117747 tmp = hash_entry->hash_head;
1512882Svi117747 for (count = 0; count < hash_entry->hash_count; count++) {
1522882Svi117747 if (del_func(tmp->sip_obj, digest, &found)) {
1532882Svi117747 if (tmp == hash_entry->hash_head) {
1542882Svi117747 if (tmp->next_obj != NULL) {
1552882Svi117747 hash_entry->hash_head = tmp->next_obj;
1562882Svi117747 tmp->next_obj->prev_obj = NULL;
1572882Svi117747 } else {
1582882Svi117747 assert(hash_entry->hash_tail ==
1592882Svi117747 hash_entry->hash_head);
1602882Svi117747 hash_entry->hash_head = NULL;
1612882Svi117747 hash_entry->hash_tail = NULL;
1622882Svi117747 }
1632882Svi117747 } else {
1642882Svi117747 sip_hash_obj_t *next = tmp->next_obj;
1652882Svi117747
1662882Svi117747 if (next != NULL) {
1672882Svi117747 tmp->prev_obj->next_obj = next;
1682882Svi117747 next->prev_obj = tmp->prev_obj;
1692882Svi117747 } else {
1702882Svi117747 assert(hash_entry->hash_tail == tmp);
1712882Svi117747 tmp->prev_obj->next_obj = NULL;
1722882Svi117747 hash_entry->hash_tail =
1732882Svi117747 tmp->prev_obj;
1742882Svi117747 }
1752882Svi117747 }
1762882Svi117747 tmp->prev_obj = NULL;
1772882Svi117747 tmp->next_obj = NULL;
1782882Svi117747 free(tmp);
1792882Svi117747 hash_entry->hash_count--;
1802882Svi117747 (void) pthread_mutex_unlock(
1812882Svi117747 &hash_entry->sip_hash_mutex);
1822882Svi117747 return;
1832882Svi117747 /*
1842882Svi117747 * If we found the object, we are done
1852882Svi117747 */
1862882Svi117747 } else if (found == 1) {
1872882Svi117747 (void) pthread_mutex_unlock(
1882882Svi117747 &hash_entry->sip_hash_mutex);
1892882Svi117747 return;
1902882Svi117747 }
1912882Svi117747 tmp = tmp->next_obj;
1922882Svi117747 }
1932882Svi117747 (void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
1942882Svi117747 }
195