add support for glibc mallinfo2

This commit is contained in:
Daniel Micay 2021-05-12 20:07:15 -04:00
parent a71ab1a2eb
commit a45dacc57b
4 changed files with 55 additions and 3 deletions

View File

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

View File

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

View File

@ -14,6 +14,7 @@ CPPFLAGS += \
EXECUTABLES := \
offset \
mallinfo \
mallinfo2 \
malloc_info \
large_array_growth

39
test/mallinfo2.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <malloc.h>
#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();
}