From c9d471091f40ad6cc0e73cd142c7f6bb99a2ead4 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Mon, 25 Apr 2022 13:30:27 +0200 Subject: [PATCH] Use mallinfo2() if available mallinfo() is deprecated because it uses `int` for the members of the returned struct, whereas mallinfo2() uses `size_t`. It's available since glibc 2.33. --- configure.ac | 2 +- scripts/malloc_speed.c | 13 ++++++++----- src/libcharon/plugins/stroke/stroke_list.c | 13 +++++++++---- src/libcharon/plugins/vici/vici_query.c | 15 ++++++++++++--- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/configure.ac b/configure.ac index 37a0a47d5e..69f32b807f 100644 --- a/configure.ac +++ b/configure.ac @@ -668,7 +668,7 @@ AC_CHECK_FUNC( ] ) -AC_CHECK_FUNCS(prctl mallinfo getpass closefrom getpwnam_r getgrnam_r getpwuid_r chown) +AC_CHECK_FUNCS(prctl mallinfo mallinfo2 getpass closefrom getpwnam_r getgrnam_r getpwuid_r chown) AC_CHECK_FUNCS(fmemopen funopen mmap memrchr setlinebuf strptime dirfd sigwaitinfo explicit_bzero) AC_CHECK_FUNC([syslog], [ diff --git a/scripts/malloc_speed.c b/scripts/malloc_speed.c index 2038098db8..c86ea3c396 100644 --- a/scripts/malloc_speed.c +++ b/scripts/malloc_speed.c @@ -18,9 +18,9 @@ #include #include -#ifdef HAVE_MALLINFO +#if defined(HAVE_MALLINFO2) || defined (HAVE_MALLINFO) #include -#endif /* HAVE_MALLINFO */ +#endif static void start_timing(struct timespec *start) { @@ -38,12 +38,15 @@ static double end_timing(struct timespec *start) static void print_mallinfo() { -#ifdef HAVE_MALLINFO +#ifdef HAVE_MALLINFO2 + struct mallinfo2 mi = mallinfo2(); + printf("malloc: sbrk %zu, mmap %zu, used %zu, free %zu\n", + mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks); +#elif defined(HAVE_MALLINFO) struct mallinfo mi = mallinfo(); - printf("malloc: sbrk %d, mmap %d, used %d, free %d\n", mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks); -#endif /* HAVE_MALLINFO */ +#endif } #define ALLOCS 1024 diff --git a/src/libcharon/plugins/stroke/stroke_list.c b/src/libcharon/plugins/stroke/stroke_list.c index 1c44fefbaf..7fb3c67541 100644 --- a/src/libcharon/plugins/stroke/stroke_list.c +++ b/src/libcharon/plugins/stroke/stroke_list.c @@ -22,9 +22,9 @@ #include #include -#ifdef HAVE_MALLINFO +#if defined(HAVE_MALLINFO2) || defined (HAVE_MALLINFO) #include -#endif /* HAVE_MALLINFO */ +#endif #include #include @@ -490,14 +490,19 @@ METHOD(stroke_list_t, status, void, } fprintf(out, "):\n uptime: %V, since %T\n", &now, &this->uptime, &since, FALSE); -#ifdef HAVE_MALLINFO { +#ifdef HAVE_MALLINFO2 + struct mallinfo2 mi = mallinfo2(); + + fprintf(out, " malloc: sbrk %zu, mmap %zu, used %zu, free %zu\n", + mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks); +#elif defined (HAVE_MALLINFO) struct mallinfo mi = mallinfo(); fprintf(out, " malloc: sbrk %u, mmap %u, used %u, free %u\n", mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks); +#endif } -#endif /* HAVE_MALLINFO */ fprintf(out, " worker threads: %d of %d idle, ", lib->processor->get_idle_threads(lib->processor), lib->processor->get_total_threads(lib->processor)); diff --git a/src/libcharon/plugins/vici/vici_query.c b/src/libcharon/plugins/vici/vici_query.c index 1b7778de83..c35f4e1a99 100644 --- a/src/libcharon/plugins/vici/vici_query.c +++ b/src/libcharon/plugins/vici/vici_query.c @@ -48,7 +48,7 @@ #ifndef WIN32 #include #endif -#ifdef HAVE_MALLINFO +#if defined(HAVE_MALLINFO2) || defined (HAVE_MALLINFO) #include #endif @@ -1697,8 +1697,17 @@ CALLBACK(stats, vici_message_t*, } #endif -#ifdef HAVE_MALLINFO { +#ifdef HAVE_MALLINFO2 + struct mallinfo2 mi = mallinfo2(); + + b->begin_section(b, "mallinfo"); + b->add_kv(b, "sbrk", "%zu", mi.arena); + b->add_kv(b, "mmap", "%zu", mi.hblkhd); + b->add_kv(b, "used", "%zu", mi.uordblks); + b->add_kv(b, "free", "%zu", mi.fordblks); + b->end_section(b); +#elif defined(HAVE_MALLINFO) struct mallinfo mi = mallinfo(); b->begin_section(b, "mallinfo"); @@ -1707,8 +1716,8 @@ CALLBACK(stats, vici_message_t*, b->add_kv(b, "used", "%u", mi.uordblks); b->add_kv(b, "free", "%u", mi.fordblks); b->end_section(b); +#endif /* HAVE_MALLINFO(2) */ } -#endif /* HAVE_MALLINFO */ return b->finalize(b); } -- 2.47.2