From 5eacd22866a4f40e67c0b0493ff92203e1ea6e26 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 18 Oct 2018 09:01:25 -0700 Subject: [PATCH] ircd::buffer: Enable the templated value for unique_buffer alignment. --- include/ircd/buffer/unique_buffer.h | 64 +++++++++++++---------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/include/ircd/buffer/unique_buffer.h b/include/ircd/buffer/unique_buffer.h index 4a04282dd..06881904f 100644 --- a/include/ircd/buffer/unique_buffer.h +++ b/include/ircd/buffer/unique_buffer.h @@ -18,10 +18,11 @@ template &&, const size_t &size); explicit unique_buffer(const buffer &); unique_buffer(); unique_buffer(unique_buffer &&) noexcept; @@ -43,53 +44,28 @@ ircd::buffer::unique_buffer::unique_buffer() template ircd::buffer::unique_buffer::unique_buffer(const buffer &src) -:buffer{[&src]() -> buffer +:unique_buffer +{ + size(src) +} { - std::unique_ptr ret - { - new __attribute__((aligned(16))) char[size(src)] - }; - const mutable_buffer dst { - ret.get(), size(src) + const_cast(data(*this)), size(*this) }; copy(dst, src); - return dst; -}()} -{ } template ircd::buffer::unique_buffer::unique_buffer(const size_t &size) -:unique_buffer -{ - std::unique_ptr - { - //TODO: Can't use a template parameter to the attribute even though - // it's known at compile time. Hardcoding this until fixed with better - // aligned dynamic memory. - //new __attribute__((aligned(alignment))) char[size] - new __attribute__((aligned(16))) char[size] - }, - size -} -{ - // Alignment can only be 16 bytes for now - assert(alignment == 16); -} - -template -ircd::buffer::unique_buffer::unique_buffer(std::unique_ptr &&b, - const size_t &size) :buffer { - typename buffer::iterator(b.release()), size + allocate(size), size +} +{ } -{} template @@ -122,7 +98,7 @@ template::~unique_buffer() noexcept { - delete[] data(*this); + std::free(const_cast(data(*this))); } template::release() static_cast(*this) = buffer{}; return ret; } + +template +char * +ircd::buffer::unique_buffer::allocate(const size_t &size) +{ + int errc; + void *ret; + if(unlikely((errc = ::posix_memalign(&ret, alignment, size)) != 0)) + throw std::system_error + { + errc, std::system_category() + }; + + assert(errc == 0); + assert(ret != nullptr); + return reinterpret_cast(ret); +}