]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mallinfo-util: assume mallinfo() exists
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 9 Jun 2025 15:39:38 +0000 (00:39 +0900)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Jun 2025 08:45:27 +0000 (10:45 +0200)
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.

meson.build
src/basic/include/malloc.h [new file with mode: 0644]
src/basic/mallinfo-util.h [deleted file]
src/libsystemd/sd-event/sd-event.c
src/shared/selinux-util.c

index c663660539879ee9ae8e66e7ed5f79bad7460c8c..12e787b552f2c39779d1c7693cd67fe420a99eeb 100644 (file)
@@ -588,7 +588,6 @@ foreach ident : [
         ['get_mempolicy',     '''#include <sys/syscall.h>'''],  # declared at numaif.h provided by libnuma, which we do not use
         ['posix_getdents',    '''#include <dirent.h>'''],       # glibc does not implement it, but musl does
         ['strerrorname_np',   '''#include <string.h>'''],       # since glibc-2.32
-        ['mallinfo',          '''#include <malloc.h>'''],       # deprecated since glibc-2.33, but check it for musl
         ['mallinfo2',         '''#include <malloc.h>'''],       # since glibc-2.33
         ['execveat',          '''#include <unistd.h>'''],       # since glibc-2.34
         ['close_range',       '''#include <unistd.h>'''],       # since glibc-2.34
diff --git a/src/basic/include/malloc.h b/src/basic/include/malloc.h
new file mode 100644 (file)
index 0000000..9605180
--- /dev/null
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include_next <malloc.h>
+
+#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 (file)
index 7fa9dd5..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-#include <malloc.h>
-
-#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
index 6d8a207b5d4b17b34532525b236c4e228df9f6d7..9f72a1bfa6392b7c99c8a851dc8c34eb2e73756b 100644 (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;
 }
index 6a4f502fc21a6d6bc0010788985c38219b40f8c8..baf5dc5c4a4296b962a42bda4b948ca25305be1c 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <fcntl.h>
+#include <malloc.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
@@ -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 */