aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/hash.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2014-07-23 12:53:08 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2014-07-23 12:53:08 +0100
commitfe79d8c5a39f2b717f78cc3f3ef21b3cfc46500b (patch)
treec84e6a5d4c5cd78a7a2cc3c7adbc7af5d0541682 /src/libutil/hash.c
parente0483657ff6cf1adc828ccce457814d61fe90a0d (diff)
downloadrspamd-fe79d8c5a39f2b717f78cc3f3ef21b3cfc46500b.tar.gz
rspamd-fe79d8c5a39f2b717f78cc3f3ef21b3cfc46500b.zip
Revert "Unify code style."
This reverts commit e0483657ff6cf1adc828ccce457814d61fe90a0d.
Diffstat (limited to 'src/libutil/hash.c')
-rw-r--r--src/libutil/hash.c167
1 files changed, 64 insertions, 103 deletions
diff --git a/src/libutil/hash.c b/src/libutil/hash.c
index 0e8b1646e..3bb381651 100644
--- a/src/libutil/hash.c
+++ b/src/libutil/hash.c
@@ -33,13 +33,11 @@
* 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);
@@ -60,8 +58,7 @@ rspamd_hash_lookup_node (rspamd_hash_t * hash,
*/
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;
@@ -86,10 +83,9 @@ rspamd_hash_lookup_node (rspamd_hash_t * hash,
* 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);
@@ -112,23 +108,19 @@ rspamd_hash_remove_node (rspamd_hash_t * hash,
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) {
@@ -158,56 +150,46 @@ 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);
@@ -216,14 +198,14 @@ rspamd_hash_new_shared (rspamd_mempool_t * pool,
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);
@@ -237,14 +219,10 @@ 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;
@@ -264,13 +242,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);
@@ -284,13 +262,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);
@@ -298,14 +276,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);
@@ -315,7 +293,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) {
@@ -330,7 +308,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);
@@ -339,14 +317,10 @@ 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;
@@ -366,21 +340,14 @@ rspamd_lru_create_node (rspamd_lru_hash_t *hash,
* @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;
@@ -401,19 +368,13 @@ rspamd_lru_hash_new (GHashFunc hash_func,
* @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;
@@ -439,7 +400,7 @@ rspamd_lru_hash_new_full (GHashFunc hash_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) {
@@ -473,17 +434,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) {
@@ -495,7 +456,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) {