shared: add min/max macros to "nm-std-aux.h"

This commit is contained in:
Thomas Haller 2020-07-04 19:04:11 +02:00
parent 5361317f68
commit b109d63376
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 65 additions and 62 deletions

View file

@ -1561,68 +1561,6 @@ nm_strcmp_p (gconstpointer a, gconstpointer b)
/*****************************************************************************/
/* Taken from systemd's UNIQ_T and UNIQ macros. */
#define NM_UNIQ_T(x, uniq) G_PASTE(__unique_prefix_, G_PASTE(x, uniq))
#define NM_UNIQ __COUNTER__
/*****************************************************************************/
/* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate
* the argument possibly twice.
*
* Taken from systemd's MIN()/MAX() macros. */
#define NM_MIN(a, b) __NM_MIN(NM_UNIQ, a, NM_UNIQ, b)
#define __NM_MIN(aq, a, bq, b) \
({ \
typeof (a) NM_UNIQ_T(A, aq) = (a); \
typeof (b) NM_UNIQ_T(B, bq) = (b); \
((NM_UNIQ_T(A, aq) < NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
})
#define NM_MAX(a, b) __NM_MAX(NM_UNIQ, a, NM_UNIQ, b)
#define __NM_MAX(aq, a, bq, b) \
({ \
typeof (a) NM_UNIQ_T(A, aq) = (a); \
typeof (b) NM_UNIQ_T(B, bq) = (b); \
((NM_UNIQ_T(A, aq) > NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
})
#define NM_CLAMP(x, low, high) __NM_CLAMP(NM_UNIQ, x, NM_UNIQ, low, NM_UNIQ, high)
#define __NM_CLAMP(xq, x, lowq, low, highq, high) \
({ \
typeof(x)NM_UNIQ_T(X,xq) = (x); \
typeof(low) NM_UNIQ_T(LOW,lowq) = (low); \
typeof(high) NM_UNIQ_T(HIGH,highq) = (high); \
\
( (NM_UNIQ_T(X,xq) > NM_UNIQ_T(HIGH,highq)) \
? NM_UNIQ_T(HIGH,highq) \
: (NM_UNIQ_T(X,xq) < NM_UNIQ_T(LOW,lowq)) \
? NM_UNIQ_T(LOW,lowq) \
: NM_UNIQ_T(X,xq)); \
})
#define NM_MAX_WITH_CMP(cmp, a, b) \
({ \
typeof (a) _a = (a); \
typeof (b) _b = (b); \
\
( ((cmp (_a, _b)) >= 0) \
? _a \
: _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)))
/*****************************************************************************/
#define nm_g_slice_free(ptr) \
g_slice_free (typeof (*(ptr)), ptr)

View file

@ -109,4 +109,69 @@
/*****************************************************************************/
#define NM_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
#define NM_PASTE(identifier1,identifier2) NM_PASTE_ARGS (identifier1, identifier2)
/* Taken from systemd's UNIQ_T and UNIQ macros. */
#define NM_UNIQ_T(x, uniq) NM_PASTE(__unique_prefix_, NM_PASTE(x, uniq))
#define NM_UNIQ __COUNTER__
/*****************************************************************************/
/* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate
* the argument possibly twice.
*
* Taken from systemd's MIN()/MAX() macros. */
#define NM_MIN(a, b) __NM_MIN(NM_UNIQ, a, NM_UNIQ, b)
#define __NM_MIN(aq, a, bq, b) \
({ \
typeof (a) NM_UNIQ_T(A, aq) = (a); \
typeof (b) NM_UNIQ_T(B, bq) = (b); \
((NM_UNIQ_T(A, aq) < NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
})
#define NM_MAX(a, b) __NM_MAX(NM_UNIQ, a, NM_UNIQ, b)
#define __NM_MAX(aq, a, bq, b) \
({ \
typeof (a) NM_UNIQ_T(A, aq) = (a); \
typeof (b) NM_UNIQ_T(B, bq) = (b); \
((NM_UNIQ_T(A, aq) > NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
})
#define NM_CLAMP(x, low, high) __NM_CLAMP(NM_UNIQ, x, NM_UNIQ, low, NM_UNIQ, high)
#define __NM_CLAMP(xq, x, lowq, low, highq, high) \
({ \
typeof(x)NM_UNIQ_T(X,xq) = (x); \
typeof(low) NM_UNIQ_T(LOW,lowq) = (low); \
typeof(high) NM_UNIQ_T(HIGH,highq) = (high); \
\
( (NM_UNIQ_T(X,xq) > NM_UNIQ_T(HIGH,highq)) \
? NM_UNIQ_T(HIGH,highq) \
: (NM_UNIQ_T(X,xq) < NM_UNIQ_T(LOW,lowq)) \
? NM_UNIQ_T(LOW,lowq) \
: NM_UNIQ_T(X,xq)); \
})
#define NM_MAX_WITH_CMP(cmp, a, b) \
({ \
typeof (a) _a = (a); \
typeof (b) _b = (b); \
\
( ((cmp (_a, _b)) >= 0) \
? _a \
: _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)))
/*****************************************************************************/
#endif /* __NM_STD_AUX_H__ */