Darwin malloc_size override support proposal.

Darwin has similar api than Linux/FreeBSD's malloc_usable_size.
This commit is contained in:
David CARLIER 2021-02-17 20:40:11 +00:00 committed by Qi Wang
parent ab0f1604b4
commit cf9724531a
13 changed files with 53 additions and 19 deletions

View File

@ -1056,6 +1056,9 @@ AC_CHECK_FUNC([memalign],
AC_CHECK_FUNC([valloc], AC_CHECK_FUNC([valloc],
[AC_DEFINE([JEMALLOC_OVERRIDE_VALLOC], [ ]) [AC_DEFINE([JEMALLOC_OVERRIDE_VALLOC], [ ])
public_syms="${public_syms} valloc"]) public_syms="${public_syms} valloc"])
AC_CHECK_FUNC([malloc_size],
[AC_DEFINE([JEMALLOC_HAVE_MALLOC_SIZE], [ ])
public_syms="${public_syms} malloc_size"])
dnl Check for allocator-related functions that should be wrapped. dnl Check for allocator-related functions that should be wrapped.
wrap_syms= wrap_syms=

View File

@ -337,6 +337,11 @@
*/ */
#undef JEMALLOC_HAVE_MEMCNTL #undef JEMALLOC_HAVE_MEMCNTL
/*
* Defined if malloc_size is supported
*/
#undef JEMALLOC_HAVE_MALLOC_SIZE
/* Define if operating system has alloca.h header. */ /* Define if operating system has alloca.h header. */
#undef JEMALLOC_HAS_ALLOCA_H #undef JEMALLOC_HAS_ALLOCA_H

View File

@ -53,6 +53,10 @@ JEMALLOC_EXPORT void JEMALLOC_NOTHROW @je_@malloc_stats_print(
const char *opts); const char *opts);
JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW @je_@malloc_usable_size( JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW @je_@malloc_usable_size(
JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW; JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;
#ifdef JEMALLOC_HAVE_MALLOC_SIZE
JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW @je_@malloc_size(
const void *ptr);
#endif
#ifdef JEMALLOC_OVERRIDE_MEMALIGN #ifdef JEMALLOC_OVERRIDE_MEMALIGN
JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN

View File

@ -3904,18 +3904,14 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
} }
#undef STATS_PRINT_BUFSIZE #undef STATS_PRINT_BUFSIZE
JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW JEMALLOC_ALWAYS_INLINE size_t
je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) { je_malloc_usable_size_impl(JEMALLOC_USABLE_SIZE_CONST void *ptr) {
size_t ret;
tsdn_t *tsdn;
LOG("core.malloc_usable_size.entry", "ptr: %p", ptr);
assert(malloc_initialized() || IS_INITIALIZER); assert(malloc_initialized() || IS_INITIALIZER);
tsdn = tsdn_fetch(); tsdn_t *tsdn = tsdn_fetch();
check_entry_exit_locking(tsdn); check_entry_exit_locking(tsdn);
size_t ret;
if (unlikely(ptr == NULL)) { if (unlikely(ptr == NULL)) {
ret = 0; ret = 0;
} else { } else {
@ -3926,12 +3922,33 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) {
ret = isalloc(tsdn, ptr); ret = isalloc(tsdn, ptr);
} }
} }
check_entry_exit_locking(tsdn); check_entry_exit_locking(tsdn);
return ret;
}
JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW
je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) {
LOG("core.malloc_usable_size.entry", "ptr: %p", ptr);
size_t ret = je_malloc_usable_size_impl(ptr);
LOG("core.malloc_usable_size.exit", "result: %zu", ret); LOG("core.malloc_usable_size.exit", "result: %zu", ret);
return ret; return ret;
} }
#ifdef JEMALLOC_HAVE_MALLOC_SIZE
JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW
je_malloc_size(const void *ptr) {
LOG("core.malloc_size.entry", "ptr: %p", ptr);
size_t ret = je_malloc_usable_size_impl(ptr);
LOG("core.malloc_size.exit", "result: %zu", ret);
return ret;
}
#endif
static void static void
batch_alloc_prof_sample_assert(tsd_t *tsd, size_t batch, size_t usize) { batch_alloc_prof_sample_assert(tsd_t *tsd, size_t batch, size_t usize) {
assert(config_prof && opt_prof); assert(config_prof && opt_prof);

View File

@ -132,6 +132,11 @@ static const bool config_debug =
#define MEXP 19937 #define MEXP 19937
#include "test/SFMT.h" #include "test/SFMT.h"
#ifndef JEMALLOC_HAVE_MALLOC_SIZE
#define TEST_MALLOC_SIZE malloc_usable_size
#else
#define TEST_MALLOC_SIZE malloc_size
#endif
/******************************************************************************/ /******************************************************************************/
/* /*
* Define always-enabled assertion macros, so that test assertions execute even * Define always-enabled assertion macros, so that test assertions execute even

View File

@ -120,7 +120,7 @@ TEST_BEGIN(test_alignment_and_size) {
"size=%zu (%#zx): %s", "size=%zu (%#zx): %s",
alignment, size, size, buf); alignment, size, size, buf);
} }
total += malloc_usable_size(ps[i]); total += TEST_MALLOC_SIZE(ps[i]);
if (total >= (MAXALIGN << 1)) { if (total >= (MAXALIGN << 1)) {
break; break;
} }
@ -141,7 +141,7 @@ TEST_END
TEST_BEGIN(test_zero_alloc) { TEST_BEGIN(test_zero_alloc) {
void *res = aligned_alloc(8, 0); void *res = aligned_alloc(8, 0);
assert(res); assert(res);
size_t usable = malloc_usable_size(res); size_t usable = TEST_MALLOC_SIZE(res);
assert(usable > 0); assert(usable > 0);
free(res); free(res);
} }

View File

@ -70,7 +70,7 @@ thd_start(void *arg) {
expect_ptr_eq(ap0, ap1, expect_ptr_eq(ap0, ap1,
"Pointer returned by \"thread.allocatedp\" should not change"); "Pointer returned by \"thread.allocatedp\" should not change");
usize = malloc_usable_size(p); usize = TEST_MALLOC_SIZE(p);
expect_u64_le(a0 + usize, a1, expect_u64_le(a0 + usize, a1,
"Allocated memory counter should increase by at least the amount " "Allocated memory counter should increase by at least the amount "
"explicitly allocated"); "explicitly allocated");

View File

@ -3,7 +3,7 @@
TEST_BEGIN(test_zero_alloc) { TEST_BEGIN(test_zero_alloc) {
void *res = malloc(0); void *res = malloc(0);
assert(res); assert(res);
size_t usable = malloc_usable_size(res); size_t usable = TEST_MALLOC_SIZE(res);
assert(usable > 0); assert(usable > 0);
free(res); free(res);
} }

View File

@ -101,7 +101,7 @@ TEST_BEGIN(test_alignment_and_size) {
"size=%zu (%#zx): %s", "size=%zu (%#zx): %s",
alignment, size, size, buf); alignment, size, size, buf);
} }
total += malloc_usable_size(ps[i]); total += TEST_MALLOC_SIZE(ps[i]);
if (total >= (MAXALIGN << 1)) { if (total >= (MAXALIGN << 1)) {
break; break;
} }

View File

@ -185,7 +185,7 @@ TEST_BEGIN(test_align_enum) {
assert_ptr_not_null(p, assert_ptr_not_null(p,
"Unexpected mallocx() error"); "Unexpected mallocx() error");
assert_zu_eq(nallocx(1, flags), assert_zu_eq(nallocx(1, flags),
malloc_usable_size(p), TEST_MALLOC_SIZE(p),
"Wrong mallocx() usable size"); "Wrong mallocx() usable size");
int flags_next = int flags_next =
MALLOCX_LG_ALIGN(lg_align_next); MALLOCX_LG_ALIGN(lg_align_next);
@ -193,7 +193,7 @@ TEST_BEGIN(test_align_enum) {
assert_ptr_not_null(p, assert_ptr_not_null(p,
"Unexpected rallocx() error"); "Unexpected rallocx() error");
expect_zu_eq(nallocx(size, flags_next), expect_zu_eq(nallocx(size, flags_next),
malloc_usable_size(p), TEST_MALLOC_SIZE(p),
"Wrong rallocx() usable size"); "Wrong rallocx() usable size");
free(p); free(p);
} }

View File

@ -69,7 +69,7 @@ malloc_mus_free(void) {
test_fail("Unexpected malloc() failure"); test_fail("Unexpected malloc() failure");
return; return;
} }
malloc_usable_size(p); TEST_MALLOC_SIZE(p);
free(p); free(p);
} }

View File

@ -30,7 +30,7 @@ do_allocs(size_t size, bool zero, size_t lg_align) {
if (opt_junk_alloc && !zero) { \ if (opt_junk_alloc && !zero) { \
expect_ptr_eq(ptr, last_junked_ptr, ""); \ expect_ptr_eq(ptr, last_junked_ptr, ""); \
expect_zu_eq(last_junked_usize, \ expect_zu_eq(last_junked_usize, \
malloc_usable_size(ptr), ""); \ TEST_MALLOC_SIZE(ptr), ""); \
} \ } \
} while (0) } while (0)
if (!zero && lg_align == 0) { if (!zero && lg_align == 0) {

View File

@ -43,7 +43,7 @@ test_combinations(szind_t ind, size_t sizes_array[N_PTRS],
int flags = flags_array[i]; int flags = flags_array[i];
void *p = mallocx(sz, flags); void *p = mallocx(sz, flags);
assert_ptr_not_null(p, "malloc() failed"); assert_ptr_not_null(p, "malloc() failed");
assert(malloc_usable_size(p) == sz_index2size(ind)); assert(TEST_MALLOC_SIZE(p) == sz_index2size(ind));
ptrs[i] = p; ptrs[i] = p;
live_req_sum += sz; live_req_sum += sz;
live_count++; live_count++;