From 062ea9ddf59ece039c642acce3866ef34872d355 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 1 Sep 2019 16:15:34 -0700 Subject: [PATCH] ircd::buffer: Support aligned allocations and aligned unique_buffer conversion. --- include/ircd/buffer/shared_buffer.h | 47 +++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/include/ircd/buffer/shared_buffer.h b/include/ircd/buffer/shared_buffer.h index 50dc2269e..ae130c65c 100644 --- a/include/ircd/buffer/shared_buffer.h +++ b/include/ircd/buffer/shared_buffer.h @@ -18,16 +18,26 @@ struct ircd::buffer::shared_buffer :std::shared_ptr ,buffer { - shared_buffer(const size_t &size); - shared_buffer(unique_buffer &&); + shared_buffer(); + explicit shared_buffer(const size_t &size, const size_t &align = 0); + shared_buffer(unique_buffer &&) noexcept; explicit shared_buffer(const const_buffer &); - shared_buffer() = default; shared_buffer(shared_buffer &&) = default; shared_buffer(const shared_buffer &) = default; shared_buffer &operator=(shared_buffer &&) = default; shared_buffer &operator=(const shared_buffer &) = default; + shared_buffer &operator=(unique_buffer &&) noexcept; + ~shared_buffer() = default; }; +template +ircd::buffer::shared_buffer::shared_buffer() +:std::shared_ptr +{ + nullptr, std::free +} +{} + template ircd::buffer::shared_buffer::shared_buffer(const const_buffer &src) :shared_buffer @@ -42,30 +52,43 @@ ircd::buffer::shared_buffer::shared_buffer(const const_buffer &src) template ircd::buffer::shared_buffer::shared_buffer(unique_buffer &&buf) +noexcept :std::shared_ptr { - data(buf), - std::default_delete() + data(buf), std::free } ,buffer { - this->std::shared_ptr::get(), - size(buf) + this->std::shared_ptr::get(), size(buf) } { buf.release(); } template -ircd::buffer::shared_buffer::shared_buffer(const size_t &size) +ircd::buffer::shared_buffer::shared_buffer(const size_t &size, + const size_t &align) :std::shared_ptr { - new __attribute__((aligned(16))) char[size], - std::default_delete() + allocator::aligned_alloc(align, size).release(), std::free } ,buffer { - this->std::shared_ptr::get(), - size + this->std::shared_ptr::get(), size } {} + +template +ircd::buffer::shared_buffer & +ircd::buffer::shared_buffer::operator=(unique_buffer &&buf) +noexcept +{ + this->std::shared_ptr::reset(data(buf), std::free); + *static_cast(this) = + { + this->std::shared_ptr::get(), size(buf) + }; + + buf.release(); + return *this; +}