Skip to content

Commit

Permalink
Use mallinfo2() if available
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tobiasbrunner committed Apr 25, 2022
1 parent 8ce4105 commit c9d4710
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -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], [
Expand Down
13 changes: 8 additions & 5 deletions scripts/malloc_speed.c
Expand Up @@ -18,9 +18,9 @@
#include <library.h>
#include <utils/debug.h>

#ifdef HAVE_MALLINFO
#if defined(HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
#include <malloc.h>
#endif /* HAVE_MALLINFO */
#endif

static void start_timing(struct timespec *start)
{
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/libcharon/plugins/stroke/stroke_list.c
Expand Up @@ -22,9 +22,9 @@
#include <time.h>
#include <sys/utsname.h>

#ifdef HAVE_MALLINFO
#if defined(HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
#include <malloc.h>
#endif /* HAVE_MALLINFO */
#endif

#include <daemon.h>
#include <collections/linked_list.h>
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 12 additions & 3 deletions src/libcharon/plugins/vici/vici_query.c
Expand Up @@ -48,7 +48,7 @@
#ifndef WIN32
#include <sys/utsname.h>
#endif
#ifdef HAVE_MALLINFO
#if defined(HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
#include <malloc.h>
#endif

Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
Expand Down

0 comments on commit c9d4710

Please sign in to comment.