summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-02-08 18:03:27 +0100
committerLennart Poettering <lennart@poettering.net>2023-02-17 14:59:54 +0100
commit9a0f0ef5576bd72c6908339df44bf382342266de (patch)
tree02be5a46cc4c9d9062d3406efdf4189187fe5763 /src
parentdissect-image: Return mount point fd if requested (diff)
downloadsystemd-9a0f0ef5576bd72c6908339df44bf382342266de.tar.xz
systemd-9a0f0ef5576bd72c6908339df44bf382342266de.zip
hashmap: expose helper for releasing memory pools independently of valgrind
Let's clean this up and export this always, so that we can later call when we are under memory pressure.
Diffstat (limited to '')
-rw-r--r--src/basic/hashmap.c32
-rw-r--r--src/basic/hashmap.h2
-rw-r--r--src/basic/mempool.c2
-rw-r--r--src/basic/mempool.h2
4 files changed, 20 insertions, 18 deletions
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c
index b9efaafa3c..c18c75d78c 100644
--- a/src/basic/hashmap.c
+++ b/src/basic/hashmap.c
@@ -274,28 +274,32 @@ static _used_ const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX
},
};
-#if VALGRIND
-_destructor_ static void cleanup_pools(void) {
- _cleanup_free_ char *t = NULL;
+void hashmap_cleanup_pools(void) {
+ int r;
- /* Be nice to valgrind */
+ /* The pool is only allocated by the main thread, but the memory can be passed to other
+ * threads. Let's clean up if we are the main thread and no other threads are live. */
- /* The pool is only allocated by the main thread, but the memory can
- * be passed to other threads. Let's clean up if we are the main thread
- * and no other threads are live. */
- /* We build our own is_main_thread() here, which doesn't use C11
- * TLS based caching of the result. That's because valgrind apparently
- * doesn't like malloc() (which C11 TLS internally uses) to be called
- * from a GCC destructors. */
+ /* We build our own is_main_thread() here, which doesn't use C11 TLS based caching of the
+ * result. That's because valgrind apparently doesn't like TLS to be used from a GCC destructor. */
if (getpid() != gettid())
- return;
+ return (void) log_debug("Not cleaning up memory pools, not in main thread.");
- if (get_process_threads(0) != 1)
- return;
+ r = get_process_threads(0);
+ if (r < 0)
+ return (void) log_debug_errno(r, "Failed to determine number of threads, not cleaning up memory pools: %m");
+ if (r != 1)
+ return (void) log_debug("Not cleaning up memory pools, running in multi-threaded process.");
mempool_drop(&hashmap_pool);
mempool_drop(&ordered_hashmap_pool);
}
+
+#if VALGRIND
+_destructor_ static void cleanup_pools(void) {
+ /* Be nice to valgrind */
+ hashmap_cleanup_pools();
+}
#endif
static unsigned n_buckets(HashmapBase *h) {
diff --git a/src/basic/hashmap.h b/src/basic/hashmap.h
index ebb5a63eb4..1b944e93b5 100644
--- a/src/basic/hashmap.h
+++ b/src/basic/hashmap.h
@@ -443,3 +443,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(IteratedCache*, iterated_cache_free);
#define _cleanup_iterated_cache_free_ _cleanup_(iterated_cache_freep)
+
+void hashmap_cleanup_pools(void);
diff --git a/src/basic/mempool.c b/src/basic/mempool.c
index fff23fdbac..92d8822bd9 100644
--- a/src/basic/mempool.c
+++ b/src/basic/mempool.c
@@ -70,7 +70,6 @@ void mempool_free_tile(struct mempool *mp, void *p) {
mp->freelist = p;
}
-#if VALGRIND
void mempool_drop(struct mempool *mp) {
struct pool *p = mp->first_pool;
while (p) {
@@ -80,4 +79,3 @@ void mempool_drop(struct mempool *mp) {
p = n;
}
}
-#endif
diff --git a/src/basic/mempool.h b/src/basic/mempool.h
index 539ccbdf06..80925cb95a 100644
--- a/src/basic/mempool.h
+++ b/src/basic/mempool.h
@@ -25,6 +25,4 @@ static struct mempool pool_name = { \
__attribute__((weak)) bool mempool_enabled(void);
-#if VALGRIND
void mempool_drop(struct mempool *mp);
-#endif