2010-02-11 02:37:56 +08:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
* Implementation of (2^1+,2) cuckoo hashing, where 2^1+ indicates that each
|
|
|
|
* hash bucket contains 2^n cells, for n >= 1, and 2 indicates that two hash
|
|
|
|
* functions are employed. The original cuckoo hashing algorithm was described
|
|
|
|
* in:
|
|
|
|
*
|
|
|
|
* Pagh, R., F.F. Rodler (2004) Cuckoo Hashing. Journal of Algorithms
|
|
|
|
* 51(2):122-144.
|
|
|
|
*
|
|
|
|
* Generalization of cuckoo hashing was discussed in:
|
|
|
|
*
|
|
|
|
* Erlingsson, U., M. Manasse, F. McSherry (2006) A cool and practical
|
|
|
|
* alternative to traditional hash tables. In Proceedings of the 7th
|
|
|
|
* Workshop on Distributed Data and Structures (WDAS'06), Santa Clara, CA,
|
|
|
|
* January 2006.
|
|
|
|
*
|
|
|
|
* This implementation uses precisely two hash functions because that is the
|
|
|
|
* fewest that can work, and supporting multiple hashes is an implementation
|
|
|
|
* burden. Here is a reproduction of Figure 1 from Erlingsson et al. (2006)
|
|
|
|
* that shows approximate expected maximum load factors for various
|
|
|
|
* configurations:
|
|
|
|
*
|
|
|
|
* | #cells/bucket |
|
|
|
|
* #hashes | 1 | 2 | 4 | 8 |
|
|
|
|
* --------+-------+-------+-------+-------+
|
|
|
|
* 1 | 0.006 | 0.006 | 0.03 | 0.12 |
|
|
|
|
* 2 | 0.49 | 0.86 |>0.93< |>0.96< |
|
|
|
|
* 3 | 0.91 | 0.97 | 0.98 | 0.999 |
|
|
|
|
* 4 | 0.97 | 0.99 | 0.999 | |
|
|
|
|
*
|
|
|
|
* The number of cells per bucket is chosen such that a bucket fits in one cache
|
|
|
|
* line. So, on 32- and 64-bit systems, we use (8,2) and (4,2) cuckoo hashing,
|
|
|
|
* respectively.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2011-03-19 08:56:14 +08:00
|
|
|
#define JEMALLOC_CKH_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Function prototypes for non-inline static functions. */
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
static bool ckh_grow(tsd_t *tsd, ckh_t *ckh);
|
|
|
|
static void ckh_shrink(tsd_t *tsd, ckh_t *ckh);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search bucket for key and return the cell number if found; SIZE_T_MAX
|
|
|
|
* otherwise.
|
|
|
|
*/
|
2013-12-11 06:23:10 +08:00
|
|
|
JEMALLOC_INLINE_C size_t
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_bucket_search(ckh_t *ckh, size_t bucket, const void *key)
|
|
|
|
{
|
|
|
|
ckhc_t *cell;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
|
|
|
|
cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
|
|
|
|
if (cell->key != NULL && ckh->keycomp(key, cell->key))
|
|
|
|
return ((bucket << LG_CKH_BUCKET_CELLS) + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (SIZE_T_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search table for key and return cell number if found; SIZE_T_MAX otherwise.
|
|
|
|
*/
|
2013-12-11 06:23:10 +08:00
|
|
|
JEMALLOC_INLINE_C size_t
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_isearch(ckh_t *ckh, const void *key)
|
|
|
|
{
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t hashes[2], bucket, cell;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh->hash(key, hashes);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Search primary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
cell = ckh_bucket_search(ckh, bucket, key);
|
|
|
|
if (cell != SIZE_T_MAX)
|
|
|
|
return (cell);
|
|
|
|
|
|
|
|
/* Search secondary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
cell = ckh_bucket_search(ckh, bucket, key);
|
|
|
|
return (cell);
|
|
|
|
}
|
|
|
|
|
2013-12-11 06:23:10 +08:00
|
|
|
JEMALLOC_INLINE_C bool
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,
|
|
|
|
const void *data)
|
|
|
|
{
|
|
|
|
ckhc_t *cell;
|
|
|
|
unsigned offset, i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cycle through the cells in the bucket, starting at a random position.
|
|
|
|
* The randomness avoids worst-case search overhead as buckets fill up.
|
|
|
|
*/
|
2012-03-03 07:59:45 +08:00
|
|
|
prng32(offset, LG_CKH_BUCKET_CELLS, ckh->prng_state, CKH_A, CKH_C);
|
2010-02-11 02:37:56 +08:00
|
|
|
for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
|
|
|
|
cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) +
|
|
|
|
((i + offset) & ((ZU(1) << LG_CKH_BUCKET_CELLS) - 1))];
|
|
|
|
if (cell->key == NULL) {
|
|
|
|
cell->key = key;
|
|
|
|
cell->data = data;
|
|
|
|
ckh->count++;
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No space is available in bucket. Randomly evict an item, then try to find an
|
|
|
|
* alternate location for that item. Iteratively repeat this
|
|
|
|
* eviction/relocation procedure until either success or detection of an
|
|
|
|
* eviction/relocation bucket cycle.
|
|
|
|
*/
|
2013-12-11 06:23:10 +08:00
|
|
|
JEMALLOC_INLINE_C bool
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_evict_reloc_insert(ckh_t *ckh, size_t argbucket, void const **argkey,
|
|
|
|
void const **argdata)
|
|
|
|
{
|
|
|
|
const void *key, *data, *tkey, *tdata;
|
|
|
|
ckhc_t *cell;
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t hashes[2], bucket, tbucket;
|
2010-02-11 02:37:56 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
bucket = argbucket;
|
|
|
|
key = *argkey;
|
|
|
|
data = *argdata;
|
|
|
|
while (true) {
|
|
|
|
/*
|
|
|
|
* Choose a random item within the bucket to evict. This is
|
|
|
|
* critical to correct function, because without (eventually)
|
|
|
|
* evicting all items within a bucket during iteration, it
|
|
|
|
* would be possible to get stuck in an infinite loop if there
|
|
|
|
* were an item for which both hashes indicated the same
|
|
|
|
* bucket.
|
|
|
|
*/
|
2012-03-03 07:59:45 +08:00
|
|
|
prng32(i, LG_CKH_BUCKET_CELLS, ckh->prng_state, CKH_A, CKH_C);
|
2010-02-11 02:37:56 +08:00
|
|
|
cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
|
|
|
|
assert(cell->key != NULL);
|
|
|
|
|
|
|
|
/* Swap cell->{key,data} and {key,data} (evict). */
|
|
|
|
tkey = cell->key; tdata = cell->data;
|
|
|
|
cell->key = key; cell->data = data;
|
|
|
|
key = tkey; data = tdata;
|
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->nrelocs++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Find the alternate bucket for the evicted item. */
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh->hash(key, hashes);
|
|
|
|
tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
if (tbucket == bucket) {
|
2013-01-23 04:02:08 +08:00
|
|
|
tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)
|
|
|
|
- 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
/*
|
|
|
|
* It may be that (tbucket == bucket) still, if the
|
|
|
|
* item's hashes both indicate this bucket. However,
|
|
|
|
* we are guaranteed to eventually escape this bucket
|
|
|
|
* during iteration, assuming pseudo-random item
|
|
|
|
* selection (true randomness would make infinite
|
|
|
|
* looping a remote possibility). The reason we can
|
|
|
|
* never get trapped forever is that there are two
|
|
|
|
* cases:
|
|
|
|
*
|
|
|
|
* 1) This bucket == argbucket, so we will quickly
|
|
|
|
* detect an eviction cycle and terminate.
|
|
|
|
* 2) An item was evicted to this bucket from another,
|
|
|
|
* which means that at least one item in this bucket
|
|
|
|
* has hashes that indicate distinct buckets.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
/* Check for a cycle. */
|
|
|
|
if (tbucket == argbucket) {
|
|
|
|
*argkey = key;
|
|
|
|
*argdata = data;
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket = tbucket;
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!ckh_try_bucket_insert(ckh, bucket, key, data))
|
2010-02-11 02:37:56 +08:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 06:23:10 +08:00
|
|
|
JEMALLOC_INLINE_C bool
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata)
|
|
|
|
{
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t hashes[2], bucket;
|
2010-02-11 02:37:56 +08:00
|
|
|
const void *key = *argkey;
|
|
|
|
const void *data = *argdata;
|
|
|
|
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh->hash(key, hashes);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Try to insert in primary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!ckh_try_bucket_insert(ckh, bucket, key, data))
|
2010-02-11 02:37:56 +08:00
|
|
|
return (false);
|
|
|
|
|
|
|
|
/* Try to insert in secondary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!ckh_try_bucket_insert(ckh, bucket, key, data))
|
2010-02-11 02:37:56 +08:00
|
|
|
return (false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to find a place for this item via iterative eviction/relocation.
|
|
|
|
*/
|
|
|
|
return (ckh_evict_reloc_insert(ckh, bucket, argkey, argdata));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to rebuild the hash table from scratch by inserting all items from the
|
|
|
|
* old table into the new.
|
|
|
|
*/
|
2013-12-11 06:23:10 +08:00
|
|
|
JEMALLOC_INLINE_C bool
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_rebuild(ckh_t *ckh, ckhc_t *aTab)
|
|
|
|
{
|
|
|
|
size_t count, i, nins;
|
|
|
|
const void *key, *data;
|
|
|
|
|
|
|
|
count = ckh->count;
|
|
|
|
ckh->count = 0;
|
|
|
|
for (i = nins = 0; nins < count; i++) {
|
|
|
|
if (aTab[i].key != NULL) {
|
|
|
|
key = aTab[i].key;
|
|
|
|
data = aTab[i].data;
|
|
|
|
if (ckh_try_insert(ckh, &key, &data)) {
|
|
|
|
ckh->count = count;
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
nins++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_grow(tsd_t *tsd, ckh_t *ckh)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
ckhc_t *tab, *ttab;
|
|
|
|
size_t lg_curcells;
|
|
|
|
unsigned lg_prevbuckets;
|
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->ngrows++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible (though unlikely, given well behaved hashes) that the
|
|
|
|
* table will have to be doubled more than once in order to create a
|
|
|
|
* usable table.
|
|
|
|
*/
|
|
|
|
lg_prevbuckets = ckh->lg_curbuckets;
|
|
|
|
lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;
|
|
|
|
while (true) {
|
2011-03-23 15:37:29 +08:00
|
|
|
size_t usize;
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
lg_curcells++;
|
2012-04-12 09:13:45 +08:00
|
|
|
usize = sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
|
2011-03-23 15:37:29 +08:00
|
|
|
if (usize == 0) {
|
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2011-03-23 15:37:29 +08:00
|
|
|
}
|
2015-02-12 16:12:44 +08:00
|
|
|
tab = (ckhc_t *)ipallocztm(tsd, usize, CACHELINE, true, NULL,
|
|
|
|
true, NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
if (tab == NULL) {
|
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
/* Swap in new table. */
|
|
|
|
ttab = ckh->tab;
|
|
|
|
ckh->tab = tab;
|
|
|
|
tab = ttab;
|
|
|
|
ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!ckh_rebuild(ckh, tab)) {
|
2015-02-12 16:12:44 +08:00
|
|
|
idalloctm(tsd, tab, tcache_get(tsd, false), true);
|
2010-02-11 02:37:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rebuilding failed, so back out partially rebuilt table. */
|
2015-02-12 16:12:44 +08:00
|
|
|
idalloctm(tsd, ckh->tab, tcache_get(tsd, false), true);
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->tab = tab;
|
|
|
|
ckh->lg_curbuckets = lg_prevbuckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = false;
|
2012-04-11 06:07:44 +08:00
|
|
|
label_return:
|
2010-02-11 02:37:56 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_shrink(tsd_t *tsd, ckh_t *ckh)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
ckhc_t *tab, *ttab;
|
2011-03-23 15:37:29 +08:00
|
|
|
size_t lg_curcells, usize;
|
2010-02-11 02:37:56 +08:00
|
|
|
unsigned lg_prevbuckets;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible (though unlikely, given well behaved hashes) that the
|
|
|
|
* table rebuild will fail.
|
|
|
|
*/
|
|
|
|
lg_prevbuckets = ckh->lg_curbuckets;
|
|
|
|
lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;
|
2012-04-12 09:13:45 +08:00
|
|
|
usize = sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
|
2011-03-23 15:37:29 +08:00
|
|
|
if (usize == 0)
|
|
|
|
return;
|
2015-02-12 16:12:44 +08:00
|
|
|
tab = (ckhc_t *)ipallocztm(tsd, usize, CACHELINE, true, NULL, true,
|
|
|
|
NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
if (tab == NULL) {
|
|
|
|
/*
|
|
|
|
* An OOM error isn't worth propagating, since it doesn't
|
|
|
|
* prevent this or future operations from proceeding.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Swap in new table. */
|
|
|
|
ttab = ckh->tab;
|
|
|
|
ckh->tab = tab;
|
|
|
|
tab = ttab;
|
|
|
|
ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!ckh_rebuild(ckh, tab)) {
|
2015-02-12 16:12:44 +08:00
|
|
|
idalloctm(tsd, tab, tcache_get(tsd, false), true);
|
2010-02-11 02:37:56 +08:00
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->nshrinks++;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rebuilding failed, so back out partially rebuilt table. */
|
2015-02-12 16:12:44 +08:00
|
|
|
idalloctm(tsd, ckh->tab, tcache_get(tsd, false), true);
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->tab = tab;
|
|
|
|
ckh->lg_curbuckets = lg_prevbuckets;
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->nshrinkfails++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,
|
|
|
|
ckh_keycomp_t *keycomp)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
bool ret;
|
2011-03-23 15:37:29 +08:00
|
|
|
size_t mincells, usize;
|
2010-02-11 02:37:56 +08:00
|
|
|
unsigned lg_mincells;
|
|
|
|
|
|
|
|
assert(minitems > 0);
|
|
|
|
assert(hash != NULL);
|
|
|
|
assert(keycomp != NULL);
|
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->ngrows = 0;
|
|
|
|
ckh->nshrinks = 0;
|
|
|
|
ckh->nshrinkfails = 0;
|
|
|
|
ckh->ninserts = 0;
|
|
|
|
ckh->nrelocs = 0;
|
|
|
|
#endif
|
2012-03-03 07:59:45 +08:00
|
|
|
ckh->prng_state = 42; /* Value doesn't really matter. */
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->count = 0;
|
|
|
|
|
|
|
|
/*
|
2014-12-09 06:40:14 +08:00
|
|
|
* Find the minimum power of 2 that is large enough to fit minitems
|
2010-02-11 02:37:56 +08:00
|
|
|
* entries. We are using (2+,2) cuckoo hashing, which has an expected
|
|
|
|
* maximum load factor of at least ~0.86, so 0.75 is a conservative load
|
2014-12-09 06:40:14 +08:00
|
|
|
* factor that will typically allow mincells items to fit without ever
|
2010-02-11 02:37:56 +08:00
|
|
|
* growing the table.
|
|
|
|
*/
|
|
|
|
assert(LG_CKH_BUCKET_CELLS > 0);
|
|
|
|
mincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;
|
|
|
|
for (lg_mincells = LG_CKH_BUCKET_CELLS;
|
|
|
|
(ZU(1) << lg_mincells) < mincells;
|
|
|
|
lg_mincells++)
|
|
|
|
; /* Do nothing. */
|
|
|
|
ckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
|
|
|
|
ckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
|
|
|
|
ckh->hash = hash;
|
|
|
|
ckh->keycomp = keycomp;
|
|
|
|
|
2012-04-12 09:13:45 +08:00
|
|
|
usize = sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);
|
2011-03-23 15:37:29 +08:00
|
|
|
if (usize == 0) {
|
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2011-03-23 15:37:29 +08:00
|
|
|
}
|
2015-02-12 16:12:44 +08:00
|
|
|
ckh->tab = (ckhc_t *)ipallocztm(tsd, usize, CACHELINE, true, NULL, true,
|
|
|
|
NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
if (ckh->tab == NULL) {
|
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = false;
|
2012-04-11 06:07:44 +08:00
|
|
|
label_return:
|
2010-02-11 02:37:56 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_delete(tsd_t *tsd, ckh_t *ckh)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
|
|
|
#ifdef CKH_VERBOSE
|
|
|
|
malloc_printf(
|
|
|
|
"%s(%p): ngrows: %"PRIu64", nshrinks: %"PRIu64","
|
|
|
|
" nshrinkfails: %"PRIu64", ninserts: %"PRIu64","
|
|
|
|
" nrelocs: %"PRIu64"\n", __func__, ckh,
|
|
|
|
(unsigned long long)ckh->ngrows,
|
|
|
|
(unsigned long long)ckh->nshrinks,
|
|
|
|
(unsigned long long)ckh->nshrinkfails,
|
|
|
|
(unsigned long long)ckh->ninserts,
|
|
|
|
(unsigned long long)ckh->nrelocs);
|
|
|
|
#endif
|
|
|
|
|
2015-02-12 16:12:44 +08:00
|
|
|
idalloctm(tsd, ckh->tab, tcache_get(tsd, false), true);
|
2013-01-23 04:14:45 +08:00
|
|
|
if (config_debug)
|
|
|
|
memset(ckh, 0x5a, sizeof(ckh_t));
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ckh_count(ckh_t *ckh)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
|
|
|
return (ckh->count);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data)
|
|
|
|
{
|
|
|
|
size_t i, ncells;
|
|
|
|
|
|
|
|
for (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +
|
|
|
|
LG_CKH_BUCKET_CELLS)); i < ncells; i++) {
|
|
|
|
if (ckh->tab[i].key != NULL) {
|
|
|
|
if (key != NULL)
|
|
|
|
*key = (void *)ckh->tab[i].key;
|
|
|
|
if (data != NULL)
|
|
|
|
*data = (void *)ckh->tab[i].data;
|
|
|
|
*tabind = i + 1;
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
assert(ckh_search(ckh, key, NULL, NULL));
|
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->ninserts++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (ckh_try_insert(ckh, &key, &data)) {
|
2014-09-23 12:09:23 +08:00
|
|
|
if (ckh_grow(tsd, ckh)) {
|
2010-02-11 02:37:56 +08:00
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = false;
|
2012-04-11 06:07:44 +08:00
|
|
|
label_return:
|
2010-02-11 02:37:56 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,
|
|
|
|
void **data)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
size_t cell;
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
|
|
|
cell = ckh_isearch(ckh, searchkey);
|
|
|
|
if (cell != SIZE_T_MAX) {
|
|
|
|
if (key != NULL)
|
|
|
|
*key = (void *)ckh->tab[cell].key;
|
|
|
|
if (data != NULL)
|
|
|
|
*data = (void *)ckh->tab[cell].data;
|
|
|
|
ckh->tab[cell].key = NULL;
|
|
|
|
ckh->tab[cell].data = NULL; /* Not necessary. */
|
|
|
|
|
|
|
|
ckh->count--;
|
|
|
|
/* Try to halve the table if it is less than 1/4 full. */
|
|
|
|
if (ckh->count < (ZU(1) << (ckh->lg_curbuckets
|
|
|
|
+ LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets
|
|
|
|
> ckh->lg_minbuckets) {
|
|
|
|
/* Ignore error due to OOM. */
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_shrink(tsd, ckh);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data)
|
|
|
|
{
|
|
|
|
size_t cell;
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
|
|
|
cell = ckh_isearch(ckh, searchkey);
|
|
|
|
if (cell != SIZE_T_MAX) {
|
|
|
|
if (key != NULL)
|
|
|
|
*key = (void *)ckh->tab[cell].key;
|
|
|
|
if (data != NULL)
|
|
|
|
*data = (void *)ckh->tab[cell].data;
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh_string_hash(const void *key, size_t r_hash[2])
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2013-01-23 04:02:08 +08:00
|
|
|
hash(key, strlen((const char *)key), 0x94122f33U, r_hash);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ckh_string_keycomp(const void *k1, const void *k2)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(k1 != NULL);
|
|
|
|
assert(k2 != NULL);
|
|
|
|
|
|
|
|
return (strcmp((char *)k1, (char *)k2) ? false : true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh_pointer_hash(const void *key, size_t r_hash[2])
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2010-09-21 10:20:48 +08:00
|
|
|
union {
|
|
|
|
const void *v;
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t i;
|
2010-09-21 10:20:48 +08:00
|
|
|
} u;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-09-21 10:20:48 +08:00
|
|
|
assert(sizeof(u.v) == sizeof(u.i));
|
|
|
|
u.v = key;
|
2013-01-23 04:02:08 +08:00
|
|
|
hash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ckh_pointer_keycomp(const void *k1, const void *k2)
|
|
|
|
{
|
|
|
|
|
|
|
|
return ((k1 == k2) ? true : false);
|
|
|
|
}
|