mdbx: add STATIC_ASSERT_MSG, refine STATIC_ASSERT.

This commit is contained in:
Leo Yuriev 2017-05-26 17:11:48 +03:00
parent b038767a18
commit 18cf804d0b
3 changed files with 23 additions and 15 deletions

View File

@ -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

View File

@ -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 */

View File

@ -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
}