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.
This commit is contained in:
Thomas Haller 2022-09-27 12:45:18 +02:00
parent 5713a533f5
commit 28cb407056
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 32 additions and 20 deletions

View file

@ -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; \
})
/*****************************************************************************/

View file

@ -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);
/*****************************************************************************/

View file

@ -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);
}