Blame jemalloc/src/ckh.c

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