From 08f5681b0ebc1bf085ca3d12180bac6e61bf6744 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 23 Sep 2016 15:03:41 +0200 Subject: [PATCH 1/9] core: const arguments for _nm_utils_ptrarray_find_*() functions --- libnm-core/nm-core-internal.h | 4 ++-- libnm-core/nm-utils.c | 6 ++---- libnm-core/tests/test-general.c | 9 ++++----- src/nm-route-manager.c | 2 +- src/platform/nm-linux-platform.c | 8 ++++---- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index e491bce6da..111b26d588 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -144,9 +144,9 @@ GPtrArray *_nm_utils_copy_array (const GPtrArray *array, GDestroyNotify free_func); GPtrArray *_nm_utils_copy_object_array (const GPtrArray *array); -gssize _nm_utils_ptrarray_find_first (gpointer *list, gssize len, gconstpointer needle); +gssize _nm_utils_ptrarray_find_first (gconstpointer *list, gssize len, gconstpointer needle); -gssize _nm_utils_ptrarray_find_binary_search (gpointer *list, gsize len, gpointer needle, GCompareDataFunc cmpfcn, gpointer user_data); +gssize _nm_utils_ptrarray_find_binary_search (gconstpointer *list, gsize len, gconstpointer needle, GCompareDataFunc cmpfcn, gpointer user_data); gssize _nm_utils_strv_find_first (char **list, gssize len, const char *needle); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index df75d56557..c55e4ba81f 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -667,10 +667,8 @@ _nm_utils_copy_object_array (const GPtrArray *array) return _nm_utils_copy_array (array, g_object_ref, g_object_unref); } -/* have @list of type 'gpointer *' instead of 'gconstpointer *' to - * reduce the necessity for annoying const-casts. */ gssize -_nm_utils_ptrarray_find_first (gpointer *list, gssize len, gconstpointer needle) +_nm_utils_ptrarray_find_first (gconstpointer *list, gssize len, gconstpointer needle) { gssize i; @@ -694,7 +692,7 @@ _nm_utils_ptrarray_find_first (gpointer *list, gssize len, gconstpointer needle) } gssize -_nm_utils_ptrarray_find_binary_search (gpointer *list, gsize len, gpointer needle, GCompareDataFunc cmpfcn, gpointer user_data) +_nm_utils_ptrarray_find_binary_search (gconstpointer *list, gsize len, gconstpointer needle, GCompareDataFunc cmpfcn, gpointer user_data) { gssize imin, imax, imid; int cmp; diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 5dfc897114..8d5997ad2c 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -4960,9 +4960,9 @@ _test_find_binary_search_do (const int *array, gsize len) { gsize i; gssize idx; - gs_free gpointer *parray = g_new (gpointer, len); - const int needle = 0; - gpointer pneedle = GINT_TO_POINTER (needle); + gs_free gconstpointer *parray = g_new (gconstpointer, len); + const int NEEDLE = 0; + gconstpointer pneedle = GINT_TO_POINTER (NEEDLE); gssize expected_result; for (i = 0; i < len; i++) @@ -5008,14 +5008,13 @@ _test_find_binary_search_do (const int *array, gsize len) } #define test_find_binary_search_do(...) \ G_STMT_START { \ - const int _array[] = { __VA_ARGS__ } ; \ + const int _array[] = { __VA_ARGS__ }; \ _test_find_binary_search_do (_array, G_N_ELEMENTS (_array)); \ } G_STMT_END static void test_nm_utils_ptrarray_find_binary_search (void) { -#define _NOT(idx) (~ ((gssize) (idx))) test_find_binary_search_do ( 0); test_find_binary_search_do ( -1, 0); test_find_binary_search_do ( -2, -1, 0); diff --git a/src/nm-route-manager.c b/src/nm-route-manager.c index 2d4c7d9595..95113f8c84 100644 --- a/src/nm-route-manager.c +++ b/src/nm-route-manager.c @@ -296,7 +296,7 @@ _route_index_find (const VTableIP *vtable, const RouteIndex *index, const NMPlat { gssize idx, idx2; - idx = _nm_utils_ptrarray_find_binary_search ((gpointer *) index->entries, index->len, (gpointer) needle, (GCompareDataFunc) _vx_route_id_cmp_full, (gpointer) vtable); + idx = _nm_utils_ptrarray_find_binary_search ((gconstpointer *) index->entries, index->len, needle, (GCompareDataFunc) _vx_route_id_cmp_full, (gpointer) vtable); if (idx < 0) return idx; diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index c30eed9447..cedc220a75 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -3028,7 +3028,7 @@ delayed_action_handle_one (NMPlatform *platform) g_ptr_array_remove_index_fast (priv->delayed_action.list_master_connected, 0); if (priv->delayed_action.list_master_connected->len == 0) priv->delayed_action.flags &= ~DELAYED_ACTION_TYPE_MASTER_CONNECTED; - nm_assert (_nm_utils_ptrarray_find_first (priv->delayed_action.list_master_connected->pdata, priv->delayed_action.list_master_connected->len, user_data) < 0); + nm_assert (_nm_utils_ptrarray_find_first ((gconstpointer *) priv->delayed_action.list_master_connected->pdata, priv->delayed_action.list_master_connected->len, user_data) < 0); _LOGt_delayed_action (DELAYED_ACTION_TYPE_MASTER_CONNECTED, user_data, "handle"); delayed_action_handle_MASTER_CONNECTED (platform, GPOINTER_TO_INT (user_data)); @@ -3069,7 +3069,7 @@ delayed_action_handle_one (NMPlatform *platform) g_ptr_array_remove_index_fast (priv->delayed_action.list_refresh_link, 0); if (priv->delayed_action.list_refresh_link->len == 0) priv->delayed_action.flags &= ~DELAYED_ACTION_TYPE_REFRESH_LINK; - nm_assert (_nm_utils_ptrarray_find_first (priv->delayed_action.list_refresh_link->pdata, priv->delayed_action.list_refresh_link->len, user_data) < 0); + nm_assert (_nm_utils_ptrarray_find_first ((gconstpointer *) priv->delayed_action.list_refresh_link->pdata, priv->delayed_action.list_refresh_link->len, user_data) < 0); _LOGt_delayed_action (DELAYED_ACTION_TYPE_REFRESH_LINK, user_data, "handle"); @@ -3118,11 +3118,11 @@ delayed_action_schedule (NMPlatform *platform, DelayedActionType action_type, gp switch (action_type) { case DELAYED_ACTION_TYPE_REFRESH_LINK: - if (_nm_utils_ptrarray_find_first (priv->delayed_action.list_refresh_link->pdata, priv->delayed_action.list_refresh_link->len, user_data) < 0) + if (_nm_utils_ptrarray_find_first ((gconstpointer *) priv->delayed_action.list_refresh_link->pdata, priv->delayed_action.list_refresh_link->len, user_data) < 0) g_ptr_array_add (priv->delayed_action.list_refresh_link, user_data); break; case DELAYED_ACTION_TYPE_MASTER_CONNECTED: - if (_nm_utils_ptrarray_find_first (priv->delayed_action.list_master_connected->pdata, priv->delayed_action.list_master_connected->len, user_data) < 0) + if (_nm_utils_ptrarray_find_first ((gconstpointer *) priv->delayed_action.list_master_connected->pdata, priv->delayed_action.list_master_connected->len, user_data) < 0) g_ptr_array_add (priv->delayed_action.list_master_connected, user_data); break; case DELAYED_ACTION_TYPE_WAIT_FOR_NL_RESPONSE: From c3ecca225cd1f13f75250d35e5356ee5f433cff2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 23 Sep 2016 13:37:22 +0200 Subject: [PATCH 2/9] core: add _nm_utils_array_find_binary_search() Also add nm_cmp_uint32_p_with_data(). Will be used later. --- libnm-core/nm-core-internal.h | 1 + libnm-core/nm-utils.c | 34 +++++++++++++++++++++++ libnm-core/tests/test-general.c | 41 ++++++++++++++++++++++++++++ shared/nm-utils/nm-macros-internal.h | 13 +++++++++ 4 files changed, 89 insertions(+) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 111b26d588..471353e741 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -147,6 +147,7 @@ GPtrArray *_nm_utils_copy_object_array (const GPtrArray *array); gssize _nm_utils_ptrarray_find_first (gconstpointer *list, gssize len, gconstpointer needle); gssize _nm_utils_ptrarray_find_binary_search (gconstpointer *list, gsize len, gconstpointer needle, GCompareDataFunc cmpfcn, gpointer user_data); +gssize _nm_utils_array_find_binary_search (gconstpointer list, gsize elem_size, gsize len, gconstpointer needle, GCompareDataFunc cmpfcn, gpointer user_data); gssize _nm_utils_strv_find_first (char **list, gssize len, const char *needle); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index c55e4ba81f..818b0c43ec 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -724,6 +724,40 @@ _nm_utils_ptrarray_find_binary_search (gconstpointer *list, gsize len, gconstpoi return ~imin; } +gssize +_nm_utils_array_find_binary_search (gconstpointer list, gsize elem_size, gsize len, gconstpointer needle, GCompareDataFunc cmpfcn, gpointer user_data) +{ + gssize imin, imax, imid; + int cmp; + + g_return_val_if_fail (list || !len, ~((gssize) 0)); + g_return_val_if_fail (cmpfcn, ~((gssize) 0)); + g_return_val_if_fail (elem_size > 0, ~((gssize) 0)); + + imin = 0; + if (len == 0) + return ~imin; + + imax = len - 1; + + while (imin <= imax) { + imid = imin + (imax - imin) / 2; + + cmp = cmpfcn (&((const char *) list)[elem_size * imid], needle, user_data); + if (cmp == 0) + return imid; + + if (cmp < 0) + imin = imid + 1; + else + imax = imid - 1; + } + + /* return the inverse of @imin. This is a negative number, but + * also is ~imin the position where the value should be inserted. */ + return ~imin; +} + GVariant * _nm_utils_bytes_to_dbus (const GValue *prop_value) { diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 8d5997ad2c..e71dd8bc56 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -5006,10 +5006,51 @@ _test_find_binary_search_do (const int *array, gsize len) } } } + +static void +_test_find_binary_search_do_uint32 (const int *int_array, gsize len) +{ + gssize idx; + const int OFFSET = 100; + const int NEEDLE = 0 + OFFSET; + gssize expected_result = -1; + guint32 array[len]; + + /* the test data has negative values. Shift them... */ + for (idx = 0; idx < len; idx++) { + int v = int_array[idx]; + + g_assert (v > -OFFSET); + g_assert (v < OFFSET); + g_assert (idx == 0 || v > int_array[idx - 1]); + array[idx] = (guint32) (int_array[idx] + OFFSET); + if (array[idx] == NEEDLE) + expected_result = idx; + } + + idx = _nm_utils_array_find_binary_search (array, + sizeof (guint32), + len, + &NEEDLE, + nm_cmp_uint32_p_with_data, + NULL); + if (expected_result >= 0) + g_assert_cmpint (expected_result, ==, idx); + else { + gssize idx2 = ~idx; + g_assert_cmpint (idx, <, 0); + + g_assert (idx2 >= 0); + g_assert (idx2 <= len); + g_assert (idx2 - 1 < 0 || array[idx2 - 1] < NEEDLE); + g_assert (idx2 >= len || array[idx2] > NEEDLE); + } +} #define test_find_binary_search_do(...) \ G_STMT_START { \ const int _array[] = { __VA_ARGS__ }; \ _test_find_binary_search_do (_array, G_N_ELEMENTS (_array)); \ + _test_find_binary_search_do_uint32 (_array, G_N_ELEMENTS (_array)); \ } G_STMT_END static void diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 9e9f5d8f3c..003a9e974f 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -516,6 +516,19 @@ nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data) return strcmp (s1, s2); } +static inline int +nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data) +{ + const guint32 a = *((const guint32 *) p_a); + const guint32 b = *((const guint32 *) p_b); + + if (a < b) + return -1; + if (a > b) + return 1; + return 0; +} + /*****************************************************************************/ /* Taken from systemd's UNIQ_T and UNIQ macros. */ From 1bb00ae66a5dda8a2f3f298d64a2e06d4be4cfc8 Mon Sep 17 00:00:00 2001 From: Marius Vollmer Date: Thu, 15 Sep 2016 23:34:24 +0300 Subject: [PATCH 3/9] manager: Add "Capabilities" property --- introspection/nm-manager.xml | 9 +++++ libnm-core/nm-dbus-interface.h | 8 +++++ libnm/libnm.ver | 1 + src/nm-manager.c | 60 +++++++++++++++++++++++++++++++++- src/nm-manager.h | 3 ++ 5 files changed, 80 insertions(+), 1 deletion(-) diff --git a/introspection/nm-manager.xml b/introspection/nm-manager.xml index cf178c3951..9893556e56 100644 --- a/introspection/nm-manager.xml +++ b/introspection/nm-manager.xml @@ -380,6 +380,15 @@ --> + + + - +