typedef struct { unsigned long fullhash; void *key; void *data; int keylen; int datalen; } mm_hash_elt; typedef struct { int nelts; int nfree; int own_data; mm_hash_elt *elts; /* interface to allocation machinery */ void *alloc_cookie; void *(*alloc_func)(void *cookie, int size); void (*free_func)(void *cookie, void *mem); } mm_hash_table; typedef struct { int start_idx; int next_idx; } mm_hash_iter; /* Creating and destroying hash tables. * mm_make_hash_table returns NULL if it cannot allocate the thing. * The own_data flag indicates whether the table machinery copies * values into the heap managed by the alloc_func and free_func, * or not. */ mm_hash_table *mm_hash_create (int initial_nelts, int own_data, void *alloc_cookie, void *(*alloc_func)(void *cookie, int size), void (*free_func)(void *cookie, void *mem)); void mm_hash_clear (mm_hash_table *); void mm_hash_destroy (mm_hash_table *); int mm_hash_empty (mm_hash_table *); /* Manipulating the elements. * * return codes for set and delete are false on error, which can only * be due to memory allocation difficulties. * * last arg to mm_hash_table_get governs creation --- if NULL, it will * not create an element; if an int*, it is set to one if a new element * was created. */ mm_hash_elt *mm_hash_get (mm_hash_table *t, void *key, int keylen, int* created_p); int mm_hash_elt_set (mm_hash_table *t, mm_hash_elt *elt, void *data, int datalen); int mm_hash_elt_delete (mm_hash_table *t, mm_hash_elt *elt); /* Iteration. mm_hash_iter_next returns NULL when it's over. * During an iteration, you can delete elements or replace their values, * but the consequences of inserting a new element are unpredictable. * (It will still terminate, but you may find yourself revisiting keys * that you've already seen, or missing some that you haven't). */ void mm_hash_iter_init (mm_hash_table *, mm_hash_iter *); mm_hash_elt *mm_hash_iter_next (mm_hash_table *, mm_hash_iter *); /* *Actual* minimum number of elements. If we go lower than this, * weird things happen with the rehash threshold arithmetic */ #define MM_HASH_REAL_MIN_NELTS 8 /* Inverse of the minimum permissible free space ratio --- if this * is four, then we don't let tables have less than one element in * four empty */ #define MM_HASH_LOAD_LIMIT 4