Coverage Report

Created: 2026-04-04 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/base/hash.c
Line
Count
Source
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/. */
4
5
/*
6
 * hash.c
7
 *
8
 * This is merely a couple wrappers around NSPR's PLHashTable, using
9
 * the identity hash and arena-aware allocators.
10
 * This is a copy of ckfw/hash.c, with modifications to use NSS types
11
 * (not Cryptoki types).  Would like for this to be a single implementation,
12
 * but doesn't seem like it will work.
13
 */
14
15
#ifndef BASE_H
16
#include "base.h"
17
#endif /* BASE_H */
18
19
#include "prbit.h"
20
21
/*
22
 * nssHash
23
 *
24
 *  nssHash_Create
25
 *  nssHash_Destroy
26
 *  nssHash_Add
27
 *  nssHash_Remove
28
 *  nssHash_Count
29
 *  nssHash_Exists
30
 *  nssHash_Lookup
31
 *  nssHash_Iterate
32
 */
33
34
struct nssHashStr {
35
    NSSArena *arena;
36
    PRBool i_alloced_arena;
37
    PRLock *mutex;
38
39
    /*
40
     * The invariant that mutex protects is:
41
     *   The count accurately reflects the hashtable state.
42
     */
43
44
    PLHashTable *plHashTable;
45
    PRUint32 count;
46
};
47
48
static PLHashNumber
49
nss_identity_hash(const void *key)
50
0
{
51
0
    return (PLHashNumber)((char *)key - (char *)NULL);
52
0
}
53
54
static PLHashNumber
55
nss_item_hash(const void *key)
56
380k
{
57
380k
    unsigned int i;
58
380k
    PLHashNumber h;
59
380k
    NSSItem *it = (NSSItem *)key;
60
380k
    h = 0;
61
10.0M
    for (i = 0; i < it->size; i++)
62
9.70M
        h = PR_ROTATE_LEFT32(h, 4) ^ ((unsigned char *)it->data)[i];
63
380k
    return h;
64
380k
}
65
66
static int
67
nss_compare_items(const void *v1, const void *v2)
68
190k
{
69
190k
    PRStatus ignore;
70
190k
    return (int)nssItem_Equal((NSSItem *)v1, (NSSItem *)v2, &ignore);
71
190k
}
72
73
/*
74
 * nssHash_create
75
 *
76
 */
77
NSS_IMPLEMENT nssHash *
78
nssHash_Create(NSSArena *arenaOpt, PRUint32 numBuckets, PLHashFunction keyHash,
79
               PLHashComparator keyCompare, PLHashComparator valueCompare)
80
96
{
81
96
    nssHash *rv;
82
96
    NSSArena *arena;
83
96
    PRBool i_alloced;
84
85
#ifdef NSSDEBUG
86
    if (arenaOpt && PR_SUCCESS != nssArena_verifyPointer(arenaOpt)) {
87
        nss_SetError(NSS_ERROR_INVALID_POINTER);
88
        return (nssHash *)NULL;
89
    }
90
#endif /* NSSDEBUG */
91
92
96
    if (arenaOpt) {
93
96
        arena = arenaOpt;
94
96
        i_alloced = PR_FALSE;
95
96
    } else {
96
0
        arena = nssArena_Create();
97
0
        i_alloced = PR_TRUE;
98
0
    }
99
100
96
    rv = nss_ZNEW(arena, nssHash);
101
96
    if ((nssHash *)NULL == rv) {
102
0
        goto loser;
103
0
    }
104
105
96
    rv->mutex = PR_NewLock();
106
96
    if ((PRLock *)NULL == rv->mutex) {
107
0
        goto loser;
108
0
    }
109
110
96
    rv->plHashTable =
111
96
        PL_NewHashTable(numBuckets, keyHash, keyCompare, valueCompare,
112
96
                        &nssArenaHashAllocOps, arena);
113
96
    if ((PLHashTable *)NULL == rv->plHashTable) {
114
0
        (void)PR_DestroyLock(rv->mutex);
115
0
        goto loser;
116
0
    }
117
118
96
    rv->count = 0;
119
96
    rv->arena = arena;
120
96
    rv->i_alloced_arena = i_alloced;
121
122
96
    return rv;
123
0
loser:
124
0
    (void)nss_ZFreeIf(rv);
125
0
    return (nssHash *)NULL;
126
96
}
127
128
/*
129
 * nssHash_CreatePointer
130
 *
131
 */
132
NSS_IMPLEMENT nssHash *
133
nssHash_CreatePointer(NSSArena *arenaOpt, PRUint32 numBuckets)
134
0
{
135
0
    return nssHash_Create(arenaOpt, numBuckets, nss_identity_hash,
136
0
                          PL_CompareValues, PL_CompareValues);
137
0
}
138
139
/*
140
 * nssHash_CreateString
141
 *
142
 */
143
NSS_IMPLEMENT nssHash *
144
nssHash_CreateString(NSSArena *arenaOpt, PRUint32 numBuckets)
145
32
{
146
32
    return nssHash_Create(arenaOpt, numBuckets, PL_HashString,
147
32
                          PL_CompareStrings, PL_CompareStrings);
148
32
}
149
150
/*
151
 * nssHash_CreateItem
152
 *
153
 */
154
NSS_IMPLEMENT nssHash *
155
nssHash_CreateItem(NSSArena *arenaOpt, PRUint32 numBuckets)
156
32
{
157
32
    return nssHash_Create(arenaOpt, numBuckets, nss_item_hash,
158
32
                          nss_compare_items, PL_CompareValues);
159
32
}
160
161
/*
162
 * nssHash_Destroy
163
 *
164
 */
165
NSS_IMPLEMENT void
166
nssHash_Destroy(nssHash *hash)
167
84
{
168
84
    (void)PR_DestroyLock(hash->mutex);
169
84
    PL_HashTableDestroy(hash->plHashTable);
170
84
    if (hash->i_alloced_arena) {
171
0
        nssArena_Destroy(hash->arena);
172
84
    } else {
173
84
        nss_ZFreeIf(hash);
174
84
    }
175
84
}
176
177
/*
178
 * nssHash_Add
179
 *
180
 */
181
NSS_IMPLEMENT PRStatus
182
nssHash_Add(nssHash *hash, const void *key, const void *value)
183
186k
{
184
186k
    PRStatus error = PR_FAILURE;
185
186k
    PLHashEntry *he;
186
187
186k
    PR_Lock(hash->mutex);
188
189
186k
    he = PL_HashTableAdd(hash->plHashTable, key, (void *)value);
190
186k
    if ((PLHashEntry *)NULL == he) {
191
0
        nss_SetError(NSS_ERROR_NO_MEMORY);
192
186k
    } else if (he->value != value) {
193
0
        nss_SetError(NSS_ERROR_HASH_COLLISION);
194
186k
    } else {
195
186k
        hash->count++;
196
186k
        error = PR_SUCCESS;
197
186k
    }
198
199
186k
    (void)PR_Unlock(hash->mutex);
200
201
186k
    return error;
202
186k
}
203
204
/*
205
 * nssHash_Remove
206
 *
207
 */
208
NSS_IMPLEMENT void
209
nssHash_Remove(nssHash *hash, const void *it)
210
186k
{
211
186k
    PRBool found;
212
213
186k
    PR_Lock(hash->mutex);
214
215
186k
    found = PL_HashTableRemove(hash->plHashTable, it);
216
186k
    if (found) {
217
186k
        hash->count--;
218
186k
    }
219
220
186k
    (void)PR_Unlock(hash->mutex);
221
186k
    return;
222
186k
}
223
224
/*
225
 * nssHash_Count
226
 *
227
 */
228
NSS_IMPLEMENT PRUint32
229
nssHash_Count(nssHash *hash)
230
28
{
231
28
    PRUint32 count;
232
233
28
    PR_Lock(hash->mutex);
234
235
28
    count = hash->count;
236
237
28
    (void)PR_Unlock(hash->mutex);
238
239
28
    return count;
240
28
}
241
242
/*
243
 * nssHash_Exists
244
 *
245
 */
246
NSS_IMPLEMENT PRBool
247
nssHash_Exists(nssHash *hash, const void *it)
248
0
{
249
0
    void *value;
250
251
0
    PR_Lock(hash->mutex);
252
253
0
    value = PL_HashTableLookup(hash->plHashTable, it);
254
255
0
    (void)PR_Unlock(hash->mutex);
256
257
0
    if ((void *)NULL == value) {
258
0
        return PR_FALSE;
259
0
    } else {
260
0
        return PR_TRUE;
261
0
    }
262
0
}
263
264
/*
265
 * nssHash_Lookup
266
 *
267
 */
268
NSS_IMPLEMENT void *
269
nssHash_Lookup(nssHash *hash, const void *it)
270
754k
{
271
754k
    void *rv;
272
273
754k
    PR_Lock(hash->mutex);
274
275
754k
    rv = PL_HashTableLookup(hash->plHashTable, it);
276
277
754k
    (void)PR_Unlock(hash->mutex);
278
279
754k
    return rv;
280
754k
}
281
282
struct arg_str {
283
    nssHashIterator fcn;
284
    void *closure;
285
};
286
287
static PRIntn
288
nss_hash_enumerator(PLHashEntry *he, PRIntn index, void *arg)
289
0
{
290
0
    struct arg_str *as = (struct arg_str *)arg;
291
0
    as->fcn(he->key, he->value, as->closure);
292
0
    return HT_ENUMERATE_NEXT;
293
0
}
294
295
/*
296
 * nssHash_Iterate
297
 *
298
 * NOTE that the iteration function will be called with the hashtable locked.
299
 */
300
NSS_IMPLEMENT void
301
nssHash_Iterate(nssHash *hash, nssHashIterator fcn, void *closure)
302
5
{
303
5
    struct arg_str as;
304
5
    as.fcn = fcn;
305
5
    as.closure = closure;
306
307
5
    PR_Lock(hash->mutex);
308
309
5
    PL_HashTableEnumerateEntries(hash->plHashTable, nss_hash_enumerator, &as);
310
311
5
    (void)PR_Unlock(hash->mutex);
312
313
5
    return;
314
5
}