summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-07-05 11:12:59 +0200
committerThomas Haller <thaller@redhat.com>2017-07-05 22:17:42 +0200
commitb2112ff471f0fd171646e5d0cd159626cb3a9fe6 (patch)
tree2deea6c5f0b2b96439bb79757f8a445b7eba0ec4
parent06598700feda25dfb05e14254c57c6f746b92b7b (diff)
downloadNetworkManager-b2112ff471f0fd171646e5d0cd159626cb3a9fe6.tar.gz
platform: refactor NMPObject cast macros using _Generic()th/dedup-multi-bgo784220
This way, we also accept void pointers, while preserving constness.
-rw-r--r--shared/nm-utils/nm-macros-internal.h37
-rw-r--r--src/nm-default-route-manager.c10
-rw-r--r--src/nm-manager.c2
-rw-r--r--src/nm-route-manager.c2
-rw-r--r--src/platform/nm-platform.c8
-rw-r--r--src/platform/nmp-object.h49
6 files changed, 65 insertions, 43 deletions
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 12794c3473..66b4aa8d60 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -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) \
({ \
diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c
index f0dc75cfd1..f6362bccf6 100644
--- a/src/nm-default-route-manager.c
+++ b/src/nm-default-route-manager.c
@@ -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) {
diff --git a/src/nm-manager.c b/src/nm-manager.c
index dd168fd0be..3a8ada4360 100644
--- a/src/nm-manager.c
+++ b/src/nm-manager.c
@@ -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);
diff --git a/src/nm-route-manager.c b/src/nm-route-manager.c
index 8372d65234..e980cfe0a6 100644
--- a/src/nm-route-manager.c
+++ b/src/nm-route-manager.c
@@ -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;
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index 73e508ff00..176f377bdb 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -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;
diff --git a/src/platform/nmp-object.h b/src/platform/nmp-object.h
index 9ea5a78ae5..9cdad2e808 100644
--- a/src/platform/nmp-object.h
+++ b/src/platform/nmp-object.h
@@ -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);