Coverage Report

Created: 2026-11-15 06:55

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
3.44M
#define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift))
18
19
/* The smallest table has 16 buckets */
20
1.71M
#define MINBUCKETSLOG2 4
21
1.71M
#define MINBUCKETS (1 << MINBUCKETSLOG2)
22
23
/* Compute the maximum entries given n buckets that we will tolerate, ~90% */
24
1.72M
#define OVERLOADED(n) ((n) - ((n) >> 3))
25
26
/* Compute the number of entries below which we shrink the table by half */
27
1.71M
#define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
28
29
/*
30
** Stubs for default hash allocator ops.
31
*/
32
700
static void* PR_CALLBACK DefaultAllocTable(void* pool, PRSize size) {
33
700
  return PR_MALLOC(size);
34
700
}
35
36
664
static void PR_CALLBACK DefaultFreeTable(void* pool, void* item) {
37
664
  PR_Free(item);
38
664
}
39
40
1.52M
static PLHashEntry* PR_CALLBACK DefaultAllocEntry(void* pool, const void* key) {
41
1.52M
  return PR_NEW(PLHashEntry);
42
1.52M
}
43
44
static void PR_CALLBACK DefaultFreeEntry(void* pool, PLHashEntry* he,
45
1.53M
                                         PRUintn flag) {
46
1.53M
  if (flag == HT_FREE_ENTRY) {
47
1.52M
    PR_Free(he);
48
1.52M
  }
49
1.53M
}
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
306
                void* allocPriv) {
58
306
  PLHashTable* ht;
59
306
  PRSize nb;
60
61
306
  if (n <= MINBUCKETS) {
62
180
    n = MINBUCKETSLOG2;
63
180
  } else {
64
126
    n = PR_CeilingLog2(n);
65
126
    if ((PRInt32)n < 0) {
66
0
      return 0;
67
0
    }
68
126
  }
69
70
306
  if (!allocOps) {
71
162
    allocOps = &defaultHashAllocOps;
72
162
  }
73
74
306
  ht = (PLHashTable*)((*allocOps->allocTable)(allocPriv, sizeof *ht));
75
306
  if (!ht) {
76
0
    return 0;
77
0
  }
78
306
  memset(ht, 0, sizeof *ht);
79
306
  ht->shift = PL_HASH_BITS - n;
80
306
  n = 1 << n;
81
306
  nb = n * sizeof(PLHashEntry*);
82
306
  ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb));
83
306
  if (!ht->buckets) {
84
0
    (*allocOps->freeTable)(allocPriv, ht);
85
0
    return 0;
86
0
  }
87
306
  memset(ht->buckets, 0, nb);
88
89
306
  ht->keyHash = keyHash;
90
306
  ht->keyCompare = keyCompare;
91
306
  ht->valueCompare = valueCompare;
92
306
  ht->allocOps = allocOps;
93
306
  ht->allocPriv = allocPriv;
94
306
  return ht;
95
306
}
96
97
PR_IMPLEMENT(void)
98
272
PL_HashTableDestroy(PLHashTable* ht) {
99
272
  PRUint32 i, n;
100
272
  PLHashEntry *he, *next;
101
272
  const PLHashAllocOps* allocOps = ht->allocOps;
102
272
  void* allocPriv = ht->allocPriv;
103
104
272
  n = NBUCKETS(ht);
105
17.0k
  for (i = 0; i < n; i++) {
106
23.8k
    for (he = ht->buckets[i]; he; he = next) {
107
7.07k
      next = he->next;
108
7.07k
      (*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY);
109
7.07k
    }
110
16.7k
  }
111
272
#ifdef DEBUG
112
272
  memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
113
272
#endif
114
272
  (*allocOps->freeTable)(allocPriv, ht->buckets);
115
272
#ifdef DEBUG
116
272
  memset(ht, 0xDB, sizeof *ht);
117
272
#endif
118
272
  (*allocOps->freeTable)(allocPriv, ht);
119
272
}
120
121
/*
122
** Multiplicative hash, from Knuth 6.4.
123
*/
124
65.1M
#define GOLDEN_RATIO 0x9E3779B9U /* 2/(1+sqrt(5))*(2^32) */
125
126
PR_IMPLEMENT(PLHashEntry**)
127
10.2M
PL_HashTableRawLookup(PLHashTable* ht, PLHashNumber keyHash, const void* key) {
128
10.2M
  PLHashEntry *he, **hep, **hep0;
129
10.2M
  PLHashNumber h;
130
131
#ifdef HASHMETER
132
  ht->nlookups++;
133
#endif
134
10.2M
  h = keyHash * GOLDEN_RATIO;
135
10.2M
  h >>= ht->shift;
136
10.2M
  hep = hep0 = &ht->buckets[h];
137
12.2M
  while ((he = *hep) != 0) {
138
6.01M
    if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
139
      /* Move to front of chain if not already there */
140
4.06M
      if (hep != hep0) {
141
543k
        *hep = he->next;
142
543k
        he->next = *hep0;
143
543k
        *hep0 = he;
144
543k
      }
145
4.06M
      return hep0;
146
4.06M
    }
147
1.95M
    hep = &he->next;
148
#ifdef HASHMETER
149
    ht->nsteps++;
150
#endif
151
1.95M
  }
152
6.19M
  return hep;
153
10.2M
}
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
54.8M
                           const void* key) {
161
54.8M
  PLHashEntry *he, **hep;
162
54.8M
  PLHashNumber h;
163
164
#ifdef HASHMETER
165
  ht->nlookups++;
166
#endif
167
54.8M
  h = keyHash * GOLDEN_RATIO;
168
54.8M
  h >>= ht->shift;
169
54.8M
  hep = &ht->buckets[h];
170
58.3M
  while ((he = *hep) != 0) {
171
56.6M
    if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
172
53.2M
      break;
173
53.2M
    }
174
3.44M
    hep = &he->next;
175
#ifdef HASHMETER
176
    ht->nsteps++;
177
#endif
178
3.44M
  }
179
54.8M
  return hep;
180
54.8M
}
181
182
PR_IMPLEMENT(PLHashEntry*)
183
PL_HashTableRawAdd(PLHashTable* ht, PLHashEntry** hep, PLHashNumber keyHash,
184
1.72M
                   const void* key, void* value) {
185
1.72M
  PRUint32 i, n;
186
1.72M
  PLHashEntry *he, *next, **oldbuckets;
187
1.72M
  PRSize nb;
188
189
  /* Grow the table if it is overloaded */
190
1.72M
  n = NBUCKETS(ht);
191
1.72M
  if (ht->nentries >= OVERLOADED(n)) {
192
364
    oldbuckets = ht->buckets;
193
364
    nb = 2 * n * sizeof(PLHashEntry*);
194
364
    ht->buckets =
195
364
        (PLHashEntry**)((*ht->allocOps->allocTable)(ht->allocPriv, nb));
196
364
    if (!ht->buckets) {
197
0
      ht->buckets = oldbuckets;
198
0
      return 0;
199
0
    }
200
364
    memset(ht->buckets, 0, nb);
201
#ifdef HASHMETER
202
    ht->ngrows++;
203
#endif
204
364
    ht->shift--;
205
206
16.4k
    for (i = 0; i < n; i++) {
207
30.1k
      for (he = oldbuckets[i]; he; he = next) {
208
14.0k
        next = he->next;
209
14.0k
        hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
210
14.0k
        PR_ASSERT(*hep == 0);
211
14.0k
        he->next = 0;
212
14.0k
        *hep = he;
213
14.0k
      }
214
16.0k
    }
215
364
#ifdef DEBUG
216
364
    memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
217
364
#endif
218
364
    (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
219
364
    hep = PL_HashTableRawLookup(ht, keyHash, key);
220
364
  }
221
222
  /* Make a new key value entry */
223
1.72M
  he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
224
1.72M
  if (!he) {
225
0
    return 0;
226
0
  }
227
1.72M
  he->keyHash = keyHash;
228
1.72M
  he->key = key;
229
1.72M
  he->value = value;
230
1.72M
  he->next = *hep;
231
1.72M
  *hep = he;
232
1.72M
  ht->nentries++;
233
1.72M
  return he;
234
1.72M
}
235
236
PR_IMPLEMENT(PLHashEntry*)
237
1.72M
PL_HashTableAdd(PLHashTable* ht, const void* key, void* value) {
238
1.72M
  PLHashNumber keyHash;
239
1.72M
  PLHashEntry *he, **hep;
240
241
1.72M
  keyHash = (*ht->keyHash)(key);
242
1.72M
  hep = PL_HashTableRawLookup(ht, keyHash, key);
243
1.72M
  if ((he = *hep) != 0) {
244
    /* Hit; see if values match */
245
1.38k
    if ((*ht->valueCompare)(he->value, value)) {
246
      /* key,value pair is already present in table */
247
0
      return he;
248
0
    }
249
1.38k
    if (he->value) {
250
1.38k
      (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE);
251
1.38k
    }
252
1.38k
    he->value = value;
253
1.38k
    return he;
254
1.38k
  }
255
1.72M
  return PL_HashTableRawAdd(ht, hep, keyHash, key, value);
256
1.72M
}
257
258
PR_IMPLEMENT(void)
259
1.71M
PL_HashTableRawRemove(PLHashTable* ht, PLHashEntry** hep, PLHashEntry* he) {
260
1.71M
  PRUint32 i, n;
261
1.71M
  PLHashEntry *next, **oldbuckets;
262
1.71M
  PRSize nb;
263
264
1.71M
  *hep = he->next;
265
1.71M
  (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY);
266
267
  /* Shrink table if it's underloaded */
268
1.71M
  n = NBUCKETS(ht);
269
1.71M
  if (--ht->nentries < UNDERLOADED(n)) {
270
251
    oldbuckets = ht->buckets;
271
251
    nb = n * sizeof(PLHashEntry*) / 2;
272
251
    ht->buckets =
273
251
        (PLHashEntry**)((*ht->allocOps->allocTable)(ht->allocPriv, nb));
274
251
    if (!ht->buckets) {
275
0
      ht->buckets = oldbuckets;
276
0
      return;
277
0
    }
278
251
    memset(ht->buckets, 0, nb);
279
#ifdef HASHMETER
280
    ht->nshrinks++;
281
#endif
282
251
    ht->shift++;
283
284
11.8k
    for (i = 0; i < n; i++) {
285
13.9k
      for (he = oldbuckets[i]; he; he = next) {
286
2.28k
        next = he->next;
287
2.28k
        hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
288
2.28k
        PR_ASSERT(*hep == 0);
289
2.28k
        he->next = 0;
290
2.28k
        *hep = he;
291
2.28k
      }
292
11.6k
    }
293
251
#ifdef DEBUG
294
251
    memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
295
251
#endif
296
251
    (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
297
251
  }
298
1.71M
}
299
300
PR_IMPLEMENT(PRBool)
301
1.71M
PL_HashTableRemove(PLHashTable* ht, const void* key) {
302
1.71M
  PLHashNumber keyHash;
303
1.71M
  PLHashEntry *he, **hep;
304
305
1.71M
  keyHash = (*ht->keyHash)(key);
306
1.71M
  hep = PL_HashTableRawLookup(ht, keyHash, key);
307
1.71M
  if ((he = *hep) == 0) {
308
0
    return PR_FALSE;
309
0
  }
310
311
  /* Hit; remove element */
312
1.71M
  PL_HashTableRawRemove(ht, hep, he);
313
1.71M
  return PR_TRUE;
314
1.71M
}
315
316
PR_IMPLEMENT(void*)
317
6.79M
PL_HashTableLookup(PLHashTable* ht, const void* key) {
318
6.79M
  PLHashNumber keyHash;
319
6.79M
  PLHashEntry *he, **hep;
320
321
6.79M
  keyHash = (*ht->keyHash)(key);
322
6.79M
  hep = PL_HashTableRawLookup(ht, keyHash, key);
323
6.79M
  if ((he = *hep) != 0) {
324
2.34M
    return he->value;
325
2.34M
  }
326
4.44M
  return 0;
327
6.79M
}
328
329
/*
330
** Same as PL_HashTableLookup but doesn't reorder the hash entries.
331
*/
332
PR_IMPLEMENT(void*)
333
54.8M
PL_HashTableLookupConst(PLHashTable* ht, const void* key) {
334
54.8M
  PLHashNumber keyHash;
335
54.8M
  PLHashEntry *he, **hep;
336
337
54.8M
  keyHash = (*ht->keyHash)(key);
338
54.8M
  hep = PL_HashTableRawLookupConst(ht, keyHash, key);
339
54.8M
  if ((he = *hep) != 0) {
340
53.2M
    return he->value;
341
53.2M
  }
342
1.62M
  return 0;
343
54.8M
}
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
177
PL_HashTableEnumerateEntries(PLHashTable* ht, PLHashEnumerator f, void* arg) {
352
177
  PLHashEntry *he, **hep;
353
177
  PRUint32 i, nbuckets;
354
177
  int rv, n = 0;
355
177
  PLHashEntry* todo = 0;
356
357
177
  nbuckets = NBUCKETS(ht);
358
4.65k
  for (i = 0; i < nbuckets; i++) {
359
4.48k
    hep = &ht->buckets[i];
360
4.63k
    while ((he = *hep) != 0) {
361
150
      rv = (*f)(he, n, arg);
362
150
      n++;
363
150
      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
150
      } else {
370
150
        hep = &he->next;
371
150
      }
372
150
      if (rv & HT_ENUMERATE_STOP) {
373
0
        goto out;
374
0
      }
375
150
    }
376
4.48k
  }
377
378
177
out:
379
177
  hep = &todo;
380
177
  while ((he = *hep) != 0) {
381
0
    PL_HashTableRawRemove(ht, hep, he);
382
0
  }
383
177
  return n;
384
177
}
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
9.20k
PL_HashString(const void* key) {
451
9.20k
  PLHashNumber h;
452
9.20k
  const PRUint8* s;
453
454
9.20k
  h = 0;
455
144k
  for (s = (const PRUint8*)key; *s; s++) {
456
135k
    h = PR_ROTATE_LEFT32(h, 4) ^ *s;
457
135k
  }
458
9.20k
  return h;
459
9.20k
}
460
461
PR_IMPLEMENT(int)
462
3.03k
PL_CompareStrings(const void* v1, const void* v2) {
463
3.03k
  return strcmp((const char*)v1, (const char*)v2) == 0;
464
3.03k
}
465
466
PR_IMPLEMENT(int)
467
52.1M
PL_CompareValues(const void* v1, const void* v2) { return v1 == v2; }