summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-04-10 09:16:11 +0200
committerThomas Haller <thaller@redhat.com>2015-06-17 11:41:43 +0200
commit2f4301bd261af371acad0a9ab00a786308060aa7 (patch)
tree2755f6e29229395c55ab5ae1cd7d21cb862f5b47
parente16fe9ddd41b84143b2645bdd98958fcf77e2c48 (diff)
downloadNetworkManager-2f4301bd261af371acad0a9ab00a786308060aa7.tar.gz
glib-compat: add g_ptr_array_insert()
-rw-r--r--include/nm-glib-compat.h32
-rw-r--r--libnm-core/tests/test-general.c29
2 files changed, 61 insertions, 0 deletions
diff --git a/include/nm-glib-compat.h b/include/nm-glib-compat.h
index c5674ea9e7..da4e2c2ce0 100644
--- a/include/nm-glib-compat.h
+++ b/include/nm-glib-compat.h
@@ -180,4 +180,36 @@ nm_g_hash_table_replace (GHashTable *hash, gpointer key, gpointer value)
}
+#if !GLIB_CHECK_VERSION(2, 40, 0) || defined (NM_GLIB_COMPAT_H_TEST)
+static inline void
+_nm_g_ptr_array_insert (GPtrArray *array,
+ gint index_,
+ gpointer data)
+{
+ g_return_if_fail (array);
+ g_return_if_fail (index_ >= -1);
+ g_return_if_fail (index_ <= (gint) array->len);
+
+ g_ptr_array_add (array, data);
+
+ if (index_ != -1 && index_ != (gint) (array->len - 1)) {
+ memmove (&(array->pdata[index_ + 1]),
+ &(array->pdata[index_]),
+ (array->len - index_ - 1) * sizeof (gpointer));
+ array->pdata[index_] = data;
+ }
+}
+#endif
+#if !GLIB_CHECK_VERSION(2, 40, 0)
+#define g_ptr_array_insert(array, index, data) G_STMT_START { _nm_g_ptr_array_insert (array, index, data); } G_STMT_END
+#else
+#define g_ptr_array_insert(array, index, data) \
+ G_STMT_START { \
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
+ g_ptr_array_insert (array, index, data); \
+ G_GNUC_END_IGNORE_DEPRECATIONS \
+ } G_STMT_END
+#endif
+
+
#endif /* __NM_GLIB_COMPAT_H__ */
diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c
index 106b67606e..670a0f3e97 100644
--- a/libnm-core/tests/test-general.c
+++ b/libnm-core/tests/test-general.c
@@ -19,6 +19,8 @@
*
*/
+#define NM_GLIB_COMPAT_H_TEST
+
#include "config.h"
#include <glib.h>
@@ -59,6 +61,7 @@
#include "nm-setting-wireless-security.h"
#include "nm-simple-connection.h"
#include "nm-keyfile-internal.h"
+#include "nm-glib-compat.h"
#include "nm-test-utils.h"
@@ -4455,6 +4458,31 @@ again:
/******************************************************************************/
+static void
+test_g_ptr_array_insert (void)
+{
+ /* this test only makes sense on a recent glib, where we compare our compat
+ * with the original implementation. */
+#if GLIB_CHECK_VERSION(2, 40, 0)
+ gs_unref_ptrarray GPtrArray *arr1 = g_ptr_array_new ();
+ gs_unref_ptrarray GPtrArray *arr2 = g_ptr_array_new ();
+ GRand *rand = nmtst_get_rand ();
+ guint i;
+
+ for (i = 0; i < 560; i++) {
+ gint32 idx = g_rand_int_range (rand, -1, arr1->len + 1);
+
+ g_ptr_array_insert (arr1, idx, GINT_TO_POINTER (i));
+ _nm_g_ptr_array_insert (arr2, idx, GINT_TO_POINTER (i));
+
+ g_assert_cmpint (arr1->len, ==, arr2->len);
+ g_assert (memcmp (arr1->pdata, arr2->pdata, arr1->len * sizeof (gpointer)) == 0);
+ }
+#endif
+}
+
+/******************************************************************************/
+
NMTST_DEFINE ();
int main (int argc, char **argv)
@@ -4557,6 +4585,7 @@ int main (int argc, char **argv)
g_test_add_func ("/core/general/_nm_utils_ascii_str_to_int64", test_nm_utils_ascii_str_to_int64);
g_test_add_func ("/core/general/nm_utils_is_power_of_two", test_nm_utils_is_power_of_two);
+ g_test_add_func ("/core/general/_glib_compat_g_ptr_array_insert", test_g_ptr_array_insert);
g_test_add_func ("/core/general/_nm_utils_dns_option_validate", test_nm_utils_dns_option_validate);
g_test_add_func ("/core/general/_nm_utils_dns_option_find_idx", test_nm_utils_dns_option_find_idx);