From 05e6d155bad695d0d584fc0b5d5086d4c8c28cdc Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Oct 2016 15:06:39 +0200 Subject: [PATCH 1/4] device: minor cleanup of nm-device-factory.c --- src/devices/nm-device-factory.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index d75de4963c..55be0cecbf 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -19,28 +19,37 @@ */ #include "nm-default.h" + +#include "nm-device-factory.h" + #include #include #include #include #include -#include "nm-device-factory.h" #include "nm-platform.h" #include "nm-utils.h" +/*****************************************************************************/ + const NMLinkType _nm_device_factory_no_default_links[] = { NM_LINK_TYPE_NONE }; const char *_nm_device_factory_no_default_settings[] = { NULL }; -G_DEFINE_INTERFACE (NMDeviceFactory, nm_device_factory, G_TYPE_OBJECT) +/*****************************************************************************/ enum { DEVICE_ADDED, COMPONENT_ADDED, LAST_SIGNAL }; + static guint signals[LAST_SIGNAL] = { 0 }; +G_DEFINE_INTERFACE (NMDeviceFactory, nm_device_factory, G_TYPE_OBJECT) + +/*****************************************************************************/ + gboolean nm_device_factory_emit_component_added (NMDeviceFactory *factory, GObject *component) { From 18660604aae1b21e3d628d8d566ca951892edcab Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Oct 2016 16:05:43 +0200 Subject: [PATCH 2/4] device: make NMDeviceFactory a class instead of an interface An interface would make sense to allow the actual device-factory to inherit from another type. However, glib interfaces make code much harder to follow and less efficient. The device factory shall be a very simple type with meta data about supported device types and the ability to create device instances. There is no need to make this an interface implementation, instead just let the factories inherit from NM_TYPE_DEVICE_FACTORY directly. --- src/devices/adsl/nm-atm-manager.c | 65 +++++++---------- src/devices/bluetooth/nm-bluez-manager.c | 61 +++++++--------- src/devices/nm-device-bond.c | 2 +- src/devices/nm-device-bridge.c | 2 +- src/devices/nm-device-ethernet.c | 5 +- src/devices/nm-device-factory.c | 51 ++++++++------ src/devices/nm-device-factory.h | 89 +++++++++++++----------- src/devices/nm-device-infiniband.c | 8 +-- src/devices/nm-device-ip-tunnel.c | 6 +- src/devices/nm-device-macvlan.c | 6 +- src/devices/nm-device-tun.c | 3 +- src/devices/nm-device-veth.c | 5 +- src/devices/nm-device-vlan.c | 6 +- src/devices/nm-device-vxlan.c | 7 +- src/devices/team/nm-team-factory.c | 55 ++++++--------- src/devices/wifi/nm-wifi-factory.c | 55 ++++++--------- src/devices/wwan/nm-wwan-factory.c | 61 +++++++--------- src/nm-manager.c | 2 +- 18 files changed, 221 insertions(+), 268 deletions(-) diff --git a/src/devices/adsl/nm-atm-manager.c b/src/devices/adsl/nm-atm-manager.c index 36f9635266..c70a6601b1 100644 --- a/src/devices/adsl/nm-atm-manager.c +++ b/src/devices/adsl/nm-atm-manager.c @@ -38,36 +38,32 @@ #define NM_IS_ATM_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_ATM_MANAGER)) #define NM_ATM_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_ATM_MANAGER, NMAtmManagerClass)) -typedef struct _NMAtmManager NMAtmManager; -typedef struct _NMAtmManagerClass NMAtmManagerClass; - -static GType nm_atm_manager_get_type (void); - -/*****************************************************************************/ - typedef struct { GUdevClient *client; GSList *devices; } NMAtmManagerPrivate; -struct _NMAtmManager { - GObject parent; +typedef struct { + NMDeviceFactory parent; NMAtmManagerPrivate _priv; -}; +} NMAtmManager; -struct _NMAtmManagerClass { - GObjectClass parent; -}; +typedef struct { + NMDeviceFactoryClass parent; +} NMAtmManagerClass; -static void device_factory_interface_init (NMDeviceFactoryInterface *factory_iface); +static GType nm_atm_manager_get_type (void); -G_DEFINE_TYPE_EXTENDED (NMAtmManager, nm_atm_manager, G_TYPE_OBJECT, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init)) +G_DEFINE_TYPE (NMAtmManager, nm_atm_manager, NM_TYPE_DEVICE_FACTORY); #define NM_ATM_MANAGER_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMAtmManager, NM_IS_ATM_MANAGER) /*****************************************************************************/ +NM_DEVICE_FACTORY_DECLARE_TYPES ( + NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_ADSL_SETTING_NAME) +); + G_MODULE_EXPORT NMDeviceFactory * nm_device_factory_create (GError **error) { @@ -241,12 +237,18 @@ handle_uevent (GUdevClient *client, adsl_remove (self, device); } -NM_DEVICE_FACTORY_DECLARE_TYPES ( - NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_ADSL_SETTING_NAME) -) - /*****************************************************************************/ +static void +nm_atm_manager_init (NMAtmManager *self) +{ + NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self); + const char *subsys[] = { "atm", NULL }; + + priv->client = g_udev_client_new (subsys); + g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self); +} + static void dispose (GObject *object) { @@ -270,25 +272,10 @@ static void nm_atm_manager_class_init (NMAtmManagerClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS (klass); object_class->dispose = dispose; -} - -/*****************************************************************************/ - -static void -nm_atm_manager_init (NMAtmManager *self) -{ - NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self); - const char *subsys[] = { "atm", NULL }; - - priv->client = g_udev_client_new (subsys); - g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self); -} - -static void -device_factory_interface_init (NMDeviceFactoryInterface *factory_iface) -{ - factory_iface->get_supported_types = get_supported_types; - factory_iface->start = start; + + factory_class->get_supported_types = get_supported_types; + factory_class->start = start; } diff --git a/src/devices/bluetooth/nm-bluez-manager.c b/src/devices/bluetooth/nm-bluez-manager.c index bc262eebdd..94e9ac387c 100644 --- a/src/devices/bluetooth/nm-bluez-manager.c +++ b/src/devices/bluetooth/nm-bluez-manager.c @@ -46,13 +46,6 @@ #define NM_IS_BLUEZ_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_BLUEZ_MANAGER)) #define NM_BLUEZ_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_BLUEZ_MANAGER, NMBluezManagerClass)) -typedef struct _NMBluezManager NMBluezManager; -typedef struct _NMBluezManagerClass NMBluezManagerClass; - -static GType nm_bluez_manager_get_type (void); - -/*****************************************************************************/ - typedef struct { int bluez_version; @@ -66,24 +59,36 @@ typedef struct { GCancellable *async_cancellable; } NMBluezManagerPrivate; -struct _NMBluezManager { - GObject parent; +typedef struct { + NMDeviceFactory parent; NMBluezManagerPrivate _priv; -}; +} NMBluezManager; -struct _NMBluezManagerClass { - GObjectClass parent; -}; +typedef struct { + NMDeviceFactoryClass parent; +} NMBluezManagerClass; -static void device_factory_interface_init (NMDeviceFactoryInterface *factory_iface); +static GType nm_bluez_manager_get_type (void); -G_DEFINE_TYPE_EXTENDED (NMBluezManager, nm_bluez_manager, G_TYPE_OBJECT, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init)) +G_DEFINE_TYPE (NMBluezManager, nm_bluez_manager, NM_TYPE_DEVICE_FACTORY); #define NM_BLUEZ_MANAGER_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMBluezManager, NM_IS_BLUEZ_MANAGER) /*****************************************************************************/ +NM_DEVICE_FACTORY_DECLARE_TYPES ( + NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_BNEP) + NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_BLUETOOTH_SETTING_NAME) +) + +G_MODULE_EXPORT NMDeviceFactory * +nm_device_factory_create (GError **error) +{ + return (NMDeviceFactory *) g_object_new (NM_TYPE_BLUEZ_MANAGER, NULL); +} + +/*****************************************************************************/ + #define _NMLOG_DOMAIN LOGD_BT #define _NMLOG_PREFIX_NAME "bluez" #define _NMLOG(level, ...) \ @@ -411,11 +416,6 @@ create_device (NMDeviceFactory *factory, return NULL; } -NM_DEVICE_FACTORY_DECLARE_TYPES ( - NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_BNEP) - NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_BLUETOOTH_SETTING_NAME) -) - /*****************************************************************************/ static void @@ -454,22 +454,11 @@ static void nm_bluez_manager_class_init (NMBluezManagerClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS (klass); object_class->dispose = dispose; -} -static void -device_factory_interface_init (NMDeviceFactoryInterface *factory_iface) -{ - factory_iface->get_supported_types = get_supported_types; - factory_iface->create_device = create_device; - factory_iface->start = start; -} - -/*****************************************************************************/ - -G_MODULE_EXPORT NMDeviceFactory * -nm_device_factory_create (GError **error) -{ - return (NMDeviceFactory *) g_object_new (NM_TYPE_BLUEZ_MANAGER, NULL); + factory_class->get_supported_types = get_supported_types; + factory_class->create_device = create_device; + factory_class->start = start; } diff --git a/src/devices/nm-device-bond.c b/src/devices/nm-device-bond.c index 2d1b986d87..ed62c5bf7a 100644 --- a/src/devices/nm-device-bond.c +++ b/src/devices/nm-device-bond.c @@ -546,5 +546,5 @@ create_device (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (BOND, Bond, bond, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_BOND) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_BOND_SETTING_NAME), - factory_iface->create_device = create_device; + factory_class->create_device = create_device; ); diff --git a/src/devices/nm-device-bridge.c b/src/devices/nm-device-bridge.c index f5c236bd80..becc072859 100644 --- a/src/devices/nm-device-bridge.c +++ b/src/devices/nm-device-bridge.c @@ -474,5 +474,5 @@ create_device (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (BRIDGE, Bridge, bridge, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_BRIDGE) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_BRIDGE_SETTING_NAME), - factory_iface->create_device = create_device; + factory_class->create_device = create_device; ); diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index 83d0af1fe6..95ff95310f 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -1698,6 +1698,5 @@ create_device (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (ETHERNET, Ethernet, ethernet, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_ETHERNET) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_PPPOE_SETTING_NAME), - factory_iface->create_device = create_device; - ) - + factory_class->create_device = create_device; +); diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index 55be0cecbf..bdc893c49b 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -46,7 +46,7 @@ enum { static guint signals[LAST_SIGNAL] = { 0 }; -G_DEFINE_INTERFACE (NMDeviceFactory, nm_device_factory, G_TYPE_OBJECT) +G_DEFINE_ABSTRACT_TYPE (NMDeviceFactory, nm_device_factory, G_TYPE_OBJECT) /*****************************************************************************/ @@ -55,6 +55,9 @@ nm_device_factory_emit_component_added (NMDeviceFactory *factory, GObject *compo { gboolean consumed = FALSE; + g_return_val_if_fail (NM_IS_DEVICE_FACTORY (factory), FALSE); + g_return_val_if_fail (G_IS_OBJECT (component), FALSE); + g_signal_emit (factory, signals[COMPONENT_ADDED], 0, component, &consumed); return consumed; } @@ -67,16 +70,16 @@ nm_device_factory_get_supported_types (NMDeviceFactory *factory, const NMLinkType *link_types_fallback; const char **setting_types_fallback; - g_return_if_fail (factory != NULL); + g_return_if_fail (NM_IS_DEVICE_FACTORY (factory)); if (!out_link_types) out_link_types = &link_types_fallback; if (!out_setting_types) out_setting_types = &setting_types_fallback; - NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_supported_types (factory, - out_link_types, - out_setting_types); + NM_DEVICE_FACTORY_GET_CLASS (factory)->get_supported_types (factory, + out_link_types, + out_setting_types); } void @@ -84,8 +87,8 @@ nm_device_factory_start (NMDeviceFactory *factory) { g_return_if_fail (factory != NULL); - if (NM_DEVICE_FACTORY_GET_INTERFACE (factory)->start) - NM_DEVICE_FACTORY_GET_INTERFACE (factory)->start (factory); + if (NM_DEVICE_FACTORY_GET_CLASS (factory)->start) + NM_DEVICE_FACTORY_GET_CLASS (factory)->start (factory); } NMDevice * @@ -96,7 +99,7 @@ nm_device_factory_create_device (NMDeviceFactory *factory, gboolean *out_ignore, GError **error) { - NMDeviceFactoryInterface *interface; + NMDeviceFactoryClass *klass; const NMLinkType *link_types = NULL; const char **setting_types = NULL; int i; @@ -142,15 +145,15 @@ nm_device_factory_create_device (NMDeviceFactory *factory, } } - interface = NM_DEVICE_FACTORY_GET_INTERFACE (factory); - if (!interface->create_device) { + klass = NM_DEVICE_FACTORY_GET_CLASS (factory); + if (!klass->create_device) { g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, "Device factory %s cannot manage new devices", G_OBJECT_TYPE_NAME (factory)); return NULL; } - device = interface->create_device (factory, iface, plink, connection, &ignore); + device = klass->create_device (factory, iface, plink, connection, &ignore); NM_SET_OUT (out_ignore, ignore); if (!device) { if (ignore) { @@ -176,8 +179,8 @@ nm_device_factory_get_connection_parent (NMDeviceFactory *factory, if (!nm_connection_is_virtual (connection)) return NULL; - if (NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_connection_parent) - return NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_connection_parent (factory, connection); + if (NM_DEVICE_FACTORY_GET_CLASS (factory)->get_connection_parent) + return NM_DEVICE_FACTORY_GET_CLASS (factory)->get_connection_parent (factory, connection); return NULL; } @@ -187,14 +190,14 @@ nm_device_factory_get_connection_iface (NMDeviceFactory *factory, const char *parent_iface, GError **error) { - NMDeviceFactoryInterface *klass; + NMDeviceFactoryClass *klass; char *ifname; g_return_val_if_fail (factory != NULL, NULL); g_return_val_if_fail (connection != NULL, NULL); g_return_val_if_fail (!error || !*error, NULL); - klass = NM_DEVICE_FACTORY_GET_INTERFACE (factory); + klass = NM_DEVICE_FACTORY_GET_CLASS (factory); ifname = g_strdup (nm_connection_get_interface_name (connection)); if (!ifname && klass->get_connection_iface) @@ -225,20 +228,26 @@ nm_device_factory_get_connection_iface (NMDeviceFactory *factory, /*****************************************************************************/ static void -nm_device_factory_default_init (NMDeviceFactoryInterface *factory_iface) +nm_device_factory_init (NMDeviceFactory *self) { - /* Signals */ +} + +static void +nm_device_factory_class_init (NMDeviceFactoryClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + signals[DEVICE_ADDED] = g_signal_new (NM_DEVICE_FACTORY_DEVICE_ADDED, - NM_TYPE_DEVICE_FACTORY, + G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMDeviceFactoryInterface, device_added), + G_STRUCT_OFFSET (NMDeviceFactoryClass, device_added), NULL, NULL, NULL, G_TYPE_NONE, 1, NM_TYPE_DEVICE); signals[COMPONENT_ADDED] = g_signal_new (NM_DEVICE_FACTORY_COMPONENT_ADDED, - NM_TYPE_DEVICE_FACTORY, + G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (NMDeviceFactoryInterface, component_added), + G_STRUCT_OFFSET (NMDeviceFactoryClass, component_added), g_signal_accumulator_true_handled, NULL, NULL, G_TYPE_BOOLEAN, 1, G_TYPE_OBJECT); } diff --git a/src/devices/nm-device-factory.h b/src/devices/nm-device-factory.h index 0179e85534..0acc270963 100644 --- a/src/devices/nm-device-factory.h +++ b/src/devices/nm-device-factory.h @@ -30,37 +30,22 @@ * not meant to enable third-party plugins. */ -typedef struct _NMDeviceFactory NMDeviceFactory; +#define NM_TYPE_DEVICE_FACTORY (nm_device_factory_get_type ()) +#define NM_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE_FACTORY, NMDeviceFactory)) +#define NM_DEVICE_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DEVICE_FACTORY, NMDeviceFactoryClass)) +#define NM_IS_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEVICE_FACTORY)) +#define NM_IS_DEVICE_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DEVICE_FACTORY)) +#define NM_DEVICE_FACTORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEVICE_FACTORY, NMDeviceFactoryClass)) -/** - * nm_device_factory_create: - * @error: an error if creation of the factory failed, or %NULL - * - * Creates a #GObject that implements the #NMDeviceFactory interface. This - * function must not emit any signals or perform any actions that would cause - * devices or components to be created immediately. Instead these should be - * deferred to the "start" interface method. - * - * Returns: the #GObject implementing #NMDeviceFactory or %NULL - */ -NMDeviceFactory *nm_device_factory_create (GError **error); - -/* Should match nm_device_factory_create() */ -typedef NMDeviceFactory * (*NMDeviceFactoryCreateFunc) (GError **error); - -/*****************************************************************************/ - -#define NM_TYPE_DEVICE_FACTORY (nm_device_factory_get_type ()) -#define NM_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE_FACTORY, NMDeviceFactory)) -#define NM_IS_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEVICE_FACTORY)) -#define NM_DEVICE_FACTORY_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_DEVICE_FACTORY, NMDeviceFactoryInterface)) - -/* signals */ #define NM_DEVICE_FACTORY_COMPONENT_ADDED "component-added" #define NM_DEVICE_FACTORY_DEVICE_ADDED "device-added" typedef struct { - GTypeInterface g_iface; + GObject parent; +} NMDeviceFactory; + +typedef struct { + GObjectClass parent; /** * get_supported_types: @@ -164,10 +149,31 @@ typedef struct { * Returns: %TRUE if the component was claimed by a device, %FALSE if not */ gboolean (*component_added) (NMDeviceFactory *factory, GObject *component); -} NMDeviceFactoryInterface; + +} NMDeviceFactoryClass; GType nm_device_factory_get_type (void); +/*****************************************************************************/ + +/** + * nm_device_factory_create: + * @error: an error if creation of the factory failed, or %NULL + * + * Creates a #GObject that implements the #NMDeviceFactory interface. This + * function must not emit any signals or perform any actions that would cause + * devices or components to be created immediately. Instead these should be + * deferred to the "start" interface method. + * + * Returns: the #GObject implementing #NMDeviceFactory or %NULL + */ +NMDeviceFactory *nm_device_factory_create (GError **error); + +/* Should match nm_device_factory_create() */ +typedef NMDeviceFactory * (*NMDeviceFactoryCreateFunc) (GError **error); + +/*****************************************************************************/ + void nm_device_factory_get_supported_types (NMDeviceFactory *factory, const NMLinkType **out_link_types, const char ***out_setting_types); @@ -180,7 +186,7 @@ char * nm_device_factory_get_connection_iface (NMDeviceFactory *factory, const char *parent_iface, GError **error); -void nm_device_factory_start (NMDeviceFactory *factory); +void nm_device_factory_start (NMDeviceFactory *factory); NMDevice * nm_device_factory_create_device (NMDeviceFactory *factory, const char *iface, @@ -220,15 +226,17 @@ extern const char *_nm_device_factory_no_default_settings[]; **************************************************************************/ #define NM_DEVICE_FACTORY_DEFINE_INTERNAL(upper, mixed, lower, st_code, dfi_code) \ - typedef GObject NM##mixed##Factory; \ - typedef GObjectClass NM##mixed##FactoryClass; \ + typedef struct { \ + NMDeviceFactory parent; \ + } NM##mixed##Factory; \ + typedef struct { \ + NMDeviceFactoryClass parent; \ + } NM##mixed##FactoryClass; \ \ static GType nm_##lower##_factory_get_type (void); \ - static void device_factory_interface_init (NMDeviceFactoryInterface *factory_iface); \ \ - G_DEFINE_TYPE_EXTENDED (NM##mixed##Factory, nm_##lower##_factory, G_TYPE_OBJECT, 0, \ - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init) \ - _nm_device_factory_internal_register_type (g_define_type_id);) \ + G_DEFINE_TYPE_WITH_CODE (NM##mixed##Factory, nm_##lower##_factory, NM_TYPE_DEVICE_FACTORY, \ + _nm_device_factory_internal_register_type (g_define_type_id);) \ \ /* Use a module constructor to register the factory's GType at load \ * time, which then calls _nm_device_factory_internal_register_type() \ @@ -242,13 +250,6 @@ extern const char *_nm_device_factory_no_default_settings[]; } \ \ NM_DEVICE_FACTORY_DECLARE_TYPES(st_code) \ - \ - static void \ - device_factory_interface_init (NMDeviceFactoryInterface *factory_iface) \ - { \ - factory_iface->get_supported_types = get_supported_types; \ - dfi_code \ - } \ \ static void \ nm_##lower##_factory_init (NM##mixed##Factory *self) \ @@ -256,8 +257,12 @@ extern const char *_nm_device_factory_no_default_settings[]; } \ \ static void \ - nm_##lower##_factory_class_init (NM##mixed##FactoryClass *lower##_class) \ + nm_##lower##_factory_class_init (NM##mixed##FactoryClass *klass) \ { \ + NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS (klass); \ + \ + factory_class->get_supported_types = get_supported_types; \ + dfi_code \ } void _nm_device_factory_internal_register_type (GType factory_type); diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c index 56bdcb6de0..f6c7f9aa3b 100644 --- a/src/devices/nm-device-infiniband.c +++ b/src/devices/nm-device-infiniband.c @@ -472,7 +472,7 @@ get_connection_iface (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (INFINIBAND, Infiniband, infiniband, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_INFINIBAND) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_INFINIBAND_SETTING_NAME), - factory_iface->create_device = create_device; - factory_iface->get_connection_parent = get_connection_parent; - factory_iface->get_connection_iface = get_connection_iface; -) + factory_class->create_device = create_device; + factory_class->get_connection_parent = get_connection_parent; + factory_class->get_connection_iface = get_connection_iface; +); diff --git a/src/devices/nm-device-ip-tunnel.c b/src/devices/nm-device-ip-tunnel.c index 036e3ea4d7..3244daebee 100644 --- a/src/devices/nm-device-ip-tunnel.c +++ b/src/devices/nm-device-ip-tunnel.c @@ -1053,7 +1053,7 @@ get_connection_iface (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (IP_TUNNEL, IPTunnel, ip_tunnel, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_GRE, NM_LINK_TYPE_SIT, NM_LINK_TYPE_IPIP) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_IP_TUNNEL_SETTING_NAME), - factory_iface->create_device = create_device; - factory_iface->get_connection_parent = get_connection_parent; - factory_iface->get_connection_iface = get_connection_iface; + factory_class->create_device = create_device; + factory_class->get_connection_parent = get_connection_parent; + factory_class->get_connection_iface = get_connection_iface; ); diff --git a/src/devices/nm-device-macvlan.c b/src/devices/nm-device-macvlan.c index 2cfd2492a6..c5a5a6b454 100644 --- a/src/devices/nm-device-macvlan.c +++ b/src/devices/nm-device-macvlan.c @@ -739,7 +739,7 @@ get_connection_iface (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (MACVLAN, Macvlan, macvlan, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_MACVLAN, NM_LINK_TYPE_MACVTAP) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_MACVLAN_SETTING_NAME), - factory_iface->create_device = create_device; - factory_iface->get_connection_parent = get_connection_parent; - factory_iface->get_connection_iface = get_connection_iface; + factory_class->create_device = create_device; + factory_class->get_connection_parent = get_connection_parent; + factory_class->get_connection_iface = get_connection_iface; ); diff --git a/src/devices/nm-device-tun.c b/src/devices/nm-device-tun.c index 2bed672b5f..f93ac072f8 100644 --- a/src/devices/nm-device-tun.c +++ b/src/devices/nm-device-tun.c @@ -532,6 +532,5 @@ create_device (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (TUN, Tun, tun, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_TUN, NM_LINK_TYPE_TAP) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_TUN_SETTING_NAME), - factory_iface->create_device = create_device; + factory_class->create_device = create_device; ); - diff --git a/src/devices/nm-device-veth.c b/src/devices/nm-device-veth.c index 0407c2a47e..d13538b8a4 100644 --- a/src/devices/nm-device-veth.c +++ b/src/devices/nm-device-veth.c @@ -202,6 +202,5 @@ create_device (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (VETH, Veth, veth, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_VETH), - factory_iface->create_device = create_device; - ) - + factory_class->create_device = create_device; +); diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c index 1c2f6dd70a..2d4dfa3910 100644 --- a/src/devices/nm-device-vlan.c +++ b/src/devices/nm-device-vlan.c @@ -761,7 +761,7 @@ get_connection_iface (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (VLAN, Vlan, vlan, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_VLAN) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_VLAN_SETTING_NAME), - factory_iface->create_device = create_device; - factory_iface->get_connection_parent = get_connection_parent; - factory_iface->get_connection_iface = get_connection_iface; + factory_class->create_device = create_device; + factory_class->get_connection_parent = get_connection_parent; + factory_class->get_connection_iface = get_connection_iface; ); diff --git a/src/devices/nm-device-vxlan.c b/src/devices/nm-device-vxlan.c index 1113a5b8b1..fa78bf73cf 100644 --- a/src/devices/nm-device-vxlan.c +++ b/src/devices/nm-device-vxlan.c @@ -817,8 +817,7 @@ get_connection_iface (NMDeviceFactory *factory, NM_DEVICE_FACTORY_DEFINE_INTERNAL (VXLAN, Vxlan, vxlan, NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_VXLAN) NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_VXLAN_SETTING_NAME), - factory_iface->create_device = create_device; - factory_iface->get_connection_parent = get_connection_parent; - factory_iface->get_connection_iface = get_connection_iface; + factory_class->create_device = create_device; + factory_class->get_connection_parent = get_connection_parent; + factory_class->get_connection_iface = get_connection_iface; ); - diff --git a/src/devices/team/nm-team-factory.c b/src/devices/team/nm-team-factory.c index 97275f7ef7..4a6f7b54c1 100644 --- a/src/devices/team/nm-team-factory.c +++ b/src/devices/team/nm-team-factory.c @@ -38,25 +38,31 @@ #define NM_IS_TEAM_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_TEAM_FACTORY)) #define NM_TEAM_FACTORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_TEAM_FACTORY, NMTeamFactoryClass)) -typedef struct _NMTeamFactory NMTeamFactory; -typedef struct _NMTeamFactoryClass NMTeamFactoryClass; +typedef struct { + NMDeviceFactory parent; +} NMTeamFactory; + +typedef struct { + NMDeviceFactoryClass parent; +} NMTeamFactoryClass; static GType nm_team_factory_get_type (void); +G_DEFINE_TYPE (NMTeamFactory, nm_team_factory, NM_TYPE_DEVICE_FACTORY) + /*****************************************************************************/ -struct _NMTeamFactory { - GObject parent; -}; +NM_DEVICE_FACTORY_DECLARE_TYPES ( + NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_TEAM) + NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_TEAM_SETTING_NAME) +) -struct _NMTeamFactoryClass { - GObjectClass parent; -}; - -static void device_factory_interface_init (NMDeviceFactoryInterface *factory_iface); - -G_DEFINE_TYPE_EXTENDED (NMTeamFactory, nm_team_factory, G_TYPE_OBJECT, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init)) +G_MODULE_EXPORT NMDeviceFactory * +nm_device_factory_create (GError **error) +{ + nm_manager_set_capability (nm_manager_get (), NM_CAPABILITY_TEAM); + return (NMDeviceFactory *) g_object_new (NM_TYPE_TEAM_FACTORY, NULL); +} /*****************************************************************************/ @@ -70,11 +76,6 @@ create_device (NMDeviceFactory *factory, return nm_device_team_new (iface); } -NM_DEVICE_FACTORY_DECLARE_TYPES ( - NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_TEAM) - NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_TEAM_SETTING_NAME) -) - /*****************************************************************************/ static void @@ -85,20 +86,8 @@ nm_team_factory_init (NMTeamFactory *self) static void nm_team_factory_class_init (NMTeamFactoryClass *klass) { -} + NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS (klass); -static void -device_factory_interface_init (NMDeviceFactoryInterface *factory_iface) -{ - factory_iface->create_device = create_device; - factory_iface->get_supported_types = get_supported_types; -} - -/*****************************************************************************/ - -G_MODULE_EXPORT NMDeviceFactory * -nm_device_factory_create (GError **error) -{ - nm_manager_set_capability (nm_manager_get (), NM_CAPABILITY_TEAM); - return (NMDeviceFactory *) g_object_new (NM_TYPE_TEAM_FACTORY, NULL); + factory_class->create_device = create_device; + factory_class->get_supported_types = get_supported_types; } diff --git a/src/devices/wifi/nm-wifi-factory.c b/src/devices/wifi/nm-wifi-factory.c index 0622a834b4..64ecca0827 100644 --- a/src/devices/wifi/nm-wifi-factory.c +++ b/src/devices/wifi/nm-wifi-factory.c @@ -39,25 +39,30 @@ #define NM_IS_WIFI_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_WIFI_FACTORY)) #define NM_WIFI_FACTORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_WIFI_FACTORY, NMWifiFactoryClass)) -typedef struct _NMWifiFactory NMWifiFactory; -typedef struct _NMWifiFactoryClass NMWifiFactoryClass; +typedef struct { + NMDeviceFactory parent; +} NMWifiFactory; + +typedef struct { + NMDeviceFactoryClass parent; +} NMWifiFactoryClass; static GType nm_wifi_factory_get_type (void); +G_DEFINE_TYPE (NMWifiFactory, nm_wifi_factory, NM_TYPE_DEVICE_FACTORY) + /*****************************************************************************/ -struct _NMWifiFactory { - GObject parent; -}; +NM_DEVICE_FACTORY_DECLARE_TYPES ( + NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_WIFI, NM_LINK_TYPE_OLPC_MESH) + NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_OLPC_MESH_SETTING_NAME) +) -struct _NMWifiFactoryClass { - GObjectClass parent; -}; - -static void device_factory_interface_init (NMDeviceFactoryInterface *factory_iface); - -G_DEFINE_TYPE_EXTENDED (NMWifiFactory, nm_wifi_factory, G_TYPE_OBJECT, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init)) +G_MODULE_EXPORT NMDeviceFactory * +nm_device_factory_create (GError **error) +{ + return (NMDeviceFactory *) g_object_new (NM_TYPE_WIFI_FACTORY, NULL); +} /*****************************************************************************/ @@ -99,11 +104,6 @@ create_device (NMDeviceFactory *factory, return nm_device_olpc_mesh_new (iface); } -NM_DEVICE_FACTORY_DECLARE_TYPES ( - NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_WIFI, NM_LINK_TYPE_OLPC_MESH) - NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_OLPC_MESH_SETTING_NAME) -) - /*****************************************************************************/ static void @@ -112,21 +112,10 @@ nm_wifi_factory_init (NMWifiFactory *self) } static void -nm_wifi_factory_class_init (NMWifiFactoryClass *wf_class) +nm_wifi_factory_class_init (NMWifiFactoryClass *klass) { -} + NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS (klass); -static void -device_factory_interface_init (NMDeviceFactoryInterface *factory_iface) -{ - factory_iface->create_device = create_device; - factory_iface->get_supported_types = get_supported_types; -} - -/*****************************************************************************/ - -G_MODULE_EXPORT NMDeviceFactory * -nm_device_factory_create (GError **error) -{ - return (NMDeviceFactory *) g_object_new (NM_TYPE_WIFI_FACTORY, NULL); + factory_class->create_device = create_device; + factory_class->get_supported_types = get_supported_types; } diff --git a/src/devices/wwan/nm-wwan-factory.c b/src/devices/wwan/nm-wwan-factory.c index c9f6cc12fb..458ab2e17a 100644 --- a/src/devices/wwan/nm-wwan-factory.c +++ b/src/devices/wwan/nm-wwan-factory.c @@ -39,35 +39,40 @@ #define NM_IS_WWAN_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_WWAN_FACTORY)) #define NM_WWAN_FACTORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_WWAN_FACTORY, NMWwanFactoryClass)) -typedef struct _NMWwanFactory NMWwanFactory; -typedef struct _NMWwanFactoryClass NMWwanFactoryClass; - -static GType nm_wwan_factory_get_type (void); - -/*****************************************************************************/ - typedef struct { NMModemManager *mm; } NMWwanFactoryPrivate; -struct _NMWwanFactory { - GObject parent; +typedef struct { + NMDeviceFactory parent; NMWwanFactoryPrivate _priv; -}; +} NMWwanFactory; -struct _NMWwanFactoryClass { - GObjectClass parent; -}; +typedef struct { + NMDeviceFactoryClass parent; +} NMWwanFactoryClass; -static void device_factory_interface_init (NMDeviceFactoryInterface *factory_iface); +static GType nm_wwan_factory_get_type (void); -G_DEFINE_TYPE_EXTENDED (NMWwanFactory, nm_wwan_factory, G_TYPE_OBJECT, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init)) +G_DEFINE_TYPE (NMWwanFactory, nm_wwan_factory, NM_TYPE_DEVICE_FACTORY) #define NM_WWAN_FACTORY_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMWwanFactory, NM_IS_WWAN_FACTORY) /*****************************************************************************/ +NM_DEVICE_FACTORY_DECLARE_TYPES ( + NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_WWAN_NET) + NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_GSM_SETTING_NAME, NM_SETTING_CDMA_SETTING_NAME) +) + +G_MODULE_EXPORT NMDeviceFactory * +nm_device_factory_create (GError **error) +{ + return (NMDeviceFactory *) g_object_new (NM_TYPE_WWAN_FACTORY, NULL); +} + +/*****************************************************************************/ + static void modem_added_cb (NMModemManager *manager, NMModem *modem, @@ -103,11 +108,6 @@ modem_added_cb (NMModemManager *manager, } -NM_DEVICE_FACTORY_DECLARE_TYPES ( - NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_WWAN_NET) - NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_GSM_SETTING_NAME, NM_SETTING_CDMA_SETTING_NAME) -) - static NMDevice * create_device (NMDeviceFactory *factory, const char *iface, @@ -160,22 +160,11 @@ static void nm_wwan_factory_class_init (NMWwanFactoryClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS (klass); object_class->dispose = dispose; -} -static void -device_factory_interface_init (NMDeviceFactoryInterface *factory_iface) -{ - factory_iface->get_supported_types = get_supported_types; - factory_iface->create_device = create_device; - factory_iface->start = start; -} - -/*****************************************************************************/ - -G_MODULE_EXPORT NMDeviceFactory * -nm_device_factory_create (GError **error) -{ - return (NMDeviceFactory *) g_object_new (NM_TYPE_WWAN_FACTORY, NULL); + factory_class->get_supported_types = get_supported_types; + factory_class->create_device = create_device; + factory_class->start = start; } diff --git a/src/nm-manager.c b/src/nm-manager.c index 74efb5dda4..5ce72acf4d 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1112,7 +1112,7 @@ nm_manager_get_connection_iface (NMManager *self, } if ( !out_parent - && !NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_connection_iface) { + && !NM_DEVICE_FACTORY_GET_CLASS (factory)->get_connection_iface) { /* optimization. Shortcut lookup of the partent device. */ iface = g_strdup (nm_connection_get_interface_name (connection)); if (!iface) { From 92b7cb2161ea5692589de4fd9291b90fa94d335b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Oct 2016 17:00:59 +0200 Subject: [PATCH 3/4] device: rename internal device factories Instead of NMBondFactory, call it NMBondDeviceFactory. --- src/devices/nm-device-bond.c | 4 ++-- src/devices/nm-device-bridge.c | 4 ++-- src/devices/nm-device-ethernet.c | 4 ++-- src/devices/nm-device-factory.h | 14 +++++++------- src/devices/nm-device-infiniband.c | 4 ++-- src/devices/nm-device-ip-tunnel.c | 4 ++-- src/devices/nm-device-macvlan.c | 4 ++-- src/devices/nm-device-tun.c | 4 ++-- src/devices/nm-device-veth.c | 4 ++-- src/devices/nm-device-vlan.c | 4 ++-- src/devices/nm-device-vxlan.c | 4 ++-- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/devices/nm-device-bond.c b/src/devices/nm-device-bond.c index ed62c5bf7a..ae029438c8 100644 --- a/src/devices/nm-device-bond.c +++ b/src/devices/nm-device-bond.c @@ -523,8 +523,8 @@ nm_device_bond_class_init (NMDeviceBondClass *klass) /*****************************************************************************/ -#define NM_TYPE_BOND_FACTORY (nm_bond_factory_get_type ()) -#define NM_BOND_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BOND_FACTORY, NMBondFactory)) +#define NM_TYPE_BOND_DEVICE_FACTORY (nm_bond_device_factory_get_type ()) +#define NM_BOND_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BOND_DEVICE_FACTORY, NMBondDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-bridge.c b/src/devices/nm-device-bridge.c index becc072859..682c260b17 100644 --- a/src/devices/nm-device-bridge.c +++ b/src/devices/nm-device-bridge.c @@ -451,8 +451,8 @@ nm_device_bridge_class_init (NMDeviceBridgeClass *klass) /*****************************************************************************/ -#define NM_TYPE_BRIDGE_FACTORY (nm_bridge_factory_get_type ()) -#define NM_BRIDGE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BRIDGE_FACTORY, NMBridgeFactory)) +#define NM_TYPE_BRIDGE_DEVICE_FACTORY (nm_bridge_device_factory_get_type ()) +#define NM_BRIDGE_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BRIDGE_DEVICE_FACTORY, NMBridgeDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index 95ff95310f..a4a560f049 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -1677,8 +1677,8 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass) /*****************************************************************************/ -#define NM_TYPE_ETHERNET_FACTORY (nm_ethernet_factory_get_type ()) -#define NM_ETHERNET_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_ETHERNET_FACTORY, NMEthernetFactory)) +#define NM_TYPE_ETHERNET_DEVICE_FACTORY (nm_ethernet_device_factory_get_type ()) +#define NM_ETHERNET_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_ETHERNET_DEVICE_FACTORY, NMEthernetDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-factory.h b/src/devices/nm-device-factory.h index 0acc270963..d831af926e 100644 --- a/src/devices/nm-device-factory.h +++ b/src/devices/nm-device-factory.h @@ -228,14 +228,14 @@ extern const char *_nm_device_factory_no_default_settings[]; #define NM_DEVICE_FACTORY_DEFINE_INTERNAL(upper, mixed, lower, st_code, dfi_code) \ typedef struct { \ NMDeviceFactory parent; \ - } NM##mixed##Factory; \ + } NM##mixed##DeviceFactory; \ typedef struct { \ NMDeviceFactoryClass parent; \ - } NM##mixed##FactoryClass; \ + } NM##mixed##DeviceFactoryClass; \ \ - static GType nm_##lower##_factory_get_type (void); \ + static GType nm_##lower##_device_factory_get_type (void); \ \ - G_DEFINE_TYPE_WITH_CODE (NM##mixed##Factory, nm_##lower##_factory, NM_TYPE_DEVICE_FACTORY, \ + G_DEFINE_TYPE_WITH_CODE (NM##mixed##DeviceFactory, nm_##lower##_device_factory, NM_TYPE_DEVICE_FACTORY, \ _nm_device_factory_internal_register_type (g_define_type_id);) \ \ /* Use a module constructor to register the factory's GType at load \ @@ -246,18 +246,18 @@ extern const char *_nm_device_factory_no_default_settings[]; register_device_factory_internal_##lower (void) \ { \ nm_g_type_init (); \ - g_type_ensure (NM_TYPE_##upper##_FACTORY); \ + g_type_ensure (NM_TYPE_##upper##_DEVICE_FACTORY); \ } \ \ NM_DEVICE_FACTORY_DECLARE_TYPES(st_code) \ \ static void \ - nm_##lower##_factory_init (NM##mixed##Factory *self) \ + nm_##lower##_device_factory_init (NM##mixed##DeviceFactory *self) \ { \ } \ \ static void \ - nm_##lower##_factory_class_init (NM##mixed##FactoryClass *klass) \ + nm_##lower##_device_factory_class_init (NM##mixed##DeviceFactoryClass *klass) \ { \ NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS (klass); \ \ diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c index f6c7f9aa3b..bc6051569c 100644 --- a/src/devices/nm-device-infiniband.c +++ b/src/devices/nm-device-infiniband.c @@ -402,8 +402,8 @@ nm_device_infiniband_class_init (NMDeviceInfinibandClass *klass) /*****************************************************************************/ -#define NM_TYPE_INFINIBAND_FACTORY (nm_infiniband_factory_get_type ()) -#define NM_INFINIBAND_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_INFINIBAND_FACTORY, NMInfinibandFactory)) +#define NM_TYPE_INFINIBAND_DEVICE_FACTORY (nm_infiniband_device_factory_get_type ()) +#define NM_INFINIBAND_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_INFINIBAND_DEVICE_FACTORY, NMInfinibandDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-ip-tunnel.c b/src/devices/nm-device-ip-tunnel.c index 3244daebee..0aef9d2ef9 100644 --- a/src/devices/nm-device-ip-tunnel.c +++ b/src/devices/nm-device-ip-tunnel.c @@ -981,8 +981,8 @@ nm_device_ip_tunnel_class_init (NMDeviceIPTunnelClass *klass) /*****************************************************************************/ -#define NM_TYPE_IP_TUNNEL_FACTORY (nm_ip_tunnel_factory_get_type ()) -#define NM_IP_TUNNEL_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_IP_TUNNEL_FACTORY, NMIPTunnelFactory)) +#define NM_TYPE_IP_TUNNEL_DEVICE_FACTORY (nm_ip_tunnel_device_factory_get_type ()) +#define NM_IP_TUNNEL_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_IP_TUNNEL_DEVICE_FACTORY, NMIPTunnelDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-macvlan.c b/src/devices/nm-device-macvlan.c index c5a5a6b454..9e32694ffd 100644 --- a/src/devices/nm-device-macvlan.c +++ b/src/devices/nm-device-macvlan.c @@ -658,8 +658,8 @@ nm_device_macvlan_class_init (NMDeviceMacvlanClass *klass) /*****************************************************************************/ -#define NM_TYPE_MACVLAN_FACTORY (nm_macvlan_factory_get_type ()) -#define NM_MACVLAN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_MACVLAN_FACTORY, NMMacvlanFactory)) +#define NM_TYPE_MACVLAN_DEVICE_FACTORY (nm_macvlan_device_factory_get_type ()) +#define NM_MACVLAN_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_MACVLAN_DEVICE_FACTORY, NMMacvlanDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-tun.c b/src/devices/nm-device-tun.c index f93ac072f8..d32caa402d 100644 --- a/src/devices/nm-device-tun.c +++ b/src/devices/nm-device-tun.c @@ -485,8 +485,8 @@ nm_device_tun_class_init (NMDeviceTunClass *klass) /*****************************************************************************/ -#define NM_TYPE_TUN_FACTORY (nm_tun_factory_get_type ()) -#define NM_TUN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_TUN_FACTORY, NMTunFactory)) +#define NM_TYPE_TUN_DEVICE_FACTORY (nm_tun_device_factory_get_type ()) +#define NM_TUN_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_TUN_DEVICE_FACTORY, NMTunDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-veth.c b/src/devices/nm-device-veth.c index d13538b8a4..bb23fdaa6c 100644 --- a/src/devices/nm-device-veth.c +++ b/src/devices/nm-device-veth.c @@ -182,8 +182,8 @@ nm_device_veth_class_init (NMDeviceVethClass *klass) /*****************************************************************************/ -#define NM_TYPE_VETH_FACTORY (nm_veth_factory_get_type ()) -#define NM_VETH_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VETH_FACTORY, NMVethFactory)) +#define NM_TYPE_VETH_DEVICE_FACTORY (nm_veth_device_factory_get_type ()) +#define NM_VETH_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VETH_DEVICE_FACTORY, NMVethDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c index 2d4dfa3910..0f241e8388 100644 --- a/src/devices/nm-device-vlan.c +++ b/src/devices/nm-device-vlan.c @@ -688,8 +688,8 @@ nm_device_vlan_class_init (NMDeviceVlanClass *klass) /*****************************************************************************/ -#define NM_TYPE_VLAN_FACTORY (nm_vlan_factory_get_type ()) -#define NM_VLAN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VLAN_FACTORY, NMVlanFactory)) +#define NM_TYPE_VLAN_DEVICE_FACTORY (nm_vlan_device_factory_get_type ()) +#define NM_VLAN_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VLAN_DEVICE_FACTORY, NMVlanDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, diff --git a/src/devices/nm-device-vxlan.c b/src/devices/nm-device-vxlan.c index fa78bf73cf..a49e64c6af 100644 --- a/src/devices/nm-device-vxlan.c +++ b/src/devices/nm-device-vxlan.c @@ -763,8 +763,8 @@ nm_device_vxlan_class_init (NMDeviceVxlanClass *klass) /*****************************************************************************/ -#define NM_TYPE_VXLAN_FACTORY (nm_vxlan_factory_get_type ()) -#define NM_VXLAN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VXLAN_FACTORY, NMVxlanFactory)) +#define NM_TYPE_VXLAN_DEVICE_FACTORY (nm_vxlan_device_factory_get_type ()) +#define NM_VXLAN_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VXLAN_DEVICE_FACTORY, NMVxlanDeviceFactory)) static NMDevice * create_device (NMDeviceFactory *factory, From 2c26e3e7f9c2bb9afbf5d59a1ccc5622034e5cce Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Oct 2016 16:48:41 +0200 Subject: [PATCH 4/4] device: make registration of internal device-factories more explicit Internal device types are a static thing. Let's not do a constructor function to register the device factory. This gets rid of all attribute((constructor)) functions inside NetworkManager core. That is desired, because we don't want to run code before main(). For example, at that point logging is not yet initialized, but with code that runs before main() it is hard to ensure that we don't log anything yet. --- src/Makefile.am | 57 ++++++++++++--------------------- src/devices/nm-device-factory.c | 44 +++++++++++++++---------- src/devices/nm-device-factory.h | 18 ++--------- src/tests/Makefile.am | 16 --------- 4 files changed, 50 insertions(+), 85 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 7d2a37f1b5..e1a0510346 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -259,38 +259,6 @@ libNetworkManagerBase_la_LIBADD = \ ############################################################################### -# These source files have a attribute((constructor)) to register their factories. -# This gets stripped out from the resulting binary if we add them to libNetworkManager.la. -# Instead, add them to the binary. An alternative would be to link with --as-needed. - -nm_device_sources = \ - devices/nm-device-bond.c \ - devices/nm-device-bridge.c \ - devices/nm-device-ethernet.c \ - devices/nm-device-infiniband.c \ - devices/nm-device-ip-tunnel.c \ - devices/nm-device-macvlan.c \ - devices/nm-device-tun.c \ - devices/nm-device-veth.c \ - devices/nm-device-vlan.c \ - devices/nm-device-vxlan.c \ - $(NULL) - -nm_device_headers = \ - devices/nm-device-bond.h \ - devices/nm-device-bridge.h \ - devices/nm-device-ethernet.h \ - devices/nm-device-infiniband.h \ - devices/nm-device-ip-tunnel.h \ - devices/nm-device-macvlan.h \ - devices/nm-device-tun.h \ - devices/nm-device-veth.h \ - devices/nm-device-vlan.h \ - devices/nm-device-vxlan.h \ - $(NULL) - -############################################################################### - libNetworkManager_la_SOURCES = \ \ nm-checkpoint-manager.c \ @@ -313,6 +281,27 @@ libNetworkManager_la_SOURCES = \ devices/nm-device-logging.h \ devices/nm-device-private.h \ \ + devices/nm-device-bond.c \ + devices/nm-device-bond.h \ + devices/nm-device-bridge.c \ + devices/nm-device-bridge.h \ + devices/nm-device-ethernet.c \ + devices/nm-device-ethernet.h \ + devices/nm-device-infiniband.c \ + devices/nm-device-infiniband.h \ + devices/nm-device-ip-tunnel.c \ + devices/nm-device-ip-tunnel.h \ + devices/nm-device-macvlan.c \ + devices/nm-device-macvlan.h \ + devices/nm-device-tun.c \ + devices/nm-device-tun.h \ + devices/nm-device-veth.c \ + devices/nm-device-veth.h \ + devices/nm-device-vlan.c \ + devices/nm-device-vlan.h \ + devices/nm-device-vxlan.c \ + devices/nm-device-vxlan.h \ + \ dhcp-manager/nm-dhcp-client.c \ dhcp-manager/nm-dhcp-client.h \ dhcp-manager/nm-dhcp-client-logging.h \ @@ -513,10 +502,6 @@ libNetworkManagerTest_la_LIBADD = \ ############################################################################### NetworkManager_SOURCES = \ - \ - $(nm_device_sources) \ - $(nm_device_headers) \ - \ main-utils.c \ main-utils.h \ main.c diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index bdc893c49b..1f65300861 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -254,21 +254,12 @@ nm_device_factory_class_init (NMDeviceFactoryClass *klass) /*****************************************************************************/ -static GSList *internal_types = NULL; static GHashTable *factories_by_link = NULL; static GHashTable *factories_by_setting = NULL; -void -_nm_device_factory_internal_register_type (GType factory_type) -{ - g_return_if_fail (g_slist_find (internal_types, GUINT_TO_POINTER (factory_type)) == NULL); - internal_types = g_slist_prepend (internal_types, GUINT_TO_POINTER (factory_type)); -} - static void __attribute__((destructor)) _cleanup (void) { - g_clear_pointer (&internal_types, g_slist_free); g_clear_pointer (&factories_by_link, g_hash_table_unref); g_clear_pointer (&factories_by_setting, g_hash_table_unref); } @@ -481,12 +472,22 @@ _add_factory (NMDeviceFactory *factory, return TRUE; } +static void +_load_internal_factory (GType factory_gtype, + NMDeviceFactoryManagerFactoryFunc callback, + gpointer user_data) +{ + NMDeviceFactory *factory; + + factory = (NMDeviceFactory *) g_object_new (factory_gtype, NULL); + _add_factory (factory, FALSE, "internal", callback, user_data); +} + void nm_device_factory_manager_load_factories (NMDeviceFactoryManagerFactoryFunc callback, gpointer user_data) { NMDeviceFactory *factory; - const GSList *iter; GError *error = NULL; char **path, **paths; @@ -496,14 +497,23 @@ nm_device_factory_manager_load_factories (NMDeviceFactoryManagerFactoryFunc call factories_by_link = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref); factories_by_setting = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref); - /* Register internal factories first */ - for (iter = internal_types; iter; iter = iter->next) { - GType ftype = (GType) GPOINTER_TO_SIZE (iter->data); +#define _ADD_INTERNAL(get_type_fcn) \ + G_STMT_START { \ + GType get_type_fcn (void); \ + _load_internal_factory (get_type_fcn (), \ + callback, user_data); \ + } G_STMT_END - factory = (NMDeviceFactory *) g_object_new (ftype, NULL); - g_assert (factory); - _add_factory (factory, FALSE, "internal", callback, user_data); - } + _ADD_INTERNAL (nm_bond_device_factory_get_type); + _ADD_INTERNAL (nm_bridge_device_factory_get_type); + _ADD_INTERNAL (nm_ethernet_device_factory_get_type); + _ADD_INTERNAL (nm_infiniband_device_factory_get_type); + _ADD_INTERNAL (nm_ip_tunnel_device_factory_get_type); + _ADD_INTERNAL (nm_macvlan_device_factory_get_type); + _ADD_INTERNAL (nm_tun_device_factory_get_type); + _ADD_INTERNAL (nm_veth_device_factory_get_type); + _ADD_INTERNAL (nm_vlan_device_factory_get_type); + _ADD_INTERNAL (nm_vxlan_device_factory_get_type); paths = read_device_factory_paths (); if (!paths) diff --git a/src/devices/nm-device-factory.h b/src/devices/nm-device-factory.h index d831af926e..2faaad9df6 100644 --- a/src/devices/nm-device-factory.h +++ b/src/devices/nm-device-factory.h @@ -233,21 +233,9 @@ extern const char *_nm_device_factory_no_default_settings[]; NMDeviceFactoryClass parent; \ } NM##mixed##DeviceFactoryClass; \ \ - static GType nm_##lower##_device_factory_get_type (void); \ + GType nm_##lower##_device_factory_get_type (void); \ \ - G_DEFINE_TYPE_WITH_CODE (NM##mixed##DeviceFactory, nm_##lower##_device_factory, NM_TYPE_DEVICE_FACTORY, \ - _nm_device_factory_internal_register_type (g_define_type_id);) \ - \ - /* Use a module constructor to register the factory's GType at load \ - * time, which then calls _nm_device_factory_internal_register_type() \ - * to register the factory's GType with the Manager. \ - */ \ - static void __attribute__((constructor)) \ - register_device_factory_internal_##lower (void) \ - { \ - nm_g_type_init (); \ - g_type_ensure (NM_TYPE_##upper##_DEVICE_FACTORY); \ - } \ + G_DEFINE_TYPE (NM##mixed##DeviceFactory, nm_##lower##_device_factory, NM_TYPE_DEVICE_FACTORY) \ \ NM_DEVICE_FACTORY_DECLARE_TYPES(st_code) \ \ @@ -265,8 +253,6 @@ extern const char *_nm_device_factory_no_default_settings[]; dfi_code \ } -void _nm_device_factory_internal_register_type (GType factory_type); - /************************************************************************** * PRIVATE FACTORY FUNCTIONS - for factory consumers (eg, NMManager). **************************************************************************/ diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index c11e7a0b5f..35c70d2103 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -168,19 +168,3 @@ TESTS = \ test-wired-defname \ test-utils - -if ENABLE_TESTS - -check-local: - @for t in bond bridge ethernet infiniband ip_tunnel macvlan tun veth vlan vxlan; do \ - # Ensure the device subclass factory registration constructors exist \ - # which could inadvertently break if src/Makefile.am gets changed \ - if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ - echo "Testing device factory symbols... FAILED" ; \ - exit 1 ; \ - fi \ - done - @echo "Testing device factory symbols... PASSED" - -endif -