From beeb676735d6242dd4c12de27bbc1b8fd3afaeee Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 28 Sep 2016 15:34:52 +0200 Subject: [PATCH] shared: add nm_clear_g_free() (cherry picked from commit b4e66c4818cf6ff14e8b5181ec9d71fa5c9f5452) --- libnm-core/tests/test-general.c | 2 +- shared/nm-utils/nm-macros-internal.h | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index f7945071f1..6631c89684 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -2527,7 +2527,7 @@ test_setting_compare_wireless_cloned_mac_address (void) g_assert_cmpstr ("stable-bia", ==, nm_setting_wireless_get_cloned_mac_address ((NMSettingWireless *) new)); g_object_get (new, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, &str1, NULL); g_assert_cmpstr ("stable-bia", ==, str1); - g_clear_pointer (&str1, g_free); + nm_clear_g_free (&str1); success = nm_setting_compare (old, new, NM_SETTING_COMPARE_FLAG_EXACT); g_assert (!success); diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index e30a50a5ce..fd23a68a30 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -366,6 +366,27 @@ nm_g_object_unref (gpointer obj) g_object_unref (obj); } +/* basically, replaces + * g_clear_pointer (&location, g_free) + * with + * nm_clear_g_free (&location) + * + * Another advantage is that by using a macro and typeof(), it is more + * typesafe and gives you for example a compiler warning when pp is a const + * pointer or points to a const-pointer. + */ +#define nm_clear_g_free(pp) \ + ({ \ + typeof (*(pp)) *_pp = (pp); \ + typeof (**_pp) *_p = *_pp; \ + \ + if (_p) { \ + *_pp = NULL; \ + g_free (_p); \ + } \ + !!_p; \ + }) + static inline gboolean nm_clear_g_source (guint *id) {