diff --git a/dlmalloc-rs b/dlmalloc-rs index e4ba849349..3b0ee3b467 160000 --- a/dlmalloc-rs +++ b/dlmalloc-rs @@ -1 +1 @@ -Subproject commit e4ba8493497c72e15f2b9d428e1dea17e26bf188 +Subproject commit 3b0ee3b467401229b005df226bd32990ee7e3414 diff --git a/src/header/malloc/mod.rs b/src/header/malloc/mod.rs index 5061e2098d..54977f414b 100644 --- a/src/header/malloc/mod.rs +++ b/src/header/malloc/mod.rs @@ -40,3 +40,9 @@ pub unsafe extern "C" fn pvalloc(size: size_t) -> *mut c_void { } } } + +/// See . +#[unsafe(no_mangle)] +pub unsafe extern "C" fn malloc_usable_size(ptr: *mut c_void) -> size_t { + platform::alloc_usable_size(ptr) +} diff --git a/src/platform/allocator/mod.rs b/src/platform/allocator/mod.rs index 61c49120e4..7dddfb8adc 100644 --- a/src/platform/allocator/mod.rs +++ b/src/platform/allocator/mod.rs @@ -97,3 +97,10 @@ pub unsafe fn free(ptr: *mut c_void) { } (*ALLOCATOR.get()).lock().free(ptr.cast()) } + +pub unsafe fn alloc_usable_size(ptr: *mut c_void) -> size_t { + if ptr.is_null() { + return 0; + } + (*ALLOCATOR.get()).lock().usable_size(ptr.cast()) +} diff --git a/tests/Makefile b/tests/Makefile index 0f3489d4a8..b8d479c99c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -49,6 +49,7 @@ EXPECT_NAMES=\ locale/duplocale \ locale/newlocale \ locale/setlocale \ + malloc/usable_size \ math \ regex \ select \ diff --git a/tests/expected/bins_dynamic/malloc/usable_size.stderr b/tests/expected/bins_dynamic/malloc/usable_size.stderr new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/expected/bins_dynamic/malloc/usable_size.stdout b/tests/expected/bins_dynamic/malloc/usable_size.stdout new file mode 100644 index 0000000000..4188b67b66 --- /dev/null +++ b/tests/expected/bins_dynamic/malloc/usable_size.stdout @@ -0,0 +1,2 @@ +malloc: 27 bytes +malloc_usable_size: 48 bytes diff --git a/tests/expected/bins_static/malloc/usable_size.stderr b/tests/expected/bins_static/malloc/usable_size.stderr new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/expected/bins_static/malloc/usable_size.stdout b/tests/expected/bins_static/malloc/usable_size.stdout new file mode 100644 index 0000000000..4188b67b66 --- /dev/null +++ b/tests/expected/bins_static/malloc/usable_size.stdout @@ -0,0 +1,2 @@ +malloc: 27 bytes +malloc_usable_size: 48 bytes diff --git a/tests/malloc/usable_size.c b/tests/malloc/usable_size.c new file mode 100644 index 0000000000..933ebf03f2 --- /dev/null +++ b/tests/malloc/usable_size.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +int main(void) { + size_t request_size = 27; + char *ptr = (char *)malloc(request_size); + + if (!ptr) { + fprintf(stderr, "Allocation failed\n"); + return 1; + } + + size_t actual_size = malloc_usable_size(ptr); + + printf("malloc: %zu bytes\n", request_size); + printf("malloc_usable_size: %zu bytes\n", actual_size); + + assert(actual_size >= request_size); + memset(ptr, 'A', actual_size); + ptr[actual_size - 1] = '\0'; + + assert(ptr[0] == 'A'); + assert(ptr[actual_size - 2] == 'A'); + assert(ptr[actual_size - 1] == '\0'); + free(ptr); + + return 0; +}