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.
This commit is contained in:
@@ -58,7 +58,9 @@ static guint signals[LAST_SIGNAL] = { 0 };
|
|||||||
/******************************************************************/
|
/******************************************************************/
|
||||||
|
|
||||||
/* Singleton NMPlatform subclass instance and cached class object */
|
/* 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()
|
/* Just always initialize a @klass instance. NM_PLATFORM_GET_CLASS()
|
||||||
* is only a plain read on the self instance, which the compiler
|
* 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
|
* NetworkManager will typically use only one platform object during
|
||||||
* its run. Test programs might want to switch platform implementations,
|
* its run. Test programs might want to switch platform implementations,
|
||||||
* though. This is done with a combination of nm_platform_free() and
|
* though.
|
||||||
* nm_*_platform_setup().
|
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
nm_platform_setup (NMPlatform *instance)
|
nm_platform_setup (NMPlatform *instance)
|
||||||
@@ -100,20 +101,10 @@ nm_platform_setup (NMPlatform *instance)
|
|||||||
g_return_if_fail (!singleton_instance);
|
g_return_if_fail (!singleton_instance);
|
||||||
|
|
||||||
singleton_instance = instance;
|
singleton_instance = instance;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
nm_singleton_instance_weak_ref_register ();
|
||||||
* nm_platform_free:
|
|
||||||
*
|
|
||||||
* Free #NMPlatform singleton created by nm_*_platform_setup().
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
nm_platform_free (void)
|
|
||||||
{
|
|
||||||
g_assert (singleton_instance);
|
|
||||||
|
|
||||||
g_object_unref (singleton_instance);
|
nm_log_dbg (LOGD_CORE, "setup NMPlatform singleton (%p, %s)", instance, G_OBJECT_TYPE_NAME (instance));
|
||||||
singleton_instance = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,8 +112,7 @@ nm_platform_free (void)
|
|||||||
* @self: platform instance
|
* @self: platform instance
|
||||||
*
|
*
|
||||||
* Retrieve #NMPlatform singleton. Use this whenever you want to connect to
|
* Retrieve #NMPlatform singleton. Use this whenever you want to connect to
|
||||||
* #NMPlatform signals. It is an error to call it before nm_*_platform_setup()
|
* #NMPlatform signals. It is an error to call it before nm_platform_setup().
|
||||||
* or after nm_platform_free().
|
|
||||||
*
|
*
|
||||||
* Returns: (transfer none): The #NMPlatform singleton reference.
|
* Returns: (transfer none): The #NMPlatform singleton reference.
|
||||||
*/
|
*/
|
||||||
|
@@ -490,7 +490,6 @@ GType nm_platform_get_type (void);
|
|||||||
|
|
||||||
void nm_platform_setup (NMPlatform *instance);
|
void nm_platform_setup (NMPlatform *instance);
|
||||||
NMPlatform *nm_platform_get (void);
|
NMPlatform *nm_platform_get (void);
|
||||||
void nm_platform_free (void);
|
|
||||||
|
|
||||||
#define NM_PLATFORM_GET (nm_platform_get ())
|
#define NM_PLATFORM_GET (nm_platform_get ())
|
||||||
|
|
||||||
|
@@ -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_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;
|
return result;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user