From 04ed48e5a06bed9d2eba20ca92488c7d8836ed97 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 18 Apr 2015 14:00:45 +0200 Subject: [PATCH] platform: drop nm_platform_free() and register weak-ref Instead of having a nm_platform_free() function, use NM_DEFINE_SINGLETON_WEAK_REF() and register a weak reference. That way, users who want to free the platform instance can just unref it. --- src/platform/nm-platform.c | 24 +++++++----------------- src/platform/nm-platform.h | 1 - src/platform/tests/test-common.c | 2 +- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index bb2a8c970..55a08ebc4 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -58,7 +58,9 @@ static guint signals[LAST_SIGNAL] = { 0 }; /******************************************************************/ /* Singleton NMPlatform subclass instance and cached class object */ -static NMPlatform *singleton_instance = NULL; +NM_DEFINE_SINGLETON_INSTANCE (NMPlatform); + +NM_DEFINE_SINGLETON_WEAK_REF (NMPlatform); /* Just always initialize a @klass instance. NM_PLATFORM_GET_CLASS() * is only a plain read on the self instance, which the compiler @@ -90,8 +92,7 @@ static NMPlatform *singleton_instance = NULL; * * NetworkManager will typically use only one platform object during * its run. Test programs might want to switch platform implementations, - * though. This is done with a combination of nm_platform_free() and - * nm_*_platform_setup(). + * though. */ void nm_platform_setup (NMPlatform *instance) @@ -100,20 +101,10 @@ nm_platform_setup (NMPlatform *instance) g_return_if_fail (!singleton_instance); singleton_instance = instance; -} -/** - * nm_platform_free: - * - * Free #NMPlatform singleton created by nm_*_platform_setup(). - */ -void -nm_platform_free (void) -{ - g_assert (singleton_instance); + nm_singleton_instance_weak_ref_register (); - g_object_unref (singleton_instance); - singleton_instance = NULL; + nm_log_dbg (LOGD_CORE, "setup NMPlatform singleton (%p, %s)", instance, G_OBJECT_TYPE_NAME (instance)); } /** @@ -121,8 +112,7 @@ nm_platform_free (void) * @self: platform instance * * Retrieve #NMPlatform singleton. Use this whenever you want to connect to - * #NMPlatform signals. It is an error to call it before nm_*_platform_setup() - * or after nm_platform_free(). + * #NMPlatform signals. It is an error to call it before nm_platform_setup(). * * Returns: (transfer none): The #NMPlatform singleton reference. */ diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index d0273910e..66b132e2d 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -490,7 +490,6 @@ GType nm_platform_get_type (void); void nm_platform_setup (NMPlatform *instance); NMPlatform *nm_platform_get (void); -void nm_platform_free (void); #define NM_PLATFORM_GET (nm_platform_get ()) diff --git a/src/platform/tests/test-common.c b/src/platform/tests/test-common.c index cedbc518e..d1627058e 100644 --- a/src/platform/tests/test-common.c +++ b/src/platform/tests/test-common.c @@ -287,6 +287,6 @@ main (int argc, char **argv) nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); - nm_platform_free (); + g_object_unref (nm_platform_get ()); return result; }