Skip to content

Commit 3e08f7b

Browse files
authored
Merge pull request #9267 from rouault/CPLVerifyConfiguration_static
CPLVerifyConfiguration(): make it rely only on static_assert()
2 parents f9a10b0 + 9ad3d29 commit 3e08f7b

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

port/cpl_conv.cpp

+24-19
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383
#endif
8484
#include <string>
8585

86+
#if __cplusplus >= 202002L
87+
#include <type_traits> // For std::endian
88+
#endif
89+
8690
#include "cpl_config.h"
8791
#include "cpl_multiproc.h"
8892
#include "cpl_string.h"
@@ -186,8 +190,6 @@ void *CPLMalloc(size_t nSize)
186190
if (nSize == 0)
187191
return nullptr;
188192

189-
CPLVerifyConfiguration();
190-
191193
if ((nSize >> (8 * sizeof(nSize) - 1)) != 0)
192194
{
193195
// coverity[dead_error_begin]
@@ -1564,33 +1566,36 @@ int CPLPrintTime(char *pszBuffer, int nMaxLen, const char *pszFormat,
15641566
void CPLVerifyConfiguration()
15651567

15661568
{
1567-
static bool verified = false;
1568-
if (verified)
1569-
{
1570-
return;
1571-
}
1572-
verified = true;
1573-
15741569
/* -------------------------------------------------------------------- */
15751570
/* Verify data types. */
15761571
/* -------------------------------------------------------------------- */
1577-
CPL_STATIC_ASSERT(sizeof(GInt32) == 4);
1578-
CPL_STATIC_ASSERT(sizeof(GInt16) == 2);
1579-
CPL_STATIC_ASSERT(sizeof(GByte) == 1);
1572+
static_assert(sizeof(short) == 2); // We unfortunately rely on this
1573+
static_assert(sizeof(int) == 4); // We unfortunately rely on this
1574+
static_assert(sizeof(float) == 4); // We unfortunately rely on this
1575+
static_assert(sizeof(double) == 8); // We unfortunately rely on this
1576+
static_assert(sizeof(GInt64) == 8);
1577+
static_assert(sizeof(GInt32) == 4);
1578+
static_assert(sizeof(GInt16) == 2);
1579+
static_assert(sizeof(GByte) == 1);
15801580

15811581
/* -------------------------------------------------------------------- */
15821582
/* Verify byte order */
15831583
/* -------------------------------------------------------------------- */
1584-
GInt32 nTest = 1;
1585-
15861584
#ifdef CPL_LSB
1587-
if (reinterpret_cast<GByte *>(&nTest)[0] != 1)
1585+
#if __cplusplus >= 202002L
1586+
static_assert(std::endian::native == std::endian::little);
1587+
#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
1588+
static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__);
15881589
#endif
1589-
#ifdef CPL_MSB
1590-
if (reinterpret_cast<GByte *>(&nTest)[3] != 1)
1590+
#elif defined(CPL_MSB)
1591+
#if __cplusplus >= 202002L
1592+
static_assert(std::endian::native == std::endian::big);
1593+
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
1594+
static_assert(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
1595+
#endif
1596+
#else
1597+
#error "CPL_LSB or CPL_MSB must be defined"
15911598
#endif
1592-
CPLError(CE_Fatal, CPLE_AppDefined,
1593-
"CPLVerifyConfiguration(): byte order set wrong.");
15941599
}
15951600

15961601
#ifdef DEBUG_CONFIG_OPTIONS

0 commit comments

Comments
 (0)