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
+40
View File
@@ -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
-24
View File
@@ -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