OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 /* | 5 /* |
6 * hash.c | 6 * hash.c |
7 * | 7 * |
8 * This is merely a couple wrappers around NSPR's PLHashTable, using | 8 * This is merely a couple wrappers around NSPR's PLHashTable, using |
9 * the identity hash and arena-aware allocators. | 9 * the identity hash and arena-aware allocators. |
10 * This is a copy of ckfw/hash.c, with modifications to use NSS types | 10 * This is a copy of ckfw/hash.c, with modifications to use NSS types |
(...skipping 14 matching lines...) Expand all Loading... |
25 * nssHash_Destroy | 25 * nssHash_Destroy |
26 * nssHash_Add | 26 * nssHash_Add |
27 * nssHash_Remove | 27 * nssHash_Remove |
28 * nssHash_Count | 28 * nssHash_Count |
29 * nssHash_Exists | 29 * nssHash_Exists |
30 * nssHash_Lookup | 30 * nssHash_Lookup |
31 * nssHash_Iterate | 31 * nssHash_Iterate |
32 */ | 32 */ |
33 | 33 |
34 struct nssHashStr { | 34 struct nssHashStr { |
35 NSSArena *arena; | 35 NSSArena *arena; |
36 PRBool i_alloced_arena; | 36 PRBool i_alloced_arena; |
37 PRLock *mutex; | 37 PRLock *mutex; |
38 | 38 |
39 /* | 39 /* |
40 * The invariant that mutex protects is: | 40 * The invariant that mutex protects is: |
41 * The count accurately reflects the hashtable state. | 41 * The count accurately reflects the hashtable state. |
42 */ | 42 */ |
43 | 43 |
44 PLHashTable *plHashTable; | 44 PLHashTable *plHashTable; |
45 PRUint32 count; | 45 PRUint32 count; |
46 }; | 46 }; |
47 | 47 |
48 static PLHashNumber | 48 static PLHashNumber |
49 nss_identity_hash | 49 nss_identity_hash(const void *key) |
50 ( | |
51 const void *key | |
52 ) | |
53 { | 50 { |
54 return (PLHashNumber)((char *)key - (char *)NULL); | 51 return (PLHashNumber)((char *)key - (char *)NULL); |
55 } | 52 } |
56 | 53 |
57 static PLHashNumber | 54 static PLHashNumber |
58 nss_item_hash | 55 nss_item_hash(const void *key) |
59 ( | |
60 const void *key | |
61 ) | |
62 { | 56 { |
63 unsigned int i; | 57 unsigned int i; |
64 PLHashNumber h; | 58 PLHashNumber h; |
65 NSSItem *it = (NSSItem *)key; | 59 NSSItem *it = (NSSItem *)key; |
66 h = 0; | 60 h = 0; |
67 for (i=0; i<it->size; i++) | 61 for (i = 0; i < it->size; i++) |
68 h = PR_ROTATE_LEFT32(h, 4) ^ ((unsigned char *)it->data)[i]; | 62 h = PR_ROTATE_LEFT32(h, 4) ^ ((unsigned char *)it->data)[i]; |
69 return h; | 63 return h; |
70 } | 64 } |
71 | 65 |
72 static int | 66 static int |
73 nss_compare_items(const void *v1, const void *v2) | 67 nss_compare_items(const void *v1, const void *v2) |
74 { | 68 { |
75 PRStatus ignore; | 69 PRStatus ignore; |
76 return (int)nssItem_Equal((NSSItem *)v1, (NSSItem *)v2, &ignore); | 70 return (int)nssItem_Equal((NSSItem *)v1, (NSSItem *)v2, &ignore); |
77 } | 71 } |
78 | 72 |
79 /* | 73 /* |
80 * nssHash_create | 74 * nssHash_create |
81 * | 75 * |
82 */ | 76 */ |
83 NSS_IMPLEMENT nssHash * | 77 NSS_IMPLEMENT nssHash * |
84 nssHash_Create | 78 nssHash_Create(NSSArena *arenaOpt, PRUint32 numBuckets, PLHashFunction keyHash, |
85 ( | 79 PLHashComparator keyCompare, PLHashComparator valueCompare) |
86 NSSArena *arenaOpt, | |
87 PRUint32 numBuckets, | |
88 PLHashFunction keyHash, | |
89 PLHashComparator keyCompare, | |
90 PLHashComparator valueCompare | |
91 ) | |
92 { | 80 { |
93 nssHash *rv; | 81 nssHash *rv; |
94 NSSArena *arena; | 82 NSSArena *arena; |
95 PRBool i_alloced; | 83 PRBool i_alloced; |
96 | 84 |
97 #ifdef NSSDEBUG | 85 #ifdef NSSDEBUG |
98 if( arenaOpt && PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { | 86 if (arenaOpt && PR_SUCCESS != nssArena_verifyPointer(arenaOpt)) { |
99 nss_SetError(NSS_ERROR_INVALID_POINTER); | 87 nss_SetError(NSS_ERROR_INVALID_POINTER); |
100 return (nssHash *)NULL; | 88 return (nssHash *)NULL; |
101 } | 89 } |
102 #endif /* NSSDEBUG */ | 90 #endif /* NSSDEBUG */ |
103 | 91 |
104 if (arenaOpt) { | 92 if (arenaOpt) { |
105 arena = arenaOpt; | 93 arena = arenaOpt; |
106 i_alloced = PR_FALSE; | 94 i_alloced = PR_FALSE; |
107 } else { | 95 } else { |
108 arena = nssArena_Create(); | 96 arena = nssArena_Create(); |
109 i_alloced = PR_TRUE; | 97 i_alloced = PR_TRUE; |
110 } | 98 } |
111 | 99 |
112 rv = nss_ZNEW(arena, nssHash); | 100 rv = nss_ZNEW(arena, nssHash); |
113 if( (nssHash *)NULL == rv ) { | 101 if ((nssHash *)NULL == rv) { |
114 goto loser; | 102 goto loser; |
115 } | 103 } |
116 | 104 |
117 rv->mutex = PZ_NewLock(nssILockOther); | 105 rv->mutex = PZ_NewLock(nssILockOther); |
118 if( (PZLock *)NULL == rv->mutex ) { | 106 if ((PZLock *)NULL == rv->mutex) { |
119 goto loser; | 107 goto loser; |
120 } | 108 } |
121 | 109 |
122 rv->plHashTable = PL_NewHashTable(numBuckets, | 110 rv->plHashTable = |
123 keyHash, keyCompare, valueCompare, | 111 PL_NewHashTable(numBuckets, keyHash, keyCompare, valueCompare, |
124 &nssArenaHashAllocOps, arena); | 112 &nssArenaHashAllocOps, arena); |
125 if( (PLHashTable *)NULL == rv->plHashTable ) { | 113 if ((PLHashTable *)NULL == rv->plHashTable) { |
126 (void)PZ_DestroyLock(rv->mutex); | 114 (void)PZ_DestroyLock(rv->mutex); |
127 goto loser; | 115 goto loser; |
128 } | 116 } |
129 | 117 |
130 rv->count = 0; | 118 rv->count = 0; |
131 rv->arena = arena; | 119 rv->arena = arena; |
132 rv->i_alloced_arena = i_alloced; | 120 rv->i_alloced_arena = i_alloced; |
133 | 121 |
134 return rv; | 122 return rv; |
135 loser: | 123 loser: |
136 (void)nss_ZFreeIf(rv); | 124 (void)nss_ZFreeIf(rv); |
137 return (nssHash *)NULL; | 125 return (nssHash *)NULL; |
138 } | 126 } |
139 | 127 |
140 /* | 128 /* |
141 * nssHash_CreatePointer | 129 * nssHash_CreatePointer |
142 * | 130 * |
143 */ | 131 */ |
144 NSS_IMPLEMENT nssHash * | 132 NSS_IMPLEMENT nssHash * |
145 nssHash_CreatePointer | 133 nssHash_CreatePointer(NSSArena *arenaOpt, PRUint32 numBuckets) |
146 ( | |
147 NSSArena *arenaOpt, | |
148 PRUint32 numBuckets | |
149 ) | |
150 { | 134 { |
151 return nssHash_Create(arenaOpt, numBuckets, | 135 return nssHash_Create(arenaOpt, numBuckets, nss_identity_hash, |
152 nss_identity_hash, PL_CompareValues, PL_CompareValues); | 136 PL_CompareValues, PL_CompareValues); |
153 } | 137 } |
154 | 138 |
155 /* | 139 /* |
156 * nssHash_CreateString | 140 * nssHash_CreateString |
157 * | 141 * |
158 */ | 142 */ |
159 NSS_IMPLEMENT nssHash * | 143 NSS_IMPLEMENT nssHash * |
160 nssHash_CreateString | 144 nssHash_CreateString(NSSArena *arenaOpt, PRUint32 numBuckets) |
161 ( | |
162 NSSArena *arenaOpt, | |
163 PRUint32 numBuckets | |
164 ) | |
165 { | 145 { |
166 return nssHash_Create(arenaOpt, numBuckets, | 146 return nssHash_Create(arenaOpt, numBuckets, PL_HashString, |
167 PL_HashString, PL_CompareStrings, PL_CompareStrings); | 147 PL_CompareStrings, PL_CompareStrings); |
168 } | 148 } |
169 | 149 |
170 /* | 150 /* |
171 * nssHash_CreateItem | 151 * nssHash_CreateItem |
172 * | 152 * |
173 */ | 153 */ |
174 NSS_IMPLEMENT nssHash * | 154 NSS_IMPLEMENT nssHash * |
175 nssHash_CreateItem | 155 nssHash_CreateItem(NSSArena *arenaOpt, PRUint32 numBuckets) |
176 ( | |
177 NSSArena *arenaOpt, | |
178 PRUint32 numBuckets | |
179 ) | |
180 { | 156 { |
181 return nssHash_Create(arenaOpt, numBuckets, | 157 return nssHash_Create(arenaOpt, numBuckets, nss_item_hash, |
182 nss_item_hash, nss_compare_items, PL_CompareValues); | 158 nss_compare_items, PL_CompareValues); |
183 } | 159 } |
184 | 160 |
185 /* | 161 /* |
186 * nssHash_Destroy | 162 * nssHash_Destroy |
187 * | 163 * |
188 */ | 164 */ |
189 NSS_IMPLEMENT void | 165 NSS_IMPLEMENT void |
190 nssHash_Destroy | 166 nssHash_Destroy(nssHash *hash) |
191 ( | |
192 nssHash *hash | |
193 ) | |
194 { | 167 { |
195 (void)PZ_DestroyLock(hash->mutex); | 168 (void)PZ_DestroyLock(hash->mutex); |
196 PL_HashTableDestroy(hash->plHashTable); | 169 PL_HashTableDestroy(hash->plHashTable); |
197 if (hash->i_alloced_arena) { | 170 if (hash->i_alloced_arena) { |
198 nssArena_Destroy(hash->arena); | 171 nssArena_Destroy(hash->arena); |
199 } else { | 172 } else { |
200 nss_ZFreeIf(hash); | 173 nss_ZFreeIf(hash); |
201 } | 174 } |
202 } | 175 } |
203 | 176 |
204 /* | 177 /* |
205 * nssHash_Add | 178 * nssHash_Add |
206 * | 179 * |
207 */ | 180 */ |
208 NSS_IMPLEMENT PRStatus | 181 NSS_IMPLEMENT PRStatus |
209 nssHash_Add | 182 nssHash_Add(nssHash *hash, const void *key, const void *value) |
210 ( | |
211 nssHash *hash, | |
212 const void *key, | |
213 const void *value | |
214 ) | |
215 { | 183 { |
216 PRStatus error = PR_FAILURE; | 184 PRStatus error = PR_FAILURE; |
217 PLHashEntry *he; | 185 PLHashEntry *he; |
218 | 186 |
219 PZ_Lock(hash->mutex); | 187 PZ_Lock(hash->mutex); |
220 | |
221 he = PL_HashTableAdd(hash->plHashTable, key, (void *)value); | |
222 if( (PLHashEntry *)NULL == he ) { | |
223 nss_SetError(NSS_ERROR_NO_MEMORY); | |
224 } else if (he->value != value) { | |
225 nss_SetError(NSS_ERROR_HASH_COLLISION); | |
226 } else { | |
227 hash->count++; | |
228 error = PR_SUCCESS; | |
229 } | |
230 | 188 |
231 (void)PZ_Unlock(hash->mutex); | 189 he = PL_HashTableAdd(hash->plHashTable, key, (void *)value); |
| 190 if ((PLHashEntry *)NULL == he) { |
| 191 nss_SetError(NSS_ERROR_NO_MEMORY); |
| 192 } else if (he->value != value) { |
| 193 nss_SetError(NSS_ERROR_HASH_COLLISION); |
| 194 } else { |
| 195 hash->count++; |
| 196 error = PR_SUCCESS; |
| 197 } |
232 | 198 |
233 return error; | 199 (void)PZ_Unlock(hash->mutex); |
| 200 |
| 201 return error; |
234 } | 202 } |
235 | 203 |
236 /* | 204 /* |
237 * nssHash_Remove | 205 * nssHash_Remove |
238 * | 206 * |
239 */ | 207 */ |
240 NSS_IMPLEMENT void | 208 NSS_IMPLEMENT void |
241 nssHash_Remove | 209 nssHash_Remove(nssHash *hash, const void *it) |
242 ( | |
243 nssHash *hash, | |
244 const void *it | |
245 ) | |
246 { | 210 { |
247 PRBool found; | 211 PRBool found; |
248 | 212 |
249 PZ_Lock(hash->mutex); | 213 PZ_Lock(hash->mutex); |
250 | 214 |
251 found = PL_HashTableRemove(hash->plHashTable, it); | 215 found = PL_HashTableRemove(hash->plHashTable, it); |
252 if( found ) { | 216 if (found) { |
253 hash->count--; | 217 hash->count--; |
254 } | 218 } |
255 | 219 |
256 (void)PZ_Unlock(hash->mutex); | 220 (void)PZ_Unlock(hash->mutex); |
257 return; | 221 return; |
258 } | 222 } |
259 | 223 |
260 /* | 224 /* |
261 * nssHash_Count | 225 * nssHash_Count |
262 * | 226 * |
263 */ | 227 */ |
264 NSS_IMPLEMENT PRUint32 | 228 NSS_IMPLEMENT PRUint32 |
265 nssHash_Count | 229 nssHash_Count(nssHash *hash) |
266 ( | |
267 nssHash *hash | |
268 ) | |
269 { | 230 { |
270 PRUint32 count; | 231 PRUint32 count; |
271 | 232 |
272 PZ_Lock(hash->mutex); | 233 PZ_Lock(hash->mutex); |
273 | 234 |
274 count = hash->count; | 235 count = hash->count; |
275 | 236 |
276 (void)PZ_Unlock(hash->mutex); | 237 (void)PZ_Unlock(hash->mutex); |
277 | 238 |
278 return count; | 239 return count; |
279 } | 240 } |
280 | 241 |
281 /* | 242 /* |
282 * nssHash_Exists | 243 * nssHash_Exists |
283 * | 244 * |
284 */ | 245 */ |
285 NSS_IMPLEMENT PRBool | 246 NSS_IMPLEMENT PRBool |
286 nssHash_Exists | 247 nssHash_Exists(nssHash *hash, const void *it) |
287 ( | |
288 nssHash *hash, | |
289 const void *it | |
290 ) | |
291 { | 248 { |
292 void *value; | 249 void *value; |
293 | 250 |
294 PZ_Lock(hash->mutex); | 251 PZ_Lock(hash->mutex); |
295 | 252 |
296 value = PL_HashTableLookup(hash->plHashTable, it); | 253 value = PL_HashTableLookup(hash->plHashTable, it); |
297 | 254 |
298 (void)PZ_Unlock(hash->mutex); | 255 (void)PZ_Unlock(hash->mutex); |
299 | 256 |
300 if( (void *)NULL == value ) { | 257 if ((void *)NULL == value) { |
301 return PR_FALSE; | 258 return PR_FALSE; |
302 } else { | 259 } else { |
303 return PR_TRUE; | 260 return PR_TRUE; |
304 } | 261 } |
305 } | 262 } |
306 | 263 |
307 /* | 264 /* |
308 * nssHash_Lookup | 265 * nssHash_Lookup |
309 * | 266 * |
310 */ | 267 */ |
311 NSS_IMPLEMENT void * | 268 NSS_IMPLEMENT void * |
312 nssHash_Lookup | 269 nssHash_Lookup(nssHash *hash, const void *it) |
313 ( | |
314 nssHash *hash, | |
315 const void *it | |
316 ) | |
317 { | 270 { |
318 void *rv; | 271 void *rv; |
319 | 272 |
320 PZ_Lock(hash->mutex); | 273 PZ_Lock(hash->mutex); |
321 | 274 |
322 rv = PL_HashTableLookup(hash->plHashTable, it); | 275 rv = PL_HashTableLookup(hash->plHashTable, it); |
323 | 276 |
324 (void)PZ_Unlock(hash->mutex); | 277 (void)PZ_Unlock(hash->mutex); |
325 | 278 |
326 return rv; | 279 return rv; |
327 } | 280 } |
328 | 281 |
329 struct arg_str { | 282 struct arg_str { |
330 nssHashIterator fcn; | 283 nssHashIterator fcn; |
331 void *closure; | 284 void *closure; |
332 }; | 285 }; |
333 | 286 |
334 static PRIntn | 287 static PRIntn |
335 nss_hash_enumerator | 288 nss_hash_enumerator(PLHashEntry *he, PRIntn index, void *arg) |
336 ( | |
337 PLHashEntry *he, | |
338 PRIntn index, | |
339 void *arg | |
340 ) | |
341 { | 289 { |
342 struct arg_str *as = (struct arg_str *)arg; | 290 struct arg_str *as = (struct arg_str *)arg; |
343 as->fcn(he->key, he->value, as->closure); | 291 as->fcn(he->key, he->value, as->closure); |
344 return HT_ENUMERATE_NEXT; | 292 return HT_ENUMERATE_NEXT; |
345 } | 293 } |
346 | 294 |
347 /* | 295 /* |
348 * nssHash_Iterate | 296 * nssHash_Iterate |
349 * | 297 * |
350 * NOTE that the iteration function will be called with the hashtable locked. | 298 * NOTE that the iteration function will be called with the hashtable locked. |
351 */ | 299 */ |
352 NSS_IMPLEMENT void | 300 NSS_IMPLEMENT void |
353 nssHash_Iterate | 301 nssHash_Iterate(nssHash *hash, nssHashIterator fcn, void *closure) |
354 ( | |
355 nssHash *hash, | |
356 nssHashIterator fcn, | |
357 void *closure | |
358 ) | |
359 { | 302 { |
360 struct arg_str as; | 303 struct arg_str as; |
361 as.fcn = fcn; | 304 as.fcn = fcn; |
362 as.closure = closure; | 305 as.closure = closure; |
363 | 306 |
364 PZ_Lock(hash->mutex); | 307 PZ_Lock(hash->mutex); |
365 | 308 |
366 PL_HashTableEnumerateEntries(hash->plHashTable, nss_hash_enumerator, &as); | 309 PL_HashTableEnumerateEntries(hash->plHashTable, nss_hash_enumerator, &as); |
367 | 310 |
368 (void)PZ_Unlock(hash->mutex); | 311 (void)PZ_Unlock(hash->mutex); |
369 | 312 |
370 return; | 313 return; |
371 } | 314 } |
OLD | NEW |