From 409a6393128a48764e2fec0d1056ddc0a608d0c5 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 19 Apr 2019 16:54:43 -0400 Subject: [PATCH] provide working malloc_info outside Android too --- h_malloc.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- h_malloc.h | 2 +- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/h_malloc.c b/h_malloc.c index bdd563e..8d76aad 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -1,9 +1,11 @@ #include #include +#include #include #include #include #include +#include #if N_ARENA > 1 #include #endif @@ -1726,14 +1728,64 @@ EXPORT struct mallinfo h_mallinfo(void) { } #endif -#ifdef __GLIBC__ +#ifndef __ANDROID__ EXPORT int h_malloc_info(int options, UNUSED FILE *fp) { if (options) { errno = EINVAL; return -1; } + +#if CONFIG_STATS + fputs("", fp); + for (unsigned arena = 0; arena < N_ARENA; arena++) { + fprintf(fp, "", arena); + + for (unsigned class = 0; class < N_SIZE_CLASSES; class++) { + struct size_class *c = &ro.size_class_metadata[arena][class]; + + u64 nmalloc; + u64 ndalloc; + size_t slab_allocated; + size_t allocated; + + mutex_lock(&c->lock); + nmalloc = c->nmalloc; + ndalloc = c->ndalloc; + slab_allocated = c->slab_allocated; + allocated = c->allocated; + mutex_unlock(&c->lock); + + if (nmalloc || ndalloc || slab_allocated || allocated) { + fprintf(fp, "", class, size_classes[class]); + fprintf(fp, "%" PRIu64 "", nmalloc); + fprintf(fp, "%" PRIu64 "", ndalloc); + fprintf(fp, "%zu", slab_allocated); + fprintf(fp, "%zu", allocated); + fputs("", fp); + } + } + + fputs("", fp); + } + + size_t region_allocated; + + struct region_allocator *ra = ro.region_allocator; + mutex_lock(&ra->lock); + region_allocated = ra->allocated; + mutex_unlock(&ra->lock); + + fprintf(fp, "", N_ARENA); + fprintf(fp, "%zu", region_allocated); + fputs("", fp); + + fputs("", fp); + + return 0; +#else errno = ENOSYS; return -1; +#endif } COLD EXPORT void *h_malloc_get_state(void) { diff --git a/h_malloc.h b/h_malloc.h index 4cf7b91..cd3cb80 100644 --- a/h_malloc.h +++ b/h_malloc.h @@ -63,7 +63,7 @@ void h_malloc_stats(void); #if defined(__GLIBC__) || defined(__ANDROID__) struct mallinfo h_mallinfo(void); #endif -#ifdef __GLIBC__ +#ifndef __ANDROID__ int h_malloc_info(int options, FILE *fp); #endif