Bug 449533. Set the mFixedPitch flag within SetFixedPitch. r+sr=vlad
[wine-gecko.git] / security / nss / lib / ckfw / hash.c
blob9cbe7b83bd85538cae453c33d70979b4b6e11163
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #ifdef DEBUG
38 static const char CVS_ID[] = "@(#) $RCSfile: hash.c,v $ $Revision: 1.3 $ $Date: 2005/01/20 02:25:45 $";
39 #endif /* DEBUG */
42 * hash.c
44 * This is merely a couple wrappers around NSPR's PLHashTable, using
45 * the identity hash and arena-aware allocators. The reason I did
46 * this is that hash tables are used in a few places throughout the
47 * NSS Cryptoki Framework in a fairly stereotyped way, and this allows
48 * me to pull the commonalities into one place. Should we ever want
49 * to change the implementation, it's all right here.
52 #ifndef CK_T
53 #include "ck.h"
54 #endif /* CK_T */
57 * nssCKFWHash
59 * nssCKFWHash_Create
60 * nssCKFWHash_Destroy
61 * nssCKFWHash_Add
62 * nssCKFWHash_Remove
63 * nssCKFWHash_Count
64 * nssCKFWHash_Exists
65 * nssCKFWHash_Lookup
66 * nssCKFWHash_Iterate
69 struct nssCKFWHashStr {
70 NSSCKFWMutex *mutex;
73 * The invariant that mutex protects is:
74 * The count accurately reflects the hashtable state.
77 PLHashTable *plHashTable;
78 CK_ULONG count;
81 static PLHashNumber
82 nss_ckfw_identity_hash
84 const void *key
87 PRUint32 i = (PRUint32)key;
88 PR_ASSERT(sizeof(PLHashNumber) == sizeof(PRUint32));
89 return (PLHashNumber)i;
93 * nssCKFWHash_Create
96 NSS_IMPLEMENT nssCKFWHash *
97 nssCKFWHash_Create
99 NSSCKFWInstance *fwInstance,
100 NSSArena *arena,
101 CK_RV *pError
104 nssCKFWHash *rv;
106 #ifdef NSSDEBUG
107 if( (CK_RV *)NULL == pError ) {
108 return (nssCKFWHash *)NULL;
111 if( PR_SUCCESS != nssArena_verifyPointer(arena) ) {
112 *pError = CKR_ARGUMENTS_BAD;
113 return (nssCKFWHash *)NULL;
115 #endif /* NSSDEBUG */
117 rv = nss_ZNEW(arena, nssCKFWHash);
118 if( (nssCKFWHash *)NULL == rv ) {
119 *pError = CKR_HOST_MEMORY;
120 return (nssCKFWHash *)NULL;
123 rv->mutex = nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
124 if( (NSSCKFWMutex *)NULL == rv->mutex ) {
125 if( CKR_OK == *pError ) {
126 *pError = CKR_GENERAL_ERROR;
128 return (nssCKFWHash *)NULL;
131 rv->plHashTable = PL_NewHashTable(0, nss_ckfw_identity_hash,
132 PL_CompareValues, PL_CompareValues, &nssArenaHashAllocOps, arena);
133 if( (PLHashTable *)NULL == rv->plHashTable ) {
134 (void)nssCKFWMutex_Destroy(rv->mutex);
135 (void)nss_ZFreeIf(rv);
136 *pError = CKR_HOST_MEMORY;
137 return (nssCKFWHash *)NULL;
140 rv->count = 0;
142 return rv;
146 * nssCKFWHash_Destroy
149 NSS_IMPLEMENT void
150 nssCKFWHash_Destroy
152 nssCKFWHash *hash
155 (void)nssCKFWMutex_Destroy(hash->mutex);
156 PL_HashTableDestroy(hash->plHashTable);
157 (void)nss_ZFreeIf(hash);
161 * nssCKFWHash_Add
164 NSS_IMPLEMENT CK_RV
165 nssCKFWHash_Add
167 nssCKFWHash *hash,
168 const void *key,
169 const void *value
172 CK_RV error = CKR_OK;
173 PLHashEntry *he;
175 error = nssCKFWMutex_Lock(hash->mutex);
176 if( CKR_OK != error ) {
177 return error;
180 he = PL_HashTableAdd(hash->plHashTable, key, (void *)value);
181 if( (PLHashEntry *)NULL == he ) {
182 error = CKR_HOST_MEMORY;
183 } else {
184 hash->count++;
187 (void)nssCKFWMutex_Unlock(hash->mutex);
189 return error;
193 * nssCKFWHash_Remove
196 NSS_IMPLEMENT void
197 nssCKFWHash_Remove
199 nssCKFWHash *hash,
200 const void *it
203 PRBool found;
205 if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
206 return;
209 found = PL_HashTableRemove(hash->plHashTable, it);
210 if( found ) {
211 hash->count--;
214 (void)nssCKFWMutex_Unlock(hash->mutex);
215 return;
219 * nssCKFWHash_Count
222 NSS_IMPLEMENT CK_ULONG
223 nssCKFWHash_Count
225 nssCKFWHash *hash
228 CK_ULONG count;
230 if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
231 return (CK_ULONG)0;
234 count = hash->count;
236 (void)nssCKFWMutex_Unlock(hash->mutex);
238 return count;
242 * nssCKFWHash_Exists
245 NSS_IMPLEMENT CK_BBOOL
246 nssCKFWHash_Exists
248 nssCKFWHash *hash,
249 const void *it
252 void *value;
254 if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
255 return CK_FALSE;
258 value = PL_HashTableLookup(hash->plHashTable, it);
260 (void)nssCKFWMutex_Unlock(hash->mutex);
262 if( (void *)NULL == value ) {
263 return CK_FALSE;
264 } else {
265 return CK_TRUE;
270 * nssCKFWHash_Lookup
273 NSS_IMPLEMENT void *
274 nssCKFWHash_Lookup
276 nssCKFWHash *hash,
277 const void *it
280 void *rv;
282 if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
283 return (void *)NULL;
286 rv = PL_HashTableLookup(hash->plHashTable, it);
288 (void)nssCKFWMutex_Unlock(hash->mutex);
290 return rv;
293 struct arg_str {
294 nssCKFWHashIterator fcn;
295 void *closure;
298 static PRIntn
299 nss_ckfwhash_enumerator
301 PLHashEntry *he,
302 PRIntn index,
303 void *arg
306 struct arg_str *as = (struct arg_str *)arg;
307 as->fcn(he->key, he->value, as->closure);
308 return HT_ENUMERATE_NEXT;
312 * nssCKFWHash_Iterate
314 * NOTE that the iteration function will be called with the hashtable locked.
316 NSS_IMPLEMENT void
317 nssCKFWHash_Iterate
319 nssCKFWHash *hash,
320 nssCKFWHashIterator fcn,
321 void *closure
324 struct arg_str as;
325 as.fcn = fcn;
326 as.closure = closure;
328 if( CKR_OK != nssCKFWMutex_Lock(hash->mutex) ) {
329 return;
332 PL_HashTableEnumerateEntries(hash->plHashTable, nss_ckfwhash_enumerator, &as);
334 (void)nssCKFWMutex_Unlock(hash->mutex);
336 return;