From dc4c0397ebbb9d4dc993fdcc3ae5bc976e955613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 14 Aug 2024 16:10:18 +0200 Subject: [PATCH] Use constexpr for NS_PER_SEC and friends constants The contexpr introduced in C23 standard makes perfect sense to be used instead of preprocessor macros - the symbols are kept, etc. Define ISC_CONSTEXPR to be `constexpr` for C23 and `static const` for the older C standards. Use the newly introduced macro for the NS_PER_SEC and friends time constants. (cherry picked from commit 122a142241ae197816490499e208526eca37361e) --- lib/isc/include/isc/attributes.h | 6 ++++++ lib/isc/include/isc/time.h | 13 +++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/isc/include/isc/attributes.h b/lib/isc/include/isc/attributes.h index edb50ed826..cdc2700016 100644 --- a/lib/isc/include/isc/attributes.h +++ b/lib/isc/include/isc/attributes.h @@ -104,3 +104,9 @@ #else #define ISC_ATTR_UNUSED __attribute__((__unused__)) #endif + +#if __STDC_VERSION__ >= 202311L +#define ISC_CONSTEXPR constexpr +#else +#define ISC_CONSTEXPR static const +#endif diff --git a/lib/isc/include/isc/time.h b/lib/isc/include/isc/time.h index 3e944352b3..ad74be9270 100644 --- a/lib/isc/include/isc/time.h +++ b/lib/isc/include/isc/time.h @@ -18,18 +18,19 @@ #include #include +#include #include #include /* * Define various time conversion constants. */ -static const unsigned int MS_PER_SEC = 1000; -static const unsigned int US_PER_MS = 1000; -static const unsigned int NS_PER_US = 1000; -static const unsigned int US_PER_SEC = 1000 * 1000; -static const unsigned int NS_PER_MS = 1000 * 1000; -static const unsigned int NS_PER_SEC = 1000 * 1000 * 1000; +ISC_CONSTEXPR unsigned int MS_PER_SEC = 1000; +ISC_CONSTEXPR unsigned int US_PER_MS = 1000; +ISC_CONSTEXPR unsigned int NS_PER_US = 1000; +ISC_CONSTEXPR unsigned int US_PER_SEC = 1000 * 1000; +ISC_CONSTEXPR unsigned int NS_PER_MS = 1000 * 1000; +ISC_CONSTEXPR unsigned int NS_PER_SEC = 1000 * 1000 * 1000; /* * ISC_FORMATHTTPTIMESTAMP_SIZE needs to be 30 in C locale and potentially