mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-06 04:28:29 +02:00
wifi: refactor getting sorted AP list
Instead of creating a GSList use an array. That way, we save the allocation and free of an GSList instance. Also, avoid cloning the export path. It is stable.
This commit is contained in:
parent
674f5f24af
commit
7c601ab8ca
1 changed files with 68 additions and 42 deletions
|
|
@ -997,66 +997,92 @@ can_auto_connect (NMDevice *device,
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gint
|
static int
|
||||||
ap_id_compare (NMWifiAP *a, NMWifiAP *b)
|
ap_id_compare (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
|
||||||
{
|
{
|
||||||
guint32 a_id = nm_wifi_ap_get_id (a);
|
guint32 a_id = nm_wifi_ap_get_id (*((NMWifiAP **) p_a));
|
||||||
guint32 b_id = nm_wifi_ap_get_id (b);
|
guint32 b_id = nm_wifi_ap_get_id (*((NMWifiAP **) p_b));
|
||||||
|
|
||||||
return a_id < b_id ? -1 : (a_id == b_id ? 0 : 1);
|
return a_id < b_id ? -1 : (a_id == b_id ? 0 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GSList *
|
static NMWifiAP **
|
||||||
get_sorted_ap_list (NMDeviceWifi *self)
|
ap_list_get_sorted (NMDeviceWifi *self, gboolean include_without_ssid)
|
||||||
{
|
{
|
||||||
GSList *sorted = NULL;
|
NMDeviceWifiPrivate *priv;
|
||||||
|
NMWifiAP **list;
|
||||||
GHashTableIter iter;
|
GHashTableIter iter;
|
||||||
NMWifiAP *ap;
|
NMWifiAP *ap;
|
||||||
|
gsize i, n;
|
||||||
|
|
||||||
g_hash_table_iter_init (&iter, NM_DEVICE_WIFI_GET_PRIVATE (self)->aps);
|
priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
|
||||||
while (g_hash_table_iter_next (&iter, NULL, (gpointer) &ap))
|
|
||||||
sorted = g_slist_prepend (sorted, ap);
|
n = g_hash_table_size (priv->aps);
|
||||||
return g_slist_sort (sorted, (GCompareFunc) ap_id_compare);
|
list = g_new (NMWifiAP *, n + 1);
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (n > 0) {
|
||||||
|
g_hash_table_iter_init (&iter, priv->aps);
|
||||||
|
while (g_hash_table_iter_next (&iter, NULL, (gpointer) &ap)) {
|
||||||
|
nm_assert (i < n);
|
||||||
|
if ( include_without_ssid
|
||||||
|
|| nm_wifi_ap_get_ssid (ap))
|
||||||
|
list[i++] = ap;
|
||||||
|
}
|
||||||
|
nm_assert (i <= n);
|
||||||
|
nm_assert (!include_without_ssid || i == n);
|
||||||
|
|
||||||
|
g_qsort_with_data (list,
|
||||||
|
i,
|
||||||
|
sizeof (gpointer),
|
||||||
|
ap_id_compare,
|
||||||
|
NULL);
|
||||||
|
}
|
||||||
|
list[i] = NULL;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char **
|
||||||
|
ap_list_get_sorted_paths (NMDeviceWifi *self, gboolean include_without_ssid)
|
||||||
|
{
|
||||||
|
gpointer *list;
|
||||||
|
gsize i, j;
|
||||||
|
|
||||||
|
list = (gpointer *) ap_list_get_sorted (self, include_without_ssid);
|
||||||
|
for (i = 0, j = 0; list[i]; i++) {
|
||||||
|
NMWifiAP *ap = list[i];
|
||||||
|
const char *path;
|
||||||
|
|
||||||
|
/* update @list inplace to hold instead the export-path. */
|
||||||
|
path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (ap));
|
||||||
|
nm_assert (path);
|
||||||
|
list[j++] = (gpointer) path;
|
||||||
|
}
|
||||||
|
return (const char **) list;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
impl_device_wifi_get_access_points (NMDeviceWifi *self,
|
impl_device_wifi_get_access_points (NMDeviceWifi *self,
|
||||||
GDBusMethodInvocation *context)
|
GDBusMethodInvocation *context)
|
||||||
{
|
{
|
||||||
GSList *sorted, *iter;
|
gs_free const char **list = NULL;
|
||||||
GPtrArray *paths;
|
GVariant *v;
|
||||||
|
|
||||||
paths = g_ptr_array_new ();
|
list = ap_list_get_sorted_paths (self, FALSE);
|
||||||
sorted = get_sorted_ap_list (self);
|
v = g_variant_new_objv (list, -1);
|
||||||
for (iter = sorted; iter; iter = iter->next) {
|
g_dbus_method_invocation_return_value (context, g_variant_new_tuple (&v, 1));
|
||||||
NMWifiAP *ap = NM_WIFI_AP (iter->data);
|
|
||||||
|
|
||||||
if (nm_wifi_ap_get_ssid (ap))
|
|
||||||
g_ptr_array_add (paths, g_strdup (nm_exported_object_get_path (NM_EXPORTED_OBJECT (ap))));
|
|
||||||
}
|
|
||||||
g_ptr_array_add (paths, NULL);
|
|
||||||
g_slist_free (sorted);
|
|
||||||
|
|
||||||
g_dbus_method_invocation_return_value (context, g_variant_new ("(^ao)", (char **) paths->pdata));
|
|
||||||
g_ptr_array_unref (paths);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
impl_device_wifi_get_all_access_points (NMDeviceWifi *self,
|
impl_device_wifi_get_all_access_points (NMDeviceWifi *self,
|
||||||
GDBusMethodInvocation *context)
|
GDBusMethodInvocation *context)
|
||||||
{
|
{
|
||||||
GSList *sorted, *iter;
|
gs_free const char **list = NULL;
|
||||||
GPtrArray *paths;
|
GVariant *v;
|
||||||
|
|
||||||
paths = g_ptr_array_new ();
|
list = ap_list_get_sorted_paths (self, TRUE);
|
||||||
sorted = get_sorted_ap_list (self);
|
v = g_variant_new_objv (list, -1);
|
||||||
for (iter = sorted; iter; iter = iter->next)
|
g_dbus_method_invocation_return_value (context, g_variant_new_tuple (&v, 1));
|
||||||
g_ptr_array_add (paths, g_strdup (nm_exported_object_get_path (NM_EXPORTED_OBJECT (iter->data))));
|
|
||||||
g_ptr_array_add (paths, NULL);
|
|
||||||
g_slist_free (sorted);
|
|
||||||
|
|
||||||
g_dbus_method_invocation_return_value (context, g_variant_new ("(^ao)", (char **) paths->pdata));
|
|
||||||
g_ptr_array_unref (paths);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
@ -1523,17 +1549,17 @@ ap_list_dump (gpointer user_data)
|
||||||
{
|
{
|
||||||
NMDeviceWifi *self = NM_DEVICE_WIFI (user_data);
|
NMDeviceWifi *self = NM_DEVICE_WIFI (user_data);
|
||||||
NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
|
NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
|
||||||
GSList *sorted, *iter;
|
gs_free NMWifiAP **list = NULL;
|
||||||
|
gsize i;
|
||||||
|
|
||||||
priv->ap_dump_id = 0;
|
priv->ap_dump_id = 0;
|
||||||
_LOGD (LOGD_WIFI_SCAN, "APs: [now:%u last:%u next:%u]",
|
_LOGD (LOGD_WIFI_SCAN, "APs: [now:%u last:%u next:%u]",
|
||||||
nm_utils_get_monotonic_timestamp_s (),
|
nm_utils_get_monotonic_timestamp_s (),
|
||||||
priv->last_scan,
|
priv->last_scan,
|
||||||
priv->scheduled_scan_time);
|
priv->scheduled_scan_time);
|
||||||
sorted = get_sorted_ap_list (self);
|
list = ap_list_get_sorted (self, TRUE);
|
||||||
for (iter = sorted; iter; iter = iter->next)
|
for (i = 0; list[i]; i++)
|
||||||
nm_wifi_ap_dump (NM_WIFI_AP (iter->data), "dump ", nm_device_get_iface (NM_DEVICE (self)));
|
nm_wifi_ap_dump (list[i], "dump ", nm_device_get_iface (NM_DEVICE (self)));
|
||||||
g_slist_free (sorted);
|
|
||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue