From 801830df57ac866a69c0393dc58ae418788a2df9 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Fri, 27 Nov 2015 11:03:16 +0100 Subject: [PATCH] util: add macros for maximum, minimum and clamp If MAX/MIN are defined in system headers, undefine them first. --- ntp_core.c | 3 +-- util.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ntp_core.c b/ntp_core.c index 8987468..26ca39b 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -1404,8 +1404,7 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins &sample_time, offset, delay, dispersion, root_delay, root_dispersion, - message->stratum > inst->min_stratum ? - message->stratum : inst->min_stratum, + MAX(message->stratum, inst->min_stratum), (NTP_Leap) pkt_leap); SRC_SelectSource(inst->source); diff --git a/util.h b/util.h index 78b7d71..ebd0d20 100644 --- a/util.h +++ b/util.h @@ -148,4 +148,17 @@ extern int UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid /* Fill buffer with random bytes */ extern void UTI_GetRandomBytes(void *buf, unsigned int len); +/* Macros to get maximum and minimum of two values */ +#ifdef MAX +#undef MAX +#endif +#define MAX(x, y) ((x) > (y) ? (x) : (y)) +#ifdef MIN +#undef MIN +#endif +#define MIN(x, y) ((x) < (y) ? (x) : (y)) + +/* Macro to clamp a value between two values */ +#define CLAMP(min, x, max) (MAX((min), MIN((x), (max)))) + #endif /* GOT_UTIL_H */