From 6e54057bf754d9311e42d92956feb9b1036ca654 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 17 May 2016 11:03:53 +0200 Subject: [PATCH] settings: add nm_settings_get_connections() This will replace nm_connection_provider_get_connections(), but has a different API. Instead of returning a (const) GSList list, it returns a (cached) NULL terminated array. The reason for this change is simply that I find arrays more convenient to use (in this case) and it doesn't have the overhead of a GSList instance per entry. Like with nm_connection_provider_get_connections(), cache the result internally. This for one is more convenient for the caller, which doesn't need to free the result. On the other hand, the list of connections is fairly static, this allows us to reuse the same list. --- src/settings/nm-settings.c | 51 ++++++++++++++++++++++++++++++++++++++ src/settings/nm-settings.h | 5 ++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index db771f88f..95c2b1b95 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -152,6 +152,7 @@ typedef struct { GSList *plugins; gboolean connections_loaded; GHashTable *connections; + NMSettingsConnection **connections_cached_list; GSList *unmanaged_specs; GSList *unrecognized_specs; GSList *get_connections_cache; @@ -409,6 +410,53 @@ connection_sort (gconstpointer pa, gconstpointer pb) return 1; } +/** + * nm_settings_get_connections: + * @self: the #NMSettings + * @out_len: (out): (allow-none): returns the number of returned + * connections. + * + * Returns: (transfer-none): a list of NMSettingsConnections. The list is + * unsorted and NULL terminated. The result is never %NULL, in case of no + * connections, it returns an empty list. + * The returned list is cached internally, only valid until the next + * NMSettings operation. + */ +NMSettingsConnection *const* +nm_settings_get_connections (NMSettings *self, guint *out_len) +{ + GHashTableIter iter; + NMSettingsPrivate *priv; + guint l, i; + NMSettingsConnection **v; + NMSettingsConnection *con; + + g_return_val_if_fail (NM_IS_SETTINGS (self), NULL); + + priv = NM_SETTINGS_GET_PRIVATE (self); + + if (priv->connections_cached_list) { + NM_SET_OUT (out_len, g_hash_table_size (priv->connections)); + return priv->connections_cached_list; + } + + l = g_hash_table_size (priv->connections); + + v = g_new (NMSettingsConnection *, l + 1); + + i = 0; + g_hash_table_iter_init (&iter, priv->connections); + while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &con)) + v[i++] = con; + v[i] = NULL; + + nm_assert (i == l); + + NM_SET_OUT (out_len, l); + priv->connections_cached_list = v; + return v; +} + /* Returns a list of NMSettingsConnections. * The list is sorted in the order suitable for auto-connecting, i.e. * first go connections with autoconnect=yes and most recent timestamp. @@ -934,6 +982,7 @@ connection_removed (NMSettingsConnection *connection, gpointer user_data) /* Forget about the connection internally */ g_hash_table_remove (priv->connections, (gpointer) cpath); + g_clear_pointer (&priv->connections_cached_list, g_free); /* Notify D-Bus */ g_signal_emit (self, signals[CONNECTION_REMOVED], 0, connection); @@ -1078,6 +1127,7 @@ claim_connection (NMSettings *self, NMSettingsConnection *connection) g_hash_table_insert (priv->connections, (gpointer) nm_connection_get_path (NM_CONNECTION (connection)), g_object_ref (connection)); + g_clear_pointer (&priv->connections_cached_list, g_free); nm_utils_log_connection_diff (NM_CONNECTION (connection), NULL, LOGL_DEBUG, LOGD_CORE, "new connection", "++ "); @@ -2391,6 +2441,7 @@ finalize (GObject *object) NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); g_hash_table_destroy (priv->connections); + g_clear_pointer (&priv->connections_cached_list, g_free); g_slist_free (priv->get_connections_cache); g_slist_free_full (priv->unmanaged_specs, g_free); diff --git a/src/settings/nm-settings.h b/src/settings/nm-settings.h index 417cfeb9f..c855d3a84 100644 --- a/src/settings/nm-settings.h +++ b/src/settings/nm-settings.h @@ -89,9 +89,8 @@ void nm_settings_add_connection_dbus (NMSettings *self, NMSettingsAddCallback callback, gpointer user_data); -/* Returns a list of NMSettingsConnections. Caller must free the list with - * g_slist_free(). - */ +NMSettingsConnection *const* nm_settings_get_connections (NMSettings *settings, guint *out_len); + GSList *nm_settings_get_connections_sorted (NMSettings *settings); NMSettingsConnection *nm_settings_add_connection (NMSettings *settings,