diff options
Diffstat (limited to 'common/hash.h')
-rw-r--r-- | common/hash.h | 52 |
1 files changed, 41 insertions, 11 deletions
diff --git a/common/hash.h b/common/hash.h index 9e8174c..2b22b64 100644 --- a/common/hash.h +++ b/common/hash.h @@ -22,7 +22,7 @@ #define __HASH_H__ /* - * Options to define. You need to build both this file and + * Features to define. You need to build both this file and * the corresponding hash.c file with whatever options you set here */ @@ -32,6 +32,9 @@ /* Keep key values internally */ #define HASH_COPYKEYS 1 +/* Hash callback functionality */ +#define HASH_CALLBACKS 1 + #ifdef __cplusplus extern "C" { @@ -49,16 +52,31 @@ typedef struct hash_t hash_t; /* Abstract type for scanning hash tables. */ typedef struct hash_index_t hash_index_t; +/* A callback during remove operations */ +#ifdef HASH_CALLBACKS + typedef void (*hash_free_val_t)(void* arg, void* val); +#endif + /* Create a hash table */ -#ifdef HASH_COPYKEYS - hash_t* hash_create(size_t klen); +#ifdef HASH_CALLBACKS + #ifdef HASH_COPYKEYS + hash_t* hash_create(size_t klen, hash_free_val_t f_free, void* arg); + #else + hash_t* hash_create(hash_free_val_t f_free, void* arg); + #endif #else - hash_t* hash_create(); -#endif + #ifdef HASH_COPYKEYS + hash_t* hash_create(size_t klen); + #else + hash_t* hash_create(); + #endif +#endif /* HASH_CALLBACKS */ + /* To release all resources for a hash table */ void hash_free(hash_t* ht); + /** * Associate a value with a key in a hash table. * @@ -70,11 +88,12 @@ void hash_free(hash_t* ht); * val must not be null */ #ifdef HASH_COPYKEYS - int hash_set(hash_t* ht, const void* key, const void* val); + int hash_set(hash_t* ht, const void* key, void* val); #else - int hash_set(hash_t* ht, const void* key, size_t klen, const void* val); + int hash_set(hash_t* ht, const void* key, size_t klen, void* val); #endif + /** * Remove a value and key form the hash table * @@ -83,11 +102,12 @@ void hash_free(hash_t* ht); * klen Length of the key. Can be HASH_KEY_STRING to use the string length */ #ifdef HASH_COPYKEYS - void hash_rem(hash_t* ht, const void* key); + void* hash_rem(hash_t* ht, const void* key); #else - void hash_rem(hash_t* ht, const void* key, size_t klen); + void* hash_rem(hash_t* ht, const void* key, size_t klen); #endif + /** * Look up the value associated with a key in a hash table. * @@ -103,6 +123,7 @@ void hash_free(hash_t* ht); void* hash_get(hash_t* ht, const void* key, size_t klen); #endif + /** * Start iterating over the entries in a hash table. * @@ -114,6 +135,7 @@ void hash_free(hash_t* ht); */ hash_index_t* hash_first(hash_t* ht); + /** * Continue iterating over the entries in a hash table. * @@ -124,6 +146,7 @@ hash_index_t* hash_first(hash_t* ht); */ hash_index_t* hash_next(hash_index_t* hi); + /** * Get the current entry's details from the iteration state. * @@ -141,19 +164,26 @@ hash_index_t* hash_next(hash_index_t* hi); void* hash_this(hash_index_t* hi, const void** key, size_t* klen); #endif + /** * Purge entries before a certain timestamp */ #ifdef HASH_TIMESTAMP + int hash_purge(hash_t* ht, time_t stamp); #ifdef HASH_COPYKEYS - void hash_touch(hash_index_t* hi, const void** key); + void hash_touch(hash_t* ht, const void* key); #else - void hash_touch(hash_index_t* hi, const void** key, size_t* klen); + void hash_touch(hash_t* ht, const void* key, size_t* klen); #endif + +/* Bumps the oldest out */ +int hash_bump(hash_t* ht); + #endif + /** * Get the number of key/value pairs in the hash table. * |