mdbx: use malloc_usable_size() and analogues.

Change-Id: I35b3fe0f1a1c884ba2013c9a861057b38f6a2174
This commit is contained in:
Leonid Yuriev 2019-10-10 14:36:57 +03:00
parent e519b8f315
commit a6aad9c918
2 changed files with 33 additions and 21 deletions

View File

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

View File

@ -85,6 +85,10 @@
#endif
#endif /* !xBSD */
#if defined(__APPLE__) || defined(__MACH__) || __has_include(<malloc/malloc.h>)
#include <malloc/malloc.h>
#endif /* MacOS */
#if defined(__linux__) || defined(__gnu_linux__)
#include <sys/sendfile.h>
#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(<sys/stat.h>)