shared: add NM_CONST_MAX() macro

There is:

 1) glib's MAX() macro, which evaluates arguments multiple times,
    but yields a constant expression, if the arguments are constant.

 2) NM's NM_MAX() macro, which evaluates arguments exactly once,
    but never yields a constant expression.

 3) systemd's MAX() which is like NM_MAX().

Now, it's sensible to use

    char buf[MAX (A_CONSTANT, ANOTHER_CONSTANT)];

and this works with glib's variant (1).

However, when we include systemd headers, 1) gets redefined to 3), and
above no longer works. That is because we we don't allow VLA and systemd's
macro gives not a constant expression.

Add NM_CONST_MAX() macro which is like systemd's CONST_MAX(). It can
only operate on constant arguments.
This commit is contained in:
Thomas Haller 2019-02-04 09:51:34 +01:00 committed by Beniamino Galvani
parent e6cf4213a7
commit b52d3e2ad3

View file

@ -1371,6 +1371,14 @@ nm_strcmp_p (gconstpointer a, gconstpointer b)
: _b); \
})
/* evaluates to (void) if _A or _B are not constant or of different types */
#define NM_CONST_MAX(_A, _B) \
(__builtin_choose_expr (( __builtin_constant_p (_A) \
&& __builtin_constant_p (_B) \
&& __builtin_types_compatible_p (typeof (_A), typeof (_B))), \
((_A) > (_B)) ? (_A) : (_B), \
((void) 0)))
/*****************************************************************************/
static inline guint