aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil/hash.c')
-rw-r--r--src/libutil/hash.c167
1 files changed, 103 insertions, 64 deletions
diff --git a/src/libutil/hash.c b/src/libutil/hash.c
index 3bb381651..0e8b1646e 100644
--- a/src/libutil/hash.c
+++ b/src/libutil/hash.c
@@ -33,11 +33,13 @@
* will use this function internally.
*/
static inline struct rspamd_hash_node **
-rspamd_hash_lookup_node (rspamd_hash_t * hash, gconstpointer key, guint * hash_return)
+rspamd_hash_lookup_node (rspamd_hash_t * hash,
+ gconstpointer key,
+ guint * hash_return)
{
- struct rspamd_hash_node **node_ptr, *node;
- guint hash_value;
- hash_value = (*hash->hash_func) (key);
+ struct rspamd_hash_node **node_ptr, *node;
+ guint hash_value;
+ hash_value = (*hash->hash_func)(key);
if (hash->shared) {
rspamd_mempool_rlock_rwlock (hash->lock);
@@ -58,7 +60,8 @@ rspamd_hash_lookup_node (rspamd_hash_t * hash, gconstpointer key, guint * hash_r
*/
if (hash->key_equal_func) {
while ((node = *node_ptr)) {
- if (node->key_hash == hash_value && hash->key_equal_func (node->key, key)) {
+ if (node->key_hash == hash_value &&
+ hash->key_equal_func (node->key, key)) {
break;
}
node_ptr = &(*node_ptr)->next;
@@ -83,9 +86,10 @@ rspamd_hash_lookup_node (rspamd_hash_t * hash, gconstpointer key, guint * hash_r
* No table resize is performed.
*/
static void
-rspamd_hash_remove_node (rspamd_hash_t * hash, struct rspamd_hash_node ***node_ptr_ptr)
+rspamd_hash_remove_node (rspamd_hash_t * hash,
+ struct rspamd_hash_node ***node_ptr_ptr)
{
- struct rspamd_hash_node **node_ptr, *node;
+ struct rspamd_hash_node **node_ptr, *node;
if (hash->shared) {
rspamd_mempool_wlock_rwlock (hash->lock);
@@ -108,19 +112,23 @@ rspamd_hash_remove_node (rspamd_hash_t * hash, struct rspamd_hash_node ***node_p
static void
rspamd_hash_resize (rspamd_hash_t * hash)
{
- struct rspamd_hash_node **new_nodes;
- struct rspamd_hash_node *node, *next;
- guint hash_val;
- gint new_size, i;
+ struct rspamd_hash_node **new_nodes;
+ struct rspamd_hash_node *node, *next;
+ guint hash_val;
+ gint new_size, i;
new_size = g_spaced_primes_closest (hash->nnodes);
new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
if (hash->shared) {
- new_nodes = rspamd_mempool_alloc_shared (hash->pool, sizeof (struct rspamd_hash_node *) * new_size);
+ new_nodes =
+ rspamd_mempool_alloc_shared (hash->pool,
+ sizeof (struct rspamd_hash_node *) * new_size);
}
else {
- new_nodes = rspamd_mempool_alloc (hash->pool, sizeof (struct rspamd_hash_node *) * new_size);
+ new_nodes =
+ rspamd_mempool_alloc (hash->pool,
+ sizeof (struct rspamd_hash_node *) * new_size);
}
if (hash->shared) {
@@ -150,46 +158,56 @@ rspamd_hash_resize (rspamd_hash_t * hash)
static inline void
rspamd_hash_maybe_resize (rspamd_hash_t * hash)
{
- gint nnodes = hash->nnodes;
- gint size = hash->size;
+ gint nnodes = hash->nnodes;
+ gint size = hash->size;
- if ((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) || (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE)) {
+ if ((size >= 3 * nnodes &&
+ size > HASH_TABLE_MIN_SIZE) ||
+ (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE)) {
rspamd_hash_resize (hash);
}
}
/* Create new hash in specified pool */
-rspamd_hash_t *
-rspamd_hash_new (rspamd_mempool_t * pool, GHashFunc hash_func, GEqualFunc key_equal_func)
+rspamd_hash_t *
+rspamd_hash_new (rspamd_mempool_t * pool,
+ GHashFunc hash_func,
+ GEqualFunc key_equal_func)
{
- rspamd_hash_t *hash;
+ rspamd_hash_t *hash;
hash = rspamd_mempool_alloc (pool, sizeof (rspamd_hash_t));
hash->size = HASH_TABLE_MIN_SIZE;
hash->nnodes = 0;
hash->hash_func = hash_func ? hash_func : g_direct_hash;
hash->key_equal_func = key_equal_func;
- hash->nodes = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_hash_node *) * hash->size);
+ hash->nodes = rspamd_mempool_alloc0 (pool,
+ sizeof (struct rspamd_hash_node *) * hash->size);
hash->shared = 0;
hash->pool = pool;
return hash;
}
-/*
- * Create new hash in specified pool using shared memory
+/*
+ * Create new hash in specified pool using shared memory
*/
-rspamd_hash_t *
-rspamd_hash_new_shared (rspamd_mempool_t * pool, GHashFunc hash_func, GEqualFunc key_equal_func, gint size)
+rspamd_hash_t *
+rspamd_hash_new_shared (rspamd_mempool_t * pool,
+ GHashFunc hash_func,
+ GEqualFunc key_equal_func,
+ gint size)
{
- rspamd_hash_t *hash;
+ rspamd_hash_t *hash;
hash = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_hash_t));
hash->size = size;
hash->nnodes = 0;
hash->hash_func = hash_func ? hash_func : g_direct_hash;
hash->key_equal_func = key_equal_func;
- hash->nodes = rspamd_mempool_alloc0_shared (pool, sizeof (struct rspamd_hash_node *) * hash->size);
+ hash->nodes =
+ rspamd_mempool_alloc0_shared (pool,
+ sizeof (struct rspamd_hash_node *) * hash->size);
hash->shared = 1;
/* Get mutex from pool for locking on insert/remove operations */
hash->lock = rspamd_mempool_get_rwlock (pool);
@@ -198,14 +216,14 @@ rspamd_hash_new_shared (rspamd_mempool_t * pool, GHashFunc hash_func, GEqualFunc
return hash;
}
-/*
- * Insert item in hash
+/*
+ * Insert item in hash
*/
void
rspamd_hash_insert (rspamd_hash_t * hash, gpointer key, gpointer value)
{
- struct rspamd_hash_node **node_ptr, *node;
- guint key_hash;
+ struct rspamd_hash_node **node_ptr, *node;
+ guint key_hash;
g_return_if_fail (hash != NULL);
node_ptr = rspamd_hash_lookup_node (hash, key, &key_hash);
@@ -219,10 +237,14 @@ rspamd_hash_insert (rspamd_hash_t * hash, gpointer key, gpointer value)
}
else {
if (hash->shared) {
- node = rspamd_mempool_alloc_shared (hash->pool, sizeof (struct rspamd_hash_node));
+ node =
+ rspamd_mempool_alloc_shared (hash->pool,
+ sizeof (struct rspamd_hash_node));
}
else {
- node = rspamd_mempool_alloc (hash->pool, sizeof (struct rspamd_hash_node));
+ node =
+ rspamd_mempool_alloc (hash->pool,
+ sizeof (struct rspamd_hash_node));
}
node->key = key;
@@ -242,13 +264,13 @@ rspamd_hash_insert (rspamd_hash_t * hash, gpointer key, gpointer value)
}
}
-/*
- * Remove item from hash
+/*
+ * Remove item from hash
*/
gboolean
rspamd_hash_remove (rspamd_hash_t * hash, gpointer key)
{
- struct rspamd_hash_node **node_ptr;
+ struct rspamd_hash_node **node_ptr;
g_return_val_if_fail (hash != NULL, FALSE);
@@ -262,13 +284,13 @@ rspamd_hash_remove (rspamd_hash_t * hash, gpointer key)
return TRUE;
}
-/*
- * Lookup item from hash
+/*
+ * Lookup item from hash
*/
gpointer
rspamd_hash_lookup (rspamd_hash_t * hash, gpointer key)
{
- struct rspamd_hash_node *node;
+ struct rspamd_hash_node *node;
g_return_val_if_fail (hash != NULL, NULL);
node = *rspamd_hash_lookup_node (hash, key, NULL);
@@ -276,14 +298,14 @@ rspamd_hash_lookup (rspamd_hash_t * hash, gpointer key)
return node ? node->value : NULL;
}
-/*
- * Iterate throught hash
+/*
+ * Iterate throught hash
*/
void
rspamd_hash_foreach (rspamd_hash_t * hash, GHFunc func, gpointer user_data)
{
- struct rspamd_hash_node *node;
- gint i;
+ struct rspamd_hash_node *node;
+ gint i;
g_return_if_fail (hash != NULL);
g_return_if_fail (func != NULL);
@@ -293,7 +315,7 @@ rspamd_hash_foreach (rspamd_hash_t * hash, GHFunc func, gpointer user_data)
}
for (i = 0; i < hash->size; i++) {
for (node = hash->nodes[i]; node; node = node->next) {
- (*func) (node->key, node->value, user_data);
+ (*func)(node->key, node->value, user_data);
}
}
if (hash->shared) {
@@ -308,7 +330,7 @@ rspamd_hash_foreach (rspamd_hash_t * hash, GHFunc func, gpointer user_data)
static void
rspamd_lru_hash_destroy_node (gpointer v)
{
- rspamd_lru_element_t *node = v;
+ rspamd_lru_element_t *node = v;
if (node->hash->value_destroy) {
node->hash->value_destroy (node->data);
@@ -317,10 +339,14 @@ rspamd_lru_hash_destroy_node (gpointer v)
g_slice_free1 (sizeof (rspamd_lru_element_t), node);
}
-static rspamd_lru_element_t*
-rspamd_lru_create_node (rspamd_lru_hash_t *hash, gpointer key, gpointer value, time_t now, guint ttl)
+static rspamd_lru_element_t *
+rspamd_lru_create_node (rspamd_lru_hash_t *hash,
+ gpointer key,
+ gpointer value,
+ time_t now,
+ guint ttl)
{
- rspamd_lru_element_t *node;
+ rspamd_lru_element_t *node;
node = g_slice_alloc (sizeof (rspamd_lru_element_t));
node->data = value;
@@ -340,14 +366,21 @@ rspamd_lru_create_node (rspamd_lru_hash_t *hash, gpointer key, gpointer value, t
* @param key_equal_func pointer to function for comparing keys
* @return new rspamd_hash object
*/
-rspamd_lru_hash_t*
-rspamd_lru_hash_new (GHashFunc hash_func, GEqualFunc key_equal_func, gint maxsize, gint maxage,
- GDestroyNotify key_destroy, GDestroyNotify value_destroy)
+rspamd_lru_hash_t *
+rspamd_lru_hash_new (GHashFunc hash_func,
+ GEqualFunc key_equal_func,
+ gint maxsize,
+ gint maxage,
+ GDestroyNotify key_destroy,
+ GDestroyNotify value_destroy)
{
- rspamd_lru_hash_t *new;
+ rspamd_lru_hash_t *new;
new = g_malloc (sizeof (rspamd_lru_hash_t));
- new->storage = g_hash_table_new_full (hash_func, key_equal_func, key_destroy, rspamd_lru_hash_destroy_node);
+ new->storage = g_hash_table_new_full (hash_func,
+ key_equal_func,
+ key_destroy,
+ rspamd_lru_hash_destroy_node);
new->maxage = maxage;
new->maxsize = maxsize;
new->value_destroy = value_destroy;
@@ -368,13 +401,19 @@ rspamd_lru_hash_new (GHashFunc hash_func, GEqualFunc key_equal_func, gint maxsiz
* @param key_equal_func pointer to function for comparing keys
* @return new rspamd_hash object
*/
-rspamd_lru_hash_t*
-rspamd_lru_hash_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
- gint maxsize, gint maxage, GDestroyNotify key_destroy, GDestroyNotify value_destroy,
- gpointer storage, lru_cache_insert_func insert_func, lru_cache_lookup_func lookup_func,
- lru_cache_delete_func delete_func)
+rspamd_lru_hash_t *
+rspamd_lru_hash_new_full (GHashFunc hash_func,
+ GEqualFunc key_equal_func,
+ gint maxsize,
+ gint maxage,
+ GDestroyNotify key_destroy,
+ GDestroyNotify value_destroy,
+ gpointer storage,
+ lru_cache_insert_func insert_func,
+ lru_cache_lookup_func lookup_func,
+ lru_cache_delete_func delete_func)
{
- rspamd_lru_hash_t *new;
+ rspamd_lru_hash_t *new;
new = g_malloc (sizeof (rspamd_lru_hash_t));
new->storage = storage;
@@ -400,7 +439,7 @@ rspamd_lru_hash_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
gpointer
rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gpointer key, time_t now)
{
- rspamd_lru_element_t *res;
+ rspamd_lru_element_t *res;
if ((res = hash->lookup_func (hash->storage, key)) != NULL) {
if (res->ttl != 0) {
@@ -434,17 +473,17 @@ rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gpointer key, time_t now)
*/
void
rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value,
- time_t now, guint ttl)
+ time_t now, guint ttl)
{
- rspamd_lru_element_t *res;
- gint removed = 0;
+ rspamd_lru_element_t *res;
+ gint removed = 0;
if ((res = hash->lookup_func (hash->storage, key)) != NULL) {
hash->delete_func (hash->storage, res->key);
}
else {
if (hash->maxsize > 0 &&
- (gint)g_queue_get_length (hash->q) >= hash->maxsize) {
+ (gint)g_queue_get_length (hash->q) >= hash->maxsize) {
/* Expire some elements */
res = g_queue_peek_tail (hash->q);
if (hash->maxage > 0) {
@@ -456,7 +495,7 @@ rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value,
break;
}
res = g_queue_peek_tail (hash->q);
- removed ++;
+ removed++;
}
}
if (removed == 0) {