mallinfo-util: assume mallinfo() exists

The check existed for musl. Let's remove it, as we explicitly request glibc.

While removing the check, this also drops generic_mallinfo, introduces
a tiny converter from struct mallinfo to struct mallinfo2 if mallinfo2()
does not exist, and renames mallinfo-util.h to malloc.h.

With this change, we can drop many ifdefs and casts in .c files.
This commit is contained in:
Yu Watanabe
2025-06-18 10:45:27 +02:00
committed by Lennart Poettering
parent 17f2b40f22
commit abb99d3168
5 changed files with 50 additions and 54 deletions
+5 -15
View File
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <malloc.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
@@ -21,7 +22,6 @@
#include "list.h"
#include "log.h"
#include "logarithm.h"
#include "mallinfo-util.h"
#include "memory-util.h"
#include "missing_magic.h"
#include "missing_wait.h"
@@ -1868,9 +1868,7 @@ _public_ int sd_event_trim_memory(void) {
log_debug("Memory pressure event, trimming malloc() memory.");
#if HAVE_GENERIC_MALLINFO
generic_mallinfo before_mallinfo = generic_mallinfo_get();
#endif
struct mallinfo2 before_mallinfo = mallinfo2();
usec_t before_timestamp = now(CLOCK_MONOTONIC);
hashmap_trim_pools();
@@ -1884,10 +1882,9 @@ _public_ int sd_event_trim_memory(void) {
usec_t period = after_timestamp - before_timestamp;
#if HAVE_GENERIC_MALLINFO
generic_mallinfo after_mallinfo = generic_mallinfo_get();
size_t l = LESS_BY((size_t) before_mallinfo.hblkhd, (size_t) after_mallinfo.hblkhd) +
LESS_BY((size_t) before_mallinfo.arena, (size_t) after_mallinfo.arena);
struct mallinfo2 after_mallinfo = mallinfo2();
size_t l = LESS_BY(before_mallinfo.hblkhd, after_mallinfo.hblkhd) +
LESS_BY(before_mallinfo.arena, after_mallinfo.arena);
log_struct(LOG_DEBUG,
LOG_MESSAGE("Memory trimming took %s, returned %s to OS.",
FORMAT_TIMESPAN(period, 0),
@@ -1895,13 +1892,6 @@ _public_ int sd_event_trim_memory(void) {
LOG_MESSAGE_ID(SD_MESSAGE_MEMORY_TRIM_STR),
LOG_ITEM("TRIMMED_BYTES=%zu", l),
LOG_ITEM("TRIMMED_USEC=" USEC_FMT, period));
#else
log_struct(LOG_DEBUG,
LOG_MESSAGE("Memory trimming took %s.",
FORMAT_TIMESPAN(period, 0)),
LOG_MESSAGE_ID(SD_MESSAGE_MEMORY_TRIM_STR),
LOG_ITEM("TRIMMED_USEC=" USEC_FMT, period));
#endif
return 0;
}