From 006f53175749564d1bb8bfc82a05f6abe12a2e5d Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Wed, 26 Jul 2017 15:54:56 +0300 Subject: [PATCH 1/6] mdbx: turn `/Wall` for MSVC. Change-Id: Idc264a9fe40a2af8629d96121fb91cba94e7147f --- dll.vcxproj | 8 ++++---- src/tools/mdbx_chk.vcxproj | 8 ++++---- src/tools/mdbx_copy.vcxproj | 8 ++++---- src/tools/mdbx_dump.vcxproj | 8 ++++---- src/tools/mdbx_load.vcxproj | 8 ++++---- src/tools/mdbx_stat.vcxproj | 8 ++++---- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/dll.vcxproj b/dll.vcxproj index 82f89ca5..746de67b 100644 --- a/dll.vcxproj +++ b/dll.vcxproj @@ -97,7 +97,7 @@ WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBMDBX_EXPORTS;%(PreprocessorDefinitions);MDBX_DEBUG=1 MultiThreadedDebugDLL - Level4 + EnableAllWarnings ProgramDatabase Disabled true @@ -113,7 +113,7 @@ WIN32;NDEBUG;_WINDOWS;_USRDLL;LIBMDBX_EXPORTS;%(PreprocessorDefinitions) MultiThreadedDLL - Level4 + EnableAllWarnings ProgramDatabase true Full @@ -139,7 +139,7 @@ - Level4 + EnableAllWarnings WIN64;_DEBUG;_WINDOWS;_USRDLL;LIBMDBX_EXPORTS;%(PreprocessorDefinitions);MDBX_DEBUG=1 MultiThreadedDebugDLL true @@ -157,7 +157,7 @@ Size true true - Level4 + EnableAllWarnings UseLinkTimeCodeGeneration diff --git a/src/tools/mdbx_chk.vcxproj b/src/tools/mdbx_chk.vcxproj index b680471d..d4cc420d 100644 --- a/src/tools/mdbx_chk.vcxproj +++ b/src/tools/mdbx_chk.vcxproj @@ -93,7 +93,7 @@ - Level4 + EnableAllWarnings Disabled _DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -106,7 +106,7 @@ - Level4 + EnableAllWarnings Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -117,7 +117,7 @@ - Level4 + EnableAllWarnings MaxSpeed @@ -133,7 +133,7 @@ - Level4 + EnableAllWarnings MaxSpeed diff --git a/src/tools/mdbx_copy.vcxproj b/src/tools/mdbx_copy.vcxproj index d2ff8254..6910060f 100644 --- a/src/tools/mdbx_copy.vcxproj +++ b/src/tools/mdbx_copy.vcxproj @@ -93,7 +93,7 @@ - Level4 + EnableAllWarnings Disabled _DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -106,7 +106,7 @@ - Level4 + EnableAllWarnings Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -117,7 +117,7 @@ - Level4 + EnableAllWarnings MaxSpeed @@ -133,7 +133,7 @@ - Level4 + EnableAllWarnings MaxSpeed diff --git a/src/tools/mdbx_dump.vcxproj b/src/tools/mdbx_dump.vcxproj index 5f8b0b24..b44eb193 100644 --- a/src/tools/mdbx_dump.vcxproj +++ b/src/tools/mdbx_dump.vcxproj @@ -93,7 +93,7 @@ - Level4 + EnableAllWarnings Disabled _DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -106,7 +106,7 @@ - Level4 + EnableAllWarnings Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -117,7 +117,7 @@ - Level4 + EnableAllWarnings MaxSpeed @@ -133,7 +133,7 @@ - Level4 + EnableAllWarnings MaxSpeed diff --git a/src/tools/mdbx_load.vcxproj b/src/tools/mdbx_load.vcxproj index 93a88f93..6af6cc45 100644 --- a/src/tools/mdbx_load.vcxproj +++ b/src/tools/mdbx_load.vcxproj @@ -93,7 +93,7 @@ - Level4 + EnableAllWarnings Disabled _DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -106,7 +106,7 @@ - Level4 + EnableAllWarnings Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -117,7 +117,7 @@ - Level4 + EnableAllWarnings MaxSpeed @@ -133,7 +133,7 @@ - Level4 + EnableAllWarnings MaxSpeed diff --git a/src/tools/mdbx_stat.vcxproj b/src/tools/mdbx_stat.vcxproj index 3475165b..1adcefde 100644 --- a/src/tools/mdbx_stat.vcxproj +++ b/src/tools/mdbx_stat.vcxproj @@ -93,7 +93,7 @@ - Level4 + EnableAllWarnings Disabled _DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -106,7 +106,7 @@ - Level4 + EnableAllWarnings Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);LIBMDBX_IMPORTS=1 true @@ -117,7 +117,7 @@ - Level4 + EnableAllWarnings MaxSpeed @@ -133,7 +133,7 @@ - Level4 + EnableAllWarnings MaxSpeed From a78edd314e6d1b3bde519e9c4779a921dfca268b Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Wed, 26 Jul 2017 15:56:39 +0300 Subject: [PATCH 2/6] mdbx: more fix MSVC '/Wall' warnings. Change-Id: I359a3056d6bb5db762fe6b6b8a89e8ed066f8a87 --- src/mdbx.c | 10 ++++++---- src/osal.h | 6 +++--- test/base.h | 1 + test/chrono.h | 2 +- test/test.cc | 6 +++--- test/utils.h | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/mdbx.c b/src/mdbx.c index b03b7de8..4cb59f50 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -3024,7 +3024,7 @@ again_on_freelist_change: if (total_room >= rpl_len) { if (total_room == rpl_len || --more < 0) break; - } else if (head_room >= env->me_maxfree_1pg && head_id > 1) { + } else if (head_room >= (ssize_t)env->me_maxfree_1pg && head_id > 1) { /* Keep current record (overflow page), add a new one */ head_id--; refill_reclaimed_pos++; @@ -3072,7 +3072,7 @@ again_on_freelist_change: /* (Re)write {key = head_id, IDL length = head_room} */ total_room -= head_room; head_room = rpl_len - total_room; - if (head_room > env->me_maxfree_1pg && head_id > 1) { + if (head_room > (ssize_t)env->me_maxfree_1pg && head_id > 1) { /* Overflow multi-page for part of me_reclaimed_pglist */ head_room /= (head_id < INT16_MAX) ? (pgno_t)head_id : INT16_MAX; /* amortize page sizes */ @@ -4647,12 +4647,14 @@ static int __cold mdbx_setup_dxb(MDBX_env *env, int lck_rc) { ", have %" PRIu64 "/%" PRIaPGNO "), " "assume collision in non-exclusive mode", expected_bytes, bytes2pgno(env, expected_bytes), - filesize_before_mmap, bytes2pgno(env, filesize_before_mmap)); + filesize_before_mmap, + bytes2pgno(env, (size_t)filesize_before_mmap)); } else { mdbx_notice("filesize mismatch (expect %" PRIuPTR "/%" PRIaPGNO ", have %" PRIu64 "/%" PRIaPGNO ")", expected_bytes, bytes2pgno(env, expected_bytes), - filesize_before_mmap, bytes2pgno(env, filesize_before_mmap)); + filesize_before_mmap, + bytes2pgno(env, (size_t)filesize_before_mmap)); if (filesize_before_mmap < used_bytes) { mdbx_error("last-page beyond end-of-file (last %" PRIaPGNO ", have %" PRIaPGNO ")", diff --git a/src/osal.h b/src/osal.h index 35d2bdbb..cb3dbd68 100644 --- a/src/osal.h +++ b/src/osal.h @@ -83,13 +83,13 @@ typedef struct { typedef CRITICAL_SECTION mdbx_fastmutex_t; #else #include +#include #include #include #include #include #include #include -#include typedef pthread_t mdbx_thread_t; typedef pthread_key_t mdbx_thread_key_t; #define INVALID_HANDLE_VALUE (-1) @@ -561,7 +561,7 @@ static __inline uint64_t mdbx_atomic_add64(volatile uint64_t *p, uint64_t v) { return __sync_fetch_and_add(p, v); #else #ifdef _MSC_VER - return _InterlockedExchangeAdd64(p, v); + return _InterlockedExchangeAdd64((volatile int64_t *)p, v); #endif #ifdef __APPLE__ return OSAtomicAdd64(v, (volatile int64_t *)p); @@ -598,7 +598,7 @@ static __inline bool mdbx_atomic_compare_and_swap64(volatile uint64_t *p, return __sync_bool_compare_and_swap(p, c, v); #else #ifdef _MSC_VER - return c == _InterlockedCompareExchange64(p, v, c); + return c == _InterlockedCompareExchange64((volatile int64_t *)p, v, c); #endif #ifdef __APPLE__ return c == OSAtomicCompareAndSwap64Barrier(c, v, (volatile uint64_t *)p); diff --git a/test/base.h b/test/base.h index 8557787d..e882c191 100644 --- a/test/base.h +++ b/test/base.h @@ -86,6 +86,7 @@ #include "../mdbx.h" #include "../src/defs.h" +#include "../src/osal.h" #ifdef _MSC_VER #pragma warning(pop) diff --git a/test/chrono.h b/test/chrono.h index b417f1e1..4be7e46e 100644 --- a/test/chrono.h +++ b/test/chrono.h @@ -62,7 +62,7 @@ inline time from_seconds(uint64_t seconds) { inline time from_utc(time_t utc) { assert(utc >= 0); - return from_seconds(utc); + return from_seconds((uint64_t)utc); } inline time infinite() { diff --git a/test/test.cc b/test/test.cc index 9a265e33..73e96986 100644 --- a/test/test.cc +++ b/test/test.cc @@ -66,8 +66,8 @@ const char *keygencase2str(const keygen_case keycase) { //----------------------------------------------------------------------------- -static void mdbx_debug_logger(int type, const char *function, int line, - const char *msg, va_list args) { +static void mdbx_logger(int type, const char *function, int line, + const char *msg, va_list args) { logging::loglevel level = logging::info; if (type & MDBX_DBG_EXTRA) level = logging::extra; @@ -120,7 +120,7 @@ void testcase::db_prepare() { mdbx_dbg_opts |= MDBX_DBG_TRACE; if (config.params.loglevel <= logging::verbose) mdbx_dbg_opts |= MDBX_DBG_PRINT; - int rc = mdbx_setup_debug(mdbx_dbg_opts, mdbx_debug_logger); + int rc = mdbx_setup_debug(mdbx_dbg_opts, mdbx_logger); log_info("set mdbx debug-opts: 0x%02x", rc); MDBX_env *env = nullptr; diff --git a/test/utils.h b/test/utils.h index 624a204c..726f676a 100644 --- a/test/utils.h +++ b/test/utils.h @@ -229,7 +229,7 @@ static __inline uint64_t mul_32x32_64(uint32_t a, uint32_t b) { static __inline unsigned add_with_carry(uint64_t *sum, uint64_t addend) { *sum += addend; - return *sum < addend; + return (*sum < addend) ? 1u : 0u; } static __inline uint64_t mul_64x64_128(uint64_t a, uint64_t b, uint64_t *h) { From fb9d7a447182e50385ea53aa6aad9aecce8ee7e2 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Wed, 26 Jul 2017 16:20:40 +0300 Subject: [PATCH 3/6] mdbx: refine freelist_list() to avoid coverity warnings. Change-Id: I38eee44e4ca926e7d11ef5e3f7f3ce2e92f71259 --- src/mdbx.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/mdbx.c b/src/mdbx.c index 4cb59f50..eacd0187 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -3066,7 +3066,10 @@ again_on_freelist_change: /* LY: note that freeDB cleanup is not needed. */ ++cleanup_reclaimed_pos; } + mdbx_tassert(txn, txn->mt_lifo_reclaimed != NULL); head_id = txn->mt_lifo_reclaimed[refill_reclaimed_pos]; + } else { + mdbx_tassert(txn, txn->mt_lifo_reclaimed == NULL); } /* (Re)write {key = head_id, IDL length = head_room} */ @@ -3132,18 +3135,23 @@ again_on_freelist_change: size_t rpl_left = env->me_reclaimed_pglist[0]; pgno_t *rpl_end = env->me_reclaimed_pglist + rpl_left; - if (!lifo) { + if (txn->mt_lifo_reclaimed == 0) { + mdbx_tassert(txn, lifo == 0); rc = mdbx_cursor_first(&mc, &key, &data); if (unlikely(rc)) goto bailout; + } else { + mdbx_tassert(txn, lifo != 0); } while (1) { txnid_t id; - if (!lifo) { + if (txn->mt_lifo_reclaimed == 0) { + mdbx_tassert(txn, lifo == 0); id = *(txnid_t *)key.iov_base; mdbx_tassert(txn, id <= env->me_last_reclaimed); } else { + mdbx_tassert(txn, lifo != 0); mdbx_tassert(txn, refill_reclaimed_pos > 0 && refill_reclaimed_pos <= txn->mt_lifo_reclaimed[0]); From 336bef7e51141cec78047bbb6a5a06a28b8fee6f Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Wed, 26 Jul 2017 17:34:59 +0300 Subject: [PATCH 4/6] mdbx: make internal 'atomics' compatible with C++. Change-Id: I1381f477e977edf4bf56871a2e34af1b302867a0 --- src/osal.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/osal.h b/src/osal.h index cb3dbd68..7ad67799 100644 --- a/src/osal.h +++ b/src/osal.h @@ -515,7 +515,8 @@ int mdbx_rpid_check(MDBX_env *env, mdbx_pid_t pid); /*----------------------------------------------------------------------------*/ /* Atomics */ -#if (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__) && \ +#if !defined(__cplusplus) && (__STDC_VERSION__ >= 201112L) && \ + !defined(__STDC_NO_ATOMICS__) && \ (__GNUC_PREREQ(4, 9) || __CLANG_PREREQ(3, 8) || \ !(defined(__GNUC__) || defined(__clang__))) #include @@ -538,7 +539,7 @@ int mdbx_rpid_check(MDBX_env *env, mdbx_pid_t pid); #endif static __inline uint32_t mdbx_atomic_add32(volatile uint32_t *p, uint32_t v) { -#if defined(ATOMIC_VAR_INIT) +#if !defined(__cplusplus) && defined(ATOMIC_VAR_INIT) assert(atomic_is_lock_free(p)); return atomic_fetch_add((_Atomic uint32_t *)p, v); #elif defined(__GNUC__) || defined(__clang__) @@ -554,7 +555,7 @@ static __inline uint32_t mdbx_atomic_add32(volatile uint32_t *p, uint32_t v) { } static __inline uint64_t mdbx_atomic_add64(volatile uint64_t *p, uint64_t v) { -#ifdef ATOMIC_VAR_INIT +#if !defined(__cplusplus) && defined(ATOMIC_VAR_INIT) assert(atomic_is_lock_free(p)); return atomic_fetch_add((_Atomic uint64_t *)p, v); #elif defined(__GNUC__) || defined(__clang__) @@ -569,12 +570,12 @@ static __inline uint64_t mdbx_atomic_add64(volatile uint64_t *p, uint64_t v) { #endif } -#define mdbx_atomic_sub32(p, v) mdbx_atomic_add32(p, -(v)) -#define mdbx_atomic_sub64(p, v) mdbx_atomic_add64(p, -(v)) +#define mdbx_atomic_sub32(p, v) mdbx_atomic_add32(p, 0 - (v)) +#define mdbx_atomic_sub64(p, v) mdbx_atomic_add64(p, 0 - (v)) static __inline bool mdbx_atomic_compare_and_swap32(volatile uint32_t *p, uint32_t c, uint32_t v) { -#ifdef ATOMIC_VAR_INIT +#if !defined(__cplusplus) && defined(ATOMIC_VAR_INIT) assert(atomic_is_lock_free(p)); return atomic_compare_exchange_strong((_Atomic uint32_t *)p, &c, v); #elif defined(__GNUC__) || defined(__clang__) @@ -591,7 +592,7 @@ static __inline bool mdbx_atomic_compare_and_swap32(volatile uint32_t *p, static __inline bool mdbx_atomic_compare_and_swap64(volatile uint64_t *p, uint64_t c, uint64_t v) { -#ifdef ATOMIC_VAR_INIT +#if !defined(__cplusplus) && defined(ATOMIC_VAR_INIT) assert(atomic_is_lock_free(p)); return atomic_compare_exchange_strong((_Atomic uint64_t *)p, &c, v); #elif defined(__GNUC__) || defined(__clang__) From 092156ee4f5c7e013118296ead5cf6847af1a955 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Wed, 26 Jul 2017 18:32:46 +0300 Subject: [PATCH 5/6] mdbx: fix/redefine ssize_t, PRI*PTR nad PRI*size macros for MSVC. Change-Id: I6cb565087350cf1d1e1735bff1c5480c3d4c02db --- mdbx.h | 11 +++---- src/mdbx.c | 74 +++++++++++++++++++------------------------ src/osal.c | 8 ++--- src/osal.h | 24 ++++++++++++++ src/tools/mdbx_chk.c | 9 +----- src/tools/mdbx_copy.c | 9 +----- src/tools/mdbx_dump.c | 9 +----- src/tools/mdbx_load.c | 37 +++++++++------------- src/tools/mdbx_stat.c | 11 ++----- 9 files changed, 86 insertions(+), 106 deletions(-) diff --git a/mdbx.h b/mdbx.h index a7bba16a..febef67b 100644 --- a/mdbx.h +++ b/mdbx.h @@ -74,7 +74,6 @@ typedef unsigned mode_t; typedef HANDLE mdbx_filehandle_t; typedef DWORD mdbx_pid_t; typedef DWORD mdbx_tid_t; -typedef SSIZE_T ssize_t; #define MDBX_ENODATA ERROR_HANDLE_EOF #define MDBX_EINVAL ERROR_INVALID_PARAMETER #define MDBX_EACCESS ERROR_ACCESS_DENIED @@ -813,11 +812,11 @@ LIBMDBX_API int mdbx_env_get_fd(MDBX_env *env, mdbx_filehandle_t *fd); * - MDBX_EINVAL - an invalid parameter was specified, * or the environment has an active write transaction. */ LIBMDBX_API int mdbx_env_set_mapsize(MDBX_env *env, size_t size); -LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, ssize_t size_lower, - ssize_t size_now, ssize_t size_upper, - ssize_t growth_step, - ssize_t shrink_threshold, - ssize_t pagesize); +LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, intptr_t size_lower, + intptr_t size_now, intptr_t size_upper, + intptr_t growth_step, + intptr_t shrink_threshold, + intptr_t pagesize); /* Set the maximum number of threads/reader slots for the environment. * diff --git a/src/mdbx.c b/src/mdbx.c index eacd0187..c10c6f28 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -37,14 +37,6 @@ #include "./bits.h" -#if defined(_MSC_VER) && _MSC_VER == 1900 -/* LY: MSVC 2015 has buggy/inconsistent PRIuPTR/PRIxPTR macros and format-arg - checker for size_t typedef. */ -#pragma warning(disable : 4777) /* format string '%10u' requires an argument \ - of type 'unsigned int', but variadic \ - argument 1 has type 'std::size_t' */ -#endif /* _MSC_VER (warnings) */ - /*----------------------------------------------------------------------------*/ /* rthc (tls keys and destructors) */ @@ -1076,7 +1068,7 @@ static void __cold mdbx_kill_page(MDBX_env *env, pgno_t pgno) { VALGRIND_MAKE_MEM_NOACCESS(&mp->mp_pages, env->me_psize - shift); ASAN_POISON_MEMORY_REGION(&mp->mp_pages, env->me_psize - shift); } else { - ssize_t len = env->me_psize - shift; + intptr_t len = env->me_psize - shift; void *buf = alloca(len); memset(buf, 0x6F /* 'o', 111 */, len); (void)mdbx_pwrite(env->me_fd, buf, len, offs + shift); @@ -2903,16 +2895,16 @@ static int mdbx_freelist_save(MDBX_txn *txn) { int rc, more = 1; txnid_t cleanup_reclaimed_id = 0, head_id = 0; pgno_t befree_count = 0; - ssize_t head_room = 0, total_room = 0; + intptr_t head_room = 0, total_room = 0; unsigned cleanup_reclaimed_pos = 0, refill_reclaimed_pos = 0; const bool lifo = (env->me_flags & MDBX_LIFORECLAIM) != 0; mdbx_cursor_init(&mc, txn, FREE_DBI, NULL); /* MDBX_RESERVE cancels meminit in ovpage malloc (when no WRITEMAP) */ - const ssize_t clean_limit = (env->me_flags & (MDBX_NOMEMINIT | MDBX_WRITEMAP)) - ? SSIZE_MAX - : env->me_maxfree_1pg; + const intptr_t clean_limit = + (env->me_flags & (MDBX_NOMEMINIT | MDBX_WRITEMAP)) ? SSIZE_MAX + : env->me_maxfree_1pg; again_on_freelist_change: while (1) { @@ -3012,7 +3004,7 @@ again_on_freelist_change: continue; } - const ssize_t rpl_len = + const intptr_t rpl_len = (env->me_reclaimed_pglist ? env->me_reclaimed_pglist[0] : 0) + txn->mt_loose_count; if (rpl_len && refill_reclaimed_pos == 0) @@ -3024,7 +3016,7 @@ again_on_freelist_change: if (total_room >= rpl_len) { if (total_room == rpl_len || --more < 0) break; - } else if (head_room >= (ssize_t)env->me_maxfree_1pg && head_id > 1) { + } else if (head_room >= (intptr_t)env->me_maxfree_1pg && head_id > 1) { /* Keep current record (overflow page), add a new one */ head_id--; refill_reclaimed_pos++; @@ -3075,7 +3067,7 @@ again_on_freelist_change: /* (Re)write {key = head_id, IDL length = head_room} */ total_room -= head_room; head_room = rpl_len - total_room; - if (head_room > (ssize_t)env->me_maxfree_1pg && head_id > 1) { + if (head_room > (intptr_t)env->me_maxfree_1pg && head_id > 1) { /* Overflow multi-page for part of me_reclaimed_pglist */ head_room /= (head_id < INT16_MAX) ? (pgno_t)head_id : INT16_MAX; /* amortize page sizes */ @@ -3094,7 +3086,7 @@ again_on_freelist_change: /* IDL is initially empty, zero out at least the length */ pgno_t *pgs = (pgno_t *)data.iov_base; - ssize_t i = head_room > clean_limit ? head_room : 0; + intptr_t i = head_room > clean_limit ? head_room : 0; do { pgs[i] = 0; } while (--i >= 0); @@ -3235,7 +3227,7 @@ static int mdbx_page_flush(MDBX_txn *txn, pgno_t keep) { pgno_t pgno = 0; MDBX_page *dp = NULL; struct iovec iov[MDBX_COMMIT_PAGES]; - ssize_t wpos = 0, wsize = 0; + intptr_t wpos = 0, wsize = 0; size_t next_pos = 1; /* impossible pos, so pos != next_pos */ int n = 0; @@ -4118,7 +4110,7 @@ int __cold mdbx_env_get_maxkeysize(MDBX_env *env) { } #define mdbx_nodemax(pagesize) \ - (((((pagesize)-PAGEHDRSZ) / MDBX_MINKEYS) & -(ssize_t)2) - sizeof(indx_t)) + (((((pagesize)-PAGEHDRSZ) / MDBX_MINKEYS) & -(intptr_t)2) - sizeof(indx_t)) #define mdbx_maxkey(nodemax) ((nodemax) - (NODESIZE + sizeof(MDBX_db))) @@ -4128,11 +4120,11 @@ int mdbx_get_maxkeysize(size_t pagesize) { if (pagesize == 0) pagesize = mdbx_syspagesize(); - ssize_t nodemax = mdbx_nodemax(pagesize); + intptr_t nodemax = mdbx_nodemax(pagesize); if (nodemax < 0) return -MDBX_EINVAL; - ssize_t maxkey = mdbx_maxkey(nodemax); + intptr_t maxkey = mdbx_maxkey(nodemax); return (maxkey > 0 && maxkey < INT_MAX) ? (int)maxkey : -MDBX_EINVAL; } @@ -4147,13 +4139,13 @@ static void __cold mdbx_setup_pagesize(MDBX_env *env, const size_t pagesize) { STATIC_ASSERT(mdbx_maxfree1pg(MIN_PAGESIZE) > 42); STATIC_ASSERT(mdbx_maxfree1pg(MAX_PAGESIZE) < MDBX_IDL_DB_MAX); - const ssize_t maxfree_1pg = (pagesize - PAGEHDRSZ) / sizeof(pgno_t) - 1; + const intptr_t maxfree_1pg = (pagesize - PAGEHDRSZ) / sizeof(pgno_t) - 1; mdbx_ensure(env, maxfree_1pg > 42 && maxfree_1pg < MDBX_IDL_DB_MAX); env->me_maxfree_1pg = (unsigned)maxfree_1pg; STATIC_ASSERT(mdbx_nodemax(MIN_PAGESIZE) > 42); STATIC_ASSERT(mdbx_nodemax(MAX_PAGESIZE) < UINT16_MAX); - const ssize_t nodemax = mdbx_nodemax(pagesize); + const intptr_t nodemax = mdbx_nodemax(pagesize); mdbx_ensure(env, nodemax > 42 && nodemax < UINT16_MAX); env->me_nodemax = (unsigned)nodemax; @@ -4161,7 +4153,7 @@ static void __cold mdbx_setup_pagesize(MDBX_env *env, const size_t pagesize) { STATIC_ASSERT(mdbx_maxkey(MIN_PAGESIZE) < MIN_PAGESIZE); STATIC_ASSERT(mdbx_maxkey(MAX_PAGESIZE) > 42); STATIC_ASSERT(mdbx_maxkey(MAX_PAGESIZE) < MAX_PAGESIZE); - const ssize_t maxkey_limit = mdbx_maxkey(env->me_nodemax); + const intptr_t maxkey_limit = mdbx_maxkey(env->me_nodemax); mdbx_ensure(env, maxkey_limit > 42 && (size_t)maxkey_limit < pagesize); env->me_maxkey_limit = (unsigned)maxkey_limit; @@ -4253,11 +4245,11 @@ static int __cold mdbx_env_map(MDBX_env *env, size_t usedsize) { return MDBX_SUCCESS; } -LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, ssize_t size_lower, - ssize_t size_now, ssize_t size_upper, - ssize_t growth_step, - ssize_t shrink_threshold, - ssize_t pagesize) { +LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, intptr_t size_lower, + intptr_t size_now, intptr_t size_upper, + intptr_t growth_step, + intptr_t shrink_threshold, + intptr_t pagesize) { if (unlikely(!env)) return MDBX_EINVAL; @@ -4282,7 +4274,7 @@ LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, ssize_t size_lower, if (pagesize < 0) pagesize = env->me_psize; - if (pagesize != (ssize_t)env->me_psize) { + if (pagesize != (intptr_t)env->me_psize) { rc = MDBX_EINVAL; goto bailout; } @@ -4434,7 +4426,7 @@ LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, ssize_t size_lower, if (env->me_map) { /* apply new params */ - mdbx_assert(env, pagesize == (ssize_t)env->me_psize); + mdbx_assert(env, pagesize == (intptr_t)env->me_psize); MDBX_meta *head = mdbx_meta_head(env); MDBX_meta meta = *head; @@ -4466,7 +4458,7 @@ LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, ssize_t size_lower, mdbx_meta_set_txnid(env, &meta, mdbx_meta_txnid_stable(env, head) + 1); rc = mdbx_sync_locked(env, env->me_flags, &meta); } - } else if (pagesize != (ssize_t)env->me_psize) { + } else if (pagesize != (intptr_t)env->me_psize) { mdbx_setup_pagesize(env, pagesize); } @@ -6984,7 +6976,7 @@ int mdbx_cursor_put(MDBX_cursor *mc, MDBX_val *key, MDBX_val *data, * Copy end of page, adjusting alignment so * compiler may copy words instead of bytes. */ const size_t off = - (PAGEHDRSZ + data->iov_len) & -(ssize_t)sizeof(size_t); + (PAGEHDRSZ + data->iov_len) & -(intptr_t)sizeof(size_t); memcpy((size_t *)((char *)np + off), (size_t *)((char *)omp + off), whole - off); memcpy(np, omp, PAGEHDRSZ); /* Copy header of page */ @@ -7389,7 +7381,7 @@ static int mdbx_node_add(MDBX_cursor *mc, unsigned indx, MDBX_val *key, MDBX_val *data, pgno_t pgno, unsigned flags) { unsigned i; size_t node_size = NODESIZE; - ssize_t room; + intptr_t room; MDBX_node *node; MDBX_page *mp = mc->mc_pg[mc->mc_top]; MDBX_page *ofp = NULL; /* overflow page */ @@ -7423,7 +7415,7 @@ static int mdbx_node_add(MDBX_cursor *mc, unsigned indx, MDBX_val *key, return MDBX_SUCCESS; } - room = (ssize_t)SIZELEFT(mp) - (ssize_t)sizeof(indx_t); + room = (intptr_t)SIZELEFT(mp) - (intptr_t)sizeof(indx_t); if (key != NULL) node_size += key->iov_len; if (IS_LEAF(mp)) { @@ -7440,7 +7432,7 @@ static int mdbx_node_add(MDBX_cursor *mc, unsigned indx, MDBX_val *key, ", put data on overflow page", data->iov_len, node_size + data->iov_len); node_size = EVEN(node_size + sizeof(pgno_t)); - if ((ssize_t)node_size > room) + if ((intptr_t)node_size > room) goto full; if ((rc = mdbx_page_new(mc, P_OVERFLOW, ovpages, &ofp))) return rc; @@ -7452,7 +7444,7 @@ static int mdbx_node_add(MDBX_cursor *mc, unsigned indx, MDBX_val *key, } } node_size = EVEN(node_size); - if (unlikely((ssize_t)node_size > room)) + if (unlikely((intptr_t)node_size > room)) goto full; update: @@ -10312,12 +10304,12 @@ int __cold mdbx_reader_list(MDBX_env *env, MDBX_msg_func *func, void *ctx) { const txnid_t txnid = lck->mti_readers[i].mr_txnid; if (txnid == ~(txnid_t)0) snprintf(buf, sizeof(buf), "%10" PRIuPTR " %" PRIxPTR " -\n", - (size_t)lck->mti_readers[i].mr_pid, - (size_t)lck->mti_readers[i].mr_tid); + (uintptr_t)lck->mti_readers[i].mr_pid, + (uintptr_t)lck->mti_readers[i].mr_tid); else snprintf(buf, sizeof(buf), "%10" PRIuPTR " %" PRIxPTR " %" PRIaTXN "\n", - (size_t)lck->mti_readers[i].mr_pid, - (size_t)lck->mti_readers[i].mr_tid, txnid); + (uintptr_t)lck->mti_readers[i].mr_pid, + (uintptr_t)lck->mti_readers[i].mr_tid, txnid); if (first) { first = 0; diff --git a/src/osal.c b/src/osal.c index 22d4253b..381594a4 100644 --- a/src/osal.c +++ b/src/osal.c @@ -508,7 +508,7 @@ int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, uint64_t offset) { #else 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); + intptr_t read = pread(fd, buf, bytes, offset); if (read < 0) { int rc = errno; return (rc == MDBX_SUCCESS) ? /* paranoia */ MDBX_EIO : rc; @@ -534,7 +534,7 @@ int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t bytes, return GetLastError(); #else int rc; - ssize_t written; + intptr_t written; do { STATIC_ASSERT_MSG(sizeof(off_t) >= sizeof(size_t), "libmdbx requires 64-bit file I/O on 64-bit systems"); @@ -562,7 +562,7 @@ int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt, : MDBX_EIO /* ERROR_WRITE_FAULT */; #else int rc; - ssize_t written; + intptr_t written; do { STATIC_ASSERT_MSG(sizeof(off_t) >= sizeof(size_t), "libmdbx requires 64-bit file I/O on 64-bit systems"); @@ -593,7 +593,7 @@ int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t bytes) { if (unlikely(!WriteFile(fd, ptr, (DWORD)chunk, &written, NULL))) return GetLastError(); #else - ssize_t written = write(fd, ptr, chunk); + intptr_t written = write(fd, ptr, chunk); if (written < 0) { int rc = errno; #ifdef SIGPIPE diff --git a/src/osal.h b/src/osal.h index 7ad67799..8d8d84cb 100644 --- a/src/osal.h +++ b/src/osal.h @@ -609,6 +609,30 @@ static __inline bool mdbx_atomic_compare_and_swap64(volatile uint64_t *p, /*----------------------------------------------------------------------------*/ +#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 1920 +/* LY: MSVC 2015/2017 has buggy/inconsistent PRIuPTR/PRIxPTR macros + * for internal format-args checker. */ +#undef PRIuPTR +#undef PRIiPTR +#undef PRIdPTR +#undef PRIxPTR +#define PRIuPTR "Iu" +#define PRIiPTR "Ii" +#define PRIdPTR "Id" +#define PRIxPTR "Ix" +#define PRIuSIZE "zu" +#define PRIiSIZE "zi" +#define PRIdSIZE "zd" +#define PRIxSIZE "zx" +#endif /* fix PRI*PTR for _MSC_VER */ + +#ifndef PRIuSIZE +#define PRIuSIZE PRIuPTR +#define PRIiSIZE PRIiPTR +#define PRIdSIZE PRIdPTR +#define PRIxSIZE PRIxPTR +#endif /* PRI*SIZE macros for MSVC */ + #ifdef _MSC_VER #pragma warning(pop) #endif diff --git a/src/tools/mdbx_chk.c b/src/tools/mdbx_chk.c index 91f5ef7b..c6d33c60 100644 --- a/src/tools/mdbx_chk.c +++ b/src/tools/mdbx_chk.c @@ -18,14 +18,7 @@ #pragma warning(disable : 4464) /* relative include path contains '..' */ #endif #pragma warning(disable : 4996) /* The POSIX name is deprecated... */ -#if _MSC_VER == 1900 -/* LY: MSVC 2015 has buggy/inconsistent PRIuPTR/PRIxPTR macros and format-arg - checker for size_t typedef. */ -#pragma warning(disable : 4777) /* format string '%10u' requires an argument \ - of type 'unsigned int', but variadic \ - argument 1 has type 'std::size_t' */ -#endif -#endif /* _MSC_VER (warnings) */ +#endif /* _MSC_VER (warnings) */ #include "../bits.h" diff --git a/src/tools/mdbx_copy.c b/src/tools/mdbx_copy.c index ec0856ca..6d571134 100644 --- a/src/tools/mdbx_copy.c +++ b/src/tools/mdbx_copy.c @@ -18,14 +18,7 @@ #pragma warning(disable : 4464) /* relative include path contains '..' */ #endif #pragma warning(disable : 4996) /* The POSIX name is deprecated... */ -#if _MSC_VER == 1900 -/* LY: MSVC 2015 has buggy/inconsistent PRIuPTR/PRIxPTR macros and format-arg - checker for size_t typedef. */ -#pragma warning(disable : 4777) /* format string '%10u' requires an argument \ - of type 'unsigned int', but variadic \ - argument 1 has type 'std::size_t' */ -#endif -#endif /* _MSC_VER (warnings) */ +#endif /* _MSC_VER (warnings) */ #include "../bits.h" diff --git a/src/tools/mdbx_dump.c b/src/tools/mdbx_dump.c index 19010c47..45a6e963 100644 --- a/src/tools/mdbx_dump.c +++ b/src/tools/mdbx_dump.c @@ -18,14 +18,7 @@ #pragma warning(disable : 4464) /* relative include path contains '..' */ #endif #pragma warning(disable : 4996) /* The POSIX name is deprecated... */ -#if _MSC_VER == 1900 -/* LY: MSVC 2015 has buggy/inconsistent PRIuPTR/PRIxPTR macros and format-arg - checker for size_t typedef. */ -#pragma warning(disable : 4777) /* format string '%10u' requires an argument \ - of type 'unsigned int', but variadic \ - argument 1 has type 'std::size_t' */ -#endif -#endif /* _MSC_VER (warnings) */ +#endif /* _MSC_VER (warnings) */ #include "../bits.h" #include diff --git a/src/tools/mdbx_load.c b/src/tools/mdbx_load.c index 0c20b1e7..19a0a3df 100644 --- a/src/tools/mdbx_load.c +++ b/src/tools/mdbx_load.c @@ -18,14 +18,7 @@ #pragma warning(disable : 4464) /* relative include path contains '..' */ #endif #pragma warning(disable : 4996) /* The POSIX name is deprecated... */ -#if _MSC_VER == 1900 -/* LY: MSVC 2015 has buggy/inconsistent PRIuPTR/PRIxPTR macros and format-arg - checker for size_t typedef. */ -#pragma warning(disable : 4777) /* format string '%10u' requires an argument \ - of type 'unsigned int', but variadic \ - argument 1 has type 'std::size_t' */ -#endif -#endif /* _MSC_VER (warnings) */ +#endif /* _MSC_VER (warnings) */ #include "../bits.h" #include @@ -96,8 +89,8 @@ static void readhdr(void) { } else if (!strncmp(dbuf.iov_base, "VERSION=", STRLENOF("VERSION="))) { version = atoi((char *)dbuf.iov_base + STRLENOF("VERSION=")); if (version > 3) { - fprintf(stderr, "%s: line %" PRIiPTR ": unsupported VERSION %d\n", prog, - lineno, version); + fprintf(stderr, "%s: line %" PRIiSIZE ": unsupported VERSION %d\n", + prog, lineno, version); exit(EXIT_FAILURE); } } else if (!strncmp(dbuf.iov_base, "HEADER=END", STRLENOF("HEADER=END"))) { @@ -108,7 +101,7 @@ static void readhdr(void) { mode |= PRINT; else if (strncmp((char *)dbuf.iov_base + STRLENOF("FORMAT="), "bytevalue", STRLENOF("bytevalue"))) { - fprintf(stderr, "%s: line %" PRIiPTR ": unsupported FORMAT %s\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": unsupported FORMAT %s\n", prog, lineno, (char *)dbuf.iov_base + STRLENOF("FORMAT=")); exit(EXIT_FAILURE); } @@ -122,7 +115,7 @@ static void readhdr(void) { } else if (!strncmp(dbuf.iov_base, "type=", STRLENOF("type="))) { if (strncmp((char *)dbuf.iov_base + STRLENOF("type="), "btree", STRLENOF("btree"))) { - fprintf(stderr, "%s: line %" PRIiPTR ": unsupported type %s\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": unsupported type %s\n", prog, lineno, (char *)dbuf.iov_base + STRLENOF("type=")); exit(EXIT_FAILURE); } @@ -134,7 +127,7 @@ static void readhdr(void) { void *unused; i = sscanf((char *)dbuf.iov_base + STRLENOF("mapaddr="), "%p", &unused); if (i != 1) { - fprintf(stderr, "%s: line %" PRIiPTR ": invalid mapaddr %s\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": invalid mapaddr %s\n", prog, lineno, (char *)dbuf.iov_base + STRLENOF("mapaddr=")); exit(EXIT_FAILURE); } @@ -146,7 +139,7 @@ static void readhdr(void) { i = sscanf((char *)dbuf.iov_base + STRLENOF("mapsize="), "%" PRIu64 "", &envinfo.mi_mapsize); if (i != 1) { - fprintf(stderr, "%s: line %" PRIiPTR ": invalid mapsize %s\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": invalid mapsize %s\n", prog, lineno, (char *)dbuf.iov_base + STRLENOF("mapsize=")); exit(EXIT_FAILURE); } @@ -159,7 +152,7 @@ static void readhdr(void) { i = sscanf((char *)dbuf.iov_base + STRLENOF("maxreaders="), "%u", &envinfo.mi_maxreaders); if (i != 1) { - fprintf(stderr, "%s: line %" PRIiPTR ": invalid maxreaders %s\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": invalid maxreaders %s\n", prog, lineno, (char *)dbuf.iov_base + STRLENOF("maxreaders=")); exit(EXIT_FAILURE); } @@ -176,13 +169,13 @@ static void readhdr(void) { if (!dbflags[i].bit) { ptr = memchr(dbuf.iov_base, '=', dbuf.iov_len); if (!ptr) { - fprintf(stderr, "%s: line %" PRIiPTR ": unexpected format\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": unexpected format\n", prog, lineno); exit(EXIT_FAILURE); } else { *ptr = '\0'; fprintf(stderr, - "%s: line %" PRIiPTR ": unrecognized keyword ignored: %s\n", + "%s: line %" PRIiSIZE ": unrecognized keyword ignored: %s\n", prog, lineno, (char *)dbuf.iov_base); } } @@ -191,7 +184,7 @@ static void readhdr(void) { } static void badend(void) { - fprintf(stderr, "%s: line %" PRIiPTR ": unexpected end of input\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": unexpected end of input\n", prog, lineno); } @@ -247,7 +240,7 @@ static int readline(MDBX_val *out, MDBX_val *buf) { buf->iov_base = realloc(buf->iov_base, buf->iov_len * 2); if (!buf->iov_base) { Eof = 1; - fprintf(stderr, "%s: line %" PRIiPTR ": out of memory, line too long\n", + fprintf(stderr, "%s: line %" PRIiSIZE ": out of memory, line too long\n", prog, lineno); return EOF; } @@ -463,7 +456,7 @@ int main(int argc, char *argv[]) { rc = readline(&data, &dbuf); if (rc) { - fprintf(stderr, "%s: line %" PRIiPTR ": failed to read key value\n", + fprintf(stderr, "%s: line %" PRIiSIZE ": failed to read key value\n", prog, lineno); goto txn_abort; } @@ -480,7 +473,7 @@ int main(int argc, char *argv[]) { if (batch == 100) { rc = mdbx_txn_commit(txn); if (rc) { - fprintf(stderr, "%s: line %" PRIiPTR ": txn_commit: %s\n", prog, + fprintf(stderr, "%s: line %" PRIiSIZE ": txn_commit: %s\n", prog, lineno, mdbx_strerror(rc)); goto env_close; } @@ -502,7 +495,7 @@ int main(int argc, char *argv[]) { rc = mdbx_txn_commit(txn); txn = NULL; if (rc) { - fprintf(stderr, "%s: line %" PRIiPTR ": txn_commit: %s\n", prog, lineno, + fprintf(stderr, "%s: line %" PRIiSIZE ": txn_commit: %s\n", prog, lineno, mdbx_strerror(rc)); goto env_close; } diff --git a/src/tools/mdbx_stat.c b/src/tools/mdbx_stat.c index f112c9bc..756dbc55 100644 --- a/src/tools/mdbx_stat.c +++ b/src/tools/mdbx_stat.c @@ -18,14 +18,7 @@ #pragma warning(disable : 4464) /* relative include path contains '..' */ #endif #pragma warning(disable : 4996) /* The POSIX name is deprecated... */ -#if _MSC_VER == 1900 -/* LY: MSVC 2015 has buggy/inconsistent PRIuPTR/PRIxPTR macros and format-arg - checker for size_t typedef. */ -#pragma warning(disable : 4777) /* format string '%10u' requires an argument \ - of type 'unsigned int', but variadic \ - argument 1 has type 'std::size_t' */ -#endif -#endif /* _MSC_VER (warnings) */ +#endif /* _MSC_VER (warnings) */ #include "../bits.h" @@ -247,7 +240,7 @@ int main(int argc, char *argv[]) { if (freinfo > 1) { char *bad = ""; pgno_t pg, prev; - ssize_t i, j, span = 0; + intptr_t i, j, span = 0; j = *iptr++; for (i = j, prev = NUM_METAS - 1; --i >= 0;) { pg = iptr[i]; From d634336e1f97f22bd26b19c0c6ccf168121497f6 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Wed, 26 Jul 2017 19:43:22 +0300 Subject: [PATCH 6/6] mdbx-test: fix snprintf/vsnprintf redefinition for MSVC. Change-Id: I4be3a4f71782d395035ff4d5b9e3a1af9ee70bb9 --- test/base.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/base.h b/test/base.h index e882c191..ffcf61ad 100644 --- a/test/base.h +++ b/test/base.h @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -99,7 +99,14 @@ #pragma warning(disable : 4512) /* assignment operator could \ not be generated */ #pragma warning(disable : 4610) /* user-defined constructor required */ -#define snprintf _snprintf +#ifndef snprintf +#define snprintf(buffer, buffer_size, format, ...) \ + _snprintf_s(buffer, buffer_size, _TRUNCATE, format, __VA_ARGS__) +#endif +#ifndef vsnprintf +#define vsnprintf(buffer, buffer_size, format, args) \ + _vsnprintf_s(buffer, buffer_size, _TRUNCATE, format, args) +#endif #pragma warning(disable : 4996) /* 'vsnprintf': This function or variable \ may be unsafe */ #endif