Files
boost_beast/include/beast/core/handler_alloc.hpp
T

141 lines
3.3 KiB
C++
Raw Normal View History

2016-04-19 20:29:16 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-04-19 20:29:16 -04:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2016-04-01 11:46:32 -04:00
2016-04-19 20:29:16 -04:00
#ifndef BEAST_HANDLER_ALLOC_HPP
#define BEAST_HANDLER_ALLOC_HPP
2016-04-01 11:46:32 -04:00
2017-01-05 09:07:18 -05:00
#include <beast/core/handler_helpers.hpp>
2016-04-01 11:46:32 -04:00
#include <cstdlib>
#include <memory>
#include <type_traits>
#include <utility>
namespace beast {
// Guidance from
// http://howardhinnant.github.io/allocator_boilerplate.html
/** An allocator that uses handler customizations.
This allocator uses the handler customizations `asio_handler_allocate`
2016-04-22 19:02:07 -04:00
and `asio_handler_deallocate` to manage memory. It meets the requirements
2016-10-14 18:58:52 -04:00
of @b Allocator and can be used anywhere a `std::allocator` is
2016-04-22 19:02:07 -04:00
accepted.
2016-04-01 11:46:32 -04:00
2016-04-22 19:02:07 -04:00
@tparam T The type of objects allocated by the allocator.
2016-04-01 11:46:32 -04:00
2017-02-06 10:56:37 -05:00
@tparam Handler The type of handler.
2016-04-22 19:02:07 -04:00
2017-01-05 09:09:59 -05:00
@note Memory allocated by this allocator must be freed before
2017-02-06 10:56:37 -05:00
the handler is invoked or undefined behavior results. This behavior
is described as the "deallocate before invocation" Asio guarantee.
2016-04-01 11:46:32 -04:00
*/
2016-04-22 19:02:07 -04:00
#if GENERATING_DOCS
2017-02-06 10:56:37 -05:00
template<class T, class Handler>
2016-04-22 19:02:07 -04:00
class handler_alloc;
#else
2017-02-06 10:56:37 -05:00
template<class T, class Handler>
2016-04-01 11:46:32 -04:00
class handler_alloc
{
private:
// We want a partial template specialization as a friend
// but that isn't allowed so we friend all versions. This
2017-02-06 10:56:37 -05:00
// should produce a compile error if Handler is not
2016-04-01 11:46:32 -04:00
// constructible from H.
//
2016-08-26 08:01:44 -04:00
template<class U, class H>
2016-04-01 11:46:32 -04:00
friend class handler_alloc;
2017-02-06 10:56:37 -05:00
Handler& h_;
2016-04-01 11:46:32 -04:00
public:
using value_type = T;
using is_always_equal = std::true_type;
2016-09-26 07:57:59 -04:00
template<class U>
struct rebind
{
2017-02-06 10:56:37 -05:00
using other = handler_alloc<U, Handler>;
2016-09-26 07:57:59 -04:00
};
2016-04-01 11:46:32 -04:00
handler_alloc() = delete;
handler_alloc(handler_alloc&&) = default;
handler_alloc(handler_alloc const&) = default;
handler_alloc& operator=(handler_alloc&&) = default;
handler_alloc& operator=(handler_alloc const&) = default;
/** Construct the allocator.
2017-01-05 09:09:59 -05:00
A reference of the handler is stored. The handler must
remain valid for at least the lifetime of the allocator.
2016-04-01 11:46:32 -04:00
*/
explicit
2017-02-06 10:56:37 -05:00
handler_alloc(Handler& h)
2016-04-01 11:46:32 -04:00
: h_(h)
{
}
2017-01-05 09:09:59 -05:00
/// Copy constructor
2016-04-01 11:46:32 -04:00
template<class U>
handler_alloc(
2017-02-06 10:56:37 -05:00
handler_alloc<U, Handler> const& other)
2016-04-01 11:46:32 -04:00
: h_(other.h_)
{
}
value_type*
allocate(std::ptrdiff_t n)
{
auto const size = n * sizeof(T);
return static_cast<value_type*>(
2017-01-05 09:07:18 -05:00
beast_asio_helpers::allocate(
2016-04-01 11:46:32 -04:00
size, h_));
}
void
deallocate(value_type* p, std::ptrdiff_t n)
{
auto const size = n * sizeof(T);
2017-01-05 09:07:18 -05:00
beast_asio_helpers::deallocate(
2016-04-01 11:46:32 -04:00
p, size, h_);
}
#ifdef _MSC_VER
// Work-around for MSVC not using allocator_traits
// in the implementation of shared_ptr
//
void
destroy(T* t)
{
t->~T();
}
#endif
template<class U>
friend
bool
operator==(handler_alloc const& lhs,
2017-02-06 10:56:37 -05:00
handler_alloc<U, Handler> const& rhs)
2016-04-01 11:46:32 -04:00
{
return true;
}
template<class U>
friend
bool
operator!=(handler_alloc const& lhs,
2017-02-06 10:56:37 -05:00
handler_alloc<U, Handler> const& rhs)
2016-04-01 11:46:32 -04:00
{
2017-02-06 10:56:37 -05:00
return ! (lhs == rhs);
2016-04-01 11:46:32 -04:00
}
};
2016-04-22 19:02:07 -04:00
#endif
2016-04-01 11:46:32 -04:00
} // beast
#endif