js/jsd/jshash.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * PR hash table package.
michael@0 9 */
michael@0 10
michael@0 11 #include "jshash.h"
michael@0 12
michael@0 13 #include "mozilla/MathAlgorithms.h"
michael@0 14
michael@0 15 #include <stdlib.h>
michael@0 16 #include <string.h>
michael@0 17
michael@0 18 #include "jstypes.h"
michael@0 19
michael@0 20 #include "js/Utility.h"
michael@0 21
michael@0 22 using namespace js;
michael@0 23
michael@0 24 using mozilla::CeilingLog2Size;
michael@0 25 using mozilla::RotateLeft;
michael@0 26
michael@0 27 /* Compute the number of buckets in ht */
michael@0 28 #define NBUCKETS(ht) JS_BIT(JS_HASH_BITS - (ht)->shift)
michael@0 29
michael@0 30 /* The smallest table has 16 buckets */
michael@0 31 #define MINBUCKETSLOG2 4
michael@0 32 #define MINBUCKETS JS_BIT(MINBUCKETSLOG2)
michael@0 33
michael@0 34 /* Compute the maximum entries given n buckets that we will tolerate, ~90% */
michael@0 35 #define OVERLOADED(n) ((n) - ((n) >> 3))
michael@0 36
michael@0 37 /* Compute the number of entries below which we shrink the table by half */
michael@0 38 #define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
michael@0 39
michael@0 40 /*
michael@0 41 ** Stubs for default hash allocator ops.
michael@0 42 */
michael@0 43 static void *
michael@0 44 DefaultAllocTable(void *pool, size_t size)
michael@0 45 {
michael@0 46 return js_malloc(size);
michael@0 47 }
michael@0 48
michael@0 49 static void
michael@0 50 DefaultFreeTable(void *pool, void *item, size_t size)
michael@0 51 {
michael@0 52 js_free(item);
michael@0 53 }
michael@0 54
michael@0 55 static JSHashEntry *
michael@0 56 DefaultAllocEntry(void *pool, const void *key)
michael@0 57 {
michael@0 58 return (JSHashEntry*) js_malloc(sizeof(JSHashEntry));
michael@0 59 }
michael@0 60
michael@0 61 static void
michael@0 62 DefaultFreeEntry(void *pool, JSHashEntry *he, unsigned flag)
michael@0 63 {
michael@0 64 if (flag == HT_FREE_ENTRY)
michael@0 65 js_free(he);
michael@0 66 }
michael@0 67
michael@0 68 static const JSHashAllocOps defaultHashAllocOps = {
michael@0 69 DefaultAllocTable, DefaultFreeTable,
michael@0 70 DefaultAllocEntry, DefaultFreeEntry
michael@0 71 };
michael@0 72
michael@0 73 JSHashTable *
michael@0 74 JS_NewHashTable(uint32_t n, JSHashFunction keyHash,
michael@0 75 JSHashComparator keyCompare, JSHashComparator valueCompare,
michael@0 76 const JSHashAllocOps *allocOps, void *allocPriv)
michael@0 77 {
michael@0 78 JSHashTable *ht;
michael@0 79 size_t nb;
michael@0 80
michael@0 81 if (n <= MINBUCKETS) {
michael@0 82 n = MINBUCKETSLOG2;
michael@0 83 } else {
michael@0 84 n = CeilingLog2Size(n);
michael@0 85 if (int32_t(n) < 0)
michael@0 86 return nullptr;
michael@0 87 }
michael@0 88
michael@0 89 if (!allocOps) allocOps = &defaultHashAllocOps;
michael@0 90
michael@0 91 ht = (JSHashTable*) allocOps->allocTable(allocPriv, sizeof *ht);
michael@0 92 if (!ht)
michael@0 93 return nullptr;
michael@0 94 memset(ht, 0, sizeof *ht);
michael@0 95 ht->shift = JS_HASH_BITS - n;
michael@0 96 n = JS_BIT(n);
michael@0 97 nb = n * sizeof(JSHashEntry *);
michael@0 98 ht->buckets = (JSHashEntry**) allocOps->allocTable(allocPriv, nb);
michael@0 99 if (!ht->buckets) {
michael@0 100 allocOps->freeTable(allocPriv, ht, nb);
michael@0 101 return nullptr;
michael@0 102 }
michael@0 103 memset(ht->buckets, 0, nb);
michael@0 104
michael@0 105 ht->keyHash = keyHash;
michael@0 106 ht->keyCompare = keyCompare;
michael@0 107 ht->valueCompare = valueCompare;
michael@0 108 ht->allocOps = allocOps;
michael@0 109 ht->allocPriv = allocPriv;
michael@0 110 return ht;
michael@0 111 }
michael@0 112
michael@0 113 void
michael@0 114 JS_HashTableDestroy(JSHashTable *ht)
michael@0 115 {
michael@0 116 uint32_t i, n;
michael@0 117 JSHashEntry *he, **hep;
michael@0 118 const JSHashAllocOps *allocOps = ht->allocOps;
michael@0 119 void *allocPriv = ht->allocPriv;
michael@0 120
michael@0 121 n = NBUCKETS(ht);
michael@0 122 for (i = 0; i < n; i++) {
michael@0 123 hep = &ht->buckets[i];
michael@0 124 while ((he = *hep) != nullptr) {
michael@0 125 *hep = he->next;
michael@0 126 allocOps->freeEntry(allocPriv, he, HT_FREE_ENTRY);
michael@0 127 }
michael@0 128 }
michael@0 129 #ifdef DEBUG
michael@0 130 memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
michael@0 131 #endif
michael@0 132 allocOps->freeTable(allocPriv, ht->buckets, n * sizeof ht->buckets[0]);
michael@0 133 #ifdef DEBUG
michael@0 134 memset(ht, 0xDB, sizeof *ht);
michael@0 135 #endif
michael@0 136 allocOps->freeTable(allocPriv, ht, sizeof *ht);
michael@0 137 }
michael@0 138
michael@0 139 /*
michael@0 140 * Multiplicative hash, from Knuth 6.4.
michael@0 141 */
michael@0 142 #define BUCKET_HEAD(ht, keyHash) \
michael@0 143 (&(ht)->buckets[((keyHash) * JS_GOLDEN_RATIO) >> (ht)->shift])
michael@0 144
michael@0 145 JSHashEntry **
michael@0 146 JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key)
michael@0 147 {
michael@0 148 JSHashEntry *he, **hep, **hep0;
michael@0 149
michael@0 150 #ifdef JS_HASHMETER
michael@0 151 ht->nlookups++;
michael@0 152 #endif
michael@0 153 hep = hep0 = BUCKET_HEAD(ht, keyHash);
michael@0 154 while ((he = *hep) != nullptr) {
michael@0 155 if (he->keyHash == keyHash && ht->keyCompare(key, he->key)) {
michael@0 156 /* Move to front of chain if not already there */
michael@0 157 if (hep != hep0) {
michael@0 158 *hep = he->next;
michael@0 159 he->next = *hep0;
michael@0 160 *hep0 = he;
michael@0 161 }
michael@0 162 return hep0;
michael@0 163 }
michael@0 164 hep = &he->next;
michael@0 165 #ifdef JS_HASHMETER
michael@0 166 ht->nsteps++;
michael@0 167 #endif
michael@0 168 }
michael@0 169 return hep;
michael@0 170 }
michael@0 171
michael@0 172 static bool
michael@0 173 Resize(JSHashTable *ht, uint32_t newshift)
michael@0 174 {
michael@0 175 size_t nb, nentries, i;
michael@0 176 JSHashEntry **oldbuckets, *he, *next, **hep;
michael@0 177 size_t nold = NBUCKETS(ht);
michael@0 178
michael@0 179 MOZ_ASSERT(newshift < JS_HASH_BITS);
michael@0 180
michael@0 181 nb = (size_t)1 << (JS_HASH_BITS - newshift);
michael@0 182
michael@0 183 /* Integer overflow protection. */
michael@0 184 if (nb > (size_t)-1 / sizeof(JSHashEntry*))
michael@0 185 return false;
michael@0 186 nb *= sizeof(JSHashEntry*);
michael@0 187
michael@0 188 oldbuckets = ht->buckets;
michael@0 189 ht->buckets = (JSHashEntry**)ht->allocOps->allocTable(ht->allocPriv, nb);
michael@0 190 if (!ht->buckets) {
michael@0 191 ht->buckets = oldbuckets;
michael@0 192 return false;
michael@0 193 }
michael@0 194 memset(ht->buckets, 0, nb);
michael@0 195
michael@0 196 ht->shift = newshift;
michael@0 197 nentries = ht->nentries;
michael@0 198
michael@0 199 for (i = 0; nentries != 0; i++) {
michael@0 200 for (he = oldbuckets[i]; he; he = next) {
michael@0 201 MOZ_ASSERT(nentries != 0);
michael@0 202 --nentries;
michael@0 203 next = he->next;
michael@0 204 hep = BUCKET_HEAD(ht, he->keyHash);
michael@0 205
michael@0 206 /*
michael@0 207 * We do not require unique entries, instead appending he to the
michael@0 208 * chain starting at hep.
michael@0 209 */
michael@0 210 while (*hep)
michael@0 211 hep = &(*hep)->next;
michael@0 212 he->next = nullptr;
michael@0 213 *hep = he;
michael@0 214 }
michael@0 215 }
michael@0 216 #ifdef DEBUG
michael@0 217 memset(oldbuckets, 0xDB, nold * sizeof oldbuckets[0]);
michael@0 218 #endif
michael@0 219 ht->allocOps->freeTable(ht->allocPriv, oldbuckets,
michael@0 220 nold * sizeof oldbuckets[0]);
michael@0 221 return true;
michael@0 222 }
michael@0 223
michael@0 224 JSHashEntry *
michael@0 225 JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **&hep,
michael@0 226 JSHashNumber keyHash, const void *key, void *value)
michael@0 227 {
michael@0 228 uint32_t n;
michael@0 229 JSHashEntry *he;
michael@0 230
michael@0 231 /* Grow the table if it is overloaded */
michael@0 232 n = NBUCKETS(ht);
michael@0 233 if (ht->nentries >= OVERLOADED(n)) {
michael@0 234 if (!Resize(ht, ht->shift - 1))
michael@0 235 return nullptr;
michael@0 236 #ifdef JS_HASHMETER
michael@0 237 ht->ngrows++;
michael@0 238 #endif
michael@0 239 hep = JS_HashTableRawLookup(ht, keyHash, key);
michael@0 240 }
michael@0 241
michael@0 242 /* Make a new key value entry */
michael@0 243 he = ht->allocOps->allocEntry(ht->allocPriv, key);
michael@0 244 if (!he)
michael@0 245 return nullptr;
michael@0 246 he->keyHash = keyHash;
michael@0 247 he->key = key;
michael@0 248 he->value = value;
michael@0 249 he->next = *hep;
michael@0 250 *hep = he;
michael@0 251 ht->nentries++;
michael@0 252 return he;
michael@0 253 }
michael@0 254
michael@0 255 JSHashEntry *
michael@0 256 JS_HashTableAdd(JSHashTable *ht, const void *key, void *value)
michael@0 257 {
michael@0 258 JSHashNumber keyHash;
michael@0 259 JSHashEntry *he, **hep;
michael@0 260
michael@0 261 keyHash = ht->keyHash(key);
michael@0 262 hep = JS_HashTableRawLookup(ht, keyHash, key);
michael@0 263 if ((he = *hep) != nullptr) {
michael@0 264 /* Hit; see if values match */
michael@0 265 if (ht->valueCompare(he->value, value)) {
michael@0 266 /* key,value pair is already present in table */
michael@0 267 return he;
michael@0 268 }
michael@0 269 if (he->value)
michael@0 270 ht->allocOps->freeEntry(ht->allocPriv, he, HT_FREE_VALUE);
michael@0 271 he->value = value;
michael@0 272 return he;
michael@0 273 }
michael@0 274 return JS_HashTableRawAdd(ht, hep, keyHash, key, value);
michael@0 275 }
michael@0 276
michael@0 277 void
michael@0 278 JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he)
michael@0 279 {
michael@0 280 uint32_t n;
michael@0 281
michael@0 282 *hep = he->next;
michael@0 283 ht->allocOps->freeEntry(ht->allocPriv, he, HT_FREE_ENTRY);
michael@0 284
michael@0 285 /* Shrink table if it's underloaded */
michael@0 286 n = NBUCKETS(ht);
michael@0 287 if (--ht->nentries < UNDERLOADED(n)) {
michael@0 288 Resize(ht, ht->shift + 1);
michael@0 289 #ifdef JS_HASHMETER
michael@0 290 ht->nshrinks++;
michael@0 291 #endif
michael@0 292 }
michael@0 293 }
michael@0 294
michael@0 295 bool
michael@0 296 JS_HashTableRemove(JSHashTable *ht, const void *key)
michael@0 297 {
michael@0 298 JSHashNumber keyHash;
michael@0 299 JSHashEntry *he, **hep;
michael@0 300
michael@0 301 keyHash = ht->keyHash(key);
michael@0 302 hep = JS_HashTableRawLookup(ht, keyHash, key);
michael@0 303 if ((he = *hep) == nullptr)
michael@0 304 return false;
michael@0 305
michael@0 306 /* Hit; remove element */
michael@0 307 JS_HashTableRawRemove(ht, hep, he);
michael@0 308 return true;
michael@0 309 }
michael@0 310
michael@0 311 void *
michael@0 312 JS_HashTableLookup(JSHashTable *ht, const void *key)
michael@0 313 {
michael@0 314 JSHashNumber keyHash;
michael@0 315 JSHashEntry *he, **hep;
michael@0 316
michael@0 317 keyHash = ht->keyHash(key);
michael@0 318 hep = JS_HashTableRawLookup(ht, keyHash, key);
michael@0 319 if ((he = *hep) != nullptr) {
michael@0 320 return he->value;
michael@0 321 }
michael@0 322 return nullptr;
michael@0 323 }
michael@0 324
michael@0 325 /*
michael@0 326 ** Iterate over the entries in the hash table calling func for each
michael@0 327 ** entry found. Stop if "f" says to (return value & JS_ENUMERATE_STOP).
michael@0 328 ** Return a count of the number of elements scanned.
michael@0 329 */
michael@0 330 int
michael@0 331 JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg)
michael@0 332 {
michael@0 333 JSHashEntry *he, **hep, **bucket;
michael@0 334 uint32_t nlimit, n, nbuckets, newlog2;
michael@0 335 int rv;
michael@0 336
michael@0 337 nlimit = ht->nentries;
michael@0 338 n = 0;
michael@0 339 for (bucket = ht->buckets; n != nlimit; ++bucket) {
michael@0 340 hep = bucket;
michael@0 341 while ((he = *hep) != nullptr) {
michael@0 342 MOZ_ASSERT(n < nlimit);
michael@0 343 rv = f(he, n, arg);
michael@0 344 n++;
michael@0 345 if (rv & HT_ENUMERATE_REMOVE) {
michael@0 346 *hep = he->next;
michael@0 347 ht->allocOps->freeEntry(ht->allocPriv, he, HT_FREE_ENTRY);
michael@0 348 --ht->nentries;
michael@0 349 } else {
michael@0 350 hep = &he->next;
michael@0 351 }
michael@0 352 if (rv & HT_ENUMERATE_STOP) {
michael@0 353 goto out;
michael@0 354 }
michael@0 355 }
michael@0 356 }
michael@0 357
michael@0 358 out:
michael@0 359 /* Shrink table if removal of entries made it underloaded */
michael@0 360 if (ht->nentries != nlimit) {
michael@0 361 MOZ_ASSERT(ht->nentries < nlimit);
michael@0 362 nbuckets = NBUCKETS(ht);
michael@0 363 if (MINBUCKETS < nbuckets && ht->nentries < UNDERLOADED(nbuckets)) {
michael@0 364 newlog2 = CeilingLog2Size(ht->nentries);
michael@0 365 if (newlog2 < MINBUCKETSLOG2)
michael@0 366 newlog2 = MINBUCKETSLOG2;
michael@0 367
michael@0 368 /* Check that we really shrink the table. */
michael@0 369 MOZ_ASSERT(JS_HASH_BITS - ht->shift > newlog2);
michael@0 370 Resize(ht, JS_HASH_BITS - newlog2);
michael@0 371 }
michael@0 372 }
michael@0 373 return (int)n;
michael@0 374 }
michael@0 375
michael@0 376 #ifdef JS_HASHMETER
michael@0 377 #include <stdio.h>
michael@0 378
michael@0 379 void
michael@0 380 JS_HashTableDumpMeter(JSHashTable *ht, JSHashEnumerator dump, FILE *fp)
michael@0 381 {
michael@0 382 double sqsum, mean, sigma;
michael@0 383 uint32_t nchains, nbuckets;
michael@0 384 uint32_t i, n, maxChain, maxChainLen;
michael@0 385 JSHashEntry *he;
michael@0 386
michael@0 387 sqsum = 0;
michael@0 388 nchains = 0;
michael@0 389 maxChain = maxChainLen = 0;
michael@0 390 nbuckets = NBUCKETS(ht);
michael@0 391 for (i = 0; i < nbuckets; i++) {
michael@0 392 he = ht->buckets[i];
michael@0 393 if (!he)
michael@0 394 continue;
michael@0 395 nchains++;
michael@0 396 for (n = 0; he; he = he->next)
michael@0 397 n++;
michael@0 398 sqsum += n * n;
michael@0 399 if (n > maxChainLen) {
michael@0 400 maxChainLen = n;
michael@0 401 maxChain = i;
michael@0 402 }
michael@0 403 }
michael@0 404
michael@0 405 mean = JS_MeanAndStdDev(nchains, ht->nentries, sqsum, &sigma);
michael@0 406
michael@0 407 fprintf(fp, "\nHash table statistics:\n");
michael@0 408 fprintf(fp, " number of lookups: %u\n", ht->nlookups);
michael@0 409 fprintf(fp, " number of entries: %u\n", ht->nentries);
michael@0 410 fprintf(fp, " number of grows: %u\n", ht->ngrows);
michael@0 411 fprintf(fp, " number of shrinks: %u\n", ht->nshrinks);
michael@0 412 fprintf(fp, " mean steps per hash: %g\n", (double)ht->nsteps
michael@0 413 / ht->nlookups);
michael@0 414 fprintf(fp, "mean hash chain length: %g\n", mean);
michael@0 415 fprintf(fp, " standard deviation: %g\n", sigma);
michael@0 416 fprintf(fp, " max hash chain length: %u\n", maxChainLen);
michael@0 417 fprintf(fp, " max hash chain: [%u]\n", maxChain);
michael@0 418
michael@0 419 for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++)
michael@0 420 if (dump(he, i, fp) != HT_ENUMERATE_NEXT)
michael@0 421 break;
michael@0 422 }
michael@0 423 #endif /* JS_HASHMETER */
michael@0 424
michael@0 425 int
michael@0 426 JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp)
michael@0 427 {
michael@0 428 int count;
michael@0 429
michael@0 430 count = JS_HashTableEnumerateEntries(ht, dump, fp);
michael@0 431 #ifdef JS_HASHMETER
michael@0 432 JS_HashTableDumpMeter(ht, dump, fp);
michael@0 433 #endif
michael@0 434 return count;
michael@0 435 }
michael@0 436
michael@0 437 JSHashNumber
michael@0 438 JS_HashString(const void *key)
michael@0 439 {
michael@0 440 JSHashNumber h;
michael@0 441 const unsigned char *s;
michael@0 442
michael@0 443 h = 0;
michael@0 444 for (s = (const unsigned char *)key; *s; s++)
michael@0 445 h = RotateLeft(h, 4) ^ *s;
michael@0 446 return h;
michael@0 447 }
michael@0 448
michael@0 449 int
michael@0 450 JS_CompareValues(const void *v1, const void *v2)
michael@0 451 {
michael@0 452 return v1 == v2;
michael@0 453 }

mercurial