shared: add nm_clear_g_free()

This commit is contained in:
Thomas Haller 2016-09-28 15:34:52 +02:00
parent a83eb773ce
commit b4e66c4818
2 changed files with 22 additions and 1 deletions

View file

@ -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);

View file

@ -373,6 +373,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)
{