From 5e0bd5f0c830536cd598c4edb9715a3e69c4f7e6 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Tue, 9 Jul 2024 22:03:08 -0700 Subject: [PATCH] [upstream_utils] Upgrade Sleipnir to avoid pool allocator crash on Windows --- upstream_utils/update_sleipnir.py | 4 ++-- .../include/sleipnir/autodiff/Expression.hpp | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/upstream_utils/update_sleipnir.py b/upstream_utils/update_sleipnir.py index de152f6e4fe..4b783c82bab 100755 --- a/upstream_utils/update_sleipnir.py +++ b/upstream_utils/update_sleipnir.py @@ -15,8 +15,8 @@ def main(): upstream_root = clone_repo( "https://github.com/SleipnirGroup/Sleipnir", - # main on 2024-07-05 - "b90f89d343379dd8dc88f22e0462eb7b59006b2c", + # main on 2024-07-09 + "b6ffa2d4fdb99cab1bf79491f715a6a9a86633b5", shallow=False, ) wpilib_root = get_repo_root() diff --git a/wpimath/src/main/native/thirdparty/sleipnir/include/sleipnir/autodiff/Expression.hpp b/wpimath/src/main/native/thirdparty/sleipnir/include/sleipnir/autodiff/Expression.hpp index 2be666e7c87..bdbeb473022 100644 --- a/wpimath/src/main/native/thirdparty/sleipnir/include/sleipnir/autodiff/Expression.hpp +++ b/wpimath/src/main/native/thirdparty/sleipnir/include/sleipnir/autodiff/Expression.hpp @@ -20,6 +20,14 @@ namespace sleipnir::detail { +// The global pool allocator uses a thread-local static pool resource, which +// isn't guaranteed to be initialized properly across DLL boundaries on Windows +#ifdef _WIN32 +inline constexpr bool kUsePoolAllocator = false; +#else +inline constexpr bool kUsePoolAllocator = true; +#endif + struct SLEIPNIR_DLLEXPORT Expression; inline void IntrusiveSharedPtrIncRefCount(Expression* expr); @@ -38,8 +46,12 @@ using ExpressionPtr = IntrusiveSharedPtr; */ template static ExpressionPtr MakeExpressionPtr(Args&&... args) { - return AllocateIntrusiveShared(GlobalPoolAllocator(), - std::forward(args)...); + if constexpr (kUsePoolAllocator) { + return AllocateIntrusiveShared( + GlobalPoolAllocator(), std::forward(args)...); + } else { + return MakeIntrusiveShared(std::forward(args)...); + } } /** @@ -437,9 +449,11 @@ inline void IntrusiveSharedPtrDecRefCount(Expression* expr) { // Not calling the destructor here is safe because it only decrements // refcounts, which was already done above. - auto alloc = GlobalPoolAllocator(); - std::allocator_traits::deallocate(alloc, elem, - sizeof(Expression)); + if constexpr (kUsePoolAllocator) { + auto alloc = GlobalPoolAllocator(); + std::allocator_traits::deallocate(alloc, elem, + sizeof(Expression)); + } } } }