···
1
+
From 791d42ddacad829d19a4f66b77dc9ca410008db9 Mon Sep 17 00:00:00 2001
2
+
From: Emily <hello@emily.moe>
3
+
Date: Sat, 13 Sep 2025 03:16:16 +0100
4
+
Subject: [PATCH] Use `dladdr` to keep `libtbbmalloc` loaded on POSIX
6
+
This fixes issues that arise when `libtbbmalloc` cannot be loaded by
7
+
relative path, makes the behaviour consistent with the Windows code
8
+
path, and eliminates the duplication of `MALLOCLIB_NAME`.
10
+
src/tbbmalloc/tbbmalloc.cpp | 34 +++++++++-------------------------
11
+
1 file changed, 9 insertions(+), 25 deletions(-)
13
+
diff --git a/src/tbbmalloc/tbbmalloc.cpp b/src/tbbmalloc/tbbmalloc.cpp
14
+
index b72e03a74f..12d62445aa 100644
15
+
--- a/src/tbbmalloc/tbbmalloc.cpp
16
+
+++ b/src/tbbmalloc/tbbmalloc.cpp
19
+
- Copyright (c) 2005-2023 Intel Corporation
20
+
+ Copyright (c) 2005-2025 Intel Corporation
22
+
Licensed under the Apache License, Version 2.0 (the "License");
23
+
you may not use this file except in compliance with the License.
26
+
namespace internal {
29
+
-#define DEBUG_SUFFIX "_debug"
31
+
-#define DEBUG_SUFFIX
32
+
-#endif /* TBB_USE_DEBUG */
34
+
-// MALLOCLIB_NAME is the name of the oneTBB memory allocator library.
36
+
-#define MALLOCLIB_NAME "tbbmalloc" DEBUG_SUFFIX ".dll"
38
+
-#define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX ".2.dylib"
39
+
-#elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX || __ANDROID__
40
+
-#define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX ".so"
42
+
-#define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX __TBB_STRING(.so.2)
47
+
void init_tbbmalloc() {
48
+
#if __TBB_USE_ITT_NOTIFY
49
+
MallocInitializeITT();
50
+
@@ -92,11 +73,14 @@ struct RegisterProcessShutdownNotification {
51
+
RegisterProcessShutdownNotification() {
52
+
// prevents unloading, POSIX case
54
+
- // We need better support for the library pinning
55
+
- // when dlopen can't find TBBmalloc library.
56
+
- // for example: void *ret = dlopen(MALLOCLIB_NAME, RTLD_NOW);
57
+
- // MALLOC_ASSERT(ret, "Allocator can't load itself.");
58
+
- dlopen(MALLOCLIB_NAME, RTLD_NOW);
60
+
+ int ret = dladdr((void*)&init_tbbmalloc, &dlinfo);
61
+
+ MALLOC_ASSERT(ret && dlinfo.dli_fname, "Allocator can't find itself.");
62
+
+ tbb::detail::suppress_unused_warning(ret);
64
+
+ void* lib = dlopen(dlinfo.dli_fname, RTLD_NOW);
65
+
+ MALLOC_ASSERT(lib, "Allocator can't load itself.");
66
+
+ tbb::detail::suppress_unused_warning(lib);
69
+
RegisterProcessShutdownNotification(RegisterProcessShutdownNotification&) = delete;