mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-04 04:50:30 +01:00
platform: refactor NMPObject cast macros using _Generic()
This way, we also accept void pointers, while preserving constness.
This commit is contained in:
parent
06598700fe
commit
b2112ff471
6 changed files with 65 additions and 43 deletions
|
|
@ -242,6 +242,35 @@ NM_G_ERROR_MSG (GError *error)
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
#if (defined (__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 9 ))) || (defined (__clang__))
|
||||
#define _NM_CC_SUPPORT_GENERIC 1
|
||||
#else
|
||||
#define _NM_CC_SUPPORT_GENERIC 0
|
||||
#endif
|
||||
|
||||
#if _NM_CC_SUPPORT_GENERIC
|
||||
#define _NM_CONSTCAST(type, obj) \
|
||||
(_Generic ((obj), \
|
||||
void * : ((type *) (obj)), \
|
||||
void *const : ((type *) (obj)), \
|
||||
const void * : ((const type *) (obj)), \
|
||||
const void *const: ((const type *) (obj)), \
|
||||
const type * : (obj), \
|
||||
const type *const: (obj), \
|
||||
type * : (obj), \
|
||||
type *const : (obj)))
|
||||
#else
|
||||
/* _NM_CONSTCAST() is there to preserve constness of a pointer.
|
||||
* It uses C11's _Generic(). If that is not supported, we fall back
|
||||
* to casting away constness. So, with _Generic, we get some additional
|
||||
* static type checking by preserving constness, without, we cast it
|
||||
* to a non-const pointer. */
|
||||
#define _NM_CONSTCAST(type, obj) \
|
||||
((type *) (obj))
|
||||
#endif
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
#define _NM_IN_SET_EVAL_1( op, _x, y) (_x == (y))
|
||||
#define _NM_IN_SET_EVAL_2( op, _x, y, ...) (_x == (y)) op _NM_IN_SET_EVAL_1 (op, _x, __VA_ARGS__)
|
||||
#define _NM_IN_SET_EVAL_3( op, _x, y, ...) (_x == (y)) op _NM_IN_SET_EVAL_2 (op, _x, __VA_ARGS__)
|
||||
|
|
@ -537,7 +566,7 @@ _notify (obj_type *obj, _PropertyEnums prop) \
|
|||
/* these are implemented as a macro, because they accept self
|
||||
* as both (type*) and (const type*), and return a const
|
||||
* private pointer accordingly. */
|
||||
#define __NM_GET_PRIVATE(self, type, is_check, result_cmd) \
|
||||
#define __NM_GET_PRIVATE(self, type, is_check, addrop) \
|
||||
({ \
|
||||
/* preserve the const-ness of self. Unfortunately, that
|
||||
* way, @self cannot be a void pointer */ \
|
||||
|
|
@ -547,11 +576,11 @@ _notify (obj_type *obj, _PropertyEnums prop) \
|
|||
_nm_unused const type *const _self2 = (_self); \
|
||||
\
|
||||
nm_assert (is_check (_self)); \
|
||||
( result_cmd ); \
|
||||
( addrop ( _NM_CONSTCAST (type, _self)->_priv) ); \
|
||||
})
|
||||
|
||||
#define _NM_GET_PRIVATE(self, type, is_check) __NM_GET_PRIVATE(self, type, is_check, &_self->_priv)
|
||||
#define _NM_GET_PRIVATE_PTR(self, type, is_check) __NM_GET_PRIVATE(self, type, is_check, _self->_priv)
|
||||
#define _NM_GET_PRIVATE(self, type, is_check) __NM_GET_PRIVATE(self, type, is_check, &)
|
||||
#define _NM_GET_PRIVATE_PTR(self, type, is_check) __NM_GET_PRIVATE(self, type, is_check, )
|
||||
|
||||
#define __NM_GET_PRIVATE_VOID(self, type, is_check, result_cmd) \
|
||||
({ \
|
||||
|
|
|
|||
|
|
@ -209,7 +209,7 @@ _vt_routes_has_entry (const VTableIP *vtable, const GPtrArray *routes, const Ent
|
|||
|
||||
if (vtable->vt->is_ip4) {
|
||||
for (i = 0; i < routes->len; i++) {
|
||||
const NMPlatformIP4Route *r = NMP_OBJECT_CAST_IP4_ROUTE ((NMPObject *) routes->pdata[i]);
|
||||
const NMPlatformIP4Route *r = NMP_OBJECT_CAST_IP4_ROUTE (routes->pdata[i]);
|
||||
|
||||
route.rx.rt_source = r->rt_source;
|
||||
if (nm_platform_ip4_route_cmp (r, &route.r4) == 0)
|
||||
|
|
@ -217,7 +217,7 @@ _vt_routes_has_entry (const VTableIP *vtable, const GPtrArray *routes, const Ent
|
|||
}
|
||||
} else {
|
||||
for (i = 0; i < routes->len; i++) {
|
||||
const NMPlatformIP6Route *r = NMP_OBJECT_CAST_IP6_ROUTE ((NMPObject *) routes->pdata[i]);
|
||||
const NMPlatformIP6Route *r = NMP_OBJECT_CAST_IP6_ROUTE (routes->pdata[i]);
|
||||
|
||||
route.rx.rt_source = r->rt_source;
|
||||
if (nm_platform_ip6_route_cmp (r, &route.r6) == 0)
|
||||
|
|
@ -346,7 +346,7 @@ _platform_route_sync_flush (const VTableIP *vtable, NMDefaultRouteManager *self,
|
|||
gboolean has_ifindex_synced = FALSE;
|
||||
Entry *entry = NULL;
|
||||
|
||||
route = NMP_OBJECT_CAST_IP_ROUTE ((NMPObject *) routes->pdata[i]);
|
||||
route = NMP_OBJECT_CAST_IP_ROUTE (routes->pdata[i]);
|
||||
|
||||
/* look at all entries and see if the route for this ifindex pair is
|
||||
* a known entry. */
|
||||
|
|
@ -442,7 +442,7 @@ _get_assumed_interface_metrics (const VTableIP *vtable, NMDefaultRouteManager *s
|
|||
gboolean ifindex_has_synced_entry = FALSE;
|
||||
const NMPlatformIPRoute *route;
|
||||
|
||||
route = NMP_OBJECT_CAST_IP_ROUTE ((NMPObject *) routes->pdata[i]);
|
||||
route = NMP_OBJECT_CAST_IP_ROUTE (routes->pdata[i]);
|
||||
|
||||
for (j = 0; j < entries->len; j++) {
|
||||
Entry *e = g_ptr_array_index (entries, j);
|
||||
|
|
@ -571,7 +571,7 @@ _resync_all (const VTableIP *vtable, NMDefaultRouteManager *self, const Entry *c
|
|||
/* However, if there is a matching route (ifindex+metric) for our current entry, we are done. */
|
||||
if (routes) {
|
||||
for (j = 0; j < routes->len; j++) {
|
||||
const NMPlatformIPRoute *r = NMP_OBJECT_CAST_IP_ROUTE ((NMPObject *) routes->pdata[i]);
|
||||
const NMPlatformIPRoute *r = NMP_OBJECT_CAST_IP_ROUTE (routes->pdata[i]);
|
||||
|
||||
if ( r->metric == expected_metric
|
||||
&& r->ifindex == entry->route.rx.ifindex) {
|
||||
|
|
|
|||
|
|
@ -2493,7 +2493,7 @@ platform_query_devices (NMManager *self)
|
|||
if (!links)
|
||||
return;
|
||||
for (i = 0; i < links->len; i++) {
|
||||
const NMPlatformLink *link = NMP_OBJECT_CAST_LINK ((const NMPObject *) links->pdata[i]);
|
||||
const NMPlatformLink *link = NMP_OBJECT_CAST_LINK (links->pdata[i]);
|
||||
gs_free NMConfigDeviceStateData *dev_state = NULL;
|
||||
|
||||
dev_state = nm_config_device_state_load (link->ifindex);
|
||||
|
|
|
|||
|
|
@ -333,7 +333,7 @@ _route_index_create_from_platform (const VTableIP *vtable,
|
|||
|
||||
j = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
const NMPlatformIPXRoute *ipx_route = NMP_OBJECT_CAST_IPX_ROUTE ((NMPObject *) storage->pdata[i]);
|
||||
const NMPlatformIPXRoute *ipx_route = NMP_OBJECT_CAST_IPX_ROUTE (storage->pdata[i]);
|
||||
|
||||
if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (ipx_route))
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -527,7 +527,7 @@ nm_platform_link_get_all (NMPlatform *self, gboolean sort_by_name)
|
|||
|
||||
unseen = g_hash_table_new (g_direct_hash, g_direct_equal);
|
||||
for (i = 0; i < links->len; i++) {
|
||||
item = NMP_OBJECT_CAST_LINK ((const NMPObject *) links->pdata[i]);
|
||||
item = NMP_OBJECT_CAST_LINK (links->pdata[i]);
|
||||
nm_assert (item->ifindex > 0);
|
||||
if (!nm_g_hash_table_insert (unseen, GINT_TO_POINTER (item->ifindex), NULL))
|
||||
nm_assert_not_reached ();
|
||||
|
|
@ -536,7 +536,7 @@ nm_platform_link_get_all (NMPlatform *self, gboolean sort_by_name)
|
|||
#if NM_MORE_ASSERTS
|
||||
/* Ensure that link_get_all returns a consistent and valid result. */
|
||||
for (i = 0; i < links->len; i++) {
|
||||
item = NMP_OBJECT_CAST_LINK ((const NMPObject *) links->pdata[i]);
|
||||
item = NMP_OBJECT_CAST_LINK (links->pdata[i]);
|
||||
|
||||
if (!item->ifindex)
|
||||
continue;
|
||||
|
|
@ -565,7 +565,7 @@ nm_platform_link_get_all (NMPlatform *self, gboolean sort_by_name)
|
|||
guint first_idx = G_MAXUINT;
|
||||
|
||||
for (i = 0; i < links->len; i++) {
|
||||
item = NMP_OBJECT_CAST_LINK ((const NMPObject *) links->pdata[i]);
|
||||
item = NMP_OBJECT_CAST_LINK (links->pdata[i]);
|
||||
|
||||
if (!item)
|
||||
continue;
|
||||
|
|
@ -594,7 +594,7 @@ skip:
|
|||
nm_assert (first_idx != G_MAXUINT);
|
||||
/* There is a loop, pop the first (remaining) element from the list.
|
||||
* This can happen for veth pairs where each peer is parent of the other end. */
|
||||
item = NMP_OBJECT_CAST_LINK ((const NMPObject *) links->pdata[first_idx]);
|
||||
item = NMP_OBJECT_CAST_LINK (links->pdata[first_idx]);
|
||||
g_hash_table_remove (unseen, GINT_TO_POINTER (item->ifindex));
|
||||
g_ptr_array_add (result, links->pdata[first_idx]);
|
||||
links->pdata[first_idx] = NULL;
|
||||
|
|
|
|||
|
|
@ -339,65 +339,58 @@ NMP_OBJECT_GET_TYPE (const NMPObject *obj)
|
|||
|
||||
#define NMP_OBJECT_CAST_LINK(obj) \
|
||||
({ \
|
||||
typeof (*(obj)) *_obj = (obj); \
|
||||
_nm_unused const NMPObject *_obj_type_check = _obj; \
|
||||
typeof (obj) _obj = (obj); \
|
||||
\
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE (_obj) == NMP_OBJECT_TYPE_LINK); \
|
||||
_obj ? &_obj->link : NULL; \
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE ((const NMPObject *) _obj) == NMP_OBJECT_TYPE_LINK); \
|
||||
_obj ? &_NM_CONSTCAST (NMPObject, _obj)->link : NULL; \
|
||||
})
|
||||
|
||||
#define NMP_OBJECT_CAST_IP4_ADDRESS(obj) \
|
||||
({ \
|
||||
typeof (*(obj)) *_obj = (obj); \
|
||||
_nm_unused const NMPObject *_obj_type_check = _obj; \
|
||||
typeof (obj) _obj = (obj); \
|
||||
\
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE (_obj) == NMP_OBJECT_TYPE_IP4_ADDRESS); \
|
||||
_obj ? &_obj->ip4_address : NULL; \
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE ((const NMPObject *) _obj) == NMP_OBJECT_TYPE_IP4_ADDRESS); \
|
||||
_obj ? &_NM_CONSTCAST (NMPObject, _obj)->ip4_address : NULL; \
|
||||
})
|
||||
|
||||
#define NMP_OBJECT_CAST_IP6_ADDRESS(obj) \
|
||||
({ \
|
||||
typeof (*(obj)) *_obj = (obj); \
|
||||
_nm_unused const NMPObject *_obj_type_check = _obj; \
|
||||
typeof (obj) _obj = (obj); \
|
||||
\
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE (_obj) == NMP_OBJECT_TYPE_IP6_ADDRESS); \
|
||||
_obj ? &_obj->ip6_address : NULL; \
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE ((const NMPObject *) _obj) == NMP_OBJECT_TYPE_IP6_ADDRESS); \
|
||||
_obj ? &_NM_CONSTCAST (NMPObject, _obj)->ip6_address : NULL; \
|
||||
})
|
||||
|
||||
#define NMP_OBJECT_CAST_IPX_ROUTE(obj) \
|
||||
({ \
|
||||
typeof (*(obj)) *_obj = (obj); \
|
||||
_nm_unused const NMPObject *_obj_type_check = _obj; \
|
||||
typeof (obj) _obj = (obj); \
|
||||
\
|
||||
nm_assert (NM_IN_SET (NMP_OBJECT_GET_TYPE (_obj), NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE)); \
|
||||
&_obj->ipx_route; \
|
||||
nm_assert (!_obj || NM_IN_SET (NMP_OBJECT_GET_TYPE (_obj), NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE)); \
|
||||
_obj ? &_NM_CONSTCAST (NMPObject, _obj)->ipx_route : NULL; \
|
||||
})
|
||||
|
||||
#define NMP_OBJECT_CAST_IP_ROUTE(obj) \
|
||||
({ \
|
||||
typeof (*(obj)) *_obj = (obj); \
|
||||
_nm_unused const NMPObject *_obj_type_check = _obj; \
|
||||
typeof (obj) _obj = (obj); \
|
||||
\
|
||||
nm_assert (NM_IN_SET (NMP_OBJECT_GET_TYPE (_obj), NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE)); \
|
||||
&_obj->ip_route; \
|
||||
nm_assert (!_obj || NM_IN_SET (NMP_OBJECT_GET_TYPE (_obj), NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE)); \
|
||||
_obj ? &_NM_CONSTCAST (NMPObject, _obj)->ip_route : NULL; \
|
||||
})
|
||||
|
||||
#define NMP_OBJECT_CAST_IP4_ROUTE(obj) \
|
||||
({ \
|
||||
typeof (*(obj)) *_obj = (obj); \
|
||||
_nm_unused const NMPObject *_obj_type_check = _obj; \
|
||||
typeof (obj) _obj = (obj); \
|
||||
\
|
||||
nm_assert (NMP_OBJECT_GET_TYPE (_obj) == NMP_OBJECT_TYPE_IP4_ROUTE); \
|
||||
&_obj->ip4_route; \
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE ((const NMPObject *) _obj) == NMP_OBJECT_TYPE_IP4_ROUTE); \
|
||||
_obj ? &_NM_CONSTCAST (NMPObject, _obj)->ip4_route : NULL; \
|
||||
})
|
||||
|
||||
#define NMP_OBJECT_CAST_IP6_ROUTE(obj) \
|
||||
({ \
|
||||
typeof (*(obj)) *_obj = (obj); \
|
||||
_nm_unused const NMPObject *_obj_type_check = _obj; \
|
||||
typeof (obj) _obj = (obj); \
|
||||
\
|
||||
nm_assert (NMP_OBJECT_GET_TYPE (_obj) == NMP_OBJECT_TYPE_IP6_ROUTE); \
|
||||
&_obj->ip6_route; \
|
||||
nm_assert (!_obj || NMP_OBJECT_GET_TYPE ((const NMPObject *) _obj) == NMP_OBJECT_TYPE_IP6_ROUTE); \
|
||||
_obj ? &_NM_CONSTCAST (NMPObject, _obj)->ip6_route : NULL; \
|
||||
})
|
||||
|
||||
const NMPClass *nmp_class_from_type (NMPObjectType obj_type);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue