From: Yu Watanabe Date: Mon, 9 Jun 2025 15:39:38 +0000 (+0900) Subject: mallinfo-util: assume mallinfo() exists X-Git-Tag: v258-rc1~294 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=abb99d3168259136187f81e0f701cda712ffd78d;p=thirdparty%2Fsystemd.git 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. --- diff --git a/meson.build b/meson.build index c6636605398..12e787b552f 100644 --- a/meson.build +++ b/meson.build @@ -588,7 +588,6 @@ foreach ident : [ ['get_mempolicy', '''#include '''], # declared at numaif.h provided by libnuma, which we do not use ['posix_getdents', '''#include '''], # glibc does not implement it, but musl does ['strerrorname_np', '''#include '''], # since glibc-2.32 - ['mallinfo', '''#include '''], # deprecated since glibc-2.33, but check it for musl ['mallinfo2', '''#include '''], # since glibc-2.33 ['execveat', '''#include '''], # since glibc-2.34 ['close_range', '''#include '''], # since glibc-2.34 diff --git a/src/basic/include/malloc.h b/src/basic/include/malloc.h new file mode 100644 index 00000000000..96051805438 --- /dev/null +++ b/src/basic/include/malloc.h @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include_next + +#include "macro.h" + +#if !HAVE_MALLINFO2 +struct mallinfo2 { + size_t arena; /* non-mmapped space allocated from system */ + size_t ordblks; /* number of free chunks */ + size_t smblks; /* number of fastbin blocks */ + size_t hblks; /* number of mmapped regions */ + size_t hblkhd; /* space in mmapped regions */ + size_t usmblks; /* always 0, preserved for backwards compatibility */ + size_t fsmblks; /* space available in freed fastbin blocks */ + size_t uordblks; /* total allocated space */ + size_t fordblks; /* total free space */ + size_t keepcost; /* top-most, releasable (via malloc_trim) space */ +}; + +static inline struct mallinfo2 mallinfo2(void) { +DISABLE_WARNING_DEPRECATED_DECLARATIONS + struct mallinfo m = mallinfo(); +REENABLE_WARNING + + return (struct mallinfo2) { + .arena = m.arena, + .ordblks = m.ordblks, + .smblks = m.smblks, + .hblks = m.hblks, + .hblkhd = m.hblkhd, + .usmblks = 0, + .fsmblks = m.fsmblks, + .uordblks = m.uordblks, + .fordblks = m.fordblks, + .keepcost = m.keepcost, + }; +} +#endif diff --git a/src/basic/mallinfo-util.h b/src/basic/mallinfo-util.h deleted file mode 100644 index 7fa9dd5ecce..00000000000 --- a/src/basic/mallinfo-util.h +++ /dev/null @@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -#pragma once - -#include - -#if HAVE_MALLINFO2 -# define HAVE_GENERIC_MALLINFO 1 -typedef struct mallinfo2 generic_mallinfo; -static inline generic_mallinfo generic_mallinfo_get(void) { - return mallinfo2(); -} -#elif HAVE_MALLINFO -# define HAVE_GENERIC_MALLINFO 1 -typedef struct mallinfo generic_mallinfo; -static inline generic_mallinfo generic_mallinfo_get(void) { - /* glibc has deprecated mallinfo(), let's suppress the deprecation warning if mallinfo2() doesn't - * exist yet. */ -DISABLE_WARNING_DEPRECATED_DECLARATIONS - return mallinfo(); -REENABLE_WARNING -} -#else -# define HAVE_GENERIC_MALLINFO 0 -#endif diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 6d8a207b5d4..9f72a1bfa63 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include #include #include @@ -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; } diff --git a/src/shared/selinux-util.c b/src/shared/selinux-util.c index 6a4f502fc21..baf5dc5c4a4 100644 --- a/src/shared/selinux-util.c +++ b/src/shared/selinux-util.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include +#include #include #include #include @@ -20,7 +21,6 @@ #include "label.h" #include "label-util.h" #include "log.h" -#include "mallinfo-util.h" #include "path-util.h" #include "selinux-util.h" #include "string-util.h" @@ -106,14 +106,10 @@ static int open_label_db(void) { struct selabel_handle *hnd; /* Avoid maybe-uninitialized false positives */ usec_t before_timestamp = USEC_INFINITY, after_timestamp = USEC_INFINITY; -# if HAVE_GENERIC_MALLINFO - generic_mallinfo before_mallinfo = {}; -# endif + struct mallinfo2 before_mallinfo = {}; if (DEBUG_LOGGING) { -# if HAVE_GENERIC_MALLINFO - before_mallinfo = generic_mallinfo_get(); -# endif + before_mallinfo = mallinfo2(); before_timestamp = now(CLOCK_MONOTONIC); } @@ -123,16 +119,11 @@ static int open_label_db(void) { if (DEBUG_LOGGING) { after_timestamp = now(CLOCK_MONOTONIC); -# if HAVE_GENERIC_MALLINFO - generic_mallinfo after_mallinfo = generic_mallinfo_get(); - size_t l = LESS_BY((size_t) after_mallinfo.uordblks, (size_t) before_mallinfo.uordblks); + struct mallinfo2 after_mallinfo = mallinfo2(); + size_t l = LESS_BY(after_mallinfo.uordblks, before_mallinfo.uordblks); log_debug("Successfully loaded SELinux database in %s, size on heap is %zuK.", FORMAT_TIMESPAN(after_timestamp - before_timestamp, 0), DIV_ROUND_UP(l, 1024)); -# else - log_debug("Successfully loaded SELinux database in %s.", - FORMAT_TIMESPAN(after_timestamp - before_timestamp, 0)); -# endif } /* release memory after measurement */