mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 08:25:20 +09:00
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:
committed by
Lennart Poettering
parent
17f2b40f22
commit
abb99d3168
@@ -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
|
||||
|
||||
40
src/basic/include/malloc.h
Normal file
40
src/basic/include/malloc.h
Normal 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
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user