]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use constexpr for NS_PER_SEC and friends constants
authorOndřej Surý <ondrej@isc.org>
Wed, 14 Aug 2024 14:10:18 +0000 (16:10 +0200)
committerOndřej Surý <ondrej@isc.org>
Mon, 19 Aug 2024 09:08:55 +0000 (09:08 +0000)
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.

lib/isc/include/isc/attributes.h
lib/isc/include/isc/time.h

index eb1bb53e2ede4de75e57b87e0052dc4801a65714..4ed540f5a8c262640899023580fdaa48a9b032b4 100644 (file)
 #else
 #define ISC_ATTR_UNUSED __attribute__((__unused__))
 #endif
+
+#if __STDC_VERSION__ >= 202311L
+#define ISC_CONSTEXPR constexpr
+#else
+#define ISC_CONSTEXPR static const
+#endif
index 3e944352b3b52ef7a8425ae0b32c32f000c23698..ad74be92705ae8ba30065f7c7ca858e7fbd13133 100644 (file)
 #include <inttypes.h>
 #include <time.h>
 
+#include <isc/attributes.h>
 #include <isc/lang.h>
 #include <isc/types.h>
 
 /*
  * 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