From 28cb40705638435df62e07d8e7c210f47c21efb3 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 27 Sep 2022 12:45:18 +0200 Subject: [PATCH 1/2] libnm: rework lookup of private data for NMConnection of NMSimpleConnection NMConnection is an interface, and as such has no data itself. In practice, there are only two implementations of this interface, NMSimpleConnection and NMRemoteConnection. The latter only exists in libnm, not the daemon. Thus, lookup of the private data is already optimized for NMSimpleConnection instances via _nm_simple_connection_private_offset. Use the same mechanism also for NMSimpleConnection itself. --- src/libnm-core-impl/nm-connection.c | 26 +++++++++++----------- src/libnm-core-impl/nm-setting-private.h | 17 ++++++++++++++ src/libnm-core-impl/nm-simple-connection.c | 9 ++------ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/libnm-core-impl/nm-connection.c b/src/libnm-core-impl/nm-connection.c index 5a46226559..eabb3a6996 100644 --- a/src/libnm-core-impl/nm-connection.c +++ b/src/libnm-core-impl/nm-connection.c @@ -124,19 +124,19 @@ _nm_connection_get_private_from_qdata(NMConnection *connection) return priv; } -#define NM_CONNECTION_GET_PRIVATE(connection) \ - ({ \ - NMConnection *_connection = (connection); \ - NMConnectionPrivate *_priv; \ - \ - if (G_LIKELY(NM_IS_SIMPLE_CONNECTION(_connection))) \ - _priv = (gpointer) (&(((char *) _connection)[_nm_simple_connection_private_offset])); \ - else \ - _priv = _nm_connection_get_private_from_qdata(_connection); \ - \ - nm_assert(_priv && _priv->self == _connection); \ - \ - _priv; \ +#define NM_CONNECTION_GET_PRIVATE(connection) \ + ({ \ + NMConnection *_connection = (connection); \ + NMConnectionPrivate *_priv; \ + \ + if (G_LIKELY(NM_IS_SIMPLE_CONNECTION(_connection))) \ + _priv = _NM_SIMPLE_CONNECTION_GET_CONNECTION_PRIVATE(_connection); \ + else \ + _priv = _nm_connection_get_private_from_qdata(_connection); \ + \ + nm_assert(_priv && _priv->self == _connection); \ + \ + _priv; \ }) /*****************************************************************************/ diff --git a/src/libnm-core-impl/nm-setting-private.h b/src/libnm-core-impl/nm-setting-private.h index 38b233e92a..01d3113cde 100644 --- a/src/libnm-core-impl/nm-setting-private.h +++ b/src/libnm-core-impl/nm-setting-private.h @@ -31,6 +31,23 @@ typedef struct { extern GTypeClass *_nm_simple_connection_class_instance; extern int _nm_simple_connection_private_offset; +#define _NM_SIMPLE_CONNECTION_GET_CONNECTION_PRIVATE(connection) \ + ({ \ + gpointer _connection_1 = (connection); \ + NMConnectionPrivate *_priv_1; \ + \ + nm_assert(NM_IS_SIMPLE_CONNECTION(_connection_1)); \ + \ + _priv_1 = (void *) (&(((char *) _connection_1)[_nm_simple_connection_private_offset])); \ + \ + nm_assert(_priv_1 \ + == G_TYPE_INSTANCE_GET_PRIVATE(_connection_1, \ + NM_TYPE_SIMPLE_CONNECTION, \ + NMConnectionPrivate)); \ + \ + _priv_1; \ + }) + void _nm_connection_private_clear(NMConnectionPrivate *priv); /*****************************************************************************/ diff --git a/src/libnm-core-impl/nm-simple-connection.c b/src/libnm-core-impl/nm-simple-connection.c index 6252dc2ce3..802be3caa4 100644 --- a/src/libnm-core-impl/nm-simple-connection.c +++ b/src/libnm-core-impl/nm-simple-connection.c @@ -47,9 +47,6 @@ G_DEFINE_TYPE_WITH_CODE(NMSimpleConnection, G_IMPLEMENT_INTERFACE(NM_TYPE_CONNECTION, nm_simple_connection_interface_init);) -#define _GET_PRIVATE(self) \ - G_TYPE_INSTANCE_GET_PRIVATE(self, NM_TYPE_SIMPLE_CONNECTION, NMConnectionPrivate) - /*****************************************************************************/ static void @@ -57,7 +54,7 @@ nm_simple_connection_init(NMSimpleConnection *self) { NMConnectionPrivate *priv; - priv = _GET_PRIVATE(self); + priv = _NM_SIMPLE_CONNECTION_GET_CONNECTION_PRIVATE(self); priv->self = (NMConnection *) self; } @@ -157,14 +154,12 @@ nm_simple_connection_new_clone(NMConnection *connection) static void dispose(GObject *object) { - NMConnection *connection = NM_CONNECTION(object); - #if NM_MORE_ASSERTS g_signal_handlers_disconnect_by_data(object, (gpointer) &_nm_assert_connection_unchanging_user_data); #endif - _nm_connection_private_clear(_GET_PRIVATE(connection)); + _nm_connection_private_clear(_NM_SIMPLE_CONNECTION_GET_CONNECTION_PRIVATE(object)); G_OBJECT_CLASS(nm_simple_connection_parent_class)->dispose(object); } From d75bfd3a3dae290c4a179ae797aa0ab7a9f06f2e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 27 Sep 2022 13:04:47 +0200 Subject: [PATCH 2/2] libnm: move optimized NM_IS_{SIMPLE,}CONNECTION() to internal header We already redefine those checks to optimize for NMSimpleConnection. Which, in particular when libnm-core is used by the daemon, is the only implementation of the NMConnection interface. Move those to the private header file. No need to keep it private to "nm-connection.c". --- src/libnm-core-impl/nm-connection.c | 27 ------------------------ src/libnm-core-impl/nm-setting-private.h | 26 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/libnm-core-impl/nm-connection.c b/src/libnm-core-impl/nm-connection.c index eabb3a6996..60e7eb7ff2 100644 --- a/src/libnm-core-impl/nm-connection.c +++ b/src/libnm-core-impl/nm-connection.c @@ -54,33 +54,6 @@ static gboolean _nm_connection_clear_settings(NMConnection *connection, NMConnec /*****************************************************************************/ -#undef NM_IS_SIMPLE_CONNECTION -#define NM_IS_SIMPLE_CONNECTION(self) \ - ({ \ - gconstpointer _self1 = (self); \ - gboolean _result; \ - \ - _result = \ - (_self1 \ - && (((GTypeInstance *) _self1)->g_class == _nm_simple_connection_class_instance)); \ - \ - nm_assert(_result == G_TYPE_CHECK_INSTANCE_TYPE(_self1, NM_TYPE_SIMPLE_CONNECTION)); \ - \ - _result; \ - }) - -#undef NM_IS_CONNECTION -#define NM_IS_CONNECTION(self) \ - ({ \ - gconstpointer _self0 = (self); \ - \ - (_self0 \ - && (NM_IS_SIMPLE_CONNECTION(_self0) \ - || G_TYPE_CHECK_INSTANCE_TYPE(_self0, NM_TYPE_CONNECTION))); \ - }) - -/*****************************************************************************/ - void _nm_connection_private_clear(NMConnectionPrivate *priv) { diff --git a/src/libnm-core-impl/nm-setting-private.h b/src/libnm-core-impl/nm-setting-private.h index 01d3113cde..005d04808a 100644 --- a/src/libnm-core-impl/nm-setting-private.h +++ b/src/libnm-core-impl/nm-setting-private.h @@ -13,6 +13,7 @@ #include "nm-setting.h" #include "nm-setting-bridge.h" #include "nm-connection.h" +#include "nm-simple-connection.h" #include "nm-core-enum-types.h" #include "libnm-core-intern/nm-core-internal.h" @@ -31,6 +32,31 @@ typedef struct { extern GTypeClass *_nm_simple_connection_class_instance; extern int _nm_simple_connection_private_offset; +#undef NM_IS_SIMPLE_CONNECTION +#define NM_IS_SIMPLE_CONNECTION(self) \ + ({ \ + gconstpointer _self1 = (self); \ + gboolean _result; \ + \ + _result = \ + (_self1 \ + && (((GTypeInstance *) _self1)->g_class == _nm_simple_connection_class_instance)); \ + \ + nm_assert(_result == G_TYPE_CHECK_INSTANCE_TYPE(_self1, NM_TYPE_SIMPLE_CONNECTION)); \ + \ + _result; \ + }) + +#undef NM_IS_CONNECTION +#define NM_IS_CONNECTION(self) \ + ({ \ + gconstpointer _self0 = (self); \ + \ + (_self0 \ + && (NM_IS_SIMPLE_CONNECTION(_self0) \ + || G_TYPE_CHECK_INSTANCE_TYPE(_self0, NM_TYPE_CONNECTION))); \ + }) + #define _NM_SIMPLE_CONNECTION_GET_CONNECTION_PRIVATE(connection) \ ({ \ gpointer _connection_1 = (connection); \