From 4442930ade090c47aacdaceaf4fffb83f392e018 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 27 Jan 2020 16:27:35 +0000 Subject: [PATCH] [Minor] Fix free logic reversal (as dtors are now in variables themselves) --- src/libutil/mem_pool.c | 111 ++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c index 9644485f4..357e8b693 100644 --- a/src/libutil/mem_pool.c +++ b/src/libutil/mem_pool.c @@ -715,6 +715,53 @@ rspamd_mempool_adjust_entry (struct rspamd_mempool_entry_point *e) memset (e->elts, 0, sizeof (e->elts)); } +static void +rspamd_mempool_variables_cleanup (rspamd_mempool_t * pool) +{ + if (pool->priv->variables) { + struct rspamd_mempool_variable *var; + kh_foreach_value_ptr (pool->priv->variables, var, { + if (var->dtor) { + var->dtor (var->data); + } + }); + + if (pool->priv->entry && pool->priv->entry->cur_vars < + kh_size (pool->priv->variables)) { + /* + * Increase preallocated size in two cases: + * 1) Our previous guess was zero + * 2) Our new variables count is not more than twice larger than + * previous count + * 3) Our variables count is less than some hard limit + */ + static const guint max_preallocated_vars = 512; + + guint cur_size = kh_size (pool->priv->variables); + guint old_guess = pool->priv->entry->cur_vars; + guint new_guess; + + if (old_guess == 0) { + new_guess = MIN (cur_size, max_preallocated_vars); + } + else { + if (old_guess * 2 < cur_size) { + new_guess = MIN (cur_size, max_preallocated_vars); + } + else { + /* Too large step */ + new_guess = MIN (old_guess * 2, max_preallocated_vars); + } + } + + pool->priv->entry->cur_vars = new_guess; + } + + kh_destroy (rspamd_mempool_vars_hash, pool->priv->variables); + pool->priv->variables = NULL; + } +} + void rspamd_mempool_destructors_enforce (rspamd_mempool_t *pool) { @@ -731,6 +778,8 @@ rspamd_mempool_destructors_enforce (rspamd_mempool_t *pool) pool->priv->dtors_head = pool->priv->dtors_tail = NULL; + rspamd_mempool_variables_cleanup (pool); + POOL_MTX_UNLOCK (); } @@ -824,6 +873,17 @@ rspamd_mempool_delete (rspamd_mempool_t * pool) } } + rspamd_mempool_variables_cleanup (pool); + + if (pool->priv->trash_stack) { + for (i = 0; i < pool->priv->trash_stack->len; i++) { + ptr = g_ptr_array_index (pool->priv->trash_stack, i); + g_free (ptr); + } + + g_ptr_array_free (pool->priv->trash_stack, TRUE); + } + for (i = 0; i < G_N_ELEMENTS (pool->priv->pools); i ++) { if (pool->priv->pools[i]) { LL_FOREACH_SAFE (pool->priv->pools[i], cur, tmp) { @@ -846,57 +906,6 @@ rspamd_mempool_delete (rspamd_mempool_t * pool) } } - if (pool->priv->variables) { - struct rspamd_mempool_variable *var; - kh_foreach_value_ptr (pool->priv->variables, var, { - if (var->dtor) { - var->dtor (var->data); - } - }); - - if (pool->priv->entry && pool->priv->entry->cur_vars < - kh_size (pool->priv->variables)) { - /* - * Increase preallocated size in two cases: - * 1) Our previous guess was zero - * 2) Our new variables count is not more than twice larger than - * previous count - * 3) Our variables count is less than some hard limit - */ - static const guint max_preallocated_vars = 512; - - guint cur_size = kh_size (pool->priv->variables); - guint old_guess = pool->priv->entry->cur_vars; - guint new_guess; - - if (old_guess == 0) { - new_guess = MIN (cur_size, max_preallocated_vars); - } - else { - if (old_guess * 2 < cur_size) { - new_guess = MIN (cur_size, max_preallocated_vars); - } - else { - /* Too large step */ - new_guess = MIN (old_guess * 2, max_preallocated_vars); - } - } - - pool->priv->entry->cur_vars = new_guess; - } - - kh_destroy (rspamd_mempool_vars_hash, pool->priv->variables); - } - - if (pool->priv->trash_stack) { - for (i = 0; i < pool->priv->trash_stack->len; i++) { - ptr = g_ptr_array_index (pool->priv->trash_stack, i); - g_free (ptr); - } - - g_ptr_array_free (pool->priv->trash_stack, TRUE); - } - g_atomic_int_inc (&mem_pool_stat->pools_freed); POOL_MTX_UNLOCK (); free (pool); /* allocated by posix_memalign */ -- 2.39.5