|
struct mhash_table * | mhash_table_new (int size) |
| Create an empty hash table. More...
|
|
void | mhash_table_free (struct mhash_table *ht) |
| Free a hash table. More...
|
|
void * | mhash_table_lookup (const struct mhash_table *ht, const char *key, size_t key_len) |
| Retrieve an entry. More...
|
|
void * | mhash_table_lookupv (const struct mhash_table *ht, const struct iovec *keyv, int keyvcnt) |
| Retrieve an entry (vector key). More...
|
|
void * | mhash_table_update (struct mhash_table *ht, const char *key, size_t key_len, void *value) |
| Add, update or remove an entry. More...
|
|
void * | mhash_table_updatev (struct mhash_table *ht, const struct iovec *keyv, int keyvcnt, void *value) |
| Add, update or remove an entry (vector key). More...
|
|
void * | mhash_table_for_each (struct mhash_table *ht, void *(*func)(const char *key, size_t key_len, void *value)) |
| Iterate through all entries. More...
|
|
void * | mhash_table_for_each2 (struct mhash_table *ht, void *(*func)(const char *key, size_t key_len, void *value, void *user), void *user) |
| Iterate through all entries. More...
|
|
int | mhash_table_delete_if (struct mhash_table *ht, int(*pred)(const char *key, size_t key_len, void *value, void *user), void *user) |
| Delete all entries matching predicate. More...
|
|
void | mhash_table_debug_dump (const struct mhash_table *ht, struct membuf_t *buf) |
| Debugging: dump contents of hash table to memory buffer. More...
|
|
A generic hash table implementation. The key of the hash table is a single block of memory. The hash table is automatically resized when necessary, in order to reduce hash collisions and keep lookup operations as O(1). The maximum number of bins is 2^31, though there is no limit to the number of entries.
◆ mhash_table_new()
struct mhash_table* mhash_table_new |
( |
int |
size | ) |
|
Create an empty hash table.
- Parameters
-
size | Initial guess at size of hash table. May be 0. |
- Returns
- Pointer to newly-allocated opaque hash table object.
Creates and initialises an empty hash table object. The initial number of bins to allocate may be specified in size, which is the base-2 logarithm of the total number (i.e. passing size as 4 gets you 16 bins). Passing anything less than 4 will still allocate 16 bins.
◆ mhash_table_free()
void mhash_table_free |
( |
struct mhash_table * |
ht | ) |
|
Free a hash table.
- Parameters
-
Frees a hash table, but does not alter the objects pointed to by the table. In order to free memory associated with entries, call mhash_table_for_each(). If ht is 0, no action is taken.
◆ mhash_table_lookup()
void* mhash_table_lookup |
( |
const struct mhash_table * |
ht, |
|
|
const char * |
key, |
|
|
size_t |
key_len |
|
) |
| |
Retrieve an entry.
- Parameters
-
ht | Hash table. |
key | Key of entry to look up. |
key_len | Length of key, in bytes. |
- Returns
- Value associated with key.
- Return values
-
0 | if there is no associated value. |
Retrieves the value associated with the entry with key key. If there is no such entry, then 0 is returned.
◆ mhash_table_lookupv()
void* mhash_table_lookupv |
( |
const struct mhash_table * |
ht, |
|
|
const struct iovec * |
keyv, |
|
|
int |
keyvcnt |
|
) |
| |
Retrieve an entry (vector key).
- Parameters
-
ht | Hash table. |
keyv | Pointer to array of memory regions which comprise the key. |
keyvcnt | Number of entries in keyv. |
- Returns
- Value associated with key.
- Return values
-
0 | if there is no associated value. |
Retrieves the value associated with the entry with key keyv. If there is no such entry, then 0 is returned.
The key is a set of bytes which may be discontiguous in memory. 1 or more regions of memory are pointed to by keyv, and the key is the sequence of bytes that would be encountered by stepping byte-by-byte through each region in turn. Note in particular that the keys "a","bc" and "ab","c" would be considered identical.
◆ mhash_table_update()
void* mhash_table_update |
( |
struct mhash_table * |
ht, |
|
|
const char * |
key, |
|
|
size_t |
key_len, |
|
|
void * |
value |
|
) |
| |
Add, update or remove an entry.
- Parameters
-
ht | Hash table. |
key | Key of entry. |
key_len | Length of key, in bytes. |
value | Value to store. 0 to delete. |
- Returns
- Previous value.
- Return values
-
0 | if there was no previous value. |
Associates the value value with an entry with key key. If key had a previous value associated with it, this previous value is returned. Passing a value of 0 will clear the entry, freeing the memory associated with the entry. If key had no previous value, 0 will be returned, and the new value will be stored (or no action will be taken if value is 0).
Note that key is copied when creating a new entry, so the memory it points to need not persist past the function call.
◆ mhash_table_updatev()
void* mhash_table_updatev |
( |
struct mhash_table * |
ht, |
|
|
const struct iovec * |
keyv, |
|
|
int |
keyvcnt, |
|
|
void * |
value |
|
) |
| |
Add, update or remove an entry (vector key).
- Parameters
-
ht | Hash table. |
keyv | Pointer to array of memory regions which comprise the key. |
keyvcnt | Number of entries in keyv. |
value | Value to store. 0 to delete. |
- Returns
- Previous value.
- Return values
-
0 | if there was no previous value. |
Associates the value value with an entry with key keyv. If keyv had a previous value associated with it, this previous value is returned. Passing a value of 0 will clear the entry, freeing the memory associated with the entry. If keyv had no previous value, 0 will be returned, and the new value will be stored (or no action will be taken if value is 0).
The key is a set of bytes which may be discontiguous in memory. 1 or more regions of memory are pointed to by keyv, and the key is the sequence of bytes that would be encountered by stepping byte-by-byte through each region in turn. Note in particular that the keys "a","bc" and "ab","c" would be considered identical. The memory regions comprising the key are copied internally (forming a single contiguous memory region) when a new entry is created.
◆ mhash_table_for_each()
void* mhash_table_for_each |
( |
struct mhash_table * |
ht, |
|
|
void *(*)(const char *key, size_t key_len, void *value) |
func |
|
) |
| |
Iterate through all entries.
- Parameters
-
ht | Hash table. |
func | Callback function for each entry. |
- Return values
-
0 | if func always returned 0. |
- Returns
- First non-zero value returned from func otherwise.
Iterates through each entry in the hash table ht and calls func for each. func takes three arguments: the key and its length, and the value. If func ever returns non-zero, the iteration is aborted and the value returned from func is returned.
◆ mhash_table_for_each2()
void* mhash_table_for_each2 |
( |
struct mhash_table * |
ht, |
|
|
void *(*)(const char *key, size_t key_len, void *value, void *user) |
func, |
|
|
void * |
user |
|
) |
| |
Iterate through all entries.
- Parameters
-
ht | Hash table. |
func | Callback function for each entry. |
user | Opaque user pointer to pass to callback. |
- Return values
-
0 | if func always returned 0. |
- Returns
- First non-zero value returned from func otherwise.
Iterates through each entry in the hash table ht and calls func for each. func takes four arguments: the key and its length, the value and an opaque pointer user. If func ever returns non-zero, the iteration is aborted and the value returned from func is returned.
◆ mhash_table_delete_if()
int mhash_table_delete_if |
( |
struct mhash_table * |
ht, |
|
|
int(*)(const char *key, size_t key_len, void *value, void *user) |
pred, |
|
|
void * |
user |
|
) |
| |
Delete all entries matching predicate.
- Parameters
-
ht | Hash table. |
pred | Predicate function (returning 0 to keep or non-0 to delete). |
user | Opaque pointer to pass to callbacks (may be 0). |
- Returns
- Number of entries removed.
For each entry in the hash table ht, this function calls pred. This callback is similar to that of mhash_table_for_each(), except that it returns an integer, with 0 meaning ‘keep element’ and non-0 meaning ‘delete element’.
If any cleanup of the element needs to be done (e.g. freeing of memory), this can be done in pred before it returns. Other hash table functions, with the exception of lookups, should not be called on ht from within the callback.
◆ mhash_table_debug_dump()
void mhash_table_debug_dump |
( |
const struct mhash_table * |
ht, |
|
|
struct membuf_t * |
buf |
|
) |
| |
Debugging: dump contents of hash table to memory buffer.
- Parameters
-
ht | Hash table. |
buf | Memory buffer to dump to. |
A debugging function which will dump the contents of the hash table ht (i.e. its keys and the pointer value associated with them) to the memory buffer buf. The dump is in human-readable form and will consist only of printable characters. It is terminated with a newline.