]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Turn mempool_enabled() into a weak symbol
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 12 Jun 2022 14:52:57 +0000 (16:52 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 29 Jun 2022 14:51:52 +0000 (16:51 +0200)
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.

meson.build
src/basic/hashmap.c
src/basic/mempool.c
src/basic/mempool.h
src/libsystemd/disable-mempool.c [deleted file]
src/libsystemd/meson.build
src/shared/enable-mempool.c
src/test/test-set-disable-mempool.c

index 2e8b69c96ae4de1d95119d49aa8d186cb3a57fdb..25bcfdba1b232f6a9c18a260ed1b87789a3edfb6 100644 (file)
@@ -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
index 62380b0e4a152546428f9b429286918a03a489fe..1fadaad9964866653c9fd3a2f170d752e866cec0 100644 (file)
@@ -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) {
index 9eedc20c4fe1d96abd59f2b29baa060eef661c49..fff23fdbacb063ae8128cb706a666e998d4c3e2e 100644 (file)
@@ -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;
index 0fe2f2789cda606064921202deb3a14629482ca0..539ccbdf06b7d35708aef0a7fba55172e8f5dec6 100644 (file)
@@ -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 (file)
index 1baf91f..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "mempool.h"
-
-const bool mempool_use_allowed = false;
index 2701efc8f9252ced9586773a20f13a209643d3e2..055fa11ede75a83ff907bb4f3599c3e5620952f7 100644 (file)
@@ -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']
 
index 1abfccbd817e1f1b59c5c9ccf8d9a3bb043d5844..fd582c0e78fcbf2a2a1496bf2726a26f87a85376 100644 (file)
@@ -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;
+}
index f02e433d7b2bffc83e65325127a889d382c3b47c..91244b25ac296eb130804ae2ccb50108171a193b 100644 (file)
@@ -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++)