2015-04-03 22:15:48 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2015-2018, Arvid Norberg
|
2015-04-03 22:15:48 +02:00
|
|
|
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_HETEROGENEOUS_QUEUE_HPP_INCLUDED
|
|
|
|
#define TORRENT_HETEROGENEOUS_QUEUE_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <vector>
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
2017-03-20 22:41:00 +01:00
|
|
|
#include <cstdlib> // for malloc
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <type_traits>
|
2017-03-20 22:41:00 +01:00
|
|
|
#include <memory>
|
2016-05-02 18:36:21 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2017-03-20 22:41:00 +01:00
|
|
|
#include "libtorrent/aux_/throw.hpp"
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
namespace libtorrent {
|
2019-08-08 00:27:51 +02:00
|
|
|
namespace aux {
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2019-08-08 00:27:51 +02:00
|
|
|
struct free_deleter
|
|
|
|
{ void operator()(char* ptr) { return std::free(ptr); } };
|
2017-04-12 19:00:57 +02:00
|
|
|
|
2019-08-08 00:27:51 +02:00
|
|
|
inline std::size_t calculate_pad_bytes(char const* inptr, std::size_t alignment)
|
|
|
|
{
|
|
|
|
std::uintptr_t const ptr = reinterpret_cast<std::uintptr_t>(inptr);
|
|
|
|
std::uintptr_t const offset = ptr & (alignment - 1);
|
|
|
|
return (alignment - offset) & (alignment - 1);
|
2017-03-20 22:41:00 +01:00
|
|
|
}
|
2019-08-08 00:27:51 +02:00
|
|
|
}
|
2017-03-20 22:41:00 +01:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
template <class T>
|
|
|
|
struct heterogeneous_queue
|
|
|
|
{
|
2017-03-20 22:41:00 +01:00
|
|
|
heterogeneous_queue() : m_storage(nullptr, aux::free_deleter()) {}
|
|
|
|
heterogeneous_queue(heterogeneous_queue const&) = delete;
|
|
|
|
heterogeneous_queue& operator=(heterogeneous_queue const&) = delete;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2016-04-28 16:30:29 +02:00
|
|
|
template <class U, typename... Args>
|
2016-06-20 17:32:06 +02:00
|
|
|
typename std::enable_if<std::is_base_of<T, U>::value, U&>::type
|
2016-04-28 16:30:29 +02:00
|
|
|
emplace_back(Args&&... args)
|
2015-04-03 22:15:48 +02:00
|
|
|
{
|
2017-03-20 22:41:00 +01:00
|
|
|
// make the conservative assumption that we'll need the maximum padding
|
|
|
|
// for this object, just for purposes of growing the storage
|
|
|
|
if (std::size_t(m_size) + sizeof(header_t) + alignof(U) + sizeof(U) > std::size_t(m_capacity))
|
|
|
|
grow_capacity(sizeof(header_t) + alignof(U) + sizeof(U));
|
|
|
|
|
|
|
|
char* ptr = m_storage.get() + m_size;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
std::size_t const pad_bytes = aux::calculate_pad_bytes(ptr + sizeof(header_t), alignof(U));
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
// pad_bytes is only 8 bits in the header, so types that need more than
|
2018-02-18 00:36:57 +01:00
|
|
|
// 256 byte alignment may not be supported
|
2017-03-20 22:41:00 +01:00
|
|
|
static_assert(alignof(U) <= 256
|
|
|
|
, "heterogeneous_queue does not support types with alignment requirements > 256");
|
|
|
|
|
|
|
|
// if this assert triggers, the type being added to the queue has
|
|
|
|
// alignment requirements stricter than what malloc() returns. This is
|
|
|
|
// not supported
|
|
|
|
TORRENT_ASSERT((reinterpret_cast<std::uintptr_t>(m_storage.get())
|
|
|
|
& (alignof(U) - 1)) == 0);
|
|
|
|
|
|
|
|
// make sure the current position in the storage is aligned for
|
|
|
|
// creating a heder_t object
|
|
|
|
TORRENT_ASSERT((reinterpret_cast<std::uintptr_t>(ptr)
|
|
|
|
& (alignof(header_t) - 1)) == 0);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
// length prefix
|
2017-03-20 22:41:00 +01:00
|
|
|
header_t* hdr = new (ptr) header_t;
|
|
|
|
hdr->pad_bytes = static_cast<std::uint8_t>(pad_bytes);
|
2015-04-03 22:15:48 +02:00
|
|
|
hdr->move = &move<U>;
|
2017-03-20 22:41:00 +01:00
|
|
|
ptr += sizeof(header_t) + pad_bytes;
|
|
|
|
hdr->len = static_cast<std::uint16_t>(sizeof(U)
|
|
|
|
+ aux::calculate_pad_bytes(ptr + sizeof(U), alignof(header_t)));
|
|
|
|
|
|
|
|
// make sure ptr is correctly aligned for the object we're about to
|
|
|
|
// create there
|
|
|
|
TORRENT_ASSERT((reinterpret_cast<std::uintptr_t>(ptr)
|
|
|
|
& (alignof(U) - 1)) == 0);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
// construct in-place
|
2017-03-20 22:41:00 +01:00
|
|
|
U* const ret = new (ptr) U(std::forward<Args>(args)...);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
// if we constructed the object without throwing any exception
|
|
|
|
// update counters to indicate the new item is in there
|
|
|
|
++m_num_items;
|
2017-03-20 22:41:00 +01:00
|
|
|
m_size += int(sizeof(header_t) + pad_bytes + hdr->len);
|
|
|
|
return *ret;
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_pointers(std::vector<T*>& out)
|
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
char* ptr = m_storage.get();
|
|
|
|
char const* const end = m_storage.get() + m_size;
|
2015-04-03 22:15:48 +02:00
|
|
|
while (ptr < end)
|
|
|
|
{
|
2015-04-18 04:33:39 +02:00
|
|
|
header_t* hdr = reinterpret_cast<header_t*>(ptr);
|
2017-03-20 22:41:00 +01:00
|
|
|
ptr += sizeof(header_t) + hdr->pad_bytes;
|
2015-04-03 22:15:48 +02:00
|
|
|
TORRENT_ASSERT(ptr + hdr->len <= end);
|
2015-04-18 04:33:39 +02:00
|
|
|
out.push_back(reinterpret_cast<T*>(ptr));
|
2015-04-03 22:15:48 +02:00
|
|
|
ptr += hdr->len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(heterogeneous_queue& rhs)
|
|
|
|
{
|
|
|
|
std::swap(m_storage, rhs.m_storage);
|
|
|
|
std::swap(m_capacity, rhs.m_capacity);
|
|
|
|
std::swap(m_size, rhs.m_size);
|
|
|
|
std::swap(m_num_items, rhs.m_num_items);
|
|
|
|
}
|
|
|
|
|
|
|
|
int size() const { return m_num_items; }
|
|
|
|
bool empty() const { return m_num_items == 0; }
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2017-03-20 22:41:00 +01:00
|
|
|
char* ptr = m_storage.get();
|
|
|
|
char const* const end = m_storage.get() + m_size;
|
2015-04-03 22:15:48 +02:00
|
|
|
while (ptr < end)
|
|
|
|
{
|
2015-04-18 04:33:39 +02:00
|
|
|
header_t* hdr = reinterpret_cast<header_t*>(ptr);
|
2017-03-20 22:41:00 +01:00
|
|
|
ptr += sizeof(header_t) + hdr->pad_bytes;
|
2015-04-03 22:15:48 +02:00
|
|
|
TORRENT_ASSERT(ptr + hdr->len <= end);
|
2015-04-18 04:33:39 +02:00
|
|
|
T* a = reinterpret_cast<T*>(ptr);
|
2015-04-03 22:15:48 +02:00
|
|
|
a->~T();
|
|
|
|
ptr += hdr->len;
|
2017-03-20 22:41:00 +01:00
|
|
|
hdr->~header_t();
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
m_size = 0;
|
|
|
|
m_num_items = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* front()
|
|
|
|
{
|
2016-06-20 17:32:06 +02:00
|
|
|
if (m_size == 0) return nullptr;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(m_size > 1);
|
2017-03-20 22:41:00 +01:00
|
|
|
char* ptr = m_storage.get();
|
|
|
|
header_t* hdr = reinterpret_cast<header_t*>(ptr);
|
|
|
|
TORRENT_ASSERT(sizeof(header_t) + hdr->pad_bytes + hdr->len
|
|
|
|
<= std::size_t(m_size));
|
|
|
|
ptr += sizeof(header_t) + hdr->pad_bytes;
|
2015-04-18 04:33:39 +02:00
|
|
|
return reinterpret_cast<T*>(ptr);
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
~heterogeneous_queue() { clear(); }
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// this header is put in front of every element. It tells us
|
2017-03-20 22:41:00 +01:00
|
|
|
// how many bytes it's using for its allocation, and it
|
2015-04-03 22:15:48 +02:00
|
|
|
// also tells us how to move this type if we need to grow our
|
|
|
|
// allocation.
|
|
|
|
struct header_t
|
|
|
|
{
|
2017-03-20 22:41:00 +01:00
|
|
|
// the size of the object. From the start of the object, skip this many
|
|
|
|
// bytes to get to the next header. Meaning this includes sufficient
|
|
|
|
// padding to have the next entry be appropriately aligned for header_t
|
|
|
|
std::uint16_t len;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
// the number of pad bytes between the end of this
|
|
|
|
// header and the start of the object. This supports allocating types with
|
|
|
|
// stricter alignment requirements
|
|
|
|
std::uint8_t pad_bytes;
|
|
|
|
|
|
|
|
void (*move)(char* dst, char* src);
|
|
|
|
};
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2016-04-04 05:50:06 +02:00
|
|
|
void grow_capacity(int const size)
|
2015-04-03 22:15:48 +02:00
|
|
|
{
|
2017-03-20 22:41:00 +01:00
|
|
|
int const amount_to_grow = (std::max)(size
|
2015-04-03 22:15:48 +02:00
|
|
|
, (std::max)(m_capacity * 3 / 2, 128));
|
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
// we use malloc() to guarantee alignment
|
|
|
|
std::unique_ptr<char, aux::free_deleter> new_storage(
|
|
|
|
static_cast<char*>(std::malloc(std::size_t(m_capacity + amount_to_grow)))
|
|
|
|
, aux::free_deleter());
|
|
|
|
|
2018-02-18 00:36:57 +01:00
|
|
|
if (!new_storage)
|
2017-03-20 22:41:00 +01:00
|
|
|
aux::throw_ex<std::bad_alloc>();
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
char* src = m_storage.get();
|
|
|
|
char* dst = new_storage.get();
|
|
|
|
char const* const end = m_storage.get() + m_size;
|
2015-04-03 22:15:48 +02:00
|
|
|
while (src < end)
|
|
|
|
{
|
2015-04-18 04:33:39 +02:00
|
|
|
header_t* src_hdr = reinterpret_cast<header_t*>(src);
|
2017-03-20 22:41:00 +01:00
|
|
|
new (dst) header_t(*src_hdr);
|
|
|
|
src += sizeof(header_t) + src_hdr->pad_bytes;
|
|
|
|
dst += sizeof(header_t) + src_hdr->pad_bytes;
|
|
|
|
int const len = src_hdr->len;
|
|
|
|
TORRENT_ASSERT(src + len <= end);
|
2017-05-28 13:14:58 +02:00
|
|
|
// this is no-throw
|
2015-04-03 22:15:48 +02:00
|
|
|
src_hdr->move(dst, src);
|
2017-03-20 22:41:00 +01:00
|
|
|
src_hdr->~header_t();
|
|
|
|
src += len ;
|
|
|
|
dst += len;
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
m_storage.swap(new_storage);
|
2015-04-03 22:15:48 +02:00
|
|
|
m_capacity += amount_to_grow;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class U>
|
2017-05-28 13:14:58 +02:00
|
|
|
static void move(char* dst, char* src) noexcept
|
2015-04-03 22:15:48 +02:00
|
|
|
{
|
2017-05-28 13:14:58 +02:00
|
|
|
static_assert(std::is_nothrow_move_constructible<U>::value
|
|
|
|
, "heterogeneous queue only supports noexcept move constructible types");
|
|
|
|
static_assert(std::is_nothrow_destructible<U>::value
|
|
|
|
, "heterogeneous queue only supports noexcept destructible types");
|
2016-05-13 14:19:44 +02:00
|
|
|
U& rhs = *reinterpret_cast<U*>(src);
|
2017-03-20 22:41:00 +01:00
|
|
|
|
|
|
|
TORRENT_ASSERT((reinterpret_cast<std::uintptr_t>(dst) & (alignof(U) - 1)) == 0);
|
|
|
|
|
|
|
|
new (dst) U(std::move(rhs));
|
2016-05-13 14:19:44 +02:00
|
|
|
rhs.~U();
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
std::unique_ptr<char, aux::free_deleter> m_storage;
|
|
|
|
// number of bytes of storage allocated
|
|
|
|
int m_capacity = 0;
|
|
|
|
// the number of bytes used under m_storage
|
|
|
|
int m_size = 0;
|
|
|
|
// the number of objects allocated in m_storage
|
|
|
|
int m_num_items = 0;
|
2015-04-03 22:15:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|