// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2018 Jason Volk // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. #pragma once #define HAVE_IRCD_BUFFER_UNIQUE_BUFFER_H namespace ircd::buffer { std::unique_ptr aligned_alloc(const size_t &align, const size_t &size); } /// Like unique_ptr, this template holds ownership of an allocated buffer /// template struct ircd::buffer::unique_buffer :buffer { buffer release(); unique_buffer(const size_t &size, const size_t &align = 0); explicit unique_buffer(const buffer &); unique_buffer(); unique_buffer(unique_buffer &&) noexcept; unique_buffer(const unique_buffer &) = delete; unique_buffer &operator=(unique_buffer &&) noexcept; unique_buffer &operator=(const unique_buffer &) = delete; ~unique_buffer() noexcept; }; template ircd::buffer::unique_buffer::unique_buffer() :buffer { nullptr, nullptr } {} template ircd::buffer::unique_buffer::unique_buffer(const buffer &src) :unique_buffer { size(src) } { const mutable_buffer dst { const_cast(data(*this)), size(*this) }; copy(dst, src); } template ircd::buffer::unique_buffer::unique_buffer(const size_t &size, const size_t &align) :buffer { aligned_alloc(align, size).release(), size } { } template ircd::buffer::unique_buffer::unique_buffer(unique_buffer &&other) noexcept :buffer { std::move(static_cast(other)) } { get<0>(other) = nullptr; } template ircd::buffer::unique_buffer & ircd::buffer::unique_buffer::operator=(unique_buffer &&other) noexcept { this->~unique_buffer(); static_cast(*this) = std::move(static_cast(other)); get<0>(other) = nullptr; return *this; } template ircd::buffer::unique_buffer::~unique_buffer() noexcept { std::free(const_cast(data(*this))); } template buffer ircd::buffer::unique_buffer::release() { const buffer ret{static_cast(*this)}; static_cast(*this) = buffer{}; return ret; } inline std::unique_ptr ircd::buffer::aligned_alloc(const size_t &align, const size_t &size) { static const size_t &align_default{16}; const size_t &alignment { align?: align_default }; int errc; void *ret; switch((errc = ::posix_memalign(&ret, alignment, size))) { case 0: break; case int(std::errc::not_enough_memory): throw std::bad_alloc{}; default: throw std::system_error { errc, std::system_category() }; } return std::unique_ptr { reinterpret_cast(ret), std::free }; }