Skip to content

Commit 0138e84

Browse files
committed
fix: don't declare bswap32/bswap64 function if macros are defined
Declaring these functions causes compilation failure on macOS because `sys/endian.h` header already defined macros with the same name Signed-off-by: botantony <antonsm21@gmail.com>
1 parent f3adfb6 commit 0138e84

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/maxminddb.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,31 +1804,37 @@ static int get_entry_data_list(const MMDB_s *const mmdb,
18041804
#define __has_builtin(x) 0
18051805
#endif
18061806

1807+
// `sys/endian.h` on macOS defined `bswap32` and `bswap64` macros
1808+
// Declaring these functions causes compilation failure
1809+
#ifndef bswap32
18071810
static inline uint32_t bswap32(uint32_t x) {
1808-
#if defined(_MSC_VER)
1811+
#if defined(_MSC_VER)
18091812
return _byteswap_ulong(x);
1810-
#elif __has_builtin(__builtin_bswap32)
1813+
#elif __has_builtin(__builtin_bswap32)
18111814
return __builtin_bswap32(x);
1812-
#else
1815+
#else
18131816
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0x00FF00FF);
18141817
return (x << 16) | (x >> 16);
1815-
#endif
1818+
#endif
18161819
}
1820+
#endif
18171821

1822+
#ifndef bswap64
18181823
static inline uint64_t bswap64(uint64_t x) {
1819-
#if defined(_MSC_VER)
1824+
#if defined(_MSC_VER)
18201825
return _byteswap_uint64(x);
1821-
#elif __has_builtin(__builtin_bswap64)
1826+
#elif __has_builtin(__builtin_bswap64)
18221827
return __builtin_bswap64(x);
1823-
#else
1828+
#else
18241829
x = ((x & 0x00000000FFFFFFFFULL) << 32) |
18251830
((x & 0xFFFFFFFF00000000ULL) >> 32);
18261831
x = ((x & 0x0000FFFF0000FFFFULL) << 16) |
18271832
((x & 0xFFFF0000FFFF0000ULL) >> 16);
18281833
return ((x & 0x00FF00FF00FF00FFULL) << 8) |
18291834
((x & 0xFF00FF00FF00FF00ULL) >> 8);
1830-
#endif
1835+
#endif
18311836
}
1837+
#endif
18321838

18331839
static float get_ieee754_float(const uint8_t *restrict p) {
18341840
float f;

0 commit comments

Comments
 (0)