From 9233c1f8fefc6ecfe015682f532a7f1ff5038143 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Mon, 18 Dec 2006 18:39:19 +0100 Subject: [PATCH] use some lists for score_t. remove some list last/ptr nonsense, rather use correct double pointers. fix a memory leak in the score_t handling (yes, destructors are good ;) Signed-off-by: Pierre Habouzit --- lib-lib/hash.c | 16 ++++----- score.c | 94 ++++++++++++++++++++++++-------------------------- 2 files changed, 51 insertions(+), 59 deletions(-) diff --git a/lib-lib/hash.c b/lib-lib/hash.c index 5520389..eea6cd6 100644 --- a/lib-lib/hash.c +++ b/lib-lib/hash.c @@ -94,11 +94,11 @@ int hash_insert(HASH *table, const char *key, void *data, int allow_dup) table->table[h] = ptr; table->curnelem++; } else { - struct hash_elem *tmp, *last; - int r; + struct hash_elem **e; + + for (e = &table->table[h]; *e; e = &(*e)->next) { + int r = m_strcmp((*e)->key, key); - for (tmp = table->table[h], last = NULL; tmp; last = tmp, tmp = tmp->next) { - r = m_strcmp(tmp->key, key); if (r == 0) { p_delete(&ptr); return (-1); @@ -106,12 +106,8 @@ int hash_insert(HASH *table, const char *key, void *data, int allow_dup) if (r > 0) break; } - if (last) { - last->next = ptr; - } else { - table->table[h] = ptr; - } - ptr->next = tmp; + ptr->next = *e; + *e = ptr; table->curnelem++; } return h; diff --git a/score.c b/score.c index 1826a95..6f58ba1 100644 --- a/score.c +++ b/score.c @@ -19,9 +19,20 @@ typedef struct score_t { int val; int exact; /* if this rule matches, don't evaluate any more */ struct score_t *next; -} SCORE; +} score_t; -SCORE *Score = NULL; +score_t *Score = NULL; + +DO_INIT(score_t, score); +static void score_wipe(score_t *sc) +{ + pattern_list_wipe(&sc->pat); + p_delete(&sc->str); +} +DO_NEW(score_t, score); +DO_DELETE(score_t, score); + +DO_SLIST(score_t, score, score_delete); void mutt_check_rescore (CONTEXT * ctx) { @@ -51,9 +62,9 @@ int mutt_parse_score (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)), BUFFER * err) { - SCORE *ptr, *last; char *pattern, *pc; struct pattern_t *pat; + score_t **last; mutt_extract_token (buf, s, 0); if (!MoreArgs (s)) { @@ -71,35 +82,32 @@ int mutt_parse_score (BUFFER * buf, BUFFER * s, /* look for an existing entry and update the value, else add it to the end of the list */ - for (ptr = Score, last = NULL; ptr; last = ptr, ptr = ptr->next) - if (m_strcmp(pattern, ptr->str) == 0) + for (last = &Score; *last; last = &(*last)->next) { + if (m_strcmp(pattern, (*last)->str) == 0) break; - if (!ptr) { - if ((pat = mutt_pattern_comp (pattern, 0, err)) == NULL) { + } + if (!*last) { + if (!(pat = mutt_pattern_comp(pattern, 0, err))) { p_delete(&pattern); return (-1); } - ptr = p_new(SCORE, 1); - if (last) - last->next = ptr; - else - Score = ptr; - ptr->pat = pat; - ptr->str = pattern; + *last = score_new(); + (*last)->pat = pat; + (*last)->str = pattern; } pc = buf->data; if (*pc == '=') { - ptr->exact = 1; + (*last)->exact = 1; pc++; } - ptr->val = atoi (pc); - set_option (OPTNEEDRESCORE); + (*last)->val = atoi(pc); + set_option(OPTNEEDRESCORE); return 0; } void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx) { - SCORE *tmp; + score_t *tmp; hdr->score = 0; /* in case of re-scoring */ for (tmp = Score; tmp; tmp = tmp->next) { @@ -122,38 +130,26 @@ void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx) _mutt_set_flag (ctx, hdr, M_FLAG, 1, upd_ctx); } -int mutt_parse_unscore (BUFFER * buf, BUFFER * s, - unsigned long data __attribute__ ((unused)), - BUFFER * err __attribute__ ((unused))) +int mutt_parse_unscore(BUFFER * buf, BUFFER * s, + unsigned long data __attribute__ ((unused)), + BUFFER * err __attribute__ ((unused))) { - SCORE *tmp, *last = NULL; - - while (MoreArgs (s)) { - mutt_extract_token (buf, s, 0); - if (!m_strcmp("*", buf->data)) { - for (tmp = Score; tmp;) { - last = tmp; - tmp = tmp->next; - pattern_list_wipe(&last->pat); - p_delete(&last); - } - Score = NULL; - } - else { - for (tmp = Score; tmp; last = tmp, tmp = tmp->next) { - if (!m_strcmp(buf->data, tmp->str)) { - if (last) - last->next = tmp->next; - else - Score = tmp->next; - pattern_list_wipe(&tmp->pat); - p_delete(&tmp); - /* there should only be one score per pattern, so we can stop here */ - break; + while (MoreArgs(s)) { + mutt_extract_token(buf, s, 0); + if (!m_strcmp("*", buf->data)) { + score_list_wipe(&Score); + } else { + score_t **last; + + for (last = &Score; *last; last = &(*last)->next) { + if (!m_strcmp(buf->data, (*last)->str)) { + score_t *tmp = score_list_pop(last); + score_delete(&tmp); + break; + } + } } - } } - } - set_option (OPTNEEDRESCORE); - return 0; + set_option (OPTNEEDRESCORE); + return 0; } -- 2.20.1