From bf0cff9a8a7ba0a7595845a002fcf52b64011e32 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Tue, 7 Apr 2020 03:21:32 +0300 Subject: [PATCH] mdbx: add & use floor_powerof2(). Change-Id: Ib5be7d5a68dc7944e208d75094050383ccdfa6d0 --- src/elements/core.c | 7 ++++--- src/elements/internals.h | 9 +++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/elements/core.c b/src/elements/core.c index 9af8e7a6..da8e002e 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -5349,7 +5349,7 @@ __hot static void mdbx_page_copy(MDBX_page *dst, MDBX_page *src, size_t psize) { * alignment so memcpy may copy words instead of bytes. */ if (unused >= MDBX_CACHELINE_SIZE * 2) { lower = ceil_powerof2(lower + PAGEHDRSZ, sizeof(void *)); - upper = (upper + PAGEHDRSZ) & ~(sizeof(void *) - 1); + upper = floor_powerof2(upper + PAGEHDRSZ, sizeof(void *)); memcpy(dst, src, lower); dst = (void *)((char *)dst + upper); src = (void *)((char *)src + upper); @@ -8478,7 +8478,8 @@ static int mdbx_sync_locked(MDBX_env *env, unsigned flags, mdbx_assert(env, ((flags ^ env->me_flags) & MDBX_WRITEMAP) == 0); MDBX_meta *const recent_steady_meta = mdbx_meta_steady(env); if (flags & MDBX_WRITEMAP) { - const size_t begin = pgno2bytes(env, NUM_METAS) & ~(env->me_os_psize - 1); + const size_t begin = + floor_powerof2(pgno2bytes(env, NUM_METAS), env->me_os_psize); const size_t end = pgno_align2os_bytes(env, pending->mm_geo.next); if (end > begin) { rc = mdbx_msync(&env->me_dxb_mmap, begin, end - begin, @@ -9430,7 +9431,7 @@ static int __cold mdbx_setup_dxb(MDBX_env *env, const int lck_rc) { head->mm_datasync_sign = MDBX_DATASIGN_WEAK; head->mm_txnid_b.inconsistent = undo_txnid; const size_t offset = (uint8_t *)data_page(head) - env->me_dxb_mmap.dxb; - const size_t paged_offset = offset & ~(env->me_os_psize - 1); + const size_t paged_offset = floor_powerof2(offset, env->me_os_psize); const size_t paged_length = ceil_powerof2( env->me_psize + offset - paged_offset, env->me_os_psize); err = mdbx_msync(&env->me_dxb_mmap, paged_offset, paged_length, false); diff --git a/src/elements/internals.h b/src/elements/internals.h index 4b87b6a8..1946476f 100644 --- a/src/elements/internals.h +++ b/src/elements/internals.h @@ -1325,7 +1325,12 @@ is_powerof2(size_t x) { } static __pure_function __always_inline __maybe_unused size_t -ceil_powerof2(size_t value, size_t granularity) { +floor_powerof2(size_t value, size_t granularity) { assert(is_powerof2(granularity)); - return (value + granularity - 1) & ~(granularity - 1); + return value & ~(granularity - 1); +} + +static __pure_function __always_inline __maybe_unused size_t +ceil_powerof2(size_t value, size_t granularity) { + return floor_powerof2(value + granularity - 1, granularity); }