util: add macros for maximum, minimum and clamp

If MAX/MIN are defined in system headers, undefine them first.
This commit is contained in:
Miroslav Lichvar
2015-11-27 11:03:16 +01:00
parent 8b235297a5
commit 801830df57
2 changed files with 14 additions and 2 deletions

13
util.h
View File

@@ -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 */