[llvm] [Support] On Windows, fix rpmalloc TLS destructor when running inside a DLL (PR #171465)

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 07:56:46 PST 2025


================
@@ -969,6 +969,47 @@ static void _rpmalloc_spin(void) {
 }
 
 #if defined(_WIN32) && (!defined(BUILD_DYNAMIC_LINK) || !BUILD_DYNAMIC_LINK)
+
+static void NTAPI RPMallocTlsOnThreadExit(PVOID module, DWORD reason,
+                                          PVOID reserved) {
+  switch (reason) {
+  case DLL_PROCESS_ATTACH:
+    break;
+  case DLL_PROCESS_DETACH:
+    rpmalloc_finalize();
+    break;
+  case DLL_THREAD_ATTACH:
+    break;
+  case DLL_THREAD_DETACH:
+    rpmalloc_thread_finalize(1);
+    break;
+  }
+}
+
+#ifdef _WIN64
+#pragma comment(linker, "/INCLUDE:_tls_used")
+#pragma comment(linker, "/INCLUDE:rpmalloc_tls_thread_exit_callback")
+
+#pragma const_seg(".CRT$XLY")
----------------
aganea wrote:

One last thing: can you please add a comment here to explain what is this .CRT$XLY? I know what it does, but it's other maintainers that will come after us. There's a pretty good comment here: https://github.com/klzgrad/naiveproxy/blob/23bb6c784d9736aca21d6df2698f5d19d68d07e1/src/base/allocator/partition_allocator/src/partition_alloc/partition_tls_win.cc#L66 which you can adapt (but don't copy that comment verbatim please).

https://github.com/llvm/llvm-project/pull/171465


More information about the llvm-commits mailing list