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
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
static const char CVS_ID[] = "@(#) $RCSfile: hash.c,v $ $Revision: 1.6 $ $Date: 2012/04/25 14:49:28 $";
12
* This is merely a couple wrappers around NSPR's PLHashTable, using
13
* the identity hash and arena-aware allocators. The reason I did
14
* this is that hash tables are used in a few places throughout the
15
* NSS Cryptoki Framework in a fairly stereotyped way, and this allows
16
* me to pull the commonalities into one place. Should we ever want
17
* to change the implementation, it's all right here.
37
struct nssCKFWHashStr {
41
* The invariant that mutex protects is:
42
* The count accurately reflects the hashtable state.
45
PLHashTable *plHashTable;
50
nss_ckfw_identity_hash
55
PRUint32 i = (PRUint32)key;
56
PR_ASSERT(sizeof(PLHashNumber) == sizeof(PRUint32));
57
return (PLHashNumber)i;
64
NSS_IMPLEMENT nssCKFWHash *
67
NSSCKFWInstance *fwInstance,
76
return (nssCKFWHash *)NULL;
79
if( PR_SUCCESS != nssArena_verifyPointer(arena) ) {
80
*pError = CKR_ARGUMENTS_BAD;
81
return (nssCKFWHash *)NULL;
85
rv = nss_ZNEW(arena, nssCKFWHash);
87
*pError = CKR_HOST_MEMORY;
88
return (nssCKFWHash *)NULL;
91
rv->mutex = nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
93
if( CKR_OK == *pError ) {
94
(void)nss_ZFreeIf(rv);
95
*pError = CKR_GENERAL_ERROR;
97
return (nssCKFWHash *)NULL;
100
rv->plHashTable = PL_NewHashTable(0, nss_ckfw_identity_hash,
101
PL_CompareValues, PL_CompareValues, &nssArenaHashAllocOps, arena);
102
if (!rv->plHashTable) {
103
(void)nssCKFWMutex_Destroy(rv->mutex);
104
(void)nss_ZFreeIf(rv);
105
*pError = CKR_HOST_MEMORY;
106
return (nssCKFWHash *)NULL;
115
* nssCKFWHash_Destroy
124
(void)nssCKFWMutex_Destroy(hash->mutex);
125
PL_HashTableDestroy(hash->plHashTable);
126
(void)nss_ZFreeIf(hash);
141
CK_RV error = CKR_OK;
144
error = nssCKFWMutex_Lock(hash->mutex);
145
if( CKR_OK != error ) {
149
he = PL_HashTableAdd(hash->plHashTable, key, (void *)value);
151
error = CKR_HOST_MEMORY;
156
(void)nssCKFWMutex_Unlock(hash->mutex);
174
if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
178
found = PL_HashTableRemove(hash->plHashTable, it);
183
(void)nssCKFWMutex_Unlock(hash->mutex);
191
NSS_IMPLEMENT CK_ULONG
199
if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
205
(void)nssCKFWMutex_Unlock(hash->mutex);
214
NSS_IMPLEMENT CK_BBOOL
223
if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
227
value = PL_HashTableLookup(hash->plHashTable, it);
229
(void)nssCKFWMutex_Unlock(hash->mutex);
251
if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
255
rv = PL_HashTableLookup(hash->plHashTable, it);
257
(void)nssCKFWMutex_Unlock(hash->mutex);
263
nssCKFWHashIterator fcn;
268
nss_ckfwhash_enumerator
275
struct arg_str *as = (struct arg_str *)arg;
276
as->fcn(he->key, he->value, as->closure);
277
return HT_ENUMERATE_NEXT;
281
* nssCKFWHash_Iterate
283
* NOTE that the iteration function will be called with the hashtable locked.
289
nssCKFWHashIterator fcn,
295
as.closure = closure;
297
if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
301
PL_HashTableEnumerateEntries(hash->plHashTable, nss_ckfwhash_enumerator, &as);
303
(void)nssCKFWMutex_Unlock(hash->mutex);