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 #define NBUCKETS(ht)    (1 << (PL_HASH_BITS - (ht)->shift))
19 
20 /* The smallest table has 16 buckets */
21 #define MINBUCKETSLOG2  4
22 #define MINBUCKETS      (1 << MINBUCKETSLOG2)
23 
24 /* Compute the maximum entries given n buckets that we will tolerate, ~90% */
25 #define OVERLOADED(n)   ((n) - ((n) >> 3))
26 
27 /* Compute the number of entries below which we shrink the table by half */
28 #define UNDERLOADED(n)  (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
29 
30 /*
31 ** Stubs for default hash allocator ops.
32 */
33 static void * PR_CALLBACK
DefaultAllocTable(void * pool,PRSize size)34 DefaultAllocTable(void *pool, PRSize size)
35 {
36     return PR_MALLOC(size);
37 }
38 
39 static void PR_CALLBACK
DefaultFreeTable(void * pool,void * item)40 DefaultFreeTable(void *pool, void *item)
41 {
42     PR_Free(item);
43 }
44 
45 static PLHashEntry * PR_CALLBACK
DefaultAllocEntry(void * pool,const void * key)46 DefaultAllocEntry(void *pool, const void *key)
47 {
48     return PR_NEW(PLHashEntry);
49 }
50 
51 static void PR_CALLBACK
DefaultFreeEntry(void * pool,PLHashEntry * he,PRUintn flag)52 DefaultFreeEntry(void *pool, PLHashEntry *he, PRUintn flag)
53 {
54     if (flag == HT_FREE_ENTRY) {
55         PR_Free(he);
56     }
57 }
58 
59 static PLHashAllocOps defaultHashAllocOps = {
60     DefaultAllocTable, DefaultFreeTable,
61     DefaultAllocEntry, DefaultFreeEntry
62 };
63 
64 PR_IMPLEMENT(PLHashTable *)
PL_NewHashTable(PRUint32 n,PLHashFunction keyHash,PLHashComparator keyCompare,PLHashComparator valueCompare,const PLHashAllocOps * allocOps,void * allocPriv)65 PL_NewHashTable(PRUint32 n, PLHashFunction keyHash,
66                 PLHashComparator keyCompare, PLHashComparator valueCompare,
67                 const PLHashAllocOps *allocOps, void *allocPriv)
68 {
69     PLHashTable *ht;
70     PRSize nb;
71 
72     if (n <= MINBUCKETS) {
73         n = MINBUCKETSLOG2;
74     } else {
75         n = PR_CeilingLog2(n);
76         if ((PRInt32)n < 0) {
77             return 0;
78         }
79     }
80 
81     if (!allocOps) {
82         allocOps = &defaultHashAllocOps;
83     }
84 
85     ht = (PLHashTable*)((*allocOps->allocTable)(allocPriv, sizeof *ht));
86     if (!ht) {
87         return 0;
88     }
89     memset(ht, 0, sizeof *ht);
90     ht->shift = PL_HASH_BITS - n;
91     n = 1 << n;
92     nb = n * sizeof(PLHashEntry *);
93     ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb));
94     if (!ht->buckets) {
95         (*allocOps->freeTable)(allocPriv, ht);
96         return 0;
97     }
98     memset(ht->buckets, 0, nb);
99 
100     ht->keyHash = keyHash;
101     ht->keyCompare = keyCompare;
102     ht->valueCompare = valueCompare;
103     ht->allocOps = allocOps;
104     ht->allocPriv = allocPriv;
105     return ht;
106 }
107 
108 PR_IMPLEMENT(void)
PL_HashTableDestroy(PLHashTable * ht)109 PL_HashTableDestroy(PLHashTable *ht)
110 {
111     PRUint32 i, n;
112     PLHashEntry *he, *next;
113     const PLHashAllocOps *allocOps = ht->allocOps;
114     void *allocPriv = ht->allocPriv;
115 
116     n = NBUCKETS(ht);
117     for (i = 0; i < n; i++) {
118         for (he = ht->buckets[i]; he; he = next) {
119             next = he->next;
120             (*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY);
121         }
122     }
123 #ifdef DEBUG
124     memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
125 #endif
126     (*allocOps->freeTable)(allocPriv, ht->buckets);
127 #ifdef DEBUG
128     memset(ht, 0xDB, sizeof *ht);
129 #endif
130     (*allocOps->freeTable)(allocPriv, ht);
131 }
132 
133 /*
134 ** Multiplicative hash, from Knuth 6.4.
135 */
136 #define GOLDEN_RATIO    0x9E3779B9U  /* 2/(1+sqrt(5))*(2^32) */
137 
138 PR_IMPLEMENT(PLHashEntry **)
PL_HashTableRawLookup(PLHashTable * ht,PLHashNumber keyHash,const void * key)139 PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key)
140 {
141     PLHashEntry *he, **hep, **hep0;
142     PLHashNumber h;
143 
144 #ifdef HASHMETER
145     ht->nlookups++;
146 #endif
147     h = keyHash * GOLDEN_RATIO;
148     h >>= ht->shift;
149     hep = hep0 = &ht->buckets[h];
150     while ((he = *hep) != 0) {
151         if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
152             /* Move to front of chain if not already there */
153             if (hep != hep0) {
154                 *hep = he->next;
155                 he->next = *hep0;
156                 *hep0 = he;
157             }
158             return hep0;
159         }
160         hep = &he->next;
161 #ifdef HASHMETER
162         ht->nsteps++;
163 #endif
164     }
165     return hep;
166 }
167 
168 /*
169 ** Same as PL_HashTableRawLookup but doesn't reorder the hash entries.
170 */
171 PR_IMPLEMENT(PLHashEntry **)
PL_HashTableRawLookupConst(PLHashTable * ht,PLHashNumber keyHash,const void * key)172 PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
173                            const void *key)
174 {
175     PLHashEntry *he, **hep;
176     PLHashNumber h;
177 
178 #ifdef HASHMETER
179     ht->nlookups++;
180 #endif
181     h = keyHash * GOLDEN_RATIO;
182     h >>= ht->shift;
183     hep = &ht->buckets[h];
184     while ((he = *hep) != 0) {
185         if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
186             break;
187         }
188         hep = &he->next;
189 #ifdef HASHMETER
190         ht->nsteps++;
191 #endif
192     }
193     return hep;
194 }
195 
196 PR_IMPLEMENT(PLHashEntry *)
PL_HashTableRawAdd(PLHashTable * ht,PLHashEntry ** hep,PLHashNumber keyHash,const void * key,void * value)197 PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep,
198                    PLHashNumber keyHash, const void *key, void *value)
199 {
200     PRUint32 i, n;
201     PLHashEntry *he, *next, **oldbuckets;
202     PRSize nb;
203 
204     /* Grow the table if it is overloaded */
205     n = NBUCKETS(ht);
206     if (ht->nentries >= OVERLOADED(n)) {
207         oldbuckets = ht->buckets;
208         nb = 2 * n * sizeof(PLHashEntry *);
209         ht->buckets = (PLHashEntry**)
210                       ((*ht->allocOps->allocTable)(ht->allocPriv, nb));
211         if (!ht->buckets) {
212             ht->buckets = oldbuckets;
213             return 0;
214         }
215         memset(ht->buckets, 0, nb);
216 #ifdef HASHMETER
217         ht->ngrows++;
218 #endif
219         ht->shift--;
220 
221         for (i = 0; i < n; i++) {
222             for (he = oldbuckets[i]; he; he = next) {
223                 next = he->next;
224                 hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
225                 PR_ASSERT(*hep == 0);
226                 he->next = 0;
227                 *hep = he;
228             }
229         }
230 #ifdef DEBUG
231         memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
232 #endif
233         (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
234         hep = PL_HashTableRawLookup(ht, keyHash, key);
235     }
236 
237     /* Make a new key value entry */
238     he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
239     if (!he) {
240         return 0;
241     }
242     he->keyHash = keyHash;
243     he->key = key;
244     he->value = value;
245     he->next = *hep;
246     *hep = he;
247     ht->nentries++;
248     return he;
249 }
250 
251 PR_IMPLEMENT(PLHashEntry *)
PL_HashTableAdd(PLHashTable * ht,const void * key,void * value)252 PL_HashTableAdd(PLHashTable *ht, const void *key, void *value)
253 {
254     PLHashNumber keyHash;
255     PLHashEntry *he, **hep;
256 
257     keyHash = (*ht->keyHash)(key);
258     hep = PL_HashTableRawLookup(ht, keyHash, key);
259     if ((he = *hep) != 0) {
260         /* Hit; see if values match */
261         if ((*ht->valueCompare)(he->value, value)) {
262             /* key,value pair is already present in table */
263             return he;
264         }
265         if (he->value) {
266             (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE);
267         }
268         he->value = value;
269         return he;
270     }
271     return PL_HashTableRawAdd(ht, hep, keyHash, key, value);
272 }
273 
274 PR_IMPLEMENT(void)
PL_HashTableRawRemove(PLHashTable * ht,PLHashEntry ** hep,PLHashEntry * he)275 PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he)
276 {
277     PRUint32 i, n;
278     PLHashEntry *next, **oldbuckets;
279     PRSize nb;
280 
281     *hep = he->next;
282     (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY);
283 
284     /* Shrink table if it's underloaded */
285     n = NBUCKETS(ht);
286     if (--ht->nentries < UNDERLOADED(n)) {
287         oldbuckets = ht->buckets;
288         nb = n * sizeof(PLHashEntry*) / 2;
289         ht->buckets = (PLHashEntry**)(
290                           (*ht->allocOps->allocTable)(ht->allocPriv, nb));
291         if (!ht->buckets) {
292             ht->buckets = oldbuckets;
293             return;
294         }
295         memset(ht->buckets, 0, nb);
296 #ifdef HASHMETER
297         ht->nshrinks++;
298 #endif
299         ht->shift++;
300 
301         for (i = 0; i < n; i++) {
302             for (he = oldbuckets[i]; he; he = next) {
303                 next = he->next;
304                 hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
305                 PR_ASSERT(*hep == 0);
306                 he->next = 0;
307                 *hep = he;
308             }
309         }
310 #ifdef DEBUG
311         memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
312 #endif
313         (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
314     }
315 }
316 
317 PR_IMPLEMENT(PRBool)
PL_HashTableRemove(PLHashTable * ht,const void * key)318 PL_HashTableRemove(PLHashTable *ht, const void *key)
319 {
320     PLHashNumber keyHash;
321     PLHashEntry *he, **hep;
322 
323     keyHash = (*ht->keyHash)(key);
324     hep = PL_HashTableRawLookup(ht, keyHash, key);
325     if ((he = *hep) == 0) {
326         return PR_FALSE;
327     }
328 
329     /* Hit; remove element */
330     PL_HashTableRawRemove(ht, hep, he);
331     return PR_TRUE;
332 }
333 
334 PR_IMPLEMENT(void *)
PL_HashTableLookup(PLHashTable * ht,const void * key)335 PL_HashTableLookup(PLHashTable *ht, const void *key)
336 {
337     PLHashNumber keyHash;
338     PLHashEntry *he, **hep;
339 
340     keyHash = (*ht->keyHash)(key);
341     hep = PL_HashTableRawLookup(ht, keyHash, key);
342     if ((he = *hep) != 0) {
343         return he->value;
344     }
345     return 0;
346 }
347 
348 /*
349 ** Same as PL_HashTableLookup but doesn't reorder the hash entries.
350 */
351 PR_IMPLEMENT(void *)
PL_HashTableLookupConst(PLHashTable * ht,const void * key)352 PL_HashTableLookupConst(PLHashTable *ht, const void *key)
353 {
354     PLHashNumber keyHash;
355     PLHashEntry *he, **hep;
356 
357     keyHash = (*ht->keyHash)(key);
358     hep = PL_HashTableRawLookupConst(ht, keyHash, key);
359     if ((he = *hep) != 0) {
360         return he->value;
361     }
362     return 0;
363 }
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)
PL_HashTableEnumerateEntries(PLHashTable * ht,PLHashEnumerator f,void * arg)371 PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg)
372 {
373     PLHashEntry *he, **hep;
374     PRUint32 i, nbuckets;
375     int rv, n = 0;
376     PLHashEntry *todo = 0;
377 
378     nbuckets = NBUCKETS(ht);
379     for (i = 0; i < nbuckets; i++) {
380         hep = &ht->buckets[i];
381         while ((he = *hep) != 0) {
382             rv = (*f)(he, n, arg);
383             n++;
384             if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) {
385                 *hep = he->next;
386                 if (rv & HT_ENUMERATE_REMOVE) {
387                     he->next = todo;
388                     todo = he;
389                 }
390             } else {
391                 hep = &he->next;
392             }
393             if (rv & HT_ENUMERATE_STOP) {
394                 goto out;
395             }
396         }
397     }
398 
399 out:
400     hep = &todo;
401     while ((he = *hep) != 0) {
402         PL_HashTableRawRemove(ht, hep, he);
403     }
404     return n;
405 }
406 
407 #ifdef HASHMETER
408 #include <math.h>
409 #include <stdio.h>
410 
411 PR_IMPLEMENT(void)
PL_HashTableDumpMeter(PLHashTable * ht,PLHashEnumerator dump,FILE * fp)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)
PL_HashTableDump(PLHashTable * ht,PLHashEnumerator dump,FILE * fp)461 PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp)
462 {
463     int count;
464 
465     count = PL_HashTableEnumerateEntries(ht, dump, fp);
466 #ifdef HASHMETER
467     PL_HashTableDumpMeter(ht, dump, fp);
468 #endif
469     return count;
470 }
471 
472 PR_IMPLEMENT(PLHashNumber)
PL_HashString(const void * key)473 PL_HashString(const void *key)
474 {
475     PLHashNumber h;
476     const PRUint8 *s;
477 
478     h = 0;
479     for (s = (const PRUint8*)key; *s; s++) {
480         h = PR_ROTATE_LEFT32(h, 4) ^ *s;
481     }
482     return h;
483 }
484 
485 PR_IMPLEMENT(int)
PL_CompareStrings(const void * v1,const void * v2)486 PL_CompareStrings(const void *v1, const void *v2)
487 {
488     return strcmp((const char*)v1, (const char*)v2) == 0;
489 }
490 
491 PR_IMPLEMENT(int)
PL_CompareValues(const void * v1,const void * v2)492 PL_CompareValues(const void *v1, const void *v2)
493 {
494     return v1 == v2;
495 }
496