From a281d6a475ceb0e7553db38a2e74656aed3a8988 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 24 Jul 2018 19:01:40 +0100 Subject: [PATCH] [Project] Adopt functions --- contrib/libucl/khash.h | 7 +++++ src/libmime/filter.c | 23 +++++++++----- src/libmime/filter.h | 2 +- src/libserver/composites.c | 10 +++--- src/libserver/protocol.c | 23 +++++--------- src/libserver/roll_history.c | 6 ++-- src/libserver/task.c | 13 +++----- src/libstat/stat_process.c | 2 +- src/lua/lua_task.c | 61 +++++++++++++----------------------- 9 files changed, 64 insertions(+), 83 deletions(-) diff --git a/contrib/libucl/khash.h b/contrib/libucl/khash.h index afc3ce3ef..a91db5b2a 100644 --- a/contrib/libucl/khash.h +++ b/contrib/libucl/khash.h @@ -576,6 +576,13 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key) code; \ } } +#define kh_foreach_value_ptr(h, pvvar, code) { khint_t __i; \ + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ + if (!kh_exist(h,__i)) continue; \ + (pvvar) = &kh_val(h,__i); \ + code; \ + } } + /* More conenient interfaces */ /*! @function diff --git a/src/libmime/filter.c b/src/libmime/filter.c index 8677ef8a1..e4862b0fd 100644 --- a/src/libmime/filter.c +++ b/src/libmime/filter.c @@ -106,6 +106,7 @@ insert_metric_result (struct rspamd_task *task, guint i; khiter_t k; gboolean single = !!(flags & RSPAMD_SYMBOL_INSERT_SINGLE); + gchar *sym_cpy; metric_res = rspamd_create_metric_result (task); @@ -236,8 +237,9 @@ insert_metric_result (struct rspamd_task *task, } } else { + sym_cpy = rspamd_mempool_strdup (task->task_pool, symbol); k = kh_put (rspamd_symbols_hash, metric_res->symbols, - symbol, &ret); + sym_cpy, &ret); s = &kh_value (metric_res->symbols, k); memset (s, 0, sizeof (*s)); @@ -250,7 +252,7 @@ insert_metric_result (struct rspamd_task *task, next_gf = task->cfg->grow_factor; } - s->name = symbol; + s->name = sym_cpy; s->sym = sdef; s->nshots = 1; @@ -318,6 +320,7 @@ rspamd_task_add_result_option (struct rspamd_task *task, { struct rspamd_symbol_option *opt; gboolean ret = FALSE; + gchar *opt_cpy; khiter_t k; gint r; @@ -329,10 +332,12 @@ rspamd_task_add_result_option (struct rspamd_task *task, k = kh_get (rspamd_options_hash, s->options, val); if (k == kh_end (s->options)) { - k = kh_put (rspamd_options_hash, s->options, val, &r); + opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt)); + opt_cpy = rspamd_mempool_strdup (task->task_pool, val); + k = kh_put (rspamd_options_hash, s->options, opt_cpy, &r); - opt = &kh_value (s->options, k); - opt->option = rspamd_mempool_strdup (task->task_pool, val); + kh_value (s->options, k) = opt; + opt->option = opt_cpy; DL_APPEND (s->opts_head, opt); ret = TRUE; @@ -340,10 +345,12 @@ rspamd_task_add_result_option (struct rspamd_task *task, } else { s->options = kh_init (rspamd_options_hash); - k = kh_put (rspamd_options_hash, s->options, val, &r); + opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt)); + opt_cpy = rspamd_mempool_strdup (task->task_pool, val); + k = kh_put (rspamd_options_hash, s->options, opt_cpy, &r); - opt = &kh_value (s->options, k); - opt->option = rspamd_mempool_strdup (task->task_pool, val); + kh_value (s->options, k) = opt; + opt->option = opt_cpy; DL_APPEND (s->opts_head, opt); ret = TRUE; diff --git a/src/libmime/filter.h b/src/libmime/filter.h index de8cb57b8..b6d7a4359 100644 --- a/src/libmime/filter.h +++ b/src/libmime/filter.h @@ -30,7 +30,7 @@ enum rspamd_symbol_result_flags { */ KHASH_INIT (rspamd_options_hash, const char *, - struct rspamd_symbol_option, + struct rspamd_symbol_option *, true, rspamd_str_hash, rspamd_str_equal); diff --git a/src/libserver/composites.c b/src/libserver/composites.c index f7c31bb20..88dc51bd2 100644 --- a/src/libserver/composites.c +++ b/src/libserver/composites.c @@ -120,7 +120,7 @@ rspamd_composite_process_single_symbol (struct composites_data *cd, struct rspamd_composite *ncomp; struct rspamd_task *task = cd->task; - if ((ms = g_hash_table_lookup (cd->metric_res->symbols, sym)) == NULL) { + if ((ms = rspamd_task_find_symbol_result (cd->task, sym)) == NULL) { msg_debug_composites ("not found symbol %s in composite %s", sym, cd->composite->sym); if ((ncomp = @@ -144,14 +144,14 @@ rspamd_composite_process_single_symbol (struct composites_data *cd, cd->composite = saved; clrbit (cd->checked, cd->composite->id * 2); - ms = g_hash_table_lookup (cd->metric_res->symbols, sym); + ms = rspamd_task_find_symbol_result (cd->task, sym); } else { /* * XXX: in case of cyclic references this would return 0 */ if (isset (cd->checked, ncomp->id * 2 + 1)) { - ms = g_hash_table_lookup (cd->metric_res->symbols, sym); + ms = rspamd_task_find_symbol_result (cd->task, sym); } } } @@ -190,7 +190,7 @@ rspamd_composite_expr_process (gpointer input, rspamd_expression_atom_t *atom) if (isset (cd->checked, cd->composite->id * 2)) { /* We have already checked this composite, so just return its value */ if (isset (cd->checked, cd->composite->id * 2 + 1)) { - ms = g_hash_table_lookup (cd->metric_res->symbols, sym); + ms = rspamd_task_find_symbol_result (cd->task, sym); } if (ms) { @@ -334,7 +334,7 @@ composites_foreach_callback (gpointer key, gpointer value, void *data) clrbit (cd->checked, comp->id * 2 + 1); } else { - if (g_hash_table_lookup (cd->metric_res->symbols, key) != NULL) { + if (rspamd_task_find_symbol_result (cd->task, key) != NULL) { /* Already set, no need to check */ msg_debug_composites ("composite %s is already in metric " "in composites bitfield", cd->composite->sym); diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index 0e4f9cb0c..c83451058 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -912,12 +912,10 @@ static ucl_object_t * rspamd_metric_result_ucl (struct rspamd_task *task, struct rspamd_metric_result *mres, ucl_object_t *top) { - GHashTableIter hiter; struct rspamd_symbol_result *sym; gboolean is_spam; enum rspamd_action_type action = METRIC_ACTION_NOACTION; ucl_object_t *obj = NULL, *sobj; - gpointer h, v; const gchar *subject; action = rspamd_check_action_metric (task, mres); @@ -966,15 +964,12 @@ rspamd_metric_result_ucl (struct rspamd_task *task, obj = ucl_object_typed_new (UCL_OBJECT); } - g_hash_table_iter_init (&hiter, mres->symbols); - - while (g_hash_table_iter_next (&hiter, &h, &v)) { - sym = (struct rspamd_symbol_result *)v; + kh_foreach_value_ptr (mres->symbols, sym, { if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { sobj = rspamd_metric_symbol_ucl (task, sym); - ucl_object_insert_key (obj, sobj, h, 0, false); + ucl_object_insert_key (obj, sobj, sym->name, 0, false); } - } + }); if (task->cmd == CMD_CHECK_V2) { ucl_object_insert_key (top, obj, "symbols", 0, false); @@ -1405,8 +1400,6 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) struct rspamd_protocol_log_message_sum *ls; lua_State *L = task->cfg->lua_state; struct rspamd_metric_result *mres; - GHashTableIter it; - gpointer k, v; struct rspamd_symbol_result *sym; gint id, i; guint32 *sid, n = 0, nextra = 0; @@ -1551,7 +1544,7 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) mres = task->result; if (mres) { - n = g_hash_table_size (mres->symbols); + n = kh_size (mres->symbols); sz = sizeof (*ls) + sizeof (struct rspamd_protocol_log_symbol_result) * (n + nextra); @@ -1574,13 +1567,11 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) ls->nresults = n; ls->nextra = nextra; - g_hash_table_iter_init (&it, mres->symbols); i = 0; - while (g_hash_table_iter_next (&it, &k, &v)) { + kh_foreach_value_ptr (mres->symbols, sym, { id = rspamd_symbols_cache_find_symbol (task->cfg->cache, - k); - sym = v; + sym->name); if (id >= 0) { ls->results[i].id = id; @@ -1592,7 +1583,7 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) } i ++; - } + }); memcpy (&ls->results[n], extra->data, nextra * sizeof (er)); } diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c index 6dbd4d338..3df597816 100644 --- a/src/libserver/roll_history.c +++ b/src/libserver/roll_history.c @@ -159,9 +159,9 @@ rspamd_roll_history_update (struct roll_history *history, row->required_score = rspamd_task_get_required_score (task, metric_res); cbdata.pos = row->symbols; cbdata.remain = sizeof (row->symbols); - g_hash_table_foreach (metric_res->symbols, - roll_history_symbols_callback, - &cbdata); + rspamd_task_symbol_result_foreach (task, + roll_history_symbols_callback, + &cbdata); if (cbdata.remain > 0) { /* Remove last whitespace and comma */ *cbdata.pos-- = '\0'; diff --git a/src/libserver/task.c b/src/libserver/task.c index d881c6897..437bc4829 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -1045,9 +1045,7 @@ rspamd_task_log_metric_res (struct rspamd_task *task, static gchar scorebuf[32]; rspamd_ftok_t res = {.begin = NULL, .len = 0}; struct rspamd_metric_result *mres; - GHashTableIter it; gboolean first = TRUE; - gpointer k, v; rspamd_fstring_t *symbuf; struct rspamd_symbol_result *sym; GPtrArray *sorted_symbols; @@ -1083,16 +1081,13 @@ rspamd_task_log_metric_res (struct rspamd_task *task, break; case RSPAMD_LOG_SYMBOLS: symbuf = rspamd_fstring_sized_new (128); - g_hash_table_iter_init (&it, mres->symbols); - sorted_symbols = g_ptr_array_sized_new (g_hash_table_size (mres->symbols)); - - while (g_hash_table_iter_next (&it, &k, &v)) { - sym = v; + sorted_symbols = g_ptr_array_sized_new (kh_size (mres->symbols)); + kh_foreach_value_ptr (mres->symbols, sym, { if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - g_ptr_array_add (sorted_symbols, v); + g_ptr_array_add (sorted_symbols, (gpointer)sym); } - } + }); g_ptr_array_sort (sorted_symbols, rspamd_task_compare_log_sym); diff --git a/src/libstat/stat_process.c b/src/libstat/stat_process.c index 978c8f3c0..f58bf6150 100644 --- a/src/libstat/stat_process.c +++ b/src/libstat/stat_process.c @@ -1093,7 +1093,7 @@ rspamd_stat_has_classifier_symbols (struct rspamd_task *task, id = g_array_index (cl->statfiles_ids, gint, i); st = g_ptr_array_index (st_ctx->statfiles, id); - if (g_hash_table_lookup (mres->symbols, st->stcf->symbol)) { + if (rspamd_task_find_symbol_result (task, st->stcf->symbol)) { if (is_spam == !!st->stcf->is_spam) { msg_debug_task ("do not autolearn %s as symbol %s is already " "added", is_spam ? "spam" : "ham", st->stcf->symbol); diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 94239e5ac..ca10a94e7 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -1446,7 +1446,7 @@ lua_task_adjust_result (lua_State * L) metric_res = task->result; if (metric_res) { - s = g_hash_table_lookup (metric_res->symbols, symbol_name); + s = rspamd_task_find_symbol_result (task, symbol_name); } else { return luaL_error (L, "no metric result"); @@ -3246,7 +3246,7 @@ lua_push_symbol_result (lua_State *L, metric_res = task->result; if (metric_res) { - s = g_hash_table_lookup (metric_res->symbols, symbol); + s = rspamd_task_find_symbol_result (task, symbol); } } else { @@ -3295,7 +3295,7 @@ lua_push_symbol_result (lua_State *L, if (s->options) { lua_pushstring (L, "options"); - lua_createtable (L, g_hash_table_size (s->options), 0); + lua_createtable (L, kh_size (s->options), 0); DL_FOREACH (s->opts_head, opt) { lua_pushstring (L, (const char*)opt->option); @@ -3349,18 +3349,12 @@ lua_task_has_symbol (lua_State *L) { struct rspamd_task *task = lua_check_task (L, 1); const gchar *symbol; - struct rspamd_metric_result *mres; gboolean found = FALSE; symbol = luaL_checkstring (L, 2); if (task && symbol) { - mres = task->result; - - if (mres) { - found = g_hash_table_lookup (mres->symbols, symbol) != NULL; - } - + found = (rspamd_task_find_symbol_result (task, symbol) != NULL); lua_pushboolean (L, found); } else { @@ -3376,28 +3370,24 @@ lua_task_get_symbols (lua_State *L) struct rspamd_task *task = lua_check_task (L, 1); struct rspamd_metric_result *mres; gint i = 1; - GHashTableIter it; - gpointer k, v; struct rspamd_symbol_result *s; if (task) { mres = task->result; if (mres) { - lua_createtable (L, g_hash_table_size (mres->symbols), 0); - lua_createtable (L, g_hash_table_size (mres->symbols), 0); - g_hash_table_iter_init (&it, mres->symbols); + lua_createtable (L, kh_size (mres->symbols), 0); + lua_createtable (L, kh_size (mres->symbols), 0); - while (g_hash_table_iter_next (&it, &k, &v)) { - s = v; + kh_foreach_value_ptr (mres->symbols, s, { if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - lua_pushstring (L, k); + lua_pushstring (L, s->name); lua_rawseti (L, -3, i); lua_pushnumber (L, s->score); lua_rawseti (L, -2, i); i++; } - } + }); } else { lua_createtable (L, 0, 0); @@ -3416,8 +3406,7 @@ lua_task_get_symbols_all (lua_State *L) { struct rspamd_task *task = lua_check_task (L, 1); struct rspamd_metric_result *mres; - GHashTableIter it; - gpointer k, v; + struct rspamd_symbol_result *s; gboolean found = FALSE; gint i = 1; @@ -3426,13 +3415,12 @@ lua_task_get_symbols_all (lua_State *L) if (mres) { found = TRUE; - lua_createtable (L, g_hash_table_size (mres->symbols), 0); - g_hash_table_iter_init (&it, mres->symbols); + lua_createtable (L, kh_size (mres->symbols), 0); - while (g_hash_table_iter_next (&it, &k, &v)) { - lua_push_symbol_result (L, task, k, v, FALSE, TRUE); + kh_foreach_value_ptr (mres->symbols, s, { + lua_push_symbol_result (L, task, s->name, s, FALSE, TRUE); lua_rawseti (L, -2, i++); - } + }); } } else { @@ -3453,32 +3441,28 @@ lua_task_get_symbols_numeric (lua_State *L) struct rspamd_task *task = lua_check_task (L, 1); struct rspamd_metric_result *mres; gint i = 1, id; - GHashTableIter it; - gpointer k, v; struct rspamd_symbol_result *s; if (task) { mres = task->result; if (mres) { - lua_createtable (L, g_hash_table_size (mres->symbols), 0); - lua_createtable (L, g_hash_table_size (mres->symbols), 0); - - g_hash_table_iter_init (&it, mres->symbols); + lua_createtable (L, kh_size (mres->symbols), 0); + lua_createtable (L, kh_size (mres->symbols), 0); - while (g_hash_table_iter_next (&it, &k, &v)) { - s = v; + lua_createtable (L, kh_size (mres->symbols), 0); + kh_foreach_value_ptr (mres->symbols, s, { if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { id = rspamd_symbols_cache_find_symbol (task->cfg->cache, - k); + s->name); lua_pushnumber (L, id); lua_rawseti (L, -3, i); lua_pushnumber (L, s->score); lua_rawseti (L, -2, i); i++; } - } + }); } else { lua_createtable (L, 0, 0); @@ -3502,7 +3486,6 @@ struct tokens_foreach_cbdata { static void tokens_foreach_cb (gint id, const gchar *sym, gint flags, gpointer ud) { - struct rspamd_metric_result *mres; struct tokens_foreach_cbdata *cbd = ud; struct rspamd_symbol_result *s; @@ -3510,9 +3493,7 @@ tokens_foreach_cb (gint id, const gchar *sym, gint flags, gpointer ud) return; } - mres = cbd->task->result; - - if (mres && (s = g_hash_table_lookup (mres->symbols, sym)) != NULL) { + if ((s = rspamd_task_find_symbol_result (cbd->task, sym)) != NULL) { if (cbd->normalize) { lua_pushnumber (cbd->L, tanh (s->score)); } -- 2.39.5