From 32ff87f7f6da97830ab002cc64565bafd0b65dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Dom=C3=ADnguez?= Date: Tue, 27 Aug 2024 00:12:24 +0200 Subject: [PATCH] Replace boost::aligned_storage with alignas std::array --- libs/ardour/ardour/chan_mapping.h | 22 +++++++++++++++++----- libs/pbd/pbd/stack_allocator.h | 19 ++++++++----------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/libs/ardour/ardour/chan_mapping.h b/libs/ardour/ardour/chan_mapping.h index 7a17e48499..8bd9a99812 100644 --- a/libs/ardour/ardour/chan_mapping.h +++ b/libs/ardour/ardour/chan_mapping.h @@ -106,12 +106,24 @@ public: #if defined(_MSC_VER) /* && (_MSC_VER < 1900) * Regarding the note (below) it was initially * thought that this got fixed in VS2015 - but - * in fact it's still faulty (JE - Feb 2021) */ + * in fact it's still faulty (JE - Feb 2021). + * 2024-09-03 update: Faulty compile up to + * VS2022 17.4. From there it compiles but does + * not link with the exception of the arm64 + * platform. This quirk is actually a bug that + * Microsoft seems to have failed to + * acknowledge as such for years judging from + * web search results about MSVC's std::map, + * and have not even fixed correctly when they + * tried. + */ /* Use the older (heap based) mapping for early versions of MSVC. - * In fact it might be safer to use this for all MSVC builds - as - * our StackAllocator class depends on 'boost::aligned_storage' - * which is known to be troublesome with Visual C++ :- - * https://www.boost.org/doc/libs/1_65_0/libs/type_traits/doc/html/boost_typetraits/reference/aligned_storage.html + * In fact it might be safer to use this for all MSVC builds. It + * was thought that this was related to issues with + * boost::aligned_storage, but actually it seems to be that there + * are bugs in the std::map implementation of MSVC that are being + * triggered, messing with the copy constructor of + * PBD::StackAllocator. */ typedef std::map TypeMapping; typedef std::map Mappings; diff --git a/libs/pbd/pbd/stack_allocator.h b/libs/pbd/pbd/stack_allocator.h index 9eed081ec0..742cf39b6d 100644 --- a/libs/pbd/pbd/stack_allocator.h +++ b/libs/pbd/pbd/stack_allocator.h @@ -19,7 +19,6 @@ #ifndef PBD_STACK_ALLOCATOR_H #define PBD_STACK_ALLOCATOR_H -#include #include #include "pbd/libpbd_visibility.h" @@ -61,22 +60,22 @@ public: }; StackAllocator () - : _ptr ((pointer)&_buf) + : _ptr (_buf.data()) { } StackAllocator (const StackAllocator&) - : _ptr ((pointer)&_buf) + : StackAllocator () { } template StackAllocator (const StackAllocator&) - : _ptr ((pointer)&_buf) + : StackAllocator () { } /* inspired by http://howardhinnant.github.io/stack_alloc.h */ pointer allocate (size_type n, void* hint = 0) { - if ((pointer)&_buf + stack_capacity >= _ptr + n) { + if (_buf.data() + stack_capacity >= _ptr + n) { DEBUG_STACK_ALLOC ("Allocate %ld item(s) of size %zu on the stack\n", n, sizeof (T)); pointer rv = _ptr; _ptr += n; @@ -108,12 +107,12 @@ public: bool operator== (StackAllocator const& a) const { - return &_buf == &a._buf; + return _buf.data() == a._buf.data(); } bool operator!= (StackAllocator const& a) const { - return &_buf != &a._buf; + return _buf.data() != a._buf.data(); } template @@ -147,12 +146,10 @@ private: bool pointer_in_buffer (pointer const p) { - return ((pointer const)&_buf <= p && p < (pointer const)&_buf + stack_capacity); + return (_buf.data() <= p && p < _buf.data() + stack_capacity); } - typedef typename boost::aligned_storage::type align_t; - - align_t _buf; + alignas(16) std::array _buf; pointer _ptr; };