summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-07-24 15:45:46 +0200
committerThomas Haller <thaller@redhat.com>2015-07-24 18:07:16 +0200
commit8bca864111a248ac4965f4f5887580c9829624da (patch)
tree0d55606a975748574d16f52de327d62f92ab9258 /src
parent981817e99829391164774ecdf40ac2ee05fd71b7 (diff)
downloadNetworkManager-8bca864111a248ac4965f4f5887580c9829624da.tar.gz
core: move NM_DEFINE_SINGLETON macros to src/NetworkManagerUtils.h
NM_DEFINE_SINGLETON is used only by core and makes use of nm-logging. It does not belong to "include/nm-macros-internal.h". Move it to "src/".
Diffstat (limited to 'src')
-rw-r--r--src/NetworkManagerUtils.h60
-rw-r--r--src/nm-auth-manager.c1
-rw-r--r--src/nm-firewall-manager.c1
-rw-r--r--src/nm-session-monitor.c1
-rw-r--r--src/nm-sleep-monitor-systemd.c1
-rw-r--r--src/settings/nm-inotify-helper.c1
6 files changed, 65 insertions, 0 deletions
diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h
index d9adfc0975..2ad132b8ec 100644
--- a/src/NetworkManagerUtils.h
+++ b/src/NetworkManagerUtils.h
@@ -29,6 +29,66 @@
#include "nm-connection.h"
#include "nm-types.h"
+/*****************************************************************************/
+
+#define NM_DEFINE_SINGLETON_INSTANCE(TYPE) \
+static TYPE *singleton_instance
+
+#define NM_DEFINE_SINGLETON_WEAK_REF(TYPE) \
+NM_DEFINE_SINGLETON_INSTANCE (TYPE); \
+static void \
+_singleton_instance_weak_ref_cb (gpointer data, \
+ GObject *where_the_object_was) \
+{ \
+ nm_log_dbg (LOGD_CORE, "disposing %s singleton (%p)", G_STRINGIFY (TYPE), singleton_instance); \
+ singleton_instance = NULL; \
+} \
+static inline void \
+nm_singleton_instance_weak_ref_register (void) \
+{ \
+ g_object_weak_ref (G_OBJECT (singleton_instance), _singleton_instance_weak_ref_cb, NULL); \
+}
+
+#define NM_DEFINE_SINGLETON_DESTRUCTOR(TYPE) \
+NM_DEFINE_SINGLETON_INSTANCE (TYPE); \
+static void __attribute__((destructor)) \
+_singleton_destructor (void) \
+{ \
+ if (singleton_instance) { \
+ if (G_OBJECT (singleton_instance)->ref_count > 1) \
+ nm_log_dbg (LOGD_CORE, "disown %s singleton (%p)", G_STRINGIFY (TYPE), singleton_instance); \
+ g_object_unref (singleton_instance); \
+ } \
+}
+
+/* By default, the getter will assert that the singleton will be created only once. You can
+ * change this by redefining NM_DEFINE_SINGLETON_ALLOW_MULTIPLE. */
+#ifndef NM_DEFINE_SINGLETON_ALLOW_MULTIPLE
+#define NM_DEFINE_SINGLETON_ALLOW_MULTIPLE FALSE
+#endif
+
+#define NM_DEFINE_SINGLETON_GETTER(TYPE, GETTER, GTYPE, ...) \
+NM_DEFINE_SINGLETON_INSTANCE (TYPE); \
+NM_DEFINE_SINGLETON_WEAK_REF (TYPE); \
+TYPE * \
+GETTER (void) \
+{ \
+ if (G_UNLIKELY (!singleton_instance)) { \
+ static char _already_created = FALSE; \
+\
+ g_assert (!_already_created || (NM_DEFINE_SINGLETON_ALLOW_MULTIPLE)); \
+ _already_created = TRUE;\
+ singleton_instance = (g_object_new (GTYPE, ##__VA_ARGS__, NULL)); \
+ g_assert (singleton_instance); \
+ nm_singleton_instance_weak_ref_register (); \
+ nm_log_dbg (LOGD_CORE, "create %s singleton (%p)", G_STRINGIFY (TYPE), singleton_instance); \
+ } \
+ return singleton_instance; \
+} \
+NM_DEFINE_SINGLETON_DESTRUCTOR(TYPE)
+
+/*****************************************************************************/
+
gboolean nm_ethernet_address_is_valid (gconstpointer addr, gssize len);
in_addr_t nm_utils_ip4_address_clear_host_address (in_addr_t addr, guint8 plen);
diff --git a/src/nm-auth-manager.c b/src/nm-auth-manager.c
index bcd1b96d4b..3ee0889298 100644
--- a/src/nm-auth-manager.c
+++ b/src/nm-auth-manager.c
@@ -25,6 +25,7 @@
#include "nm-logging.h"
#include "nm-errors.h"
#include "nm-core-internal.h"
+#include "NetworkManagerUtils.h"
#define POLKIT_SERVICE "org.freedesktop.PolicyKit1"
#define POLKIT_OBJECT_PATH "/org/freedesktop/PolicyKit1/Authority"
diff --git a/src/nm-firewall-manager.c b/src/nm-firewall-manager.c
index 711b542ec4..9bffac7599 100644
--- a/src/nm-firewall-manager.c
+++ b/src/nm-firewall-manager.c
@@ -27,6 +27,7 @@
#include "nm-firewall-manager.h"
#include "nm-logging.h"
#include "gsystem-local-alloc.h"
+#include "NetworkManagerUtils.h"
#define NM_FIREWALL_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
NM_TYPE_FIREWALL_MANAGER, \
diff --git a/src/nm-session-monitor.c b/src/nm-session-monitor.c
index 80d0bcc456..84436d08b9 100644
--- a/src/nm-session-monitor.c
+++ b/src/nm-session-monitor.c
@@ -30,6 +30,7 @@
#include "nm-glib-compat.h"
#include "nm-session-monitor.h"
#include "nm-logging.h"
+#include "NetworkManagerUtils.h"
#ifdef SESSION_TRACKING_SYSTEMD
#include <systemd/sd-login.h>
diff --git a/src/nm-sleep-monitor-systemd.c b/src/nm-sleep-monitor-systemd.c
index 18c847831e..1e3a309758 100644
--- a/src/nm-sleep-monitor-systemd.c
+++ b/src/nm-sleep-monitor-systemd.c
@@ -29,6 +29,7 @@
#include "nm-logging.h"
#include "nm-dbus-manager.h"
#include "nm-core-internal.h"
+#include "NetworkManagerUtils.h"
#include "nm-sleep-monitor.h"
diff --git a/src/settings/nm-inotify-helper.c b/src/settings/nm-inotify-helper.c
index 3732e77a39..2d0210aee4 100644
--- a/src/settings/nm-inotify-helper.c
+++ b/src/settings/nm-inotify-helper.c
@@ -28,6 +28,7 @@
#include "nm-inotify-helper.h"
#include "nm-logging.h"
+#include "NetworkManagerUtils.h"
/* NOTE: this code should be killed once we depend on a new enough glib to
* include the patches from https://bugzilla.gnome.org/show_bug.cgi?id=532815