2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-08-12 00:47:03 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Double hashing implementation.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "pldhash.h"
|
2012-03-03 02:20:44 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-11-13 04:22:38 +04:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2005-08-31 21:20:09 +04:00
|
|
|
#include "nsDebug.h" /* for PR_ASSERT */
|
2012-09-28 01:44:47 +04:00
|
|
|
#include "nsAlgorithm.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-03-03 09:12:46 +04:00
|
|
|
#include "mozilla/ChaosMode.h"
|
2005-08-12 00:47:03 +04:00
|
|
|
|
|
|
|
#ifdef PL_DHASHMETER
|
|
|
|
# define METER(x) x
|
|
|
|
#else
|
|
|
|
# define METER(x) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
2006-04-18 22:30:50 +04:00
|
|
|
/*
|
|
|
|
* The following DEBUG-only code is used to assert that calls to one of
|
|
|
|
* table->ops or to an enumerator do not cause re-entry into a call that
|
2013-10-31 02:10:06 +04:00
|
|
|
* can mutate the table.
|
2006-04-18 22:30:50 +04:00
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
2009-01-10 19:28:16 +03:00
|
|
|
/*
|
|
|
|
* Most callers that assert about the recursion level don't care about
|
|
|
|
* this magical value because they are asserting that mutation is
|
|
|
|
* allowed (and therefore the level is 0 or 1, depending on whether they
|
|
|
|
* incremented it).
|
|
|
|
*
|
|
|
|
* Only PL_DHashTableFinish needs to allow this special value.
|
|
|
|
*/
|
2013-10-31 02:10:06 +04:00
|
|
|
#define IMMUTABLE_RECURSION_LEVEL ((uint16_t)-1)
|
2009-01-10 19:28:16 +03:00
|
|
|
|
|
|
|
#define RECURSION_LEVEL_SAFE_TO_FINISH(table_) \
|
2013-10-31 02:10:06 +04:00
|
|
|
(table_->recursionLevel == 0 || \
|
|
|
|
table_->recursionLevel == IMMUTABLE_RECURSION_LEVEL)
|
2006-04-18 22:30:50 +04:00
|
|
|
|
2009-01-10 19:28:16 +03:00
|
|
|
#define INCREMENT_RECURSION_LEVEL(table_) \
|
2013-11-13 04:22:38 +04:00
|
|
|
do { \
|
2013-10-31 02:10:06 +04:00
|
|
|
if (table_->recursionLevel != IMMUTABLE_RECURSION_LEVEL) \
|
|
|
|
++table_->recursionLevel; \
|
2013-11-13 04:22:38 +04:00
|
|
|
} while(0)
|
2009-01-10 19:28:16 +03:00
|
|
|
#define DECREMENT_RECURSION_LEVEL(table_) \
|
2013-11-13 04:22:38 +04:00
|
|
|
do { \
|
2014-06-27 05:35:39 +04:00
|
|
|
if (table_->recursionLevel != IMMUTABLE_RECURSION_LEVEL) { \
|
|
|
|
MOZ_ASSERT(table_->recursionLevel > 0); \
|
|
|
|
--table_->recursionLevel; \
|
2009-01-10 19:28:16 +03:00
|
|
|
} \
|
2013-11-13 04:22:38 +04:00
|
|
|
} while(0)
|
2006-04-18 22:30:50 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2013-11-13 04:22:38 +04:00
|
|
|
#define INCREMENT_RECURSION_LEVEL(table_) do { } while(0)
|
|
|
|
#define DECREMENT_RECURSION_LEVEL(table_) do { } while(0)
|
2006-04-18 22:30:50 +04:00
|
|
|
|
|
|
|
#endif /* defined(DEBUG) */
|
|
|
|
|
2012-03-03 02:20:44 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
void*
|
|
|
|
PL_DHashAllocTable(PLDHashTable* aTable, uint32_t aNBytes)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
return malloc(aNBytes);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashFreeTable(PLDHashTable* aTable, void* aPtr)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
free(aPtr);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
PLDHashNumber
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashStringKey(PLDHashTable* aTable, const void* aKey)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
return HashString(static_cast<const char*>(aKey));
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
PLDHashNumber
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashVoidPtrKeyStub(PLDHashTable* aTable, const void* aKey)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
return (PLDHashNumber)(ptrdiff_t)aKey >> 2;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashMatchEntryStub(PLDHashTable* aTable,
|
|
|
|
const PLDHashEntryHdr* aEntry,
|
|
|
|
const void* aKey)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
const PLDHashEntryStub* stub = (const PLDHashEntryStub*)aEntry;
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return stub->key == aKey;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashMatchStringKey(PLDHashTable* aTable,
|
|
|
|
const PLDHashEntryHdr* aEntry,
|
|
|
|
const void* aKey)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
const PLDHashEntryStub* stub = (const PLDHashEntryStub*)aEntry;
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* XXX tolerate null keys on account of sloppy Mozilla callers. */
|
|
|
|
return stub->key == aKey ||
|
|
|
|
(stub->key && aKey &&
|
|
|
|
strcmp((const char*)stub->key, (const char*)aKey) == 0);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashMoveEntryStub(PLDHashTable* aTable,
|
|
|
|
const PLDHashEntryHdr* aFrom,
|
|
|
|
PLDHashEntryHdr* aTo)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
memcpy(aTo, aFrom, aTable->entrySize);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashClearEntryStub(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
memset(aEntry, 0, aTable->entrySize);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashFreeStringKey(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
const PLDHashEntryStub* stub = (const PLDHashEntryStub*)aEntry;
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
free((void*)stub->key);
|
|
|
|
memset(aEntry, 0, aTable->entrySize);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashFinalizeStub(PLDHashTable* aTable)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PLDHashTableOps stub_ops = {
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
PL_DHashVoidPtrKeyStub,
|
|
|
|
PL_DHashMatchEntryStub,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
|
|
|
PL_DHashFinalizeStub,
|
|
|
|
nullptr
|
2005-08-12 00:47:03 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
const PLDHashTableOps*
|
2005-08-12 00:47:03 +04:00
|
|
|
PL_DHashGetStubOps(void)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
return &stub_ops;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 02:36:45 +04:00
|
|
|
static bool
|
2014-06-27 05:35:39 +04:00
|
|
|
SizeOfEntryStore(uint32_t aCapacity, uint32_t aEntrySize, uint32_t* aNbytes)
|
2013-10-22 02:36:45 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
uint64_t nbytes64 = uint64_t(aCapacity) * uint64_t(aEntrySize);
|
|
|
|
*aNbytes = aCapacity * aEntrySize;
|
|
|
|
return uint64_t(*aNbytes) == nbytes64; // returns false on overflow
|
2013-10-22 02:36:45 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
PLDHashTable*
|
|
|
|
PL_NewDHashTable(const PLDHashTableOps* aOps, void* aData, uint32_t aEntrySize,
|
|
|
|
uint32_t aCapacity)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
PLDHashTable* table = (PLDHashTable*)malloc(sizeof(*table));
|
|
|
|
if (!table) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!PL_DHashTableInit(table, aOps, aData, aEntrySize, aCapacity,
|
|
|
|
fallible_t())) {
|
|
|
|
free(table);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return table;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableDestroy(PLDHashTable* aTable)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableFinish(aTable);
|
|
|
|
free(aTable);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableInit(PLDHashTable* aTable, const PLDHashTableOps* aOps,
|
|
|
|
void* aData, uint32_t aEntrySize, uint32_t aCapacity,
|
|
|
|
const fallible_t&)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aEntrySize > 16 * sizeof(void*)) {
|
|
|
|
printf_stderr(
|
|
|
|
"pldhash: for the aTable at address %p, the given aEntrySize"
|
|
|
|
" of %lu definitely favors chaining over double hashing.\n",
|
|
|
|
(void*)aTable,
|
|
|
|
(unsigned long) aEntrySize);
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
#endif
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
aTable->ops = aOps;
|
|
|
|
aTable->data = aData;
|
|
|
|
if (aCapacity < PL_DHASH_MIN_SIZE) {
|
|
|
|
aCapacity = PL_DHASH_MIN_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log2 = CeilingLog2(aCapacity);
|
|
|
|
|
|
|
|
aCapacity = 1u << log2;
|
|
|
|
if (aCapacity > PL_DHASH_MAX_SIZE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
aTable->hashShift = PL_DHASH_BITS - log2;
|
|
|
|
aTable->entrySize = aEntrySize;
|
|
|
|
aTable->entryCount = aTable->removedCount = 0;
|
|
|
|
aTable->generation = 0;
|
|
|
|
uint32_t nbytes;
|
|
|
|
if (!SizeOfEntryStore(aCapacity, aEntrySize, &nbytes))
|
|
|
|
return false; // overflowed
|
|
|
|
|
|
|
|
aTable->entryStore = (char*)aOps->allocTable(aTable, nbytes);
|
|
|
|
if (!aTable->entryStore) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memset(aTable->entryStore, 0, nbytes);
|
|
|
|
METER(memset(&aTable->stats, 0, sizeof(aTable->stats)));
|
2006-04-18 22:30:50 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-06-27 05:35:39 +04:00
|
|
|
aTable->recursionLevel = 0;
|
2006-04-18 22:30:50 +04:00
|
|
|
#endif
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return true;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 22:04:09 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableInit(PLDHashTable* aTable, const PLDHashTableOps* aOps, void* aData,
|
|
|
|
uint32_t aEntrySize, uint32_t aCapacity)
|
2014-02-27 22:04:09 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!PL_DHashTableInit(aTable, aOps, aData, aEntrySize, aCapacity, fallible_t())) {
|
|
|
|
if (aCapacity > PL_DHASH_MAX_SIZE) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
uint32_t nbytes;
|
|
|
|
if (!SizeOfEntryStore(aCapacity, aEntrySize, &nbytes)) {
|
|
|
|
MOZ_CRASH();
|
2014-02-27 22:04:09 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_ABORT_OOM(nbytes);
|
|
|
|
}
|
2014-02-27 22:04:09 +04:00
|
|
|
}
|
|
|
|
|
2005-08-12 00:47:03 +04:00
|
|
|
/*
|
2013-10-31 05:22:31 +04:00
|
|
|
* Compute max and min load numbers (entry counts). We have a secondary max
|
|
|
|
* that allows us to overload a table reasonably if it cannot be grown further
|
|
|
|
* (i.e. if ChangeTable() fails). The table slows down drastically if the
|
|
|
|
* secondary max is too close to 1, but 0.96875 gives only a slight slowdown
|
|
|
|
* while allowing 1.3x more elements.
|
2005-08-12 00:47:03 +04:00
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
static inline uint32_t
|
|
|
|
MaxLoad(uint32_t aSize)
|
|
|
|
{
|
|
|
|
return aSize - (aSize >> 2); // == aSize * 0.75
|
2013-10-21 07:17:48 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
static inline uint32_t
|
|
|
|
MaxLoadOnGrowthFailure(uint32_t aSize)
|
|
|
|
{
|
|
|
|
return aSize - (aSize >> 5); // == aSize * 0.96875
|
2013-10-31 05:22:31 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
static inline uint32_t
|
|
|
|
MinLoad(uint32_t aSize)
|
|
|
|
{
|
|
|
|
return aSize >> 2; // == aSize * 0.25
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Double hashing needs the second hash code to be relatively prime to table
|
|
|
|
* size, so we simply make hash2 odd.
|
|
|
|
*/
|
|
|
|
#define HASH1(hash0, shift) ((hash0) >> (shift))
|
|
|
|
#define HASH2(hash0,log2,shift) ((((hash0) << (log2)) >> (shift)) | 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reserve keyHash 0 for free entries and 1 for removed-entry sentinels. Note
|
|
|
|
* that a removed-entry sentinel need be stored only if the removed entry had
|
|
|
|
* a colliding entry added after it. Therefore we can use 1 as the collision
|
|
|
|
* flag in addition to the removed-entry sentinel value. Multiplicative hash
|
|
|
|
* uses the high order bits of keyHash, so this least-significant reservation
|
|
|
|
* should not hurt the hash function's effectiveness much.
|
|
|
|
*
|
|
|
|
* If you change any of these magic numbers, also update PL_DHASH_ENTRY_IS_LIVE
|
|
|
|
* in pldhash.h. It used to be private to pldhash.c, but then became public to
|
|
|
|
* assist iterator writers who inspect table->entryStore directly.
|
|
|
|
*/
|
|
|
|
#define COLLISION_FLAG ((PLDHashNumber) 1)
|
|
|
|
#define MARK_ENTRY_FREE(entry) ((entry)->keyHash = 0)
|
|
|
|
#define MARK_ENTRY_REMOVED(entry) ((entry)->keyHash = 1)
|
|
|
|
#define ENTRY_IS_REMOVED(entry) ((entry)->keyHash == 1)
|
|
|
|
#define ENTRY_IS_LIVE(entry) PL_DHASH_ENTRY_IS_LIVE(entry)
|
|
|
|
#define ENSURE_LIVE_KEYHASH(hash0) if (hash0 < 2) hash0 -= 2; else (void)0
|
|
|
|
|
|
|
|
/* Match an entry's keyHash against an unstored one computed from a key. */
|
|
|
|
#define MATCH_ENTRY_KEYHASH(entry,hash0) \
|
|
|
|
(((entry)->keyHash & ~COLLISION_FLAG) == (hash0))
|
|
|
|
|
|
|
|
/* Compute the address of the indexed entry in table. */
|
|
|
|
#define ADDRESS_ENTRY(table, index) \
|
|
|
|
((PLDHashEntryHdr *)((table)->entryStore + (index) * (table)->entrySize))
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableFinish(PLDHashTable* aTable)
|
|
|
|
{
|
|
|
|
INCREMENT_RECURSION_LEVEL(aTable);
|
|
|
|
|
|
|
|
/* Call finalize before clearing entries, so it can enumerate them. */
|
|
|
|
aTable->ops->finalize(aTable);
|
|
|
|
|
|
|
|
/* Clear any remaining live entries. */
|
|
|
|
char* entryAddr = aTable->entryStore;
|
|
|
|
uint32_t entrySize = aTable->entrySize;
|
|
|
|
char* entryLimit = entryAddr + PL_DHASH_TABLE_SIZE(aTable) * entrySize;
|
|
|
|
while (entryAddr < entryLimit) {
|
|
|
|
PLDHashEntryHdr* entry = (PLDHashEntryHdr*)entryAddr;
|
|
|
|
if (ENTRY_IS_LIVE(entry)) {
|
|
|
|
METER(aTable->stats.removeEnums++);
|
|
|
|
aTable->ops->clearEntry(aTable, entry);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
entryAddr += entrySize;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
DECREMENT_RECURSION_LEVEL(aTable);
|
|
|
|
MOZ_ASSERT(RECURSION_LEVEL_SAFE_TO_FINISH(aTable));
|
2006-04-18 22:30:50 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* Free entry storage last. */
|
|
|
|
aTable->ops->freeTable(aTable, aTable->entryStore);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashEntryHdr* PL_DHASH_FASTCALL
|
|
|
|
SearchTable(PLDHashTable* aTable, const void* aKey, PLDHashNumber aKeyHash,
|
|
|
|
PLDHashOperator aOp)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
METER(aTable->stats.searches++);
|
|
|
|
NS_ASSERTION(!(aKeyHash & COLLISION_FLAG),
|
|
|
|
"!(aKeyHash & COLLISION_FLAG)");
|
|
|
|
|
|
|
|
/* Compute the primary hash address. */
|
|
|
|
int hashShift = aTable->hashShift;
|
|
|
|
PLDHashNumber hash1 = HASH1(aKeyHash, hashShift);
|
|
|
|
PLDHashEntryHdr* entry = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
|
|
|
|
/* Miss: return space for a new entry. */
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(aTable->stats.misses++);
|
|
|
|
return entry;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* Hit: return entry. */
|
|
|
|
PLDHashMatchEntry matchEntry = aTable->ops->matchEntry;
|
|
|
|
if (MATCH_ENTRY_KEYHASH(entry, aKeyHash) &&
|
|
|
|
matchEntry(aTable, entry, aKey)) {
|
|
|
|
METER(aTable->stats.hits++);
|
|
|
|
return entry;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* Collision: double hash. */
|
|
|
|
int sizeLog2 = PL_DHASH_BITS - aTable->hashShift;
|
|
|
|
PLDHashNumber hash2 = HASH2(aKeyHash, sizeLog2, hashShift);
|
|
|
|
uint32_t sizeMask = (1u << sizeLog2) - 1;
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* Save the first removed entry pointer so PL_DHASH_ADD can recycle it. */
|
|
|
|
PLDHashEntryHdr* firstRemoved = nullptr;
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
for (;;) {
|
|
|
|
if (MOZ_UNLIKELY(ENTRY_IS_REMOVED(entry))) {
|
|
|
|
if (!firstRemoved) {
|
|
|
|
firstRemoved = entry;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (aOp == PL_DHASH_ADD) {
|
|
|
|
entry->keyHash |= COLLISION_FLAG;
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 19:32:38 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
METER(aTable->stats.steps++);
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
entry = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(aTable->stats.misses++);
|
|
|
|
return (firstRemoved && aOp == PL_DHASH_ADD) ? firstRemoved : entry;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (MATCH_ENTRY_KEYHASH(entry, aKeyHash) &&
|
|
|
|
matchEntry(aTable, entry, aKey)) {
|
|
|
|
METER(aTable->stats.hits++);
|
|
|
|
return entry;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
return nullptr;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2007-03-24 02:32:40 +03:00
|
|
|
/*
|
|
|
|
* This is a copy of SearchTable, used by ChangeTable, hardcoded to
|
2014-06-27 05:35:39 +04:00
|
|
|
* 1. assume |aOp == PL_DHASH_ADD|,
|
|
|
|
* 2. assume that |aKey| will never match an existing entry, and
|
2007-03-24 02:32:40 +03:00
|
|
|
* 3. assume that no entries have been removed from the current table
|
|
|
|
* structure.
|
2014-06-27 05:35:39 +04:00
|
|
|
* Avoiding the need for |aKey| means we can avoid needing a way to map
|
2007-03-24 02:32:40 +03:00
|
|
|
* entries to keys, which means callers can use complex key types more
|
|
|
|
* easily.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashEntryHdr* PL_DHASH_FASTCALL
|
|
|
|
FindFreeEntry(PLDHashTable* aTable, PLDHashNumber aKeyHash)
|
2007-03-24 02:32:40 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
METER(aTable->stats.searches++);
|
|
|
|
NS_ASSERTION(!(aKeyHash & COLLISION_FLAG),
|
|
|
|
"!(aKeyHash & COLLISION_FLAG)");
|
|
|
|
|
|
|
|
/* Compute the primary hash address. */
|
|
|
|
int hashShift = aTable->hashShift;
|
|
|
|
PLDHashNumber hash1 = HASH1(aKeyHash, hashShift);
|
|
|
|
PLDHashEntryHdr* entry = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
|
|
|
|
/* Miss: return space for a new entry. */
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(aTable->stats.misses++);
|
|
|
|
return entry;
|
|
|
|
}
|
2007-03-24 02:32:40 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* Collision: double hash. */
|
|
|
|
int sizeLog2 = PL_DHASH_BITS - aTable->hashShift;
|
|
|
|
PLDHashNumber hash2 = HASH2(aKeyHash, sizeLog2, hashShift);
|
|
|
|
uint32_t sizeMask = (1u << sizeLog2) - 1;
|
2007-03-24 02:32:40 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
for (;;) {
|
|
|
|
NS_ASSERTION(!ENTRY_IS_REMOVED(entry),
|
|
|
|
"!ENTRY_IS_REMOVED(entry)");
|
|
|
|
entry->keyHash |= COLLISION_FLAG;
|
2007-03-24 02:32:40 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
METER(aTable->stats.steps++);
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
2007-03-24 02:32:40 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
entry = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(aTable->stats.misses++);
|
|
|
|
return entry;
|
2007-03-24 02:32:40 +03:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2007-03-24 02:32:40 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
return nullptr;
|
2007-03-24 02:32:40 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2014-06-27 05:35:39 +04:00
|
|
|
ChangeTable(PLDHashTable* aTable, int aDeltaLog2)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
/* Look, but don't touch, until we succeed in getting new entry store. */
|
|
|
|
int oldLog2 = PL_DHASH_BITS - aTable->hashShift;
|
|
|
|
int newLog2 = oldLog2 + aDeltaLog2;
|
|
|
|
uint32_t newCapacity = 1u << newLog2;
|
|
|
|
if (newCapacity > PL_DHASH_MAX_SIZE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t entrySize = aTable->entrySize;
|
|
|
|
uint32_t nbytes;
|
|
|
|
if (!SizeOfEntryStore(newCapacity, entrySize, &nbytes)) {
|
|
|
|
return false; // overflowed
|
|
|
|
}
|
|
|
|
|
|
|
|
char* newEntryStore = (char*)aTable->ops->allocTable(aTable, nbytes);
|
|
|
|
if (!newEntryStore) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can't fail from here on, so update table parameters. */
|
2006-04-18 22:30:50 +04:00
|
|
|
#ifdef DEBUG
|
2014-06-27 05:35:39 +04:00
|
|
|
uint32_t recursionLevel = aTable->recursionLevel;
|
2006-04-18 22:30:50 +04:00
|
|
|
#endif
|
2014-06-27 05:35:39 +04:00
|
|
|
aTable->hashShift = PL_DHASH_BITS - newLog2;
|
|
|
|
aTable->removedCount = 0;
|
|
|
|
aTable->generation++;
|
|
|
|
|
|
|
|
/* Assign the new entry store to table. */
|
|
|
|
memset(newEntryStore, 0, nbytes);
|
|
|
|
char* oldEntryStore;
|
|
|
|
char* oldEntryAddr;
|
|
|
|
oldEntryAddr = oldEntryStore = aTable->entryStore;
|
|
|
|
aTable->entryStore = newEntryStore;
|
|
|
|
PLDHashMoveEntry moveEntry = aTable->ops->moveEntry;
|
2006-04-18 22:30:50 +04:00
|
|
|
#ifdef DEBUG
|
2014-06-27 05:35:39 +04:00
|
|
|
aTable->recursionLevel = recursionLevel;
|
2006-04-18 22:30:50 +04:00
|
|
|
#endif
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/* Copy only live entries, leaving removed ones behind. */
|
|
|
|
uint32_t oldCapacity = 1u << oldLog2;
|
|
|
|
for (uint32_t i = 0; i < oldCapacity; ++i) {
|
|
|
|
PLDHashEntryHdr* oldEntry = (PLDHashEntryHdr*)oldEntryAddr;
|
|
|
|
if (ENTRY_IS_LIVE(oldEntry)) {
|
|
|
|
oldEntry->keyHash &= ~COLLISION_FLAG;
|
|
|
|
PLDHashEntryHdr* newEntry = FindFreeEntry(aTable, oldEntry->keyHash);
|
|
|
|
NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(newEntry),
|
|
|
|
"PL_DHASH_ENTRY_IS_FREE(newEntry)");
|
|
|
|
moveEntry(aTable, oldEntry, newEntry);
|
|
|
|
newEntry->keyHash = oldEntry->keyHash;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
oldEntryAddr += entrySize;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
aTable->ops->freeTable(aTable, oldEntryStore);
|
|
|
|
return true;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
PLDHashEntryHdr* PL_DHASH_FASTCALL
|
|
|
|
PL_DHashTableOperate(PLDHashTable* aTable, const void* aKey, PLDHashOperator aOp)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
PLDHashEntryHdr* entry;
|
|
|
|
|
|
|
|
MOZ_ASSERT(aOp == PL_DHASH_LOOKUP || aTable->recursionLevel == 0);
|
|
|
|
INCREMENT_RECURSION_LEVEL(aTable);
|
|
|
|
|
|
|
|
PLDHashNumber keyHash = aTable->ops->hashKey(aTable, aKey);
|
|
|
|
keyHash *= PL_DHASH_GOLDEN_RATIO;
|
|
|
|
|
|
|
|
/* Avoid 0 and 1 hash codes, they indicate free and removed entries. */
|
|
|
|
ENSURE_LIVE_KEYHASH(keyHash);
|
|
|
|
keyHash &= ~COLLISION_FLAG;
|
|
|
|
|
|
|
|
switch (aOp) {
|
|
|
|
case PL_DHASH_LOOKUP:
|
|
|
|
METER(aTable->stats.lookups++);
|
|
|
|
entry = SearchTable(aTable, aKey, keyHash, aOp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PL_DHASH_ADD: {
|
|
|
|
/*
|
|
|
|
* If alpha is >= .75, grow or compress the table. If aKey is already
|
|
|
|
* in the table, we may grow once more than necessary, but only if we
|
|
|
|
* are on the edge of being overloaded.
|
|
|
|
*/
|
|
|
|
uint32_t size = PL_DHASH_TABLE_SIZE(aTable);
|
|
|
|
if (aTable->entryCount + aTable->removedCount >= MaxLoad(size)) {
|
|
|
|
/* Compress if a quarter or more of all entries are removed. */
|
|
|
|
int deltaLog2;
|
|
|
|
if (aTable->removedCount >= size >> 2) {
|
|
|
|
METER(aTable->stats.compresses++);
|
|
|
|
deltaLog2 = 0;
|
|
|
|
} else {
|
|
|
|
METER(aTable->stats.grows++);
|
|
|
|
deltaLog2 = 1;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
|
|
|
/*
|
2014-06-27 05:35:39 +04:00
|
|
|
* Grow or compress aTable. If ChangeTable() fails, allow
|
|
|
|
* overloading up to the secondary max. Once we hit the secondary
|
|
|
|
* max, return null.
|
2005-08-12 00:47:03 +04:00
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!ChangeTable(aTable, deltaLog2) &&
|
|
|
|
aTable->entryCount + aTable->removedCount >=
|
|
|
|
MaxLoadOnGrowthFailure(size)) {
|
|
|
|
METER(aTable->stats.addFailures++);
|
|
|
|
entry = nullptr;
|
|
|
|
break;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/*
|
|
|
|
* Look for entry after possibly growing, so we don't have to add it,
|
|
|
|
* then skip it while growing the table and re-add it after.
|
|
|
|
*/
|
|
|
|
entry = SearchTable(aTable, aKey, keyHash, aOp);
|
|
|
|
if (!ENTRY_IS_LIVE(entry)) {
|
|
|
|
/* Initialize the entry, indicating that it's no longer free. */
|
|
|
|
METER(aTable->stats.addMisses++);
|
|
|
|
if (ENTRY_IS_REMOVED(entry)) {
|
|
|
|
METER(aTable->stats.addOverRemoved++);
|
|
|
|
aTable->removedCount--;
|
|
|
|
keyHash |= COLLISION_FLAG;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aTable->ops->initEntry &&
|
|
|
|
!aTable->ops->initEntry(aTable, entry, aKey)) {
|
|
|
|
/* We haven't claimed entry yet; fail with null return. */
|
|
|
|
memset(entry + 1, 0, aTable->entrySize - sizeof(*entry));
|
|
|
|
entry = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
entry->keyHash = keyHash;
|
|
|
|
aTable->entryCount++;
|
2013-11-01 09:33:29 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
METER(else {
|
|
|
|
aTable->stats.addHits++;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
case PL_DHASH_REMOVE:
|
|
|
|
entry = SearchTable(aTable, aKey, keyHash, aOp);
|
|
|
|
if (ENTRY_IS_LIVE(entry)) {
|
|
|
|
/* Clear this entry and mark it as "removed". */
|
|
|
|
METER(aTable->stats.removeHits++);
|
|
|
|
PL_DHashTableRawRemove(aTable, entry);
|
|
|
|
|
|
|
|
/* Shrink if alpha is <= .25 and aTable isn't too small already. */
|
|
|
|
uint32_t size = PL_DHASH_TABLE_SIZE(aTable);
|
|
|
|
if (size > PL_DHASH_MIN_SIZE &&
|
|
|
|
aTable->entryCount <= MinLoad(size)) {
|
|
|
|
METER(aTable->stats.shrinks++);
|
|
|
|
(void) ChangeTable(aTable, -1);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
METER(else {
|
|
|
|
aTable->stats.removeMisses++;
|
|
|
|
});
|
|
|
|
entry = nullptr;
|
|
|
|
break;
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("0");
|
|
|
|
entry = nullptr;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
DECREMENT_RECURSION_LEVEL(aTable);
|
2006-04-18 22:30:50 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return entry;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableRawRemove(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
MOZ_ASSERT(aTable->recursionLevel != IMMUTABLE_RECURSION_LEVEL);
|
|
|
|
|
|
|
|
NS_ASSERTION(PL_DHASH_ENTRY_IS_LIVE(aEntry),
|
|
|
|
"PL_DHASH_ENTRY_IS_LIVE(aEntry)");
|
|
|
|
|
|
|
|
/* Load keyHash first in case clearEntry() goofs it. */
|
|
|
|
PLDHashNumber keyHash = aEntry->keyHash;
|
|
|
|
aTable->ops->clearEntry(aTable, aEntry);
|
|
|
|
if (keyHash & COLLISION_FLAG) {
|
|
|
|
MARK_ENTRY_REMOVED(aEntry);
|
|
|
|
aTable->removedCount++;
|
|
|
|
} else {
|
|
|
|
METER(aTable->stats.removeFrees++);
|
|
|
|
MARK_ENTRY_FREE(aEntry);
|
|
|
|
}
|
|
|
|
aTable->entryCount--;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableEnumerate(PLDHashTable* aTable, PLDHashEnumerator aEtor, void* aArg)
|
2005-08-12 00:47:03 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
INCREMENT_RECURSION_LEVEL(aTable);
|
|
|
|
|
|
|
|
char* entryAddr = aTable->entryStore;
|
|
|
|
uint32_t entrySize = aTable->entrySize;
|
|
|
|
uint32_t capacity = PL_DHASH_TABLE_SIZE(aTable);
|
|
|
|
uint32_t tableSize = capacity * entrySize;
|
|
|
|
char* entryLimit = entryAddr + tableSize;
|
|
|
|
uint32_t i = 0;
|
|
|
|
bool didRemove = false;
|
|
|
|
|
|
|
|
if (ChaosMode::isActive()) {
|
|
|
|
// Start iterating at a random point in the hashtable. It would be
|
|
|
|
// even more chaotic to iterate in fully random order, but that's a lot
|
|
|
|
// more work.
|
|
|
|
entryAddr += ChaosMode::randomUint32LessThan(capacity) * entrySize;
|
|
|
|
if (entryAddr >= entryLimit) {
|
|
|
|
entryAddr -= tableSize;
|
2014-03-03 09:12:46 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t e = 0; e < capacity; ++e) {
|
|
|
|
PLDHashEntryHdr* entry = (PLDHashEntryHdr*)entryAddr;
|
|
|
|
if (ENTRY_IS_LIVE(entry)) {
|
|
|
|
PLDHashOperator op = aEtor(aTable, entry, i++, aArg);
|
|
|
|
if (op & PL_DHASH_REMOVE) {
|
|
|
|
METER(aTable->stats.removeEnums++);
|
|
|
|
PL_DHashTableRawRemove(aTable, entry);
|
|
|
|
didRemove = true;
|
|
|
|
}
|
|
|
|
if (op & PL_DHASH_STOP) {
|
|
|
|
break;
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
entryAddr += entrySize;
|
|
|
|
if (entryAddr >= entryLimit) {
|
|
|
|
entryAddr -= tableSize;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!didRemove || aTable->recursionLevel == 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shrink or compress if a quarter or more of all entries are removed, or
|
|
|
|
* if the table is underloaded according to the minimum alpha, and is not
|
|
|
|
* minimal-size already. Do this only if we removed above, so non-removing
|
|
|
|
* enumerations can count on stable aTable->entryStore until the next
|
|
|
|
* non-lookup-Operate or removing-Enumerate.
|
|
|
|
*/
|
|
|
|
if (didRemove &&
|
|
|
|
(aTable->removedCount >= capacity >> 2 ||
|
|
|
|
(capacity > PL_DHASH_MIN_SIZE &&
|
|
|
|
aTable->entryCount <= MinLoad(capacity)))) {
|
|
|
|
METER(aTable->stats.enumShrinks++);
|
|
|
|
capacity = aTable->entryCount;
|
|
|
|
capacity += capacity >> 1;
|
|
|
|
if (capacity < PL_DHASH_MIN_SIZE) {
|
|
|
|
capacity = PL_DHASH_MIN_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ceiling = CeilingLog2(capacity);
|
|
|
|
ceiling -= PL_DHASH_BITS - aTable->hashShift;
|
|
|
|
|
|
|
|
(void) ChangeTable(aTable, ceiling);
|
|
|
|
}
|
2006-04-18 22:30:50 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
DECREMENT_RECURSION_LEVEL(aTable);
|
2006-04-18 22:30:50 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return i;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
|
2011-12-16 02:59:53 +04:00
|
|
|
struct SizeOfEntryExcludingThisArg
|
2011-12-06 02:24:28 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
size_t total;
|
|
|
|
PLDHashSizeOfEntryExcludingThisFun sizeOfEntryExcludingThis;
|
|
|
|
MallocSizeOf mallocSizeOf;
|
|
|
|
void* arg; // the arg passed by the user
|
2011-12-06 02:24:28 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2014-06-27 05:35:39 +04:00
|
|
|
SizeOfEntryExcludingThisEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
|
|
|
|
uint32_t aNumber, void* aArg)
|
2011-12-06 02:24:28 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
SizeOfEntryExcludingThisArg* e = (SizeOfEntryExcludingThisArg*)aArg;
|
|
|
|
e->total += e->sizeOfEntryExcludingThis(aHdr, e->mallocSizeOf, e->arg);
|
|
|
|
return PL_DHASH_NEXT;
|
2011-12-06 02:24:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableSizeOfExcludingThis(
|
|
|
|
const PLDHashTable* aTable,
|
|
|
|
PLDHashSizeOfEntryExcludingThisFun aSizeOfEntryExcludingThis,
|
|
|
|
MallocSizeOf aMallocSizeOf, void* aArg /* = nullptr */)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
n += aMallocSizeOf(aTable->entryStore);
|
|
|
|
if (aSizeOfEntryExcludingThis) {
|
|
|
|
SizeOfEntryExcludingThisArg arg2 = {
|
|
|
|
0, aSizeOfEntryExcludingThis, aMallocSizeOf, aArg
|
|
|
|
};
|
|
|
|
PL_DHashTableEnumerate(const_cast<PLDHashTable*>(aTable),
|
|
|
|
SizeOfEntryExcludingThisEnumerator, &arg2);
|
|
|
|
n += arg2.total;
|
|
|
|
}
|
|
|
|
return n;
|
2011-12-06 02:24:28 +04:00
|
|
|
}
|
|
|
|
|
2011-11-28 07:03:14 +04:00
|
|
|
size_t
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableSizeOfIncludingThis(
|
|
|
|
const PLDHashTable* aTable,
|
|
|
|
PLDHashSizeOfEntryExcludingThisFun aSizeOfEntryExcludingThis,
|
|
|
|
MallocSizeOf aMallocSizeOf, void* aArg /* = nullptr */)
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(aTable) +
|
|
|
|
PL_DHashTableSizeOfExcludingThis(aTable, aSizeOfEntryExcludingThis,
|
|
|
|
aMallocSizeOf, aArg);
|
2011-09-15 04:37:45 +04:00
|
|
|
}
|
|
|
|
|
2009-01-10 19:28:16 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashMarkTableImmutable(PLDHashTable* aTable)
|
2009-01-10 19:28:16 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
aTable->recursionLevel = IMMUTABLE_RECURSION_LEVEL;
|
2009-01-10 19:28:16 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-08-12 00:47:03 +04:00
|
|
|
#ifdef PL_DHASHMETER
|
|
|
|
#include <math.h>
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
PL_DHashTableDumpMeter(PLDHashTable* aTable, PLDHashEnumerator aDump, FILE* aFp)
|
|
|
|
{
|
|
|
|
PLDHashNumber hash1, hash2, maxChainHash1, maxChainHash2;
|
|
|
|
double sqsum, mean, variance, sigma;
|
|
|
|
PLDHashEntryHdr* entry;
|
|
|
|
|
|
|
|
char* entryAddr = aTable->entryStore;
|
|
|
|
uint32_t entrySize = aTable->entrySize;
|
|
|
|
int hashShift = aTable->hashShift;
|
|
|
|
int sizeLog2 = PL_DHASH_BITS - hashShift;
|
|
|
|
uint32_t tableSize = PL_DHASH_TABLE_SIZE(aTable);
|
|
|
|
uint32_t sizeMask = (1u << sizeLog2) - 1;
|
|
|
|
uint32_t chainCount = 0, maxChainLen = 0;
|
|
|
|
hash2 = 0;
|
|
|
|
sqsum = 0;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < tableSize; i++) {
|
|
|
|
entry = (PLDHashEntryHdr*)entryAddr;
|
|
|
|
entryAddr += entrySize;
|
|
|
|
if (!ENTRY_IS_LIVE(entry)) {
|
|
|
|
continue;
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
hash1 = HASH1(entry->keyHash & ~COLLISION_FLAG, hashShift);
|
|
|
|
PLDHashNumber saveHash1 = hash1;
|
|
|
|
PLDHashEntryHdr* probe = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
uint32_t chainLen = 1;
|
|
|
|
if (probe == entry) {
|
|
|
|
/* Start of a (possibly unit-length) chain. */
|
|
|
|
chainCount++;
|
2005-08-12 00:47:03 +04:00
|
|
|
} else {
|
2014-06-27 05:35:39 +04:00
|
|
|
hash2 = HASH2(entry->keyHash & ~COLLISION_FLAG, sizeLog2,
|
|
|
|
hashShift);
|
|
|
|
do {
|
|
|
|
chainLen++;
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
|
|
|
probe = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
} while (probe != entry);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
sqsum += chainLen * chainLen;
|
|
|
|
if (chainLen > maxChainLen) {
|
|
|
|
maxChainLen = chainLen;
|
|
|
|
maxChainHash1 = saveHash1;
|
|
|
|
maxChainHash2 = hash2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t entryCount = aTable->entryCount;
|
|
|
|
if (entryCount && chainCount) {
|
|
|
|
mean = (double)entryCount / chainCount;
|
|
|
|
variance = chainCount * sqsum - entryCount * entryCount;
|
|
|
|
if (variance < 0 || chainCount == 1) {
|
|
|
|
variance = 0;
|
|
|
|
} else {
|
|
|
|
variance /= chainCount * (chainCount - 1);
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
sigma = sqrt(variance);
|
|
|
|
} else {
|
|
|
|
mean = sigma = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(aFp, "Double hashing statistics:\n");
|
|
|
|
fprintf(aFp, " table size (in entries): %u\n", tableSize);
|
|
|
|
fprintf(aFp, " number of entries: %u\n", aTable->entryCount);
|
|
|
|
fprintf(aFp, " number of removed entries: %u\n", aTable->removedCount);
|
|
|
|
fprintf(aFp, " number of searches: %u\n", aTable->stats.searches);
|
|
|
|
fprintf(aFp, " number of hits: %u\n", aTable->stats.hits);
|
|
|
|
fprintf(aFp, " number of misses: %u\n", aTable->stats.misses);
|
|
|
|
fprintf(aFp, " mean steps per search: %g\n",
|
|
|
|
aTable->stats.searches ?
|
|
|
|
(double)aTable->stats.steps / aTable->stats.searches : 0.);
|
|
|
|
fprintf(aFp, " mean hash chain length: %g\n", mean);
|
|
|
|
fprintf(aFp, " standard deviation: %g\n", sigma);
|
|
|
|
fprintf(aFp, " maximum hash chain length: %u\n", maxChainLen);
|
|
|
|
fprintf(aFp, " number of lookups: %u\n", aTable->stats.lookups);
|
|
|
|
fprintf(aFp, " adds that made a new entry: %u\n", aTable->stats.addMisses);
|
|
|
|
fprintf(aFp, "adds that recycled removeds: %u\n", aTable->stats.addOverRemoved);
|
|
|
|
fprintf(aFp, " adds that found an entry: %u\n", aTable->stats.addHits);
|
|
|
|
fprintf(aFp, " add failures: %u\n", aTable->stats.addFailures);
|
|
|
|
fprintf(aFp, " useful removes: %u\n", aTable->stats.removeHits);
|
|
|
|
fprintf(aFp, " useless removes: %u\n", aTable->stats.removeMisses);
|
|
|
|
fprintf(aFp, "removes that freed an entry: %u\n", aTable->stats.removeFrees);
|
|
|
|
fprintf(aFp, " removes while enumerating: %u\n", aTable->stats.removeEnums);
|
|
|
|
fprintf(aFp, " number of grows: %u\n", aTable->stats.grows);
|
|
|
|
fprintf(aFp, " number of shrinks: %u\n", aTable->stats.shrinks);
|
|
|
|
fprintf(aFp, " number of compresses: %u\n", aTable->stats.compresses);
|
|
|
|
fprintf(aFp, "number of enumerate shrinks: %u\n", aTable->stats.enumShrinks);
|
|
|
|
|
|
|
|
if (aDump && maxChainLen && hash2) {
|
|
|
|
fputs("Maximum hash chain:\n", aFp);
|
|
|
|
hash1 = maxChainHash1;
|
|
|
|
hash2 = maxChainHash2;
|
|
|
|
entry = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
uint32_t i = 0;
|
|
|
|
do {
|
|
|
|
if (aDump(aTable, entry, i++, aFp) != PL_DHASH_NEXT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
|
|
|
entry = ADDRESS_ENTRY(aTable, hash1);
|
|
|
|
} while (PL_DHASH_ENTRY_IS_BUSY(entry));
|
|
|
|
}
|
2005-08-12 00:47:03 +04:00
|
|
|
}
|
|
|
|
#endif /* PL_DHASHMETER */
|