summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-06-12 16:52:57 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-06-29 16:51:52 +0200
commitb01f31954f1c7c4601925173ae2638b572224e9a (patch)
tree1eb4c844727641e5f2682539f9f127581b4b82df
parentf63d1b0efa64fe716c2855a0410ac47ad67f7dec (diff)
downloadsystemd-b01f31954f1c7c4601925173ae2638b572224e9a.tar.gz
Turn mempool_enabled() into a weak symbol
Before we had the following scheme: mempool_enabled() would check mempool_use_allowed, and libsystemd-shared would be linked with a .c file that provides mempool_use_allowed=true, while other things would linked with a different .c file with mempool_use_allowed=false. In the new scheme, mempool_enabled() itself is a weak symbol. If it's not found, we assume false. So it only needs to be provided for libsystemd-shared, where it can return false or true. test-set-disable-mempool is libshared, so it gets the symbol. But then we actually disable the mempool via envvar. mempool_enable() is called to check its return value directly.
-rw-r--r--meson.build6
-rw-r--r--src/basic/hashmap.c7
-rw-r--r--src/basic/mempool.c17
-rw-r--r--src/basic/mempool.h3
-rw-r--r--src/libsystemd/disable-mempool.c5
-rw-r--r--src/libsystemd/meson.build1
-rw-r--r--src/shared/enable-mempool.c16
-rw-r--r--src/test/test-set-disable-mempool.c7
8 files changed, 26 insertions, 36 deletions
diff --git a/meson.build b/meson.build
index 2e8b69c96a..25bcfdba1b 100644
--- a/meson.build
+++ b/meson.build
@@ -1928,7 +1928,6 @@ alias_target('devel', libsystemd_pc, libudev_pc)
libsystemd = shared_library(
'systemd',
- disable_mempool_c,
version : libsystemd_version,
include_directories : libsystemd_includes,
link_args : ['-shared',
@@ -1953,7 +1952,6 @@ install_libsystemd_static = static_library(
basic_gcrypt_sources,
basic_compress_sources,
fundamental_sources,
- disable_mempool_c,
include_directories : libsystemd_includes,
build_by_default : static_libsystemd != 'false',
install : static_libsystemd != 'false',
@@ -1975,7 +1973,6 @@ install_libsystemd_static = static_library(
libudev = shared_library(
'udev',
- disable_mempool_c,
version : libudev_version,
include_directories : includes,
link_args : ['-shared',
@@ -1997,7 +1994,6 @@ install_libudev_static = static_library(
shared_sources,
libsystemd_sources,
libudev_sources,
- disable_mempool_c,
include_directories : includes,
build_by_default : static_libudev != 'false',
install : static_libudev != 'false',
@@ -2118,7 +2114,6 @@ subdir('test')
test_dlopen = executable(
'test-dlopen',
test_dlopen_c,
- disable_mempool_c,
include_directories : includes,
link_with : [libbasic],
dependencies : [libdl],
@@ -2148,7 +2143,6 @@ foreach tuple : [['myhostname', 'ENABLE_NSS_MYHOSTNAME'],
nss = shared_library(
'nss_' + module,
sources,
- disable_mempool_c,
version : '2',
include_directories : incs,
# Note that we link NSS modules with '-z nodelete' so that mempools never get orphaned
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c
index 62380b0e4a..1fadaad996 100644
--- a/src/basic/hashmap.c
+++ b/src/basic/hashmap.c
@@ -771,16 +771,15 @@ static void shared_hash_key_initialize(void) {
static struct HashmapBase* hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
HashmapBase *h;
const struct hashmap_type_info *hi = &hashmap_type_info[type];
- bool up;
- up = mempool_enabled();
+ bool use_pool = mempool_enabled && mempool_enabled();
- h = up ? mempool_alloc0_tile(hi->mempool) : malloc0(hi->head_size);
+ h = use_pool ? mempool_alloc0_tile(hi->mempool) : malloc0(hi->head_size);
if (!h)
return NULL;
h->type = type;
- h->from_pool = up;
+ h->from_pool = use_pool;
h->hash_ops = hash_ops ?: &trivial_hash_ops;
if (type == HASHMAP_TYPE_ORDERED) {
diff --git a/src/basic/mempool.c b/src/basic/mempool.c
index 9eedc20c4f..fff23fdbac 100644
--- a/src/basic/mempool.c
+++ b/src/basic/mempool.c
@@ -3,12 +3,9 @@
#include <stdint.h>
#include <stdlib.h>
-#include "env-util.h"
#include "macro.h"
#include "memory-util.h"
#include "mempool.h"
-#include "process-util.h"
-#include "util.h"
struct pool {
struct pool *next;
@@ -73,20 +70,6 @@ void mempool_free_tile(struct mempool *mp, void *p) {
mp->freelist = p;
}
-bool mempool_enabled(void) {
- static int b = -1;
-
- if (!is_main_thread())
- return false;
-
- if (!mempool_use_allowed)
- b = false;
- if (b < 0)
- b = getenv_bool("SYSTEMD_MEMPOOL") != 0;
-
- return b;
-}
-
#if VALGRIND
void mempool_drop(struct mempool *mp) {
struct pool *p = mp->first_pool;
diff --git a/src/basic/mempool.h b/src/basic/mempool.h
index 0fe2f2789c..539ccbdf06 100644
--- a/src/basic/mempool.h
+++ b/src/basic/mempool.h
@@ -23,8 +23,7 @@ static struct mempool pool_name = { \
.at_least = alloc_at_least, \
}
-extern const bool mempool_use_allowed;
-bool mempool_enabled(void);
+__attribute__((weak)) bool mempool_enabled(void);
#if VALGRIND
void mempool_drop(struct mempool *mp);
diff --git a/src/libsystemd/disable-mempool.c b/src/libsystemd/disable-mempool.c
deleted file mode 100644
index 1baf91f05b..0000000000
--- a/src/libsystemd/disable-mempool.c
+++ /dev/null
@@ -1,5 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "mempool.h"
-
-const bool mempool_use_allowed = false;
diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build
index 2701efc8f9..055fa11ede 100644
--- a/src/libsystemd/meson.build
+++ b/src/libsystemd/meson.build
@@ -158,7 +158,6 @@ libsystemd_sources = files(
'sd-utf8/sd-utf8.c',
) + sd_journal_sources + id128_sources + sd_daemon_sources + sd_event_sources + sd_login_sources
-disable_mempool_c = files('disable-mempool.c')
libsystemd_c_args = ['-fvisibility=default']
diff --git a/src/shared/enable-mempool.c b/src/shared/enable-mempool.c
index 1abfccbd81..fd582c0e78 100644
--- a/src/shared/enable-mempool.c
+++ b/src/shared/enable-mempool.c
@@ -1,5 +1,19 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#include <stdbool.h>
+
+#include "env-util.h"
#include "mempool.h"
+#include "process-util.h"
+
+bool mempool_enabled(void) {
+ static int cache = -1;
+
+ if (!is_main_thread())
+ return false;
+
+ if (cache < 0)
+ cache = getenv_bool("SYSTEMD_MEMPOOL") != 0;
-const bool mempool_use_allowed = true;
+ return cache;
+}
diff --git a/src/test/test-set-disable-mempool.c b/src/test/test-set-disable-mempool.c
index f02e433d7b..91244b25ac 100644
--- a/src/test/test-set-disable-mempool.c
+++ b/src/test/test-set-disable-mempool.c
@@ -2,6 +2,7 @@
#include <pthread.h>
+#include "mempool.h"
#include "process-util.h"
#include "set.h"
#include "tests.h"
@@ -15,6 +16,9 @@ static void* thread(void *p) {
assert_se(*s);
assert_se(!is_main_thread());
+ assert_se(mempool_enabled);
+ assert_se(!mempool_enabled());
+
assert_se(set_size(*s) == NUM);
*s = set_free(*s);
@@ -29,7 +33,10 @@ static void test_one(const char *val) {
log_info("Testing with SYSTEMD_MEMPOOL=%s", val);
assert_se(setenv("SYSTEMD_MEMPOOL", val, true) == 0);
+
assert_se(is_main_thread());
+ assert_se(mempool_enabled); /* It is a weak symbol, but we expect it to be available */
+ assert_se(!mempool_enabled());
assert_se(s = set_new(NULL));
for (i = 0; i < NUM; i++)