From c15a575e34ed8f81b59f96990df54b876342d22e Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 20 Jan 2017 18:27:20 -0500 Subject: [PATCH] remove dependency on aligned_storage and aligned_union to support older versions of GCC --- include/libtorrent/Makefile.am | 2 + include/libtorrent/aux_/aligned_storage.hpp | 61 ++++++++++++++++ include/libtorrent/aux_/aligned_union.hpp | 72 +++++++++++++++++++ .../libtorrent/aux_/allocating_handler.hpp | 5 +- include/libtorrent/chained_buffer.hpp | 3 +- include/libtorrent/entry.hpp | 3 +- include/libtorrent/socket_type.hpp | 5 +- src/cpuid.cpp | 2 +- src/kademlia/rpc_manager.cpp | 3 +- 9 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 include/libtorrent/aux_/aligned_storage.hpp create mode 100644 include/libtorrent/aux_/aligned_union.hpp diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 5af5c2ef8..696fcead7 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -158,6 +158,8 @@ nobase_include_HEADERS = \ span.hpp \ \ aux_/allocating_handler.hpp \ + aux_/aligned_storage.hpp \ + aux_/aligned_union.hpp \ aux_/bind_to_device.hpp \ aux_/block_cache_reference.hpp \ aux_/cpuid.hpp \ diff --git a/include/libtorrent/aux_/aligned_storage.hpp b/include/libtorrent/aux_/aligned_storage.hpp new file mode 100644 index 000000000..9871e7144 --- /dev/null +++ b/include/libtorrent/aux_/aligned_storage.hpp @@ -0,0 +1,61 @@ +/* + +Copyright (c) 2017, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_ALIGNED_STORAGE_HPP_INCLUDE +#define TORRENT_ALIGNED_STORAGE_HPP_INCLUDE + +#include + +namespace libtorrent { namespace aux { + +#if defined __GNUC__ && __GNUC__ < 5 + +// this is for backwards compatibility with not-quite C++11 compilers +template +struct aligned_storage +{ + struct type + { + alignas(Align) char buffer[Len]; + }; +}; + +#else + +using std::aligned_storage; + +#endif + +}} + +#endif + diff --git a/include/libtorrent/aux_/aligned_union.hpp b/include/libtorrent/aux_/aligned_union.hpp new file mode 100644 index 000000000..aeee17724 --- /dev/null +++ b/include/libtorrent/aux_/aligned_union.hpp @@ -0,0 +1,72 @@ +/* + +Copyright (c) 2017, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_ALIGNED_UNION_HPP_INCLUDE +#define TORRENT_ALIGNED_UNION_HPP_INCLUDE + +#include + +namespace libtorrent { namespace aux { + +#if defined __GNUC__ && __GNUC__ < 5 + +constexpr std::size_t max(size_t a) +{ return a; } + +constexpr std::size_t max(size_t a, size_t b) +{ return a > b ? a : b; } + +template +constexpr std::size_t max(size_t a, size_t b, Vals... v) +{ return max(a, max(b, v...)); } + +// this is for backwards compatibility with not-quite C++11 compilers +template +struct aligned_union +{ + struct type + { + alignas(max(alignof(Types)...)) + char buffer[max(Len, max(sizeof(Types)...))]; + }; +}; + +#else + +using std::aligned_union; + +#endif + +}} + +#endif + diff --git a/include/libtorrent/aux_/allocating_handler.hpp b/include/libtorrent/aux_/allocating_handler.hpp index 0da1fac9f..58ba8a063 100644 --- a/include/libtorrent/aux_/allocating_handler.hpp +++ b/include/libtorrent/aux_/allocating_handler.hpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/error_code.hpp" +#include "libtorrent/aux_/aligned_storage.hpp" #include @@ -54,10 +55,8 @@ namespace libtorrent { namespace aux {} bool used; -#else - handler_storage() {} #endif - typename std::aligned_storage::type bytes; + typename aux::aligned_storage::type bytes; private: handler_storage(handler_storage const&); }; diff --git a/include/libtorrent/chained_buffer.hpp b/include/libtorrent/chained_buffer.hpp index ec406da0e..845c8dfd6 100644 --- a/include/libtorrent/chained_buffer.hpp +++ b/include/libtorrent/chained_buffer.hpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/aux_/block_cache_reference.hpp" +#include "libtorrent/aux_/aligned_storage.hpp" #include "libtorrent/debug.hpp" #include "libtorrent/buffer.hpp" @@ -111,7 +112,7 @@ namespace libtorrent #if TORRENT_CPP98_DEQUE move_construct_holder_fun move_holder; #endif - std::aligned_storage<24>::type holder; + aux::aligned_storage<24>::type holder; char* buf; // the first byte of the buffer int size; // the total size of the buffer int used_size; // this is the number of bytes to send/receive diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index fadf57f31..03dd4fd3e 100644 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -73,6 +73,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/error_code.hpp" #include "libtorrent/span.hpp" #include "libtorrent/string_view.hpp" +#include "libtorrent/aux_/aligned_union.hpp" namespace libtorrent { @@ -311,7 +312,7 @@ namespace libtorrent void to_string_impl(std::string& out, int indent) const; - std::aligned_union<1 + aux::aligned_union<1 #if TORRENT_COMPLETE_TYPES_REQUIRED // for implementations that require complete types, use char and hope // for the best diff --git a/include/libtorrent/socket_type.hpp b/include/libtorrent/socket_type.hpp index 890deeec5..26e1a2577 100644 --- a/include/libtorrent/socket_type.hpp +++ b/include/libtorrent/socket_type.hpp @@ -33,9 +33,8 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_SOCKET_TYPE #define TORRENT_SOCKET_TYPE -#include // for aligned_union - #include "libtorrent/config.hpp" +#include "libtorrent/aux_/aligned_union.hpp" #include "libtorrent/socket.hpp" #include "libtorrent/socks5_stream.hpp" #include "libtorrent/http_stream.hpp" @@ -300,7 +299,7 @@ namespace libtorrent io_service& m_io_service; int m_type; - std::aligned_union<1 + aux::aligned_union<1 , tcp::socket , socks5_stream , http_stream diff --git a/src/cpuid.cpp b/src/cpuid.cpp index 6800013e2..a061609d1 100644 --- a/src/cpuid.cpp +++ b/src/cpuid.cpp @@ -46,7 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include // for std::memset #endif -#if TORRENT_HAS_ARM +#if TORRENT_HAS_ARM && TORRENT_HAS_ARM_NEON #include #endif diff --git a/src/kademlia/rpc_manager.cpp b/src/kademlia/rpc_manager.cpp index 23156643b..69f5a93fb 100644 --- a/src/kademlia/rpc_manager.cpp +++ b/src/kademlia/rpc_manager.cpp @@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include // for dht_settings #include // for aux::time_now +#include #include #include @@ -135,7 +136,7 @@ void observer::set_id(node_id const& id) if (m_algorithm) m_algorithm->resort_results(); } -using observer_storage = std::aligned_union<1 +using observer_storage = aux::aligned_union<1 , find_data_observer , announce_observer , put_data_observer