remove dependency on aligned_storage and aligned_union to support older versions of GCC

This commit is contained in:
arvidn 2017-01-20 18:27:20 -05:00 committed by Arvid Norberg
parent 8b98bf2c9d
commit c15a575e34
9 changed files with 146 additions and 10 deletions

View File

@ -158,6 +158,8 @@ nobase_include_HEADERS = \
span.hpp \ span.hpp \
\ \
aux_/allocating_handler.hpp \ aux_/allocating_handler.hpp \
aux_/aligned_storage.hpp \
aux_/aligned_union.hpp \
aux_/bind_to_device.hpp \ aux_/bind_to_device.hpp \
aux_/block_cache_reference.hpp \ aux_/block_cache_reference.hpp \
aux_/cpuid.hpp \ aux_/cpuid.hpp \

View File

@ -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 <type_traits>
namespace libtorrent { namespace aux {
#if defined __GNUC__ && __GNUC__ < 5
// this is for backwards compatibility with not-quite C++11 compilers
template <size_t Len, size_t Align = alignof(void*)>
struct aligned_storage
{
struct type
{
alignas(Align) char buffer[Len];
};
};
#else
using std::aligned_storage;
#endif
}}
#endif

View File

@ -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 <type_traits>
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 <typename... Vals>
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 <size_t Len, typename... Types>
struct aligned_union
{
struct type
{
alignas(max(alignof(Types)...))
char buffer[max(Len, max(sizeof(Types)...))];
};
};
#else
using std::aligned_union;
#endif
}}
#endif

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/error_code.hpp" #include "libtorrent/error_code.hpp"
#include "libtorrent/aux_/aligned_storage.hpp"
#include <type_traits> #include <type_traits>
@ -54,10 +55,8 @@ namespace libtorrent { namespace aux
{} {}
bool used; bool used;
#else
handler_storage() {}
#endif #endif
typename std::aligned_storage<Size>::type bytes; typename aux::aligned_storage<Size>::type bytes;
private: private:
handler_storage(handler_storage const&); handler_storage(handler_storage const&);
}; };

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/aux_/block_cache_reference.hpp" #include "libtorrent/aux_/block_cache_reference.hpp"
#include "libtorrent/aux_/aligned_storage.hpp"
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
#include "libtorrent/buffer.hpp" #include "libtorrent/buffer.hpp"
@ -111,7 +112,7 @@ namespace libtorrent
#if TORRENT_CPP98_DEQUE #if TORRENT_CPP98_DEQUE
move_construct_holder_fun move_holder; move_construct_holder_fun move_holder;
#endif #endif
std::aligned_storage<24>::type holder; aux::aligned_storage<24>::type holder;
char* buf; // the first byte of the buffer char* buf; // the first byte of the buffer
int size; // the total size of the buffer int size; // the total size of the buffer
int used_size; // this is the number of bytes to send/receive int used_size; // this is the number of bytes to send/receive

View File

@ -73,6 +73,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/error_code.hpp" #include "libtorrent/error_code.hpp"
#include "libtorrent/span.hpp" #include "libtorrent/span.hpp"
#include "libtorrent/string_view.hpp" #include "libtorrent/string_view.hpp"
#include "libtorrent/aux_/aligned_union.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -311,7 +312,7 @@ namespace libtorrent
void to_string_impl(std::string& out, int indent) const; void to_string_impl(std::string& out, int indent) const;
std::aligned_union<1 aux::aligned_union<1
#if TORRENT_COMPLETE_TYPES_REQUIRED #if TORRENT_COMPLETE_TYPES_REQUIRED
// for implementations that require complete types, use char and hope // for implementations that require complete types, use char and hope
// for the best // for the best

View File

@ -33,9 +33,8 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_SOCKET_TYPE #ifndef TORRENT_SOCKET_TYPE
#define TORRENT_SOCKET_TYPE #define TORRENT_SOCKET_TYPE
#include <type_traits> // for aligned_union
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/aux_/aligned_union.hpp"
#include "libtorrent/socket.hpp" #include "libtorrent/socket.hpp"
#include "libtorrent/socks5_stream.hpp" #include "libtorrent/socks5_stream.hpp"
#include "libtorrent/http_stream.hpp" #include "libtorrent/http_stream.hpp"
@ -300,7 +299,7 @@ namespace libtorrent
io_service& m_io_service; io_service& m_io_service;
int m_type; int m_type;
std::aligned_union<1 aux::aligned_union<1
, tcp::socket , tcp::socket
, socks5_stream , socks5_stream
, http_stream , http_stream

View File

@ -46,7 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstring> // for std::memset #include <cstring> // for std::memset
#endif #endif
#if TORRENT_HAS_ARM #if TORRENT_HAS_ARM && TORRENT_HAS_ARM_NEON
#include <sys/auxv.h> #include <sys/auxv.h>
#endif #endif

View File

@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/hasher.hpp> #include <libtorrent/hasher.hpp>
#include <libtorrent/session_settings.hpp> // for dht_settings #include <libtorrent/session_settings.hpp> // for dht_settings
#include <libtorrent/aux_/time.hpp> // for aux::time_now #include <libtorrent/aux_/time.hpp> // for aux::time_now
#include <libtorrent/aux_/aligned_union.hpp>
#include <type_traits> #include <type_traits>
#include <functional> #include <functional>
@ -135,7 +136,7 @@ void observer::set_id(node_id const& id)
if (m_algorithm) m_algorithm->resort_results(); if (m_algorithm) m_algorithm->resort_results();
} }
using observer_storage = std::aligned_union<1 using observer_storage = aux::aligned_union<1
, find_data_observer , find_data_observer
, announce_observer , announce_observer
, put_data_observer , put_data_observer