Coverage Report

Created: 2026-06-07 07:11

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