From 9b8dcc26653deab6d320631ada811bf5babc4c31 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 13 Jan 2020 14:45:03 +0100 Subject: [PATCH 1/3] shared: cleanup NM_NARG() and related macros - add new helper macros NM_MACRO_CALL(), _NM_MACRO_SELECT_ARG_64(), NM_NARG_MAX1(), NM_NARG_MAX2(). - cleanup implementation of NM_NARG() and _NM_UTILS_MACRO_REST() macros by reusing helper macros. --- shared/nm-glib-aux/nm-macros-internal.h | 88 ++++++++++++++----------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/shared/nm-glib-aux/nm-macros-internal.h b/shared/nm-glib-aux/nm-macros-internal.h index d1ea04c009..4c7d80306b 100644 --- a/shared/nm-glib-aux/nm-macros-internal.h +++ b/shared/nm-glib-aux/nm-macros-internal.h @@ -337,52 +337,60 @@ _nm_auto_freev (gpointer ptr) /*****************************************************************************/ -/* http://stackoverflow.com/a/11172679 */ -#define _NM_UTILS_MACRO_FIRST(...) __NM_UTILS_MACRO_FIRST_HELPER(__VA_ARGS__, throwaway) -#define __NM_UTILS_MACRO_FIRST_HELPER(first, ...) first - -#define _NM_UTILS_MACRO_REST(...) __NM_UTILS_MACRO_REST_HELPER(__NM_UTILS_MACRO_REST_NUM(__VA_ARGS__), __VA_ARGS__) -#define __NM_UTILS_MACRO_REST_HELPER(qty, ...) __NM_UTILS_MACRO_REST_HELPER2(qty, __VA_ARGS__) -#define __NM_UTILS_MACRO_REST_HELPER2(qty, ...) __NM_UTILS_MACRO_REST_HELPER_##qty(__VA_ARGS__) -#define __NM_UTILS_MACRO_REST_HELPER_ONE(first) -#define __NM_UTILS_MACRO_REST_HELPER_TWOORMORE(first, ...) , __VA_ARGS__ -#define __NM_UTILS_MACRO_REST_NUM(...) \ - __NM_UTILS_MACRO_REST_SELECT_30TH(__VA_ARGS__, \ - TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\ - TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\ - TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\ - TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\ - TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\ - TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway) -#define __NM_UTILS_MACRO_REST_SELECT_30TH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, ...) a30 - -/*****************************************************************************/ - -/* http://stackoverflow.com/a/2124385/354393 - * https://stackoverflow.com/questions/11317474/macro-to-count-number-of-arguments - */ - -#define NM_NARG(...) \ - _NM_NARG(, ##__VA_ARGS__, _NM_NARG_RSEQ_N()) -#define _NM_NARG(...) \ - _NM_NARG_ARG_N(__VA_ARGS__) -#define _NM_NARG_ARG_N( \ - _0, \ +#define _NM_MACRO_SELECT_ARG_64( \ _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \ _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \ _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \ _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \ _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \ _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \ - _61,_62,_63,N,...) N -#define _NM_NARG_RSEQ_N() \ - 63,62,61,60, \ - 59,58,57,56,55,54,53,52,51,50, \ - 49,48,47,46,45,44,43,42,41,40, \ - 39,38,37,36,35,34,33,32,31,30, \ - 29,28,27,26,25,24,23,22,21,20, \ - 19,18,17,16,15,14,13,12,11,10, \ - 9,8,7,6,5,4,3,2,1,0 + _61,_62,_63, N,...) N + +/* http://stackoverflow.com/a/2124385/354393 + * https://stackoverflow.com/questions/11317474/macro-to-count-number-of-arguments + */ + +#define NM_NARG(...) \ + _NM_MACRO_SELECT_ARG_64(, ##__VA_ARGS__, \ + 62,61,60, \ + 59,58,57,56,55,54,53,52,51,50, \ + 49,48,47,46,45,44,43,42,41,40, \ + 39,38,37,36,35,34,33,32,31,30, \ + 29,28,27,26,25,24,23,22,21,20, \ + 19,18,17,16,15,14,13,12,11,10, \ + 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) +#define NM_NARG_MAX1(...) \ + _NM_MACRO_SELECT_ARG_64(, ##__VA_ARGS__, \ + 1, 1, 1, \ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) +#define NM_NARG_MAX2(...) \ + _NM_MACRO_SELECT_ARG_64(, ##__VA_ARGS__, \ + 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 1, 0) + +#define _NM_MACRO_CALL(macro, ...) macro (__VA_ARGS__) + +/*****************************************************************************/ + + +/* http://stackoverflow.com/a/11172679 */ +#define _NM_UTILS_MACRO_FIRST(...) __NM_UTILS_MACRO_FIRST_HELPER(__VA_ARGS__, throwaway) +#define __NM_UTILS_MACRO_FIRST_HELPER(first, ...) first + +#define _NM_UTILS_MACRO_REST(...) _NM_MACRO_CALL (G_PASTE (__NM_UTILS_MACRO_REST_, NM_NARG_MAX2(__VA_ARGS__)), __VA_ARGS__) +#define __NM_UTILS_MACRO_REST_0() +#define __NM_UTILS_MACRO_REST_1(first) +#define __NM_UTILS_MACRO_REST_2(first, ...) , __VA_ARGS__ /*****************************************************************************/ From 0cb0ee0482072af348c98977e17101535242b1ed Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 13 Jan 2020 15:28:14 +0100 Subject: [PATCH 2/3] shared: add _NM_MACRO_COMMA_IF_ARGS() helper macro --- shared/nm-glib-aux/nm-macros-internal.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shared/nm-glib-aux/nm-macros-internal.h b/shared/nm-glib-aux/nm-macros-internal.h index 4c7d80306b..65f2390edf 100644 --- a/shared/nm-glib-aux/nm-macros-internal.h +++ b/shared/nm-glib-aux/nm-macros-internal.h @@ -382,6 +382,11 @@ _nm_auto_freev (gpointer ptr) /*****************************************************************************/ +#define _NM_MACRO_COMMA_IF_ARGS(...) _NM_MACRO_CALL (G_PASTE (__NM_MACRO_COMMA_IF_ARGS_, NM_NARG_MAX1(__VA_ARGS__)), __VA_ARGS__) +#define __NM_MACRO_COMMA_IF_ARGS_0() +#define __NM_MACRO_COMMA_IF_ARGS_1(...) , + +/*****************************************************************************/ /* http://stackoverflow.com/a/11172679 */ #define _NM_UTILS_MACRO_FIRST(...) __NM_UTILS_MACRO_FIRST_HELPER(__VA_ARGS__, throwaway) From 86787e0278284c014588727ffd709668ee7df575 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 13 Jan 2020 15:32:14 +0100 Subject: [PATCH 3/3] core: improve implementation of NM_DEVICE_DEFINE_LINK_TYPES() macro I think it's technically not correct to rely on the "sentinal" field being immediately after the previous field, due to alignment. Implement the macro differently. --- src/devices/nm-device-private.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/devices/nm-device-private.h b/src/devices/nm-device-private.h index e87733ef3f..2e33d8a4a7 100644 --- a/src/devices/nm-device-private.h +++ b/src/devices/nm-device-private.h @@ -180,15 +180,14 @@ void nm_device_commit_mtu (NMDevice *self); ((NM_NARG (__VA_ARGS__) == 0) \ ? NULL \ : ({ \ - static const struct { \ - const NMLinkType types[NM_NARG (__VA_ARGS__)]; \ - const NMLinkType sentinel; \ - } _link_types = { \ - .types = { __VA_ARGS__ }, \ - .sentinel = NM_LINK_TYPE_NONE, \ + const NMLinkType _types[NM_NARG (__VA_ARGS__) + 1] = { \ + __VA_ARGS__ \ + _NM_MACRO_COMMA_IF_ARGS (__VA_ARGS__) \ + NM_LINK_TYPE_NONE, \ }; \ \ - _link_types.types; \ + nm_assert (_types[NM_NARG (__VA_ARGS__)] == NM_LINK_TYPE_NONE); \ + _types; \ })\ )