From ca1fe95ce0aac9b19a7e6c16fcf52b4046370eb0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 20 May 2019 17:08:35 +0200 Subject: [PATCH] settings: use nm_utils_g_slist_find_str() in update_specs() NMSettings is complicated enough. We should try to move independent code out of it, so that there is only logic that is essential there. While at it, rework how we copy the GSList items. I don't like GSList as a data structure, but there really is no need to allocate a new list. Just unlink the list element and prepend it in the other list. --- src/settings/nm-settings.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 0e7dfcd337..b0f37f62f3 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -535,18 +535,6 @@ nm_settings_get_unmanaged_specs (NMSettings *self) return priv->unmanaged_specs; } -static gboolean -find_spec (GSList *spec_list, const char *spec) -{ - GSList *iter; - - for (iter = spec_list; iter; iter = g_slist_next (iter)) { - if (!strcmp ((const char *) iter->data, spec)) - return TRUE; - } - return FALSE; -} - static void update_specs (NMSettings *self, GSList **specs_ptr, GSList * (*get_specs_func) (NMSettingsPlugin *)) @@ -554,21 +542,24 @@ update_specs (NMSettings *self, GSList **specs_ptr, NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); GSList *iter; - g_slist_free_full (*specs_ptr, g_free); - *specs_ptr = NULL; + g_slist_free_full (g_steal_pointer (specs_ptr), g_free); for (iter = priv->plugins; iter; iter = g_slist_next (iter)) { - GSList *specs, *specs_iter; + GSList *specs; - specs = get_specs_func (NM_SETTINGS_PLUGIN (iter->data)); - for (specs_iter = specs; specs_iter; specs_iter = specs_iter->next) { - if (!find_spec (*specs_ptr, (const char *) specs_iter->data)) { - *specs_ptr = g_slist_prepend (*specs_ptr, specs_iter->data); - } else - g_free (specs_iter->data); + specs = get_specs_func (iter->data); + while (specs) { + GSList *s = specs; + + specs = g_slist_remove_link (specs, s); + if (nm_utils_g_slist_find_str (*specs_ptr, s->data)) { + g_free (s->data); + g_slist_free_1 (s); + continue; + } + s->next = *specs_ptr; + *specs_ptr = s; } - - g_slist_free (specs); } }