Use g_ptr_array_new_with_free_func() where reasonably.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2023-02-12 13:19:37 +03:00
parent 273fbd2afe
commit cf223bec27
14 changed files with 44 additions and 84 deletions

View File

@ -78,10 +78,9 @@ new_codepage_desc (const char *id, const char *name)
/* --------------------------------------------------------------------------------------------- */
static void
free_codepage_desc (gpointer data, gpointer user_data)
free_codepage_desc (gpointer data)
{
codepage_desc *desc = (codepage_desc *) data;
(void) user_data;
g_free (desc->id);
g_free (desc->name);
@ -216,7 +215,7 @@ load_codepages_list (void)
/* files are not found, add default codepage */
fprintf (stderr, "%s\n", _("Warning: cannot load codepages list"));
codepages = g_ptr_array_new ();
codepages = g_ptr_array_new_with_free_func (free_codepage_desc);
g_ptr_array_add (codepages, new_codepage_desc (DEFAULT_CHARSET, _("7-bit ASCII")));
}
}
@ -226,7 +225,6 @@ load_codepages_list (void)
void
free_codepages_list (void)
{
g_ptr_array_foreach (codepages, free_codepage_desc, NULL);
g_ptr_array_free (codepages, TRUE);
/* NULL-ize pointer to make unit tests happy */
codepages = NULL;

View File

@ -40,22 +40,16 @@
/*** file scope variables ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
mc_event_group_destroy_value (gpointer data)
{
GPtrArray *callbacks;
callbacks = (GPtrArray *) data;
g_ptr_array_foreach (callbacks, (GFunc) g_free, NULL);
g_ptr_array_free (callbacks, TRUE);
g_ptr_array_free ((GPtrArray *) data, TRUE);
}
/* --------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
@ -120,12 +114,8 @@ mc_event_del (const gchar * event_group_name, const gchar * event_name,
return;
cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
if (cb == NULL)
return;
g_ptr_array_remove (callbacks, (gpointer) cb);
g_free ((gpointer) cb);
if (cb != NULL)
g_ptr_array_remove (callbacks, (gpointer) cb);
}
/* --------------------------------------------------------------------------------------------- */
@ -194,7 +184,7 @@ mc_event_get_event_by_name (GTree * event_group, const gchar * event_name, gbool
callbacks = (GPtrArray *) g_tree_lookup (event_group, (gconstpointer) event_name);
if (callbacks == NULL && create_new)
{
callbacks = g_ptr_array_new ();
callbacks = g_ptr_array_new_with_free_func (g_free);
if (callbacks == NULL)
{
mc_propagate_error (mcerror, 0, _("Unable to create event '%s'!"), event_name);

View File

@ -38,10 +38,16 @@
/*** file scope variables ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
mc_fhl_filter_free (void *data)
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void
mc_fhl_filter_free (gpointer data)
{
mc_fhl_filter_t *filter = (mc_fhl_filter_t *) data;
@ -57,14 +63,9 @@ void
mc_fhl_array_free (mc_fhl_t * fhl)
{
if (fhl->filters != NULL)
{
g_ptr_array_foreach (fhl->filters, (GFunc) mc_fhl_filter_free, NULL);
fhl->filters = (GPtrArray *) g_ptr_array_free (fhl->filters, TRUE);
}
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
mc_fhl_t *

View File

@ -234,7 +234,7 @@ mc_fhl_parse_ini_file (mc_fhl_t * fhl)
gboolean ok;
mc_fhl_array_free (fhl);
fhl->filters = g_ptr_array_new ();
fhl->filters = g_ptr_array_new_with_free_func (mc_fhl_filter_free);
orig_group_names = mc_config_get_groups (fhl->config, NULL);
ok = (*orig_group_names != NULL);

View File

@ -51,6 +51,7 @@ typedef struct mc_fhl_filter_struct
/*** declarations of public functions ************************************************************/
void mc_fhl_filter_free (gpointer data);
void mc_fhl_array_free (mc_fhl_t * fhl);
gboolean mc_fhl_init_from_standard_files (mc_fhl_t * fhl);

View File

@ -91,8 +91,10 @@ mc_search__cond_struct_new (mc_search_t * lc_mc_search, const GString * str, con
/* --------------------------------------------------------------------------------------------- */
static void
mc_search__cond_struct_free (mc_search_cond_t * mc_search_cond)
mc_search__cond_struct_free (gpointer data)
{
mc_search_cond_t *mc_search_cond = (mc_search_cond_t *) data;
if (mc_search_cond->upper != NULL)
g_string_free (mc_search_cond->upper, TRUE);
@ -112,15 +114,6 @@ mc_search__cond_struct_free (mc_search_cond_t * mc_search_cond)
g_free (mc_search_cond);
}
/* --------------------------------------------------------------------------------------------- */
static void
mc_search__conditions_free (GPtrArray * array)
{
g_ptr_array_foreach (array, (GFunc) mc_search__cond_struct_free, NULL);
g_ptr_array_free (array, TRUE);
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
@ -187,7 +180,7 @@ mc_search_free (mc_search_t * lc_mc_search)
g_free (lc_mc_search->error_str);
if (lc_mc_search->prepared.conditions != NULL)
mc_search__conditions_free (lc_mc_search->prepared.conditions);
g_ptr_array_free (lc_mc_search->prepared.conditions, TRUE);
#ifdef SEARCH_TYPE_GLIB
if (lc_mc_search->regex_match_info != NULL)
@ -212,7 +205,7 @@ mc_search_prepare (mc_search_t * lc_mc_search)
if (lc_mc_search->prepared.conditions != NULL)
return lc_mc_search->prepared.result;
ret = g_ptr_array_new ();
ret = g_ptr_array_new_with_free_func (mc_search__cond_struct_free);
#ifdef HAVE_CHARSET
if (!lc_mc_search->is_all_charsets)
g_ptr_array_add (ret,

View File

@ -133,7 +133,7 @@ mc_skin_list (void)
{
GPtrArray *list;
list = g_ptr_array_new ();
list = g_ptr_array_new_with_free_func (g_free);
mc_skin_get_list_from_dir (mc_config_get_data_path (), list);
mc_skin_get_list_from_dir (mc_global.sysconfig_dir, list);
mc_skin_get_list_from_dir (mc_global.share_data_dir, list);

View File

@ -163,10 +163,7 @@ context_rule_free (gpointer rule)
g_free (r->keyword_first_chars);
if (r->keyword != NULL)
{
g_ptr_array_foreach (r->keyword, (GFunc) syntax_keyword_free, NULL);
g_ptr_array_free (r->keyword, TRUE);
}
g_free (r);
}
@ -941,7 +938,7 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
edit->rules = g_ptr_array_new ();
edit->rules = g_ptr_array_new_with_free_func (context_rule_free);
if (edit->defines == NULL)
edit->defines = g_tree_new ((GCompareFunc) strcmp);
@ -1086,7 +1083,7 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
c->first_left = c->left->str[0];
c->first_right = c->right->str[0];
}
c->keyword = g_ptr_array_new ();
c->keyword = g_ptr_array_new_with_free_func (syntax_keyword_free);
k = g_new0 (syntax_keyword_t, 1);
g_ptr_array_add (c->keyword, k);
no_words = FALSE;
@ -1480,7 +1477,6 @@ edit_free_syntax_rules (WEdit * edit)
edit_get_rule (edit, -1);
MC_PTR_FREE (edit->syntax_type);
g_ptr_array_foreach (edit->rules, (GFunc) context_rule_free, NULL);
g_ptr_array_free (edit->rules, TRUE);
edit->rules = NULL;
g_clear_slist (&edit->syntax_marker, g_free);
@ -1562,7 +1558,7 @@ edit_syntax_dialog (WEdit * edit)
GPtrArray *names;
int syntax;
names = g_ptr_array_new ();
names = g_ptr_array_new_with_free_func (g_free);
/* We fill the list of syntax files every time the editor is invoked.
Instead we could save the list to a file and update it once the syntax
@ -1603,7 +1599,6 @@ edit_syntax_dialog (WEdit * edit)
g_free (current_syntax);
}
g_ptr_array_foreach (names, (GFunc) g_free, NULL);
g_ptr_array_free (names, TRUE);
}

View File

@ -667,7 +667,6 @@ appearance_box (void)
}
g_free (current_skin_name);
g_ptr_array_foreach (skin_names, (GFunc) g_free, NULL);
g_ptr_array_free (skin_names, TRUE);
}

View File

@ -65,7 +65,7 @@ void
mcview_growbuf_init (WView * view)
{
view->growbuf_in_use = TRUE;
view->growbuf_blockptr = g_ptr_array_new ();
view->growbuf_blockptr = g_ptr_array_new_with_free_func (g_free);
view->growbuf_lastindex = VIEW_PAGE_SIZE;
view->growbuf_finished = FALSE;
}
@ -96,10 +96,7 @@ mcview_growbuf_free (WView * view)
{
g_assert (view->growbuf_in_use);
g_ptr_array_foreach (view->growbuf_blockptr, (GFunc) g_free, NULL);
(void) g_ptr_array_free (view->growbuf_blockptr, TRUE);
g_ptr_array_free (view->growbuf_blockptr, TRUE);
view->growbuf_blockptr = NULL;
view->growbuf_in_use = FALSE;
}

View File

@ -100,23 +100,20 @@ sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
static void
sigaction__init (void)
{
sigaction_signum__captured = g_ptr_array_new ();
sigaction_act__captured = g_ptr_array_new ();
sigaction_oldact__captured = g_ptr_array_new ();
sigaction_signum__captured = g_ptr_array_new_with_free_func (g_free);
sigaction_act__captured = g_ptr_array_new_with_free_func (g_free);
sigaction_oldact__captured = g_ptr_array_new_with_free_func (g_free);
}
static void
sigaction__deinit (void)
{
g_ptr_array_foreach (sigaction_signum__captured, (GFunc) g_free, NULL);
g_ptr_array_free (sigaction_signum__captured, TRUE);
sigaction_signum__captured = NULL;
g_ptr_array_foreach (sigaction_act__captured, (GFunc) g_free, NULL);
g_ptr_array_free (sigaction_act__captured, TRUE);
sigaction_act__captured = NULL;
g_ptr_array_foreach (sigaction_oldact__captured, (GFunc) g_free, NULL);
g_ptr_array_free (sigaction_oldact__captured, TRUE);
sigaction_oldact__captured = NULL;
}
@ -158,18 +155,16 @@ signal (int signum, sighandler_t handler)
static void
signal__init (void)
{
signal_signum__captured = g_ptr_array_new ();
signal_handler__captured = g_ptr_array_new ();
signal_signum__captured = g_ptr_array_new_with_free_func (g_free);
signal_handler__captured = g_ptr_array_new_with_free_func (g_free);
}
static void
signal__deinit (void)
{
g_ptr_array_foreach (signal_signum__captured, (GFunc) g_free, NULL);
g_ptr_array_free (signal_signum__captured, TRUE);
signal_signum__captured = NULL;
g_ptr_array_foreach (signal_handler__captured, (GFunc) g_free, NULL);
g_ptr_array_free (signal_handler__captured, TRUE);
signal_handler__captured = NULL;
}
@ -222,13 +217,12 @@ execvp (const char *file, char *const argv[])
static void
execvp__init (void)
{
execvp__args__captured = g_ptr_array_new ();
execvp__args__captured = g_ptr_array_new_with_free_func (g_free);
}
static void
execvp__deinit (void)
{
g_ptr_array_foreach (execvp__args__captured, (GFunc) g_free, NULL);
g_ptr_array_free (execvp__args__captured, TRUE);
execvp__args__captured = NULL;
MC_PTR_FREE (execvp__file__captured);

View File

@ -48,22 +48,21 @@ vfs_file_is_local (const vfs_path_t * vpath)
}
static void
vfs_file_is_local__init (void)
vpath_captured_free (gpointer data)
{
vfs_file_is_local__vpath__captured = g_ptr_array_new ();
vfs_path_free ((vfs_path_t *) data, TRUE);
}
static void
vpath_captured_free (gpointer data, gpointer user_data)
vfs_file_is_local__init (void)
{
(void) user_data;
vfs_path_free ((vfs_path_t *) data, TRUE);
vfs_file_is_local__vpath__captured =
g_ptr_array_new_with_free_func (vpath_captured_free);
}
static void
vfs_file_is_local__deinit (void)
{
g_ptr_array_foreach (vfs_file_is_local__vpath__captured, vpath_captured_free, NULL);
g_ptr_array_free (vfs_file_is_local__vpath__captured, TRUE);
}
@ -193,13 +192,12 @@ mc_stat (const vfs_path_t * vpath, struct stat *stat_ignored)
static void
mc_stat__init (void)
{
mc_stat__vpath__captured = g_ptr_array_new ();
mc_stat__vpath__captured = g_ptr_array_new_with_free_func (vpath_captured_free);
}
static void
mc_stat__deinit (void)
{
g_ptr_array_foreach (mc_stat__vpath__captured, vpath_captured_free, NULL);
g_ptr_array_free (mc_stat__vpath__captured, TRUE);
mc_stat__vpath__captured = NULL;
}

View File

@ -97,7 +97,7 @@ static void
do_executev__init (void)
{
do_executev__lc_shell__captured = NULL;
do_executev__argv__captured = g_ptr_array_new ();
do_executev__argv__captured = g_ptr_array_new_with_free_func (g_free);
do_executev__flags__captured = 0;
}
@ -105,7 +105,6 @@ static void
do_executev__deinit (void)
{
g_free (do_executev__lc_shell__captured);
g_ptr_array_foreach (do_executev__argv__captured, (GFunc) g_free, NULL);
g_ptr_array_free (do_executev__argv__captured, TRUE);
}

View File

@ -67,9 +67,9 @@ mc_config_get_string_raw (mc_config_t * config_ignored, const gchar * group,
static void
mc_config_get_string__init (void)
{
mc_config_get_string__group__captured = g_ptr_array_new ();
mc_config_get_string__param__captured = g_ptr_array_new ();
mc_config_get_string__default_value__captured = g_ptr_array_new ();
mc_config_get_string__group__captured = g_ptr_array_new_with_free_func (g_free);
mc_config_get_string__param__captured = g_ptr_array_new_with_free_func (g_free);
mc_config_get_string__default_value__captured = g_ptr_array_new_with_free_func (g_free);
mc_config_get_string__return_value = g_ptr_array_new ();
}
@ -77,13 +77,8 @@ mc_config_get_string__init (void)
static void
mc_config_get_string__deinit (void)
{
g_ptr_array_foreach (mc_config_get_string__group__captured, (GFunc) g_free, NULL);
g_ptr_array_free (mc_config_get_string__group__captured, TRUE);
g_ptr_array_foreach (mc_config_get_string__param__captured, (GFunc) g_free, NULL);
g_ptr_array_free (mc_config_get_string__param__captured, TRUE);
g_ptr_array_foreach (mc_config_get_string__default_value__captured, (GFunc) g_free, NULL);
g_ptr_array_free (mc_config_get_string__default_value__captured, TRUE);
g_ptr_array_foreach (mc_config_get_string__return_value, (GFunc) g_free, NULL);