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.
This commit is contained in:
Ondřej Surý
2024-08-14 16:10:18 +02:00
parent b03e90e0d4
commit 122a142241
2 changed files with 13 additions and 6 deletions

View File

@@ -102,3 +102,9 @@
#else
#define ISC_ATTR_UNUSED __attribute__((__unused__))
#endif
#if __STDC_VERSION__ >= 202311L
#define ISC_CONSTEXPR constexpr
#else
#define ISC_CONSTEXPR static const
#endif

View File

@@ -18,18 +18,19 @@
#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