From a45dacc57be53d6eae523f826bc2e3bb5657a6be Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 12 May 2021 20:07:15 -0400 Subject: [PATCH] add support for glibc mallinfo2 --- h_malloc.c | 17 ++++++++++++++--- include/h_malloc.h | 1 + test/Makefile | 1 + test/mallinfo2.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 test/mallinfo2.c diff --git a/h_malloc.c b/h_malloc.c index a091791..83f9732 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -1808,12 +1808,23 @@ EXPORT int h_malloc_trim(UNUSED size_t pad) { EXPORT void h_malloc_stats(void) {} -#if defined(__GLIBC__) || defined(__ANDROID__) +// glibc mallinfo is broken and replaced with mallinfo2 +#if defined(__GLIBC__) +EXPORT struct mallinfo h_mallinfo(void) { + return (struct mallinfo){0}; +} +#endif + +#if (defined(__GLIBC__) && __GLIBC_PREREQ(2, 33)) || defined(__ANDROID__) +#ifndef __GLIBC__ EXPORT struct mallinfo h_mallinfo(void) { struct mallinfo info = {0}; +#else +EXPORT struct mallinfo2 h_mallinfo2(void) { + struct mallinfo2 info = {0}; +#endif - // glibc mallinfo type definition and implementation are both broken -#if CONFIG_STATS && !defined(__GLIBC__) +#if CONFIG_STATS if (!is_init()) { return info; } diff --git a/include/h_malloc.h b/include/h_malloc.h index af2fa76..e3f58de 100644 --- a/include/h_malloc.h +++ b/include/h_malloc.h @@ -23,6 +23,7 @@ extern "C" { #define h_malloc_trim malloc_trim #define h_malloc_stats malloc_stats #define h_mallinfo mallinfo +#define h_mallinfo2 mallinfo2 #define h_malloc_info malloc_info #define h_memalign memalign diff --git a/test/Makefile b/test/Makefile index 027cb97..9e16557 100644 --- a/test/Makefile +++ b/test/Makefile @@ -14,6 +14,7 @@ CPPFLAGS += \ EXECUTABLES := \ offset \ mallinfo \ + mallinfo2 \ malloc_info \ large_array_growth diff --git a/test/mallinfo2.c b/test/mallinfo2.c new file mode 100644 index 0000000..0f486d5 --- /dev/null +++ b/test/mallinfo2.c @@ -0,0 +1,39 @@ +#include + +#include + +#include "test_util.h" + +static void print_mallinfo2(void) { + struct mallinfo2 info = mallinfo2(); + printf("mallinfo2:\n"); + printf("arena: %zu\n", (size_t)info.arena); + printf("ordblks: %zu\n", (size_t)info.ordblks); + printf("smblks: %zu\n", (size_t)info.smblks); + printf("hblks: %zu\n", (size_t)info.hblks); + printf("hblkhd: %zu\n", (size_t)info.hblkhd); + printf("usmblks: %zu\n", (size_t)info.usmblks); + printf("fsmblks: %zu\n", (size_t)info.fsmblks); + printf("uordblks: %zu\n", (size_t)info.uordblks); + printf("fordblks: %zu\n", (size_t)info.fordblks); + printf("keepcost: %zu\n", (size_t)info.keepcost); +} + +OPTNONE int main(void) { + void *a[4]; + + a[0] = malloc(1024 * 1024 * 1024); + a[1] = malloc(16); + a[2] = malloc(32); + a[3] = malloc(64); + + print_mallinfo2(); + + free(a[0]); + free(a[1]); + free(a[2]); + free(a[3]); + + printf("\n"); + print_mallinfo2(); +}