From 813ea5995bb5a35c115c46f30eefe18db2886afb Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 16 Dec 2013 16:52:36 +0100 Subject: [PATCH] core: allow dynamic strings for pending action names Use a GSList of the string values, instead of an array of GQuarks. Using GQuarks does not allow to add arbitrary strings, because they would leak the internalized strings. The next patch will begin using unique, non-const action strings. Given the rather small number of expected pending states, a singly linked list seems appropriate. Signed-off-by: Thomas Haller (some fixes and simplifications by dcbw based on patch reviews) --- src/devices/nm-device.c | 49 +++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index c1c7c69d6..e4644bf91 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -188,7 +188,7 @@ typedef struct { NMDeviceStateReason state_reason; QueuedState queued_state; guint queued_ip_config_id; - GArray *pending_actions; + GSList *pending_actions; char * udi; char * path; @@ -395,7 +395,6 @@ nm_device_init (NMDevice *self) priv->rfkill_type = RFKILL_TYPE_UNKNOWN; priv->autoconnect = DEFAULT_AUTOCONNECT; priv->available_connections = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, NULL); - priv->pending_actions = g_array_sized_new (FALSE, TRUE, sizeof (GQuark), 4); } static void @@ -5372,7 +5371,7 @@ finalize (GObject *object) if (priv->fw_manager) g_object_unref (priv->fw_manager); - g_array_free (priv->pending_actions, TRUE); + g_slist_free_full (priv->pending_actions, g_free); g_free (priv->udi); g_free (priv->path); @@ -7157,30 +7156,31 @@ void nm_device_add_pending_action (NMDevice *device, const char *action) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device); - GQuark qaction; - guint i; + GSList *iter; + guint count; - qaction = g_quark_from_static_string (action); + g_return_if_fail (action); /* Shouldn't ever add the same pending action twice */ - for (i = 0; i < priv->pending_actions->len; i++) { - if (qaction == g_array_index (priv->pending_actions, GQuark, i)) { + for (iter = priv->pending_actions; iter; iter = iter->next) { + if (!strcmp (action, iter->data)) { nm_log_warn (LOGD_DEVICE, "(%s): add_pending_action (%d): '%s' already added", nm_device_get_iface (device), - priv->pending_actions->len, + g_slist_length (priv->pending_actions), action); - g_warn_if_reached (); - return; + g_return_val_if_reached (FALSE); } } - g_array_append_val (priv->pending_actions, qaction); + priv->pending_actions = g_slist_append (priv->pending_actions, g_strdup (action)); + count = g_slist_length (priv->pending_actions); + nm_log_dbg (LOGD_DEVICE, "(%s): add_pending_action (%d): '%s'", nm_device_get_iface (device), - priv->pending_actions->len, + count, action); - if (priv->pending_actions->len == 1) + if (count == 1) g_object_notify (G_OBJECT (device), NM_DEVICE_HAS_PENDING_ACTION); } @@ -7195,21 +7195,21 @@ void nm_device_remove_pending_action (NMDevice *device, const char *action) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device); - GQuark qaction; - guint i; + GSList *iter; - qaction = g_quark_from_static_string (action); + g_return_if_fail (action); /* Shouldn't ever add the same pending action twice */ - for (i = 0; i < priv->pending_actions->len; i++) { - if (qaction == g_array_index (priv->pending_actions, GQuark, i)) { - g_array_remove_index (priv->pending_actions, i); + for (iter = priv->pending_actions; iter; iter = iter->next) { + if (!strcmp (action, iter->data)) { + g_free (iter->data); + priv->pending_actions = g_slist_delete_link (priv->pending_actions, iter); nm_log_dbg (LOGD_DEVICE, "(%s): remove_pending_action (%d): '%s'", nm_device_get_iface (device), - priv->pending_actions->len, + g_slist_length (priv->pending_actions), action); - if (priv->pending_actions->len == 0) + if (priv->pending_actions == NULL) g_object_notify (G_OBJECT (device), NM_DEVICE_HAS_PENDING_ACTION); return; } @@ -7217,8 +7217,9 @@ nm_device_remove_pending_action (NMDevice *device, const char *action) nm_log_warn (LOGD_DEVICE, "(%s): remove_pending_action (%d): '%s' never added", nm_device_get_iface (device), - priv->pending_actions->len, + g_slist_length (priv->pending_actions), action); + g_return_if_reached (); } gboolean @@ -7226,7 +7227,7 @@ nm_device_has_pending_action (NMDevice *device) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device); - return priv->pending_actions->len > 0; + return !!priv->pending_actions; } const char *