summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-04-18 14:00:45 +0200
committerThomas Haller <thaller@redhat.com>2015-04-22 16:51:11 +0200
commit04ed48e5a06bed9d2eba20ca92488c7d8836ed97 (patch)
treea0e58963ec42849140c65eaf8a3b5c69c2ce5c70
parentfe2608c9032932e1a731dfeb6fb3196bd15a1cbf (diff)
downloadNetworkManager-04ed48e5a06bed9d2eba20ca92488c7d8836ed97.tar.gz
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.
-rw-r--r--src/platform/nm-platform.c24
-rw-r--r--src/platform/nm-platform.h1
-rw-r--r--src/platform/tests/test-common.c2
3 files changed, 8 insertions, 19 deletions
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index bb2a8c970e..55a08ebc43 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 d0273910e9..66b132e2dc 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 cedbc518e6..d1627058e9 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;
}