From a6aad9c918510bb7b73ac72a6863ab17e18525da Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Thu, 10 Oct 2019 14:36:57 +0300 Subject: [PATCH] mdbx: use malloc_usable_size() and analogues. Change-Id: I35b3fe0f1a1c884ba2013c9a861057b38f6a2174 --- src/elements/core.c | 42 +++++++++++++++++++++--------------------- src/elements/osal.h | 12 ++++++++++++ 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/elements/core.c b/src/elements/core.c index bde70386..a3adee6c 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -1056,12 +1056,12 @@ static __inline pgno_t bytes2pnl(const size_t bytes) { } static MDBX_PNL mdbx_pnl_alloc(size_t size) { - const size_t bytes = pnl2bytes(size); + size_t bytes = pnl2bytes(size); MDBX_PNL pl = mdbx_malloc(bytes); if (likely(pl)) { -#if __GLIBC_PREREQ(2, 12) - const size_t bytes = malloc_usable_size(pl); -#endif +#if __GLIBC_PREREQ(2, 12) || defined(__FreeBSD__) || defined(malloc_usable_size) + bytes = malloc_usable_size(pl); +#endif /* malloc_usable_size */ pl[0] = bytes2pnl(bytes); assert(pl[0] >= size); pl[1] = 0; @@ -1082,13 +1082,13 @@ static void mdbx_pnl_shrink(MDBX_PNL *ppl) { MDBX_PNL_ALLOCLEN(*ppl) >= MDBX_PNL_SIZE(*ppl)); MDBX_PNL_SIZE(*ppl) = 0; if (unlikely(MDBX_PNL_ALLOCLEN(*ppl) > - MDBX_PNL_INITIAL + MDBX_CACHELINE_SIZE / sizeof(pgno_t))) { - const size_t bytes = pnl2bytes(MDBX_PNL_INITIAL); + MDBX_PNL_INITIAL * 2 - MDBX_CACHELINE_SIZE / sizeof(pgno_t))) { + size_t bytes = pnl2bytes(MDBX_PNL_INITIAL); MDBX_PNL pl = mdbx_realloc(*ppl - 1, bytes); if (likely(pl)) { -#if __GLIBC_PREREQ(2, 12) - const size_t bytes = malloc_usable_size(pl); -#endif +#if __GLIBC_PREREQ(2, 12) || defined(__FreeBSD__) || defined(malloc_usable_size) + bytes = malloc_usable_size(pl); +#endif /* malloc_usable_size */ *pl = bytes2pnl(bytes); *ppl = pl + 1; } @@ -1109,12 +1109,12 @@ static int mdbx_pnl_reserve(MDBX_PNL *ppl, const size_t wanna) { const size_t size = (wanna + wanna - allocated < MDBX_PNL_MAX) ? wanna + wanna - allocated : MDBX_PNL_MAX; - const size_t bytes = pnl2bytes(size); + size_t bytes = pnl2bytes(size); MDBX_PNL pl = mdbx_realloc(*ppl - 1, bytes); if (likely(pl)) { -#if __GLIBC_PREREQ(2, 12) - const size_t bytes = malloc_usable_size(pl); -#endif +#if __GLIBC_PREREQ(2, 12) || defined(__FreeBSD__) || defined(malloc_usable_size) + bytes = malloc_usable_size(pl); +#endif /* malloc_usable_size */ *pl = bytes2pnl(bytes); assert(*pl >= wanna); *ppl = pl + 1; @@ -1264,12 +1264,12 @@ static __inline size_t bytes2txl(const size_t bytes) { } static MDBX_TXL mdbx_txl_alloc(void) { - const size_t bytes = txl2bytes(MDBX_TXL_INITIAL); + size_t bytes = txl2bytes(MDBX_TXL_INITIAL); MDBX_TXL tl = mdbx_malloc(bytes); if (likely(tl)) { -#if __GLIBC_PREREQ(2, 12) - const size_t bytes = malloc_usable_size(tl); -#endif +#if __GLIBC_PREREQ(2, 12) || defined(__FreeBSD__) || defined(malloc_usable_size) + bytes = malloc_usable_size(tl); +#endif /* malloc_usable_size */ tl[0] = bytes2txl(bytes); assert(tl[0] >= MDBX_TXL_INITIAL); tl[1] = 0; @@ -1296,12 +1296,12 @@ static int mdbx_txl_reserve(MDBX_TXL *ptl, const size_t wanna) { const size_t size = (wanna + wanna - allocated < MDBX_TXL_MAX) ? wanna + wanna - allocated : MDBX_TXL_MAX; - const size_t bytes = txl2bytes(size); + size_t bytes = txl2bytes(size); MDBX_TXL tl = mdbx_realloc(*ptl - 1, bytes); if (likely(tl)) { -#if __GLIBC_PREREQ(2, 12) - const size_t bytes = malloc_usable_size(tl); -#endif +#if __GLIBC_PREREQ(2, 12) || defined(__FreeBSD__) || defined(malloc_usable_size) + bytes = malloc_usable_size(tl); +#endif /* malloc_usable_size */ *tl = bytes2txl(bytes); assert(*tl >= wanna); *ptl = tl + 1; diff --git a/src/elements/osal.h b/src/elements/osal.h index 3eef0c4e..2f057808 100644 --- a/src/elements/osal.h +++ b/src/elements/osal.h @@ -85,6 +85,10 @@ #endif #endif /* !xBSD */ +#if defined(__APPLE__) || defined(__MACH__) || __has_include() +#include +#endif /* MacOS */ + #if defined(__linux__) || defined(__gnu_linux__) #include #endif /* Linux */ @@ -179,6 +183,14 @@ typedef pthread_mutex_t mdbx_fastmutex_t; #define mdbx_strdup strdup #endif /* Platform */ +#if __GLIBC_PREREQ(2, 12) || defined(__FreeBSD__) || defined(malloc_usable_size) +/* malloc_usable_size() already provided */ +#elif defined(__APPLE__) +#define malloc_usable_size(ptr) malloc_size(ptr) +#elif defined(_MSC_VER) && !MDBX_AVOID_CRT +#define malloc_usable_size(ptr) _msize(ptr) +#endif /* malloc_usable_size */ + /* *INDENT-OFF* */ /* clang-format off */ #if defined(HAVE_SYS_STAT_H) || __has_include()