Coverage Report

Created: 2024-05-20 06:23

/src/nspr/lib/ds/plhash.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
/*
7
 * PL hash table package.
8
 */
9
#include "plhash.h"
10
#include "prbit.h"
11
#include "prlog.h"
12
#include "prmem.h"
13
#include "prtypes.h"
14
#include <stdlib.h>
15
#include <string.h>
16
17
/* Compute the number of buckets in ht */
18
0
#define NBUCKETS(ht)    (1 << (PL_HASH_BITS - (ht)->shift))
19
20
/* The smallest table has 16 buckets */
21
0
#define MINBUCKETSLOG2  4
22
0
#define MINBUCKETS      (1 << MINBUCKETSLOG2)
23
24
/* Compute the maximum entries given n buckets that we will tolerate, ~90% */
25
0
#define OVERLOADED(n)   ((n) - ((n) >> 3))
26
27
/* Compute the number of entries below which we shrink the table by half */
28
0
#define UNDERLOADED(n)  (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
29
30
/*
31
** Stubs for default hash allocator ops.
32
*/
33
static void * PR_CALLBACK
34
DefaultAllocTable(void *pool, PRSize size)
35
0
{
36
0
    return PR_MALLOC(size);
37
0
}
38
39
static void PR_CALLBACK
40
DefaultFreeTable(void *pool, void *item)
41
0
{
42
0
    PR_Free(item);
43
0
}
44
45
static PLHashEntry * PR_CALLBACK
46
DefaultAllocEntry(void *pool, const void *key)
47
0
{
48
0
    return PR_NEW(PLHashEntry);
49
0
}
50
51
static void PR_CALLBACK
52
DefaultFreeEntry(void *pool, PLHashEntry *he, PRUintn flag)
53
0
{
54
0
    if (flag == HT_FREE_ENTRY) {
55
0
        PR_Free(he);
56
0
    }
57
0
}
58
59
static PLHashAllocOps defaultHashAllocOps = {
60
    DefaultAllocTable, DefaultFreeTable,
61
    DefaultAllocEntry, DefaultFreeEntry
62
};
63
64
PR_IMPLEMENT(PLHashTable *)
65
PL_NewHashTable(PRUint32 n, PLHashFunction keyHash,
66
                PLHashComparator keyCompare, PLHashComparator valueCompare,
67
                const PLHashAllocOps *allocOps, void *allocPriv)
68
0
{
69
0
    PLHashTable *ht;
70
0
    PRSize nb;
71
72
0
    if (n <= MINBUCKETS) {
73
0
        n = MINBUCKETSLOG2;
74
0
    } else {
75
0
        n = PR_CeilingLog2(n);
76
0
        if ((PRInt32)n < 0) {
77
0
            return 0;
78
0
        }
79
0
    }
80
81
0
    if (!allocOps) {
82
0
        allocOps = &defaultHashAllocOps;
83
0
    }
84
85
0
    ht = (PLHashTable*)((*allocOps->allocTable)(allocPriv, sizeof *ht));
86
0
    if (!ht) {
87
0
        return 0;
88
0
    }
89
0
    memset(ht, 0, sizeof *ht);
90
0
    ht->shift = PL_HASH_BITS - n;
91
0
    n = 1 << n;
92
0
    nb = n * sizeof(PLHashEntry *);
93
0
    ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb));
94
0
    if (!ht->buckets) {
95
0
        (*allocOps->freeTable)(allocPriv, ht);
96
0
        return 0;
97
0
    }
98
0
    memset(ht->buckets, 0, nb);
99
100
0
    ht->keyHash = keyHash;
101
0
    ht->keyCompare = keyCompare;
102
0
    ht->valueCompare = valueCompare;
103
0
    ht->allocOps = allocOps;
104
0
    ht->allocPriv = allocPriv;
105
0
    return ht;
106
0
}
107
108
PR_IMPLEMENT(void)
109
PL_HashTableDestroy(PLHashTable *ht)
110
0
{
111
0
    PRUint32 i, n;
112
0
    PLHashEntry *he, *next;
113
0
    const PLHashAllocOps *allocOps = ht->allocOps;
114
0
    void *allocPriv = ht->allocPriv;
115
116
0
    n = NBUCKETS(ht);
117
0
    for (i = 0; i < n; i++) {
118
0
        for (he = ht->buckets[i]; he; he = next) {
119
0
            next = he->next;
120
0
            (*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY);
121
0
        }
122
0
    }
123
0
#ifdef DEBUG
124
0
    memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
125
0
#endif
126
0
    (*allocOps->freeTable)(allocPriv, ht->buckets);
127
0
#ifdef DEBUG
128
0
    memset(ht, 0xDB, sizeof *ht);
129
0
#endif
130
0
    (*allocOps->freeTable)(allocPriv, ht);
131
0
}
132
133
/*
134
** Multiplicative hash, from Knuth 6.4.
135
*/
136
0
#define GOLDEN_RATIO    0x9E3779B9U  /* 2/(1+sqrt(5))*(2^32) */
137
138
PR_IMPLEMENT(PLHashEntry **)
139
PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key)
140
0
{
141
0
    PLHashEntry *he, **hep, **hep0;
142
0
    PLHashNumber h;
143
144
#ifdef HASHMETER
145
    ht->nlookups++;
146
#endif
147
0
    h = keyHash * GOLDEN_RATIO;
148
0
    h >>= ht->shift;
149
0
    hep = hep0 = &ht->buckets[h];
150
0
    while ((he = *hep) != 0) {
151
0
        if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
152
            /* Move to front of chain if not already there */
153
0
            if (hep != hep0) {
154
0
                *hep = he->next;
155
0
                he->next = *hep0;
156
0
                *hep0 = he;
157
0
            }
158
0
            return hep0;
159
0
        }
160
0
        hep = &he->next;
161
#ifdef HASHMETER
162
        ht->nsteps++;
163
#endif
164
0
    }
165
0
    return hep;
166
0
}
167
168
/*
169
** Same as PL_HashTableRawLookup but doesn't reorder the hash entries.
170
*/
171
PR_IMPLEMENT(PLHashEntry **)
172
PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
173
                           const void *key)
174
0
{
175
0
    PLHashEntry *he, **hep;
176
0
    PLHashNumber h;
177
178
#ifdef HASHMETER
179
    ht->nlookups++;
180
#endif
181
0
    h = keyHash * GOLDEN_RATIO;
182
0
    h >>= ht->shift;
183
0
    hep = &ht->buckets[h];
184
0
    while ((he = *hep) != 0) {
185
0
        if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
186
0
            break;
187
0
        }
188
0
        hep = &he->next;
189
#ifdef HASHMETER
190
        ht->nsteps++;
191
#endif
192
0
    }
193
0
    return hep;
194
0
}
195
196
PR_IMPLEMENT(PLHashEntry *)
197
PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep,
198
                   PLHashNumber keyHash, const void *key, void *value)
199
0
{
200
0
    PRUint32 i, n;
201
0
    PLHashEntry *he, *next, **oldbuckets;
202
0
    PRSize nb;
203
204
    /* Grow the table if it is overloaded */
205
0
    n = NBUCKETS(ht);
206
0
    if (ht->nentries >= OVERLOADED(n)) {
207
0
        oldbuckets = ht->buckets;
208
0
        nb = 2 * n * sizeof(PLHashEntry *);
209
0
        ht->buckets = (PLHashEntry**)
210
0
                      ((*ht->allocOps->allocTable)(ht->allocPriv, nb));
211
0
        if (!ht->buckets) {
212
0
            ht->buckets = oldbuckets;
213
0
            return 0;
214
0
        }
215
0
        memset(ht->buckets, 0, nb);
216
#ifdef HASHMETER
217
        ht->ngrows++;
218
#endif
219
0
        ht->shift--;
220
221
0
        for (i = 0; i < n; i++) {
222
0
            for (he = oldbuckets[i]; he; he = next) {
223
0
                next = he->next;
224
0
                hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
225
0
                PR_ASSERT(*hep == 0);
226
0
                he->next = 0;
227
0
                *hep = he;
228
0
            }
229
0
        }
230
0
#ifdef DEBUG
231
0
        memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
232
0
#endif
233
0
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
234
0
        hep = PL_HashTableRawLookup(ht, keyHash, key);
235
0
    }
236
237
    /* Make a new key value entry */
238
0
    he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
239
0
    if (!he) {
240
0
        return 0;
241
0
    }
242
0
    he->keyHash = keyHash;
243
0
    he->key = key;
244
0
    he->value = value;
245
0
    he->next = *hep;
246
0
    *hep = he;
247
0
    ht->nentries++;
248
0
    return he;
249
0
}
250
251
PR_IMPLEMENT(PLHashEntry *)
252
PL_HashTableAdd(PLHashTable *ht, const void *key, void *value)
253
0
{
254
0
    PLHashNumber keyHash;
255
0
    PLHashEntry *he, **hep;
256
257
0
    keyHash = (*ht->keyHash)(key);
258
0
    hep = PL_HashTableRawLookup(ht, keyHash, key);
259
0
    if ((he = *hep) != 0) {
260
        /* Hit; see if values match */
261
0
        if ((*ht->valueCompare)(he->value, value)) {
262
            /* key,value pair is already present in table */
263
0
            return he;
264
0
        }
265
0
        if (he->value) {
266
0
            (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE);
267
0
        }
268
0
        he->value = value;
269
0
        return he;
270
0
    }
271
0
    return PL_HashTableRawAdd(ht, hep, keyHash, key, value);
272
0
}
273
274
PR_IMPLEMENT(void)
275
PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he)
276
0
{
277
0
    PRUint32 i, n;
278
0
    PLHashEntry *next, **oldbuckets;
279
0
    PRSize nb;
280
281
0
    *hep = he->next;
282
0
    (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY);
283
284
    /* Shrink table if it's underloaded */
285
0
    n = NBUCKETS(ht);
286
0
    if (--ht->nentries < UNDERLOADED(n)) {
287
0
        oldbuckets = ht->buckets;
288
0
        nb = n * sizeof(PLHashEntry*) / 2;
289
0
        ht->buckets = (PLHashEntry**)(
290
0
                          (*ht->allocOps->allocTable)(ht->allocPriv, nb));
291
0
        if (!ht->buckets) {
292
0
            ht->buckets = oldbuckets;
293
0
            return;
294
0
        }
295
0
        memset(ht->buckets, 0, nb);
296
#ifdef HASHMETER
297
        ht->nshrinks++;
298
#endif
299
0
        ht->shift++;
300
301
0
        for (i = 0; i < n; i++) {
302
0
            for (he = oldbuckets[i]; he; he = next) {
303
0
                next = he->next;
304
0
                hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
305
0
                PR_ASSERT(*hep == 0);
306
0
                he->next = 0;
307
0
                *hep = he;
308
0
            }
309
0
        }
310
0
#ifdef DEBUG
311
0
        memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
312
0
#endif
313
0
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
314
0
    }
315
0
}
316
317
PR_IMPLEMENT(PRBool)
318
PL_HashTableRemove(PLHashTable *ht, const void *key)
319
0
{
320
0
    PLHashNumber keyHash;
321
0
    PLHashEntry *he, **hep;
322
323
0
    keyHash = (*ht->keyHash)(key);
324
0
    hep = PL_HashTableRawLookup(ht, keyHash, key);
325
0
    if ((he = *hep) == 0) {
326
0
        return PR_FALSE;
327
0
    }
328
329
    /* Hit; remove element */
330
0
    PL_HashTableRawRemove(ht, hep, he);
331
0
    return PR_TRUE;
332
0
}
333
334
PR_IMPLEMENT(void *)
335
PL_HashTableLookup(PLHashTable *ht, const void *key)
336
0
{
337
0
    PLHashNumber keyHash;
338
0
    PLHashEntry *he, **hep;
339
340
0
    keyHash = (*ht->keyHash)(key);
341
0
    hep = PL_HashTableRawLookup(ht, keyHash, key);
342
0
    if ((he = *hep) != 0) {
343
0
        return he->value;
344
0
    }
345
0
    return 0;
346
0
}
347
348
/*
349
** Same as PL_HashTableLookup but doesn't reorder the hash entries.
350
*/
351
PR_IMPLEMENT(void *)
352
PL_HashTableLookupConst(PLHashTable *ht, const void *key)
353
0
{
354
0
    PLHashNumber keyHash;
355
0
    PLHashEntry *he, **hep;
356
357
0
    keyHash = (*ht->keyHash)(key);
358
0
    hep = PL_HashTableRawLookupConst(ht, keyHash, key);
359
0
    if ((he = *hep) != 0) {
360
0
        return he->value;
361
0
    }
362
0
    return 0;
363
0
}
364
365
/*
366
** Iterate over the entries in the hash table calling func for each
367
** entry found. Stop if "f" says to (return value & PR_ENUMERATE_STOP).
368
** Return a count of the number of elements scanned.
369
*/
370
PR_IMPLEMENT(int)
371
PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg)
372
0
{
373
0
    PLHashEntry *he, **hep;
374
0
    PRUint32 i, nbuckets;
375
0
    int rv, n = 0;
376
0
    PLHashEntry *todo = 0;
377
378
0
    nbuckets = NBUCKETS(ht);
379
0
    for (i = 0; i < nbuckets; i++) {
380
0
        hep = &ht->buckets[i];
381
0
        while ((he = *hep) != 0) {
382
0
            rv = (*f)(he, n, arg);
383
0
            n++;
384
0
            if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) {
385
0
                *hep = he->next;
386
0
                if (rv & HT_ENUMERATE_REMOVE) {
387
0
                    he->next = todo;
388
0
                    todo = he;
389
0
                }
390
0
            } else {
391
0
                hep = &he->next;
392
0
            }
393
0
            if (rv & HT_ENUMERATE_STOP) {
394
0
                goto out;
395
0
            }
396
0
        }
397
0
    }
398
399
0
out:
400
0
    hep = &todo;
401
0
    while ((he = *hep) != 0) {
402
0
        PL_HashTableRawRemove(ht, hep, he);
403
0
    }
404
0
    return n;
405
0
}
406
407
#ifdef HASHMETER
408
#include <math.h>
409
#include <stdio.h>
410
411
PR_IMPLEMENT(void)
412
PL_HashTableDumpMeter(PLHashTable *ht, PLHashEnumerator dump, FILE *fp)
413
{
414
    double mean, variance;
415
    PRUint32 nchains, nbuckets;
416
    PRUint32 i, n, maxChain, maxChainLen;
417
    PLHashEntry *he;
418
419
    variance = 0;
420
    nchains = 0;
421
    maxChainLen = 0;
422
    nbuckets = NBUCKETS(ht);
423
    for (i = 0; i < nbuckets; i++) {
424
        he = ht->buckets[i];
425
        if (!he) {
426
            continue;
427
        }
428
        nchains++;
429
        for (n = 0; he; he = he->next) {
430
            n++;
431
        }
432
        variance += n * n;
433
        if (n > maxChainLen) {
434
            maxChainLen = n;
435
            maxChain = i;
436
        }
437
    }
438
    mean = (double)ht->nentries / nchains;
439
    variance = fabs(variance / nchains - mean * mean);
440
441
    fprintf(fp, "\nHash table statistics:\n");
442
    fprintf(fp, "     number of lookups: %u\n", ht->nlookups);
443
    fprintf(fp, "     number of entries: %u\n", ht->nentries);
444
    fprintf(fp, "       number of grows: %u\n", ht->ngrows);
445
    fprintf(fp, "     number of shrinks: %u\n", ht->nshrinks);
446
    fprintf(fp, "   mean steps per hash: %g\n", (double)ht->nsteps
447
            / ht->nlookups);
448
    fprintf(fp, "mean hash chain length: %g\n", mean);
449
    fprintf(fp, "    standard deviation: %g\n", sqrt(variance));
450
    fprintf(fp, " max hash chain length: %u\n", maxChainLen);
451
    fprintf(fp, "        max hash chain: [%u]\n", maxChain);
452
453
    for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++)
454
        if ((*dump)(he, i, fp) != HT_ENUMERATE_NEXT) {
455
            break;
456
        }
457
}
458
#endif /* HASHMETER */
459
460
PR_IMPLEMENT(int)
461
PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp)
462
0
{
463
0
    int count;
464
465
0
    count = PL_HashTableEnumerateEntries(ht, dump, fp);
466
#ifdef HASHMETER
467
    PL_HashTableDumpMeter(ht, dump, fp);
468
#endif
469
0
    return count;
470
0
}
471
472
PR_IMPLEMENT(PLHashNumber)
473
PL_HashString(const void *key)
474
0
{
475
0
    PLHashNumber h;
476
0
    const PRUint8 *s;
477
478
0
    h = 0;
479
0
    for (s = (const PRUint8*)key; *s; s++) {
480
0
        h = PR_ROTATE_LEFT32(h, 4) ^ *s;
481
0
    }
482
0
    return h;
483
0
}
484
485
PR_IMPLEMENT(int)
486
PL_CompareStrings(const void *v1, const void *v2)
487
0
{
488
0
    return strcmp((const char*)v1, (const char*)v2) == 0;
489
0
}
490
491
PR_IMPLEMENT(int)
492
PL_CompareValues(const void *v1, const void *v2)
493
0
{
494
0
    return v1 == v2;
495
0
}