mdbx: use uintptr_t instead of size_t for pointer-wide values and types.

More for https://github.com/erthink/libmdbx/issues/88

Change-Id: Ic625096df3c09d0dd578c7b79aa302be35480699
This commit is contained in:
Leo Yuriev 2020-04-04 16:51:36 +03:00
parent 75de63dff1
commit 3f7c9846e7
4 changed files with 36 additions and 40 deletions

View File

@ -1012,13 +1012,13 @@ static __inline int thread_key_create(mdbx_thread_key_t *key) {
#else #else
rc = pthread_key_create(key, nullptr); rc = pthread_key_create(key, nullptr);
#endif #endif
mdbx_trace("&key = %p, value 0x%x, rc %d", __Wpedantic_format_voidptr(key), mdbx_trace("&key = %p, value %" PRIuPTR ", rc %d",
(unsigned)*key, rc); __Wpedantic_format_voidptr(key), (uintptr_t)*key, rc);
return rc; return rc;
} }
static __inline void thread_key_delete(mdbx_thread_key_t key) { static __inline void thread_key_delete(mdbx_thread_key_t key) {
mdbx_trace("key = 0x%x", (unsigned)key); mdbx_trace("key = %" PRIuPTR, (uintptr_t)key);
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
mdbx_ensure(nullptr, TlsFree(key)); mdbx_ensure(nullptr, TlsFree(key));
#else #else
@ -1045,7 +1045,7 @@ static void thread_rthc_set(mdbx_thread_key_t key, const void *value) {
static __thread uint32_t thread_registration_state; static __thread uint32_t thread_registration_state;
if (value && unlikely(thread_registration_state == MDBX_THREAD_RTHC_ZERO)) { if (value && unlikely(thread_registration_state == MDBX_THREAD_RTHC_ZERO)) {
thread_registration_state = MDBX_THREAD_RTHC_REGISTERD; thread_registration_state = MDBX_THREAD_RTHC_REGISTERD;
mdbx_trace("thread registered 0x%" PRIxPTR, (uintptr_t)mdbx_thread_self()); mdbx_trace("thread registered 0x%" PRIxPTR, mdbx_thread_self());
if (&__cxa_thread_atexit_impl == nullptr || if (&__cxa_thread_atexit_impl == nullptr ||
__cxa_thread_atexit_impl(mdbx_rthc_thread_dtor, __cxa_thread_atexit_impl(mdbx_rthc_thread_dtor,
&thread_registration_state, &thread_registration_state,
@ -1055,8 +1055,8 @@ static void thread_rthc_set(mdbx_thread_key_t key, const void *value) {
thread_registration_state = MDBX_THREAD_RTHC_COUNTED; thread_registration_state = MDBX_THREAD_RTHC_COUNTED;
const unsigned count_before = atomic_add32(&rthc_pending, 1); const unsigned count_before = atomic_add32(&rthc_pending, 1);
mdbx_ensure(nullptr, count_before < INT_MAX); mdbx_ensure(nullptr, count_before < INT_MAX);
mdbx_trace("fallback to pthreads' tsd, key 0x%x, count %u", mdbx_trace("fallback to pthreads' tsd, key %" PRIuPTR ", count %u",
(unsigned)rthc_key, count_before); (uintptr_t)rthc_key, count_before);
(void)count_before; (void)count_before;
} }
} }
@ -1098,7 +1098,7 @@ __cold void mdbx_rthc_global_init(void) {
__cold void mdbx_rthc_thread_dtor(void *ptr) { __cold void mdbx_rthc_thread_dtor(void *ptr) {
rthc_lock(); rthc_lock();
mdbx_trace(">> pid %d, thread 0x%" PRIxPTR ", rthc %p", mdbx_getpid(), mdbx_trace(">> pid %d, thread 0x%" PRIxPTR ", rthc %p", mdbx_getpid(),
(uintptr_t)mdbx_thread_self(), ptr); mdbx_thread_self(), ptr);
const uint32_t self_pid = mdbx_getpid(); const uint32_t self_pid = mdbx_getpid();
for (unsigned i = 0; i < rthc_count; ++i) { for (unsigned i = 0; i < rthc_count; ++i) {
@ -1112,7 +1112,7 @@ __cold void mdbx_rthc_thread_dtor(void *ptr) {
if (pthread_setspecific(key, nullptr) != 0) { if (pthread_setspecific(key, nullptr) != 0) {
mdbx_trace("== thread 0x%" PRIxPTR mdbx_trace("== thread 0x%" PRIxPTR
", rthc %p: ignore race with tsd-key deletion", ", rthc %p: ignore race with tsd-key deletion",
(uintptr_t)mdbx_thread_self(), ptr); mdbx_thread_self(), ptr);
continue /* ignore race with tsd-key deletion by mdbx_env_close() */; continue /* ignore race with tsd-key deletion by mdbx_env_close() */;
} }
#endif #endif
@ -1120,39 +1120,35 @@ __cold void mdbx_rthc_thread_dtor(void *ptr) {
mdbx_trace("== thread 0x%" PRIxPTR mdbx_trace("== thread 0x%" PRIxPTR
", rthc %p, [%i], %p ... %p (%+i), rtch-pid %i, " ", rthc %p, [%i], %p ... %p (%+i), rtch-pid %i, "
"current-pid %i", "current-pid %i",
(uintptr_t)mdbx_thread_self(), __Wpedantic_format_voidptr(rthc), mdbx_thread_self(), __Wpedantic_format_voidptr(rthc), i,
i, __Wpedantic_format_voidptr(rthc_table[i].begin), __Wpedantic_format_voidptr(rthc_table[i].begin),
__Wpedantic_format_voidptr(rthc_table[i].end), __Wpedantic_format_voidptr(rthc_table[i].end),
(int)(rthc - rthc_table[i].begin), rthc->mr_pid, self_pid); (int)(rthc - rthc_table[i].begin), rthc->mr_pid, self_pid);
if (rthc->mr_pid == self_pid) { if (rthc->mr_pid == self_pid) {
mdbx_trace("==== thread 0x%" PRIxPTR ", rthc %p, cleanup", mdbx_trace("==== thread 0x%" PRIxPTR ", rthc %p, cleanup",
(uintptr_t)mdbx_thread_self(), mdbx_thread_self(), __Wpedantic_format_voidptr(rthc));
__Wpedantic_format_voidptr(rthc));
rthc->mr_pid = 0; rthc->mr_pid = 0;
} }
} }
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
mdbx_trace("<< thread 0x%" PRIxPTR ", rthc %p", (uintptr_t)mdbx_thread_self(), mdbx_trace("<< thread 0x%" PRIxPTR ", rthc %p", mdbx_thread_self(), ptr);
ptr);
rthc_unlock(); rthc_unlock();
#else #else
const char self_registration = *(char *)ptr; const char self_registration = *(char *)ptr;
*(char *)ptr = MDBX_THREAD_RTHC_ZERO; *(char *)ptr = MDBX_THREAD_RTHC_ZERO;
mdbx_trace("== thread 0x%" PRIxPTR ", rthc %p, pid %d, self-status %d", mdbx_trace("== thread 0x%" PRIxPTR ", rthc %p, pid %d, self-status %d",
(uintptr_t)mdbx_thread_self(), ptr, mdbx_getpid(), mdbx_thread_self(), ptr, mdbx_getpid(), self_registration);
self_registration);
if (self_registration == MDBX_THREAD_RTHC_COUNTED) if (self_registration == MDBX_THREAD_RTHC_COUNTED)
mdbx_ensure(nullptr, atomic_sub32(&rthc_pending, 1) > 0); mdbx_ensure(nullptr, atomic_sub32(&rthc_pending, 1) > 0);
if (rthc_pending == 0) { if (rthc_pending == 0) {
mdbx_trace("== thread 0x%" PRIxPTR ", rthc %p, pid %d, wake", mdbx_trace("== thread 0x%" PRIxPTR ", rthc %p, pid %d, wake",
(uintptr_t)mdbx_thread_self(), ptr, mdbx_getpid()); mdbx_thread_self(), ptr, mdbx_getpid());
mdbx_ensure(nullptr, pthread_cond_broadcast(&rthc_cond) == 0); mdbx_ensure(nullptr, pthread_cond_broadcast(&rthc_cond) == 0);
} }
mdbx_trace("<< thread 0x%" PRIxPTR ", rthc %p", (uintptr_t)mdbx_thread_self(), mdbx_trace("<< thread 0x%" PRIxPTR ", rthc %p", mdbx_thread_self(), ptr);
ptr);
/* Allow tail call optimization, i.e. gcc should generate the jmp instruction /* Allow tail call optimization, i.e. gcc should generate the jmp instruction
* instead of a call for pthread_mutex_unlock() and therefore CPU could not * instead of a call for pthread_mutex_unlock() and therefore CPU could not
* return to current DSO's code section, which may be unloaded immediately * return to current DSO's code section, which may be unloaded immediately
@ -1168,7 +1164,7 @@ __cold void mdbx_rthc_global_dtor(void) {
#if !defined(_WIN32) && !defined(_WIN64) #if !defined(_WIN32) && !defined(_WIN64)
char *rthc = (char *)pthread_getspecific(rthc_key); char *rthc = (char *)pthread_getspecific(rthc_key);
mdbx_trace("== thread 0x%" PRIxPTR ", rthc %p, pid %d, self-status %d", mdbx_trace("== thread 0x%" PRIxPTR ", rthc %p, pid %d, self-status %d",
(uintptr_t)mdbx_thread_self(), __Wpedantic_format_voidptr(rthc), mdbx_thread_self(), __Wpedantic_format_voidptr(rthc),
mdbx_getpid(), rthc ? *rthc : -1); mdbx_getpid(), rthc ? *rthc : -1);
if (rthc) { if (rthc) {
const char self_registration = *(char *)rthc; const char self_registration = *(char *)rthc;
@ -1205,9 +1201,9 @@ __cold void mdbx_rthc_global_dtor(void) {
thread_key_delete(key); thread_key_delete(key);
for (MDBX_reader *rthc = rthc_table[i].begin; rthc < rthc_table[i].end; for (MDBX_reader *rthc = rthc_table[i].begin; rthc < rthc_table[i].end;
++rthc) { ++rthc) {
mdbx_trace("== [%i] = key %zu, %p ... %p, rthc %p (%+i), " mdbx_trace("== [%i] = key %" PRIuPTR ", %p ... %p, rthc %p (%+i), "
"rthc-pid %i, current-pid %i", "rthc-pid %i, current-pid %i",
i, (size_t)key, i, (uintptr_t)key,
__Wpedantic_format_voidptr(rthc_table[i].begin), __Wpedantic_format_voidptr(rthc_table[i].begin),
__Wpedantic_format_voidptr(rthc_table[i].end), __Wpedantic_format_voidptr(rthc_table[i].end),
__Wpedantic_format_voidptr(rthc), __Wpedantic_format_voidptr(rthc),
@ -1251,8 +1247,8 @@ __cold int mdbx_rthc_alloc(mdbx_thread_key_t *key, MDBX_reader *begin,
rthc_lock(); rthc_lock();
const mdbx_thread_key_t new_key = key ? *key : 0; const mdbx_thread_key_t new_key = key ? *key : 0;
mdbx_trace(">> key %zu, rthc_count %u, rthc_limit %u", (size_t)new_key, mdbx_trace(">> key %" PRIuPTR ", rthc_count %u, rthc_limit %u",
rthc_count, rthc_limit); (uintptr_t)new_key, rthc_count, rthc_limit);
if (rthc_count == rthc_limit) { if (rthc_count == rthc_limit) {
rthc_entry_t *new_table = rthc_entry_t *new_table =
mdbx_realloc((rthc_table == rthc_table_static) ? nullptr : rthc_table, mdbx_realloc((rthc_table == rthc_table_static) ? nullptr : rthc_table,
@ -1266,16 +1262,16 @@ __cold int mdbx_rthc_alloc(mdbx_thread_key_t *key, MDBX_reader *begin,
rthc_table = new_table; rthc_table = new_table;
rthc_limit *= 2; rthc_limit *= 2;
} }
mdbx_trace("== [%i] = key %zu, %p ... %p", rthc_count, (size_t)new_key, mdbx_trace("== [%i] = key %" PRIuPTR ", %p ... %p", rthc_count,
__Wpedantic_format_voidptr(begin), (uintptr_t)new_key, __Wpedantic_format_voidptr(begin),
__Wpedantic_format_voidptr(end)); __Wpedantic_format_voidptr(end));
rthc_table[rthc_count].key_valid = key ? true : false; rthc_table[rthc_count].key_valid = key ? true : false;
rthc_table[rthc_count].thr_tls_key = key ? new_key : 0; rthc_table[rthc_count].thr_tls_key = key ? new_key : 0;
rthc_table[rthc_count].begin = begin; rthc_table[rthc_count].begin = begin;
rthc_table[rthc_count].end = end; rthc_table[rthc_count].end = end;
++rthc_count; ++rthc_count;
mdbx_trace("<< key %zu, rthc_count %u, rthc_limit %u", (size_t)new_key, mdbx_trace("<< key %" PRIuPTR ", rthc_count %u, rthc_limit %u",
rthc_count, rthc_limit); (uintptr_t)new_key, rthc_count, rthc_limit);
rthc_unlock(); rthc_unlock();
return MDBX_SUCCESS; return MDBX_SUCCESS;
@ -1324,7 +1320,7 @@ __cold void mdbx_rthc_remove(const mdbx_thread_key_t key) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#define RTHC_ENVLIST_END ((MDBX_env *)((size_t)50459)) #define RTHC_ENVLIST_END ((MDBX_env *)((uintptr_t)50459))
static MDBX_env *inprocess_lcklist_head = RTHC_ENVLIST_END; static MDBX_env *inprocess_lcklist_head = RTHC_ENVLIST_END;
static __inline void lcklist_lock(void) { static __inline void lcklist_lock(void) {
@ -1378,8 +1374,8 @@ static int uniq_peek(const mdbx_mmap_t *pending, mdbx_mmap_t *scan) {
static int uniq_poke(const mdbx_mmap_t *pending, mdbx_mmap_t *scan, static int uniq_poke(const mdbx_mmap_t *pending, mdbx_mmap_t *scan,
uint64_t *abra) { uint64_t *abra) {
if (*abra == 0) { if (*abra == 0) {
const size_t tid = mdbx_thread_self(); const uintptr_t tid = mdbx_thread_self();
size_t uit = 0; uintptr_t uit = 0;
memcpy(&uit, &tid, (sizeof(tid) < sizeof(uit)) ? sizeof(tid) : sizeof(uit)); memcpy(&uit, &tid, (sizeof(tid) < sizeof(uit)) ? sizeof(tid) : sizeof(uit));
*abra = *abra =
rrxmrrxmsx_0(mdbx_osal_monotime() + UINT64_C(5873865991930747) * uit); rrxmrrxmsx_0(mdbx_osal_monotime() + UINT64_C(5873865991930747) * uit);
@ -5827,12 +5823,12 @@ static int mdbx_txn_renew0(MDBX_txn *txn, unsigned flags) {
mdbx_assert(env, (flags & ~(MDBX_TXN_BEGIN_FLAGS | MDBX_TXN_SPILLS | mdbx_assert(env, (flags & ~(MDBX_TXN_BEGIN_FLAGS | MDBX_TXN_SPILLS |
MDBX_WRITEMAP)) == 0); MDBX_WRITEMAP)) == 0);
const size_t tid = mdbx_thread_self(); const uintptr_t tid = mdbx_thread_self();
if (flags & MDBX_RDONLY) { if (flags & MDBX_RDONLY) {
txn->mt_flags = txn->mt_flags =
MDBX_RDONLY | (env->me_flags & (MDBX_NOTLS | MDBX_WRITEMAP)); MDBX_RDONLY | (env->me_flags & (MDBX_NOTLS | MDBX_WRITEMAP));
MDBX_reader *r = txn->to.reader; MDBX_reader *r = txn->to.reader;
STATIC_ASSERT(sizeof(size_t) == sizeof(r->mr_tid)); STATIC_ASSERT(sizeof(uintptr_t) == sizeof(r->mr_tid));
if (likely(env->me_flags & MDBX_ENV_TXKEY)) { if (likely(env->me_flags & MDBX_ENV_TXKEY)) {
mdbx_assert(env, !(env->me_flags & MDBX_NOTLS)); mdbx_assert(env, !(env->me_flags & MDBX_NOTLS));
r = thread_rthc_get(env->me_txkey); r = thread_rthc_get(env->me_txkey);

View File

@ -243,13 +243,13 @@ static int suspend_and_append(mdbx_handle_array_t **array,
MDBX_INTERNAL_FUNC int MDBX_INTERNAL_FUNC int
mdbx_suspend_threads_before_remap(MDBX_env *env, mdbx_handle_array_t **array) { mdbx_suspend_threads_before_remap(MDBX_env *env, mdbx_handle_array_t **array) {
const size_t CurrentTid = GetCurrentThreadId(); const uintptr_t CurrentTid = GetCurrentThreadId();
int rc; int rc;
if (env->me_lck) { if (env->me_lck) {
/* Scan LCK for threads of the current process */ /* Scan LCK for threads of the current process */
const MDBX_reader *const begin = env->me_lck->mti_readers; const MDBX_reader *const begin = env->me_lck->mti_readers;
const MDBX_reader *const end = begin + env->me_lck->mti_numreaders; const MDBX_reader *const end = begin + env->me_lck->mti_numreaders;
const size_t WriteTxnOwner = env->me_txn0 ? env->me_txn0->mt_owner : 0; const uintptr_t WriteTxnOwner = env->me_txn0 ? env->me_txn0->mt_owner : 0;
for (const MDBX_reader *reader = begin; reader < end; ++reader) { for (const MDBX_reader *reader = begin; reader < end; ++reader) {
if (reader->mr_pid != env->me_pid || !reader->mr_tid) { if (reader->mr_pid != env->me_pid || !reader->mr_tid) {
skip_lck: skip_lck:

View File

@ -632,15 +632,15 @@ static __maybe_unused __inline uint32_t mdbx_getpid(void) {
#endif #endif
} }
static __maybe_unused __inline size_t mdbx_thread_self(void) { static __maybe_unused __inline uintptr_t mdbx_thread_self(void) {
mdbx_tid_t thunk; mdbx_tid_t thunk;
STATIC_ASSERT(sizeof(size_t) >= sizeof(thunk)); STATIC_ASSERT(sizeof(uintptr_t) >= sizeof(thunk));
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
thunk = GetCurrentThreadId(); thunk = GetCurrentThreadId();
#else #else
thunk = pthread_self(); thunk = pthread_self();
#endif #endif
return (size_t)thunk; return (uintptr_t)thunk;
} }
MDBX_INTERNAL_FUNC void __maybe_unused mdbx_osal_jitter(bool tiny); MDBX_INTERNAL_FUNC void __maybe_unused mdbx_osal_jitter(bool tiny);

View File

@ -77,8 +77,8 @@ static int reader_list_func(void *ctx, int num, int slot, mdbx_pid_t pid,
"pid", (int)sizeof(size_t) * 2, "thread", "txnid", "lag", "used", "pid", (int)sizeof(size_t) * 2, "thread", "txnid", "lag", "used",
"retained"); "retained");
printf(" %3d)\t[%d]\t%6" PRIdSIZE " %*" PRIxSIZE, num, slot, (size_t)pid, printf(" %3d)\t[%d]\t%6" PRIdSIZE " %*" PRIxPTR, num, slot, (size_t)pid,
(int)sizeof(size_t) * 2, (size_t)thread); (int)sizeof(size_t) * 2, (uintptr_t)thread);
if (txnid) if (txnid)
printf(" %20" PRIu64 " %10" PRIu64 " %12.1fM %12.1fM\n", txnid, lag, printf(" %20" PRIu64 " %10" PRIu64 " %12.1fM %12.1fM\n", txnid, lag,
bytes_used / 1048576.0, bytes_retained / 1048576.0); bytes_used / 1048576.0, bytes_retained / 1048576.0);