From 18cf804d0b85c66ba27ed80a412f17097330d506 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Fri, 26 May 2017 17:11:48 +0300 Subject: [PATCH] mdbx: add STATIC_ASSERT_MSG, refine STATIC_ASSERT. --- src/bits.h | 3 +++ src/defs.h | 15 ++++++++++----- src/osal.c | 20 ++++++++++---------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/bits.h b/src/bits.h index 96967ff3..9aa76327 100644 --- a/src/bits.h +++ b/src/bits.h @@ -31,6 +31,9 @@ /*----------------------------------------------------------------------------*/ /* Should be defined before any includes */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE 1 +#endif #ifndef _FILE_OFFSET_BITS #define _FILE_OFFSET_BITS 64 #endif diff --git a/src/defs.h b/src/defs.h index 290a854d..79e65d44 100644 --- a/src/defs.h +++ b/src/defs.h @@ -368,15 +368,20 @@ #define FIXME "FIXME: " __FILE__ ", " STRINGIFY(__LINE__) -#ifndef STATIC_ASSERT -# if __STDC_VERSION__ >= 201112L -# define STATIC_ASSERT(expr, msg) _Static_assert(expr, msg) +#ifndef STATIC_ASSERT_MSG +# if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \ + || __has_feature(c_static_assert) +# define STATIC_ASSERT_MSG(expr, msg) _Static_assert(expr, msg) # elif defined(static_assert) -# define STATIC_ASSERT(expr, msg) static_assert(expr, msg) +# define STATIC_ASSERT_MSG(expr, msg) static_assert(expr, msg) # else -# define STATIC_ASSERT(expr, msg) switch (0) {case 0:case (expr):;} +# define STATIC_ASSERT_MSG(expr, msg) switch (0) {case 0:case (expr):;} # endif #endif /* STATIC_ASSERT */ +#ifndef STATIC_ASSERT +# define STATIC_ASSERT(expr) STATIC_ASSERT_MSG(expr, #expr) +#endif + /* *INDENT-ON* */ /* clang-format on */ diff --git a/src/osal.c b/src/osal.c index 7e98aa82..4b56be70 100644 --- a/src/osal.c +++ b/src/osal.c @@ -407,8 +407,8 @@ int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, uint64_t offset) { return (rc == MDBX_SUCCESS) ? /* paranoia */ ERROR_READ_FAULT : rc; } #else - STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), - "libmdbx requires 64-bit file I/O on 64-bit systems"); + STATIC_ASSERT_MSG(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); ssize_t read = pread(fd, buf, bytes, offset); if (read < 0) { int rc = errno; @@ -437,8 +437,8 @@ int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t bytes, int rc; ssize_t written; do { - STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), - "libmdbx requires 64-bit file I/O on 64-bit systems"); + STATIC_ASSERT_MSG(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); written = pwrite(fd, buf, bytes, offset); if (likely(bytes == (size_t)written)) return MDBX_SUCCESS; @@ -465,8 +465,8 @@ int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt, int rc; ssize_t written; do { - STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), - "libmdbx requires 64-bit file I/O on 64-bit systems"); + STATIC_ASSERT_MSG(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); written = pwritev(fd, iov, iovcnt, offset); if (likely(expected_written == (size_t)written)) return MDBX_SUCCESS; @@ -556,8 +556,8 @@ int mdbx_filesize(mdbx_filehandle_t fd, uint64_t *length) { #else struct stat st; - STATIC_ASSERT(sizeof(off_t) <= sizeof(uint64_t), - "libmdbx requires 64-bit file I/O on 64-bit systems"); + STATIC_ASSERT_MSG(sizeof(off_t) <= sizeof(uint64_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); if (fstat(fd, &st)) return errno; @@ -574,8 +574,8 @@ int mdbx_ftruncate(mdbx_filehandle_t fd, uint64_t length) { ? MDBX_SUCCESS : mdbx_get_errno_checked(); #else - STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), - "libmdbx requires 64-bit file I/O on 64-bit systems"); + STATIC_ASSERT_MSG(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); return ftruncate(fd, length) == 0 ? MDBX_SUCCESS : errno; #endif }