Add construct, destroy to handler_alloc

fix #432
This commit is contained in:
Vinnie Falco
2017-06-08 14:46:42 -07:00
parent 196d9c60fa
commit 7b56f352b4
3 changed files with 32 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ Version 51
* Use BOOST_FALLTHROUGH
* Use BOOST_STRINGIZE
* DynamicBuffer benchmarks
* Add construct, destroy to handler_alloc
API Changes:

View File

@@ -10,6 +10,7 @@
#include <beast/config.hpp>
#include <boost/asio/handler_alloc_hook.hpp>
#include <boost/config.hpp>
#include <cstddef>
#include <cstdlib>
#include <memory>
@@ -96,7 +97,7 @@ public:
}
value_type*
allocate(std::ptrdiff_t n)
allocate(size_type n)
{
auto const size = n * sizeof(T);
using boost::asio::asio_handler_allocate;
@@ -105,21 +106,28 @@ public:
}
void
deallocate(value_type* p, std::ptrdiff_t n)
deallocate(value_type* p, size_type n)
{
auto const size = n * sizeof(T);
using boost::asio::asio_handler_deallocate;
asio_handler_deallocate(p, size, std::addressof(h_));
}
#ifdef _MSC_VER
// Work-around for MSVC not using allocator_traits
// in the implementation of shared_ptr
//
//#if BOOST_WORKAROUND(BOOST_GCC, < 60000) // Works, but too coarse
#if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000
template<class U, class... Args>
void
destroy(T* t)
construct(U* ptr, Args&&... args)
{
t->~T();
::new((void*)ptr) U(std::forward<Args>(args)...);
}
template<class U>
void
destroy(U* ptr)
{
ptr->~U();
}
#endif

View File

@@ -9,6 +9,7 @@
#include <beast/core/handler_alloc.hpp>
#include <beast/unit_test/suite.hpp>
#include <list>
#include <vector>
namespace beast {
@@ -24,9 +25,23 @@ public:
}
};
// https://github.com/vinniefalco/Beast/issues/432
void
testRegression432()
{
handler h;
handler_alloc<int, handler> a{h};
std::list<int, handler_alloc<int, handler>> v{a};
v.push_back(1);
v.push_back(2);
v.push_back(3);
}
void
run() override
{
testRegression432();
handler h;
handler h2;
handler_alloc<char, handler> a1{h};