From: Richard Russon Date: Sat, 22 Dec 2018 21:28:22 +0000 (+0000) Subject: refactor hash X-Git-Tag: 2019-10-25~418 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0a6aba3ae9f2094fc4297bbae04accb2070fa243;p=neomutt refactor hash --- diff --git a/config/set.c b/config/set.c index 30ece0443..f1e4c47a6 100644 --- a/config/set.c +++ b/config/set.c @@ -45,7 +45,7 @@ struct ConfigSetType RegisteredTypes[18] = { }; /** - * destroy - Callback function for the Hash Table - Implements ::hash_destructor_t + * destroy - Callback function for the Hash Table - Implements ::hashelem_free_t * @param type Object type, e.g. #DT_STRING * @param obj Object to destroy * @param data ConfigSet associated with the object @@ -179,7 +179,7 @@ void cs_free(struct ConfigSet **cs) if (!cs || !*cs) return; /* LCOV_EXCL_LINE */ - mutt_hash_destroy(&(*cs)->hash); + mutt_hash_free(&(*cs)->hash); FREE(cs); } diff --git a/email/thread.c b/email/thread.c index 98341afbb..2b4443cd3 100644 --- a/email/thread.c +++ b/email/thread.c @@ -101,7 +101,7 @@ void insert_message(struct MuttThread **new, struct MuttThread *newparent, } /** - * thread_hash_destructor - Hash Destructor callback - Implements ::hash_destructor_t + * thread_hash_destructor - Hash Destructor callback - Implements ::hashelem_free_t */ void thread_hash_destructor(int type, void *obj, intptr_t data) { diff --git a/imap/util.c b/imap/util.c index f0ca6a04b..2411d364a 100644 --- a/imap/util.c +++ b/imap/util.c @@ -202,7 +202,7 @@ struct ImapMboxData *imap_mdata_new(struct ImapAccountData *adata, const char *n */ void imap_mdata_cache_reset(struct ImapMboxData *mdata) { - mutt_hash_destroy(&mdata->uid_hash); + mutt_hash_free(&mdata->uid_hash); FREE(&mdata->msn_index); mdata->msn_index_size = 0; mdata->max_msn = 0; diff --git a/init.c b/init.c index 12c683c67..cd0ab2a90 100644 --- a/init.c +++ b/init.c @@ -2787,9 +2787,9 @@ void mutt_free_opts(void) mutt_regexlist_free(&UnSubscribedLists); mutt_grouplist_free(); - mutt_hash_destroy(&ReverseAliases); - mutt_hash_destroy(&TagFormats); - mutt_hash_destroy(&TagTransforms); + mutt_hash_free(&ReverseAliases); + mutt_hash_free(&TagFormats); + mutt_hash_free(&TagTransforms); /* Lists of strings */ mutt_list_free(&AlternativeOrderList); diff --git a/maildir/maildir.c b/maildir/maildir.c index 32e94e84f..cbe354646 100644 --- a/maildir/maildir.c +++ b/maildir/maildir.c @@ -512,7 +512,7 @@ int maildir_mbox_check(struct Context *ctx, int *index_hint) } /* destroy the file name hash */ - mutt_hash_destroy(&fnames); + mutt_hash_free(&fnames); /* If we didn't just get new mail, update the tables. */ if (occult) diff --git a/maildir/mh.c b/maildir/mh.c index 5f753bb25..ac20e5e03 100644 --- a/maildir/mh.c +++ b/maildir/mh.c @@ -701,7 +701,7 @@ int mh_mbox_check(struct Context *ctx, int *index_hint) /* destroy the file name hash */ - mutt_hash_destroy(&fnames); + mutt_hash_free(&fnames); /* If we didn't just get new mail, update the tables. */ if (occult) diff --git a/mbox/mbox.c b/mbox/mbox.c index f003fc44e..e78821601 100644 --- a/mbox/mbox.c +++ b/mbox/mbox.c @@ -594,9 +594,9 @@ static int reopen_mailbox(struct Context *ctx, int *index_hint) old_msgcount = 0; /* simulate a close */ - mutt_hash_destroy(&m->id_hash); - mutt_hash_destroy(&m->subj_hash); - mutt_hash_destroy(&m->label_hash); + mutt_hash_free(&m->id_hash); + mutt_hash_free(&m->subj_hash); + mutt_hash_free(&m->label_hash); mutt_clear_threads(ctx); FREE(&m->v2r); if (m->readonly) diff --git a/mutt/group.c b/mutt/group.c index fad257fa2..5fd355134 100644 --- a/mutt/group.c +++ b/mutt/group.c @@ -58,7 +58,7 @@ void mutt_grouplist_init(void) */ void mutt_grouplist_free(void) { - mutt_hash_destroy(&Groups); + mutt_hash_free(&Groups); } /** diff --git a/mutt/hash.c b/mutt/hash.c index d28eb7214..d512df48d 100644 --- a/mutt/hash.c +++ b/mutt/hash.c @@ -253,8 +253,8 @@ static void union_hash_delete(struct Hash *table, union HashKey key, const void if ((data == ptr->data || !data) && table->cmp_key(ptr->key, key) == 0) { *last = ptr->next; - if (table->destroy) - table->destroy(ptr->type, ptr->data, table->dest_data); + if (table->elem_free) + table->elem_free(ptr->type, ptr->data, table->hash_data); if (table->strdup_keys) FREE(&ptr->key.strkey); FREE(&ptr); @@ -317,10 +317,10 @@ struct Hash *mutt_hash_int_new(size_t nelem, int flags) * @param fn Callback function to free Hash Table's resources * @param fn_data Data to pass to the callback function */ -void mutt_hash_set_destructor(struct Hash *table, hash_destructor_t fn, intptr_t fn_data) +void mutt_hash_set_destructor(struct Hash *table, hashelem_free_t fn, intptr_t fn_data) { - table->destroy = fn; - table->dest_data = fn_data; + table->elem_free = fn; + table->hash_data = fn_data; } /** @@ -452,10 +452,10 @@ void mutt_hash_int_delete(struct Hash *table, unsigned int intkey, const void *d } /** - * mutt_hash_destroy - Destroy a hash table + * mutt_hash_free - elem_free a hash table * @param ptr Hash Table to be freed */ -void mutt_hash_destroy(struct Hash **ptr) +void mutt_hash_free(struct Hash **ptr) { struct Hash *pptr = NULL; struct HashElem *elem = NULL, *tmp = NULL; @@ -470,8 +470,8 @@ void mutt_hash_destroy(struct Hash **ptr) { tmp = elem; elem = elem->next; - if (pptr->destroy) - pptr->destroy(tmp->type, tmp->data, pptr->dest_data); + if (pptr->elem_free) + pptr->elem_free(tmp->type, tmp->data, pptr->hash_data); if (pptr->strdup_keys) FREE(&tmp->key.strkey); FREE(&tmp); diff --git a/mutt/hash.h b/mutt/hash.h index 68e368295..086cce851 100644 --- a/mutt/hash.h +++ b/mutt/hash.h @@ -48,26 +48,26 @@ struct HashElem }; /** - * typedef hash_destructor_t - Prototype for Hash Destructor callback function + * typedef hashelem_free_t - Prototype for Hash Destructor callback function * @param type Hash Type * @param obj Object to free * @param data Data associated with the Hash */ -typedef void (*hash_destructor_t)(int type, void *obj, intptr_t data); +typedef void (*hashelem_free_t)(int type, void *obj, intptr_t data); /** * struct Hash - A Hash Table */ struct Hash { - size_t nelem; - bool strdup_keys : 1; /**< if set, the key->strkey is strdup'ed */ - bool allow_dups : 1; /**< if set, duplicate keys are allowed */ - struct HashElem **table; - size_t (*gen_hash)(union HashKey, size_t); - int (*cmp_key)(union HashKey, union HashKey); - hash_destructor_t destroy; - intptr_t dest_data; + size_t nelem; ///< Number of elements in the Hash table + bool strdup_keys : 1; ///< if set, the key->strkey is strdup'ed + bool allow_dups : 1; ///< if set, duplicate keys are allowed + struct HashElem **table; ///< Array of Hash keys + size_t (*gen_hash)(union HashKey, size_t); ///< Function to generate hash id from the key + int (*cmp_key)(union HashKey, union HashKey); ///< Function to compare two Hash keys + hashelem_free_t elem_free; ///< Function to free a Hash element + intptr_t hash_data; ///< Data to pass to the elem_free() function }; /* flags for mutt_hash_new() */ @@ -75,18 +75,18 @@ struct Hash #define MUTT_HASH_STRDUP_KEYS (1 << 1) /**< make a copy of the keys */ #define MUTT_HASH_ALLOW_DUPS (1 << 2) /**< allow duplicate keys to be inserted */ -struct Hash * mutt_hash_new(size_t nelem, int flags); void mutt_hash_delete(struct Hash *table, const char *strkey, const void *data); -void mutt_hash_destroy(struct Hash **ptr); -void * mutt_hash_find(const struct Hash *table, const char *strkey); struct HashElem *mutt_hash_find_bucket(const struct Hash *table, const char *strkey); +void * mutt_hash_find(const struct Hash *table, const char *strkey); struct HashElem *mutt_hash_find_elem(const struct Hash *table, const char *strkey); +void mutt_hash_free(struct Hash **ptr); struct HashElem *mutt_hash_insert(struct Hash *table, const char *strkey, void *data); -struct Hash * mutt_hash_int_new(size_t nelem, int flags); void mutt_hash_int_delete(struct Hash *table, unsigned int intkey, const void *data); void * mutt_hash_int_find(const struct Hash *table, unsigned int intkey); struct HashElem *mutt_hash_int_insert(struct Hash *table, unsigned int intkey, void *data); -void mutt_hash_set_destructor(struct Hash *table, hash_destructor_t fn, intptr_t fn_data); +struct Hash * mutt_hash_int_new(size_t nelem, int flags); +struct Hash * mutt_hash_new(size_t nelem, int flags); +void mutt_hash_set_destructor(struct Hash *table, hashelem_free_t fn, intptr_t fn_data); struct HashElem *mutt_hash_typed_insert(struct Hash *table, const char *strkey, int type, void *data); /** diff --git a/mutt/history.c b/mutt/history.c index 724bd6340..ee6fc5ec3 100644 --- a/mutt/history.c +++ b/mutt/history.c @@ -299,7 +299,7 @@ cleanup: } if (HistoryRemoveDups) for (hclass = 0; hclass < HC_MAX; hclass++) - mutt_hash_destroy(&dup_hashes[hclass]); + mutt_hash_free(&dup_hashes[hclass]); } /** diff --git a/mutt_thread.c b/mutt_thread.c index 8ec31bfdd..d3d759170 100644 --- a/mutt_thread.c +++ b/mutt_thread.c @@ -614,7 +614,7 @@ void mutt_clear_threads(struct Context *ctx) ctx->tree = NULL; if (ctx->thread_hash) - mutt_hash_destroy(&ctx->thread_hash); + mutt_hash_free(&ctx->thread_hash); } /** diff --git a/mx.c b/mx.c index 0ec980d22..7da034ae5 100644 --- a/mx.c +++ b/mx.c @@ -416,9 +416,9 @@ void mx_fastclose_mailbox(struct Context *ctx) if (m->mx_ops) m->mx_ops->mbox_close(m); - mutt_hash_destroy(&m->subj_hash); - mutt_hash_destroy(&m->id_hash); - mutt_hash_destroy(&m->label_hash); + mutt_hash_free(&m->subj_hash); + mutt_hash_free(&m->id_hash); + mutt_hash_free(&m->label_hash); if (m->emails) { mutt_clear_threads(ctx); @@ -1254,13 +1254,13 @@ void mx_update_context(struct Context *ctx) if (m->subj_hash) { - mutt_hash_destroy(&m->subj_hash); + mutt_hash_free(&m->subj_hash); } m->subj_hash = NULL; if (m->id_hash) { - mutt_hash_destroy(&m->id_hash); + mutt_hash_free(&m->id_hash); } m->id_hash = NULL; diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 314a3fa99..c1694b7e2 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -116,14 +116,6 @@ void nntp_acache_free(struct NntpMboxData *mdata) } } -/** - * nntp_hash_destructor_t - Free our hash table data - Implements ::hash_destructor_t - */ -void nntp_hash_destructor_t(int type, void *obj, intptr_t data) -{ - nntp_mdata_free(&obj); -} - /** * nntp_newsrc_close - Unlock and close .newsrc file * @param adata NNTP server @@ -1168,7 +1160,7 @@ struct NntpAccountData *nntp_select_server(struct Mailbox *m, char *server, bool if (rc < 0) { - mutt_hash_destroy(&adata->groups_hash); + mutt_hash_free(&adata->groups_hash); FREE(&adata->groups_list); FREE(&adata->newsrc_file); FREE(&adata->authenticators); diff --git a/nntp/nntp.c b/nntp/nntp.c index dfc2696d1..af8b50566 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -133,6 +133,14 @@ static void nntp_adata_free(void **ptr) FREE(ptr); } +/** + * nntp_hashelem_free - Free our hash table data - Implements ::hashelem_free_t + */ +void nntp_hashelem_free(int type, void *obj, intptr_t data) +{ + nntp_mdata_free(&obj); +} + /** * nntp_adata_new - Allocate and initialise a new NntpAccountData structure * @retval ptr New NntpAccountData @@ -142,7 +150,7 @@ struct NntpAccountData *nntp_adata_new(struct Connection *conn) struct NntpAccountData *adata = mutt_mem_calloc(1, sizeof(struct NntpAccountData)); adata->conn = conn; adata->groups_hash = mutt_hash_new(1009, 0); - mutt_hash_set_destructor(adata->groups_hash, nntp_hash_destructor_t, 0); + mutt_hash_set_destructor(adata->groups_hash, nntp_hashelem_free, 0); adata->groups_max = 16; adata->groups_list = mutt_mem_malloc(adata->groups_max * sizeof(struct NntpMboxData *));