Blame jemalloc/include/jemalloc/internal/ckh.h

Packit 345191
#ifndef JEMALLOC_INTERNAL_CKH_H
Packit 345191
#define JEMALLOC_INTERNAL_CKH_H
Packit 345191
Packit 345191
#include "jemalloc/internal/tsd.h"
Packit 345191
Packit 345191
/* Cuckoo hashing implementation.  Skip to the end for the interface. */
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
/* INTERNAL DEFINITIONS -- IGNORE */
Packit 345191
/******************************************************************************/
Packit 345191
Packit 345191
/* Maintain counters used to get an idea of performance. */
Packit 345191
/* #define CKH_COUNT */
Packit 345191
/* Print counter values in ckh_delete() (requires CKH_COUNT). */
Packit 345191
/* #define CKH_VERBOSE */
Packit 345191
Packit 345191
/*
Packit 345191
 * There are 2^LG_CKH_BUCKET_CELLS cells in each hash table bucket.  Try to fit
Packit 345191
 * one bucket per L1 cache line.
Packit 345191
 */
Packit 345191
#define LG_CKH_BUCKET_CELLS (LG_CACHELINE - LG_SIZEOF_PTR - 1)
Packit 345191
Packit 345191
/* Typedefs to allow easy function pointer passing. */
Packit 345191
typedef void ckh_hash_t (const void *, size_t[2]);
Packit 345191
typedef bool ckh_keycomp_t (const void *, const void *);
Packit 345191
Packit 345191
/* Hash table cell. */
Packit 345191
typedef struct {
Packit 345191
	const void *key;
Packit 345191
	const void *data;
Packit 345191
} ckhc_t;
Packit 345191
Packit 345191
/* The hash table itself. */
Packit 345191
typedef struct {
Packit 345191
#ifdef CKH_COUNT
Packit 345191
	/* Counters used to get an idea of performance. */
Packit 345191
	uint64_t ngrows;
Packit 345191
	uint64_t nshrinks;
Packit 345191
	uint64_t nshrinkfails;
Packit 345191
	uint64_t ninserts;
Packit 345191
	uint64_t nrelocs;
Packit 345191
#endif
Packit 345191
Packit 345191
	/* Used for pseudo-random number generation. */
Packit 345191
	uint64_t prng_state;
Packit 345191
Packit 345191
	/* Total number of items. */
Packit 345191
	size_t count;
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Minimum and current number of hash table buckets.  There are
Packit 345191
	 * 2^LG_CKH_BUCKET_CELLS cells per bucket.
Packit 345191
	 */
Packit 345191
	unsigned lg_minbuckets;
Packit 345191
	unsigned lg_curbuckets;
Packit 345191
Packit 345191
	/* Hash and comparison functions. */
Packit 345191
	ckh_hash_t *hash;
Packit 345191
	ckh_keycomp_t *keycomp;
Packit 345191
Packit 345191
	/* Hash table with 2^lg_curbuckets buckets. */
Packit 345191
	ckhc_t *tab;
Packit 345191
} ckh_t;
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
/* BEGIN PUBLIC API */
Packit 345191
/******************************************************************************/
Packit 345191
Packit 345191
/* Lifetime management.  Minitems is the initial capacity. */
Packit 345191
bool ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,
Packit 345191
    ckh_keycomp_t *keycomp);
Packit 345191
void ckh_delete(tsd_t *tsd, ckh_t *ckh);
Packit 345191
Packit 345191
/* Get the number of elements in the set. */
Packit 345191
size_t ckh_count(ckh_t *ckh);
Packit 345191
Packit 345191
/*
Packit 345191
 * To iterate over the elements in the table, initialize *tabind to 0 and call
Packit 345191
 * this function until it returns true.  Each call that returns false will
Packit 345191
 * update *key and *data to the next element in the table, assuming the pointers
Packit 345191
 * are non-NULL.
Packit 345191
 */
Packit 345191
bool ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data);
Packit 345191
Packit 345191
/*
Packit 345191
 * Basic hash table operations -- insert, removal, lookup.  For ckh_remove and
Packit 345191
 * ckh_search, key or data can be NULL.  The hash-table only stores pointers to
Packit 345191
 * the key and value, and doesn't do any lifetime management.
Packit 345191
 */
Packit 345191
bool ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data);
Packit 345191
bool ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,
Packit 345191
    void **data);
Packit 345191
bool ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data);
Packit 345191
Packit 345191
/* Some useful hash and comparison functions for strings and pointers. */
Packit 345191
void ckh_string_hash(const void *key, size_t r_hash[2]);
Packit 345191
bool ckh_string_keycomp(const void *k1, const void *k2);
Packit 345191
void ckh_pointer_hash(const void *key, size_t r_hash[2]);
Packit 345191
bool ckh_pointer_keycomp(const void *k1, const void *k2);
Packit 345191
Packit 345191
#endif /* JEMALLOC_INTERNAL_CKH_H */