Merge pull request #1643 from philix/ga_deep_clear

GA_DEEP_CLEAR macro for garray memory deallocation
This commit is contained in:
Justin M. Keyes 2014-12-11 19:05:58 -05:00
commit ab1f0bd119
10 changed files with 86 additions and 108 deletions

View File

@ -5420,20 +5420,12 @@ static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int
{ {
garray_T join_ga; garray_T join_ga;
int retval; int retval;
join_T *p;
ga_init(&join_ga, (int)sizeof(join_T), l->lv_len); ga_init(&join_ga, (int)sizeof(join_T), l->lv_len);
retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga); retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
/* Dispose each item in join_ga. */ # define FREE_JOIN_TOFREE(join) free((join)->tofree)
if (join_ga.ga_data != NULL) { GA_DEEP_CLEAR(&join_ga, join_T, FREE_JOIN_TOFREE);
p = (join_T *)join_ga.ga_data;
for (int i = 0; i < join_ga.ga_len; ++i) {
free(p->tofree);
++p;
}
ga_clear(&join_ga);
}
return retval; return retval;
} }

View File

@ -5625,9 +5625,7 @@ helptags_one (
if (mix) if (mix)
got_int = FALSE; /* continue with other languages */ got_int = FALSE; /* continue with other languages */
for (int i = 0; i < ga.ga_len; ++i) GA_DEEP_CLEAR_PTR(&ga);
free(((char_u **)ga.ga_data)[i]);
ga_clear(&ga);
fclose(fd_tags); /* there is no check for an error... */ fclose(fd_tags); /* there is no check for an error... */
} }

View File

@ -2580,13 +2580,11 @@ char_u *get_scriptname(scid_T id)
} }
# if defined(EXITFREE) || defined(PROTO) # if defined(EXITFREE) || defined(PROTO)
void free_scriptnames(void) void free_scriptnames()
{ {
for (int i = script_items.ga_len; i > 0; --i) # define FREE_SCRIPTNAME(item) free((item)->sn_name)
free(SCRIPT_ITEM(i).sn_name); GA_DEEP_CLEAR(&script_items, scriptitem_T, FREE_SCRIPTNAME);
ga_clear(&script_items);
} }
# endif # endif

View File

@ -97,6 +97,8 @@ typedef struct {
linenr_T lnum; /* sourcing_lnum of the line */ linenr_T lnum; /* sourcing_lnum of the line */
} wcmd_T; } wcmd_T;
#define FREE_WCMD(wcmd) free((wcmd)->line)
/* /*
* Structure used to store info for line position in a while or for loop. * Structure used to store info for line position in a while or for loop.
* This is required, because do_one_cmd() may invoke ex_function(), which * This is required, because do_one_cmd() may invoke ex_function(), which
@ -708,9 +710,8 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline,
*/ */
if (cstack.cs_looplevel == 0) { if (cstack.cs_looplevel == 0) {
if (!GA_EMPTY(&lines_ga)) { if (!GA_EMPTY(&lines_ga)) {
sourcing_lnum = sourcing_lnum = ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum; GA_DEEP_CLEAR(&lines_ga, wcmd_T, FREE_WCMD);
free_cmdlines(&lines_ga);
} }
current_line = 0; current_line = 0;
} }
@ -777,8 +778,7 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline,
free(cmdline_copy); free(cmdline_copy);
did_emsg_syntax = FALSE; did_emsg_syntax = FALSE;
free_cmdlines(&lines_ga); GA_DEEP_CLEAR(&lines_ga, wcmd_T, FREE_WCMD);
ga_clear(&lines_ga);
if (cstack.cs_idx >= 0) { if (cstack.cs_idx >= 0) {
/* /*
@ -1017,17 +1017,6 @@ static void store_loop_line(garray_T *gap, char_u *line)
p->lnum = sourcing_lnum; p->lnum = sourcing_lnum;
} }
/*
* Free the lines stored for a ":while" or ":for" loop.
*/
static void free_cmdlines(garray_T *gap)
{
while (!GA_EMPTY(gap)) {
free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
--gap->ga_len;
}
}
/* /*
* If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
* "func". * Otherwise return TRUE when "fgetline" equals "func". * "func". * Otherwise return TRUE when "fgetline" equals "func".
@ -4546,20 +4535,18 @@ void ex_comclear(exarg_T *eap)
uc_clear(&curbuf->b_ucmds); uc_clear(&curbuf->b_ucmds);
} }
static void free_ucmd(ucmd_T* cmd) {
free(cmd->uc_name);
free(cmd->uc_rep);
free(cmd->uc_compl_arg);
}
/* /*
* Clear all user commands for "gap". * Clear all user commands for "gap".
*/ */
void uc_clear(garray_T *gap) void uc_clear(garray_T *gap)
{ {
ucmd_T *cmd; GA_DEEP_CLEAR(gap, ucmd_T, free_ucmd);
for (int i = 0; i < gap->ga_len; ++i) {
cmd = USER_CMD_GA(gap, i);
free(cmd->uc_name);
free(cmd->uc_rep);
free(cmd->uc_compl_arg);
}
ga_clear(gap);
} }
static void ex_delcommand(exarg_T *eap) static void ex_delcommand(exarg_T *eap)
@ -5488,9 +5475,8 @@ static void ex_goto(exarg_T *eap)
*/ */
void alist_clear(alist_T *al) void alist_clear(alist_T *al)
{ {
while (--al->al_ga.ga_len >= 0) # define FREE_AENTRY_FNAME(arg) free(arg->ae_fname)
free(AARGLIST(al)[al->al_ga.ga_len].ae_fname); GA_DEEP_CLEAR(&al->al_ga, aentry_T, FREE_AENTRY_FNAME);
ga_clear(&al->al_ga);
} }
/* /*

View File

@ -1336,9 +1336,8 @@ static void deleteFoldEntry(garray_T *gap, int idx, int recursive)
*/ */
void deleteFoldRecurse(garray_T *gap) void deleteFoldRecurse(garray_T *gap)
{ {
for (int i = 0; i < gap->ga_len; ++i) # define DELETE_FOLD_NESTED(fd) deleteFoldRecurse(&((fd)->fd_nested))
deleteFoldRecurse(&(((fold_T *)(gap->ga_data))[i].fd_nested)); GA_DEEP_CLEAR(gap, fold_T, DELETE_FOLD_NESTED);
ga_clear(gap);
} }
/* foldMarkAdjust() {{{2 */ /* foldMarkAdjust() {{{2 */

View File

@ -37,10 +37,7 @@ void ga_clear(garray_T *gap)
/// @param gap /// @param gap
void ga_clear_strings(garray_T *gap) void ga_clear_strings(garray_T *gap)
{ {
for (int i = 0; i < gap->ga_len; ++i) { GA_DEEP_CLEAR_PTR(gap);
free(((char_u **)(gap->ga_data))[i]);
}
ga_clear(gap);
} }
/// Initialize a growing array. /// Initialize a growing array.

View File

@ -43,4 +43,30 @@ static inline void *ga_append_via_ptr(garray_T *gap, size_t item_size)
return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++); return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++);
} }
/// Deep free a garray of specific type using a custom free function.
/// Items in the array as well as the array itself are freed.
///
/// @param gap the garray to be freed
/// @param item_type type of the item in the garray
/// @param free_item_fn free function that takes (*item_type) as parameter
#define GA_DEEP_CLEAR(gap, item_type, free_item_fn) \
do { \
garray_T *_gap = (gap); \
if (_gap->ga_data != NULL) { \
for (int i = 0; i < _gap->ga_len; i++) { \
item_type *_item = &(((item_type *)_gap->ga_data)[i]); \
free_item_fn(_item); \
} \
} \
ga_clear(_gap); \
} while (false)
#define FREE_PTR_PTR(ptr) free(*(ptr))
/// Call `free` for every pointer stored in the garray and then frees the
/// garray.
///
/// @param gap the garray to be freed
#define GA_DEEP_CLEAR_PTR(gap) GA_DEEP_CLEAR(gap, void*, FREE_PTR_PTR)
#endif // NVIM_GARRAY_H #endif // NVIM_GARRAY_H

View File

@ -1424,6 +1424,12 @@ typedef struct {
static garray_T menutrans_ga = GA_EMPTY_INIT_VALUE; static garray_T menutrans_ga = GA_EMPTY_INIT_VALUE;
#define FREE_MENUTRANS(mt) \
menutrans_T* _mt = (mt); \
free(_mt->from); \
free(_mt->from_noamp); \
free(_mt->to)
/* /*
* ":menutrans". * ":menutrans".
* This function is also defined without the +multi_lang feature, in which * This function is also defined without the +multi_lang feature, in which
@ -1441,13 +1447,8 @@ void ex_menutranslate(exarg_T *eap)
* ":menutrans clear": clear all translations. * ":menutrans clear": clear all translations.
*/ */
if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5))) { if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5))) {
menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data; GA_DEEP_CLEAR(&menutrans_ga, menutrans_T, FREE_MENUTRANS);
for (int i = 0; i < menutrans_ga.ga_len; ++i) {
free(tp[i].from);
free(tp[i].from_noamp);
free(tp[i].to);
}
ga_clear(&menutrans_ga);
/* Delete all "menutrans_" global variables. */ /* Delete all "menutrans_" global variables. */
del_menutrans_vars(); del_menutrans_vars();
} else { } else {

View File

@ -2379,13 +2379,26 @@ static void slang_free(slang_T *lp)
free(lp); free(lp);
} }
/// Frees a salitem_T
static void free_salitem(salitem_T *smp) {
free(smp->sm_lead);
// Don't free sm_oneof and sm_rules, they point into sm_lead.
free(smp->sm_to);
free(smp->sm_lead_w);
free(smp->sm_oneof_w);
free(smp->sm_to_w);
}
/// Frees a fromto_T
static void free_fromto(fromto_T *ftp) {
free(ftp->ft_from);
free(ftp->ft_to);
}
// Clear an slang_T so that the file can be reloaded. // Clear an slang_T so that the file can be reloaded.
static void slang_clear(slang_T *lp) static void slang_clear(slang_T *lp)
{ {
garray_T *gap; garray_T *gap;
fromto_T *ftp;
salitem_T *smp;
int round;
free(lp->sl_fbyts); free(lp->sl_fbyts);
lp->sl_fbyts = NULL; lp->sl_fbyts = NULL;
@ -2401,36 +2414,17 @@ static void slang_clear(slang_T *lp)
free(lp->sl_pidxs); free(lp->sl_pidxs);
lp->sl_pidxs = NULL; lp->sl_pidxs = NULL;
for (round = 1; round <= 2; ++round) { GA_DEEP_CLEAR(&lp->sl_rep, fromto_T, free_fromto);
gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal; GA_DEEP_CLEAR(&lp->sl_repsal, fromto_T, free_fromto);
while (!GA_EMPTY(gap)) {
ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
free(ftp->ft_from);
free(ftp->ft_to);
}
ga_clear(gap);
}
gap = &lp->sl_sal; gap = &lp->sl_sal;
if (lp->sl_sofo) { if (lp->sl_sofo) {
// "ga_len" is set to 1 without adding an item for latin1 // "ga_len" is set to 1 without adding an item for latin1
if (gap->ga_data != NULL) GA_DEEP_CLEAR_PTR(gap);
// SOFOFROM and SOFOTO items: free lists of wide characters. } else {
for (int i = 0; i < gap->ga_len; ++i) {
free(((int **)gap->ga_data)[i]);
}
} else
// SAL items: free salitem_T items // SAL items: free salitem_T items
while (!GA_EMPTY(gap)) { GA_DEEP_CLEAR(gap, salitem_T, free_salitem);
smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; }
free(smp->sm_lead);
// Don't free sm_oneof and sm_rules, they point into sm_lead.
free(smp->sm_to);
free(smp->sm_lead_w);
free(smp->sm_oneof_w);
free(smp->sm_to_w);
}
ga_clear(gap);
for (int i = 0; i < lp->sl_prefixcnt; ++i) { for (int i = 0; i < lp->sl_prefixcnt; ++i) {
vim_regfree(lp->sl_prefprog[i]); vim_regfree(lp->sl_prefprog[i]);
@ -9206,15 +9200,10 @@ static void tree_count_words(char_u *byts, idx_T *idxs)
// Free the info put in "*su" by spell_find_suggest(). // Free the info put in "*su" by spell_find_suggest().
static void spell_find_cleanup(suginfo_T *su) static void spell_find_cleanup(suginfo_T *su)
{ {
# define FREE_SUG_WORD(sug) free(sug->st_word)
// Free the suggestions. // Free the suggestions.
for (int i = 0; i < su->su_ga.ga_len; ++i) { GA_DEEP_CLEAR(&su->su_ga, suggest_T, FREE_SUG_WORD);
free(SUG(su->su_ga, i).st_word); GA_DEEP_CLEAR(&su->su_sga, suggest_T, FREE_SUG_WORD);
}
ga_clear(&su->su_ga);
for (int i = 0; i < su->su_sga.ga_len; ++i) {
free(SUG(su->su_sga, i).st_word);
}
ga_clear(&su->su_sga);
// Free the banned words. // Free the banned words.
hash_clear_all(&su->su_banned, 0); hash_clear_all(&su->su_banned, 0);

View File

@ -550,14 +550,9 @@ void syntax_start(win_T *wp, linenr_T lnum)
*/ */
static void clear_syn_state(synstate_T *p) static void clear_syn_state(synstate_T *p)
{ {
garray_T *gap;
if (p->sst_stacksize > SST_FIX_STATES) { if (p->sst_stacksize > SST_FIX_STATES) {
gap = &(p->sst_union.sst_ga); # define UNREF_BUFSTATE_EXTMATCH(bs) unref_extmatch((bs)->bs_extmatch)
for (int i = 0; i < gap->ga_len; i++) { GA_DEEP_CLEAR(&(p->sst_union.sst_ga), bufstate_T, UNREF_BUFSTATE_EXTMATCH);
unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
}
ga_clear(gap);
} else { } else {
for (int i = 0; i < p->sst_stacksize; i++) { for (int i = 0; i < p->sst_stacksize; i++) {
unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch); unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
@ -570,11 +565,8 @@ static void clear_syn_state(synstate_T *p)
*/ */
static void clear_current_state(void) static void clear_current_state(void)
{ {
stateitem_T *sip = (stateitem_T *)(current_state.ga_data); # define UNREF_STATEITEM_EXTMATCH(si) unref_extmatch((si)->si_extmatch)
for (int i = 0; i < current_state.ga_len; i++) { GA_DEEP_CLEAR(&current_state, stateitem_T, UNREF_STATEITEM_EXTMATCH);
unref_extmatch(sip[i].si_extmatch);
}
ga_clear(&current_state);
} }
/* /*