1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include "sip_hash.h"
/*
* This file implements functions that add, search or remove an object
* from the hash table. The object is opaque to the hash functions. To add
* an object to the hash table, the caller provides the hash table,
* the object and the index into the hash table. To search an object,
* the caller provides the hash table, the digest (opaque), the index into
* the hash table and the function that does the actual match. Similarly,
* for removing an object, the caller provides the hash table, the digest
* (opaque), the index into the hash table and the function that does
* the acutal deletion of the object - if the deletion is successful,
* the object is taken off of the hash table.
*/
/*
* Given an object and the hash index, add it to the given hash table
*/
int
sip_hash_add(sip_hash_t *sip_hash, void *obj, int hindex)
{
sip_hash_obj_t *new_obj;
sip_hash_t *hash_entry;
assert(obj != NULL);
new_obj = (sip_hash_obj_t *)malloc(sizeof (*new_obj));
if (new_obj == NULL)
return (-1);
new_obj->sip_obj = obj;
new_obj->next_obj = NULL;
new_obj->prev_obj = NULL;
hash_entry = &sip_hash[hindex];
(void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
if (hash_entry->hash_count == 0) {
assert(hash_entry->hash_head == NULL);
assert(hash_entry->hash_tail == NULL);
hash_entry->hash_head = new_obj;
} else {
hash_entry->hash_tail->next_obj = new_obj;
new_obj->prev_obj = hash_entry->hash_tail;
}
hash_entry->hash_tail = new_obj;
hash_entry->hash_count++;
(void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
return (0);
}
/*
* Given the hash table, the digest to be searched for, index into the hash
* table and the function to do the actual matching, return the object,
* if found.
*/
void *
sip_hash_find(sip_hash_t *sip_hash, void *digest, int hindex,
boolean_t (*match_func)(void *, void *))
{
int count;
sip_hash_obj_t *tmp;
sip_hash_t *hash_entry;
hash_entry = &sip_hash[hindex];
(void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
tmp = hash_entry->hash_head;
for (count = 0; count < hash_entry->hash_count; count++) {
if (match_func(tmp->sip_obj, digest)) {
(void) pthread_mutex_unlock(
&hash_entry->sip_hash_mutex);
return (tmp->sip_obj);
}
tmp = tmp->next_obj;
}
(void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
return (NULL);
}
/*
* Walk the hash table and invoke func on each object. 'arg' is passed
* to 'func'
*/
void
sip_walk_hash(sip_hash_t *sip_hash, void (*func)(void *, void *), void *arg)
{
sip_hash_t *hash_entry;
int count;
int hcount;
sip_hash_obj_t *tmp;
for (count = 0; count < SIP_HASH_SZ; count++) {
hash_entry = &sip_hash[count];
(void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
tmp = hash_entry->hash_head;
for (hcount = 0; hcount < hash_entry->hash_count; hcount++) {
assert(tmp->sip_obj != NULL);
func(tmp->sip_obj, arg);
tmp = tmp->next_obj;
}
(void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
}
}
/*
* Given the hash table, the digest to be searched for, the index into the
* hash table and the delete function provided to do the actual deletion,
* remove the object from the hash table (i.e. only if the object is deleted).
*/
void
sip_hash_delete(sip_hash_t *sip_hash, void *digest, int hindex,
boolean_t (*del_func)(void *, void *, int *))
{
sip_hash_t *hash_entry;
int count;
sip_hash_obj_t *tmp;
int found;
hash_entry = &sip_hash[hindex];
(void) pthread_mutex_lock(&hash_entry->sip_hash_mutex);
tmp = hash_entry->hash_head;
for (count = 0; count < hash_entry->hash_count; count++) {
if (del_func(tmp->sip_obj, digest, &found)) {
if (tmp == hash_entry->hash_head) {
if (tmp->next_obj != NULL) {
hash_entry->hash_head = tmp->next_obj;
tmp->next_obj->prev_obj = NULL;
} else {
assert(hash_entry->hash_tail ==
hash_entry->hash_head);
hash_entry->hash_head = NULL;
hash_entry->hash_tail = NULL;
}
} else {
sip_hash_obj_t *next = tmp->next_obj;
if (next != NULL) {
tmp->prev_obj->next_obj = next;
next->prev_obj = tmp->prev_obj;
} else {
assert(hash_entry->hash_tail == tmp);
tmp->prev_obj->next_obj = NULL;
hash_entry->hash_tail =
tmp->prev_obj;
}
}
tmp->prev_obj = NULL;
tmp->next_obj = NULL;
free(tmp);
hash_entry->hash_count--;
(void) pthread_mutex_unlock(
&hash_entry->sip_hash_mutex);
return;
/*
* If we found the object, we are done
*/
} else if (found == 1) {
(void) pthread_mutex_unlock(
&hash_entry->sip_hash_mutex);
return;
}
tmp = tmp->next_obj;
}
(void) pthread_mutex_unlock(&hash_entry->sip_hash_mutex);
}
|