libnm: cleanup redundant nm_connection_get_setting functions

Refactor and cleanup the functions to get a setting from a connection.

As the NMConnection tracks the settings in an array indexed by
NMMetaSettingType, the most direct and efficient way is to look up via
that enum.

Previously, nm_connection_get_setting_by_name() would first look up the GType
(which already involved looking up the NMMetaSettingInfo), then based on the
GType it would look up the NMMetaSettingInfo again to get the meta_type. That
is unnecessary. Directly look up the NMMetaSettingInfo, which directly
gives the meta_type.
This commit is contained in:
Thomas Haller
2023-05-04 11:34:58 +02:00
parent 27cbf584bd
commit c60a4649b8
2 changed files with 37 additions and 43 deletions

View File

@@ -276,26 +276,7 @@ nm_connection_remove_setting(NMConnection *connection, GType setting_type)
} }
static gpointer static gpointer
_connection_get_setting(NMConnection *connection, GType setting_type) _get_setting_by_metatype(NMConnectionPrivate *priv, NMMetaSettingType meta_type)
{
NMSetting *setting;
const NMMetaSettingInfo *setting_info;
nm_assert(NM_IS_CONNECTION(connection));
nm_assert(g_type_is_a(setting_type, NM_TYPE_SETTING));
setting_info = _nm_meta_setting_info_from_gtype(setting_type);
if (!setting_info)
g_return_val_if_reached(NULL);
setting = NM_CONNECTION_GET_PRIVATE(connection)->settings[setting_info->meta_type];
nm_assert(!setting || G_TYPE_CHECK_INSTANCE_TYPE(setting, setting_type));
return setting;
}
static gpointer
_connection_get_setting_by_meta_type(NMConnectionPrivate *priv, NMMetaSettingType meta_type)
{ {
nm_assert(priv); nm_assert(priv);
nm_assert(_NM_INT_NOT_NEGATIVE(meta_type)); nm_assert(_NM_INT_NOT_NEGATIVE(meta_type));
@@ -304,20 +285,12 @@ _connection_get_setting_by_meta_type(NMConnectionPrivate *priv, NMMetaSettingTyp
return priv->settings[meta_type]; return priv->settings[meta_type];
} }
static gpointer
_connection_get_setting_check(NMConnection *connection, GType setting_type)
{
g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL);
return _connection_get_setting(connection, setting_type);
}
static gpointer static gpointer
_nm_connection_get_setting_by_metatype(NMConnection *connection, NMMetaSettingType meta_type) _nm_connection_get_setting_by_metatype(NMConnection *connection, NMMetaSettingType meta_type)
{ {
g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL); g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL);
return _connection_get_setting_by_meta_type(NM_CONNECTION_GET_PRIVATE(connection), meta_type); return _get_setting_by_metatype(NM_CONNECTION_GET_PRIVATE(connection), meta_type);
} }
/** /**
@@ -334,19 +307,34 @@ _nm_connection_get_setting_by_metatype(NMConnection *connection, NMMetaSettingTy
NMSetting * NMSetting *
nm_connection_get_setting(NMConnection *connection, GType setting_type) nm_connection_get_setting(NMConnection *connection, GType setting_type)
{ {
g_return_val_if_fail(g_type_is_a(setting_type, NM_TYPE_SETTING), NULL); NMSetting *setting;
const NMMetaSettingInfo *setting_info;
return _connection_get_setting_check(connection, setting_type); g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL);
setting_info = _nm_meta_setting_info_from_gtype(setting_type);
if (!setting_info)
g_return_val_if_reached(NULL);
setting = NM_CONNECTION_GET_PRIVATE(connection)->settings[setting_info->meta_type];
nm_assert(!setting || G_TYPE_CHECK_INSTANCE_TYPE(setting, setting_type));
return setting;
} }
NMSettingIPConfig * NMSettingIPConfig *
nm_connection_get_setting_ip_config(NMConnection *connection, int addr_family) nm_connection_get_setting_ip_config(NMConnection *connection, int addr_family)
{ {
g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL);
nm_assert_addr_family(addr_family); nm_assert_addr_family(addr_family);
return NM_SETTING_IP_CONFIG(_connection_get_setting( return NM_SETTING_IP_CONFIG(_get_setting_by_metatype(NM_CONNECTION_GET_PRIVATE(connection),
connection, (addr_family == AF_INET)
(addr_family == AF_INET) ? NM_TYPE_SETTING_IP4_CONFIG : NM_TYPE_SETTING_IP6_CONFIG)); ? NM_META_SETTING_TYPE_IP4_CONFIG
: NM_META_SETTING_TYPE_IP6_CONFIG));
} }
/** /**
@@ -363,12 +351,14 @@ nm_connection_get_setting_ip_config(NMConnection *connection, int addr_family)
NMSetting * NMSetting *
nm_connection_get_setting_by_name(NMConnection *connection, const char *name) nm_connection_get_setting_by_name(NMConnection *connection, const char *name)
{ {
GType type; const NMMetaSettingInfo *setting_info;
g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL); g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL);
type = nm_setting_lookup_type(name); setting_info = nm_meta_setting_infos_by_name(name);
return type ? _connection_get_setting(connection, type) : NULL; return setting_info ? _get_setting_by_metatype(NM_CONNECTION_GET_PRIVATE(connection),
setting_info->meta_type)
: NULL;
} }
/*****************************************************************************/ /*****************************************************************************/
@@ -1672,8 +1662,8 @@ _normalize_802_1x_empty_strings(NMConnection *self)
NMSetting8021x *s_8021x; NMSetting8021x *s_8021x;
gboolean changed = FALSE; gboolean changed = FALSE;
s_8021x = _connection_get_setting_by_meta_type(NM_CONNECTION_GET_PRIVATE(self), s_8021x =
NM_META_SETTING_TYPE_802_1X); _get_setting_by_metatype(NM_CONNECTION_GET_PRIVATE(self), NM_META_SETTING_TYPE_802_1X);
if (!s_8021x) if (!s_8021x)
return FALSE; return FALSE;
@@ -1823,7 +1813,7 @@ _nm_connection_verify(NMConnection *connection, GError **error)
priv = NM_CONNECTION_GET_PRIVATE(connection); priv = NM_CONNECTION_GET_PRIVATE(connection);
if (!_connection_get_setting_by_meta_type(priv, NM_META_SETTING_TYPE_CONNECTION)) { if (!_get_setting_by_metatype(priv, NM_META_SETTING_TYPE_CONNECTION)) {
g_set_error_literal(error, g_set_error_literal(error,
NM_CONNECTION_ERROR, NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_MISSING_SETTING, NM_CONNECTION_ERROR_MISSING_SETTING,
@@ -1868,9 +1858,9 @@ _nm_connection_verify(NMConnection *connection, GError **error)
g_clear_error(&verify_error); g_clear_error(&verify_error);
} }
s_ip4 = _connection_get_setting_by_meta_type(priv, NM_META_SETTING_TYPE_IP4_CONFIG); s_ip4 = _get_setting_by_metatype(priv, NM_META_SETTING_TYPE_IP4_CONFIG);
s_ip6 = _connection_get_setting_by_meta_type(priv, NM_META_SETTING_TYPE_IP6_CONFIG); s_ip6 = _get_setting_by_metatype(priv, NM_META_SETTING_TYPE_IP6_CONFIG);
s_proxy = _connection_get_setting_by_meta_type(priv, NM_META_SETTING_TYPE_PROXY); s_proxy = _get_setting_by_metatype(priv, NM_META_SETTING_TYPE_PROXY);
nm_assert(normalizable_error_type != NM_SETTING_VERIFY_ERROR); nm_assert(normalizable_error_type != NM_SETTING_VERIFY_ERROR);
if (NM_IN_SET(normalizable_error_type, if (NM_IN_SET(normalizable_error_type,

View File

@@ -110,6 +110,10 @@ nm_setting_lookup_type(const char *name)
{ {
const NMMetaSettingInfo *setting_info; const NMMetaSettingInfo *setting_info;
/* various callers check whether the result is valid with plain `if (gtype)`.
* Assert that G_TYPE_INVALID is zero. */
G_STATIC_ASSERT(G_TYPE_INVALID == 0);
g_return_val_if_fail(name, G_TYPE_INVALID); g_return_val_if_fail(name, G_TYPE_INVALID);
setting_info = nm_meta_setting_infos_by_name(name); setting_info = nm_meta_setting_infos_by_name(name);