2018-03-12 21:17:09 -07:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2019-04-18 00:17:33 -07:00
|
|
|
// Fwd decl; circ dep.
|
|
|
|
namespace ircd::allocator
|
2018-11-01 23:27:51 -07:00
|
|
|
{
|
2019-04-18 00:17:33 -07:00
|
|
|
std::unique_ptr<char, decltype(&std::free)>
|
|
|
|
aligned_alloc(const size_t &align, const size_t &size);
|
2018-11-01 23:27:51 -07:00
|
|
|
}
|
|
|
|
|
2018-03-12 21:17:09 -07:00
|
|
|
/// Like unique_ptr, this template holds ownership of an allocated buffer
|
|
|
|
///
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
2018-03-12 21:17:09 -07:00
|
|
|
struct ircd::buffer::unique_buffer
|
2020-10-29 00:08:06 -07:00
|
|
|
:buffer_type
|
2018-03-12 21:17:09 -07:00
|
|
|
{
|
2019-09-14 17:12:55 -07:00
|
|
|
explicit operator bool() const;
|
2020-10-29 00:08:06 -07:00
|
|
|
bool operator!() const;
|
2019-09-14 17:12:55 -07:00
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
buffer_type release();
|
2018-08-18 17:43:59 -07:00
|
|
|
|
2019-04-12 09:57:57 -07:00
|
|
|
unique_buffer() = default;
|
2018-11-09 15:55:20 -08:00
|
|
|
unique_buffer(const size_t &size, const size_t &align = 0);
|
2019-04-18 00:17:58 -07:00
|
|
|
explicit unique_buffer(const const_buffer &);
|
|
|
|
unique_buffer(unique_buffer &&) noexcept;
|
2018-03-12 21:17:09 -07:00
|
|
|
unique_buffer(const unique_buffer &) = delete;
|
2019-04-12 09:57:57 -07:00
|
|
|
unique_buffer &operator=(unique_buffer &&) & noexcept;
|
2018-03-12 21:17:09 -07:00
|
|
|
unique_buffer &operator=(const unique_buffer &) = delete;
|
|
|
|
~unique_buffer() noexcept;
|
|
|
|
};
|
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::unique_buffer(const const_buffer &src)
|
|
|
|
:buffer_type
|
2018-10-18 09:01:25 -07:00
|
|
|
{
|
2019-04-18 00:17:58 -07:00
|
|
|
allocator::aligned_alloc(0, ircd::buffer::size(src)).release(), ircd::buffer::size(src)
|
2018-10-18 09:01:25 -07:00
|
|
|
}
|
2018-08-18 17:41:19 -07:00
|
|
|
{
|
2019-04-18 00:17:58 -07:00
|
|
|
using ircd::buffer::size;
|
|
|
|
|
2019-04-10 13:38:47 -07:00
|
|
|
assert(this->begin() != nullptr);
|
|
|
|
assert(size(src) == size(*this));
|
2018-08-18 17:41:19 -07:00
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
2019-04-10 13:38:47 -07:00
|
|
|
const_cast<char *>(this->begin()), size(src)
|
2018-08-18 17:41:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
2018-03-12 21:17:09 -07:00
|
|
|
}
|
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::unique_buffer(const size_t &size,
|
|
|
|
const size_t &align)
|
|
|
|
:buffer_type
|
2018-03-12 21:17:09 -07:00
|
|
|
{
|
2019-04-18 00:17:58 -07:00
|
|
|
allocator::aligned_alloc(align, size).release(), size
|
2018-03-12 21:17:09 -07:00
|
|
|
}
|
2019-04-12 09:57:57 -07:00
|
|
|
{}
|
2018-08-18 17:41:19 -07:00
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::unique_buffer(unique_buffer &&other)
|
2018-03-12 21:17:09 -07:00
|
|
|
noexcept
|
2020-10-29 00:08:06 -07:00
|
|
|
:buffer_type
|
2018-03-12 21:17:09 -07:00
|
|
|
{
|
2019-04-10 13:38:47 -07:00
|
|
|
other.release()
|
2018-03-12 21:17:09 -07:00
|
|
|
}
|
|
|
|
{
|
2019-04-10 13:38:47 -07:00
|
|
|
assert(std::get<0>(other) == nullptr);
|
2018-03-12 21:17:09 -07:00
|
|
|
}
|
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline ircd::buffer::unique_buffer<buffer_type> &
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::operator=(unique_buffer &&other)
|
2019-04-12 09:57:57 -07:00
|
|
|
& noexcept
|
2018-03-12 21:17:09 -07:00
|
|
|
{
|
|
|
|
this->~unique_buffer();
|
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
static_cast<buffer_type &>(*this) = other.release();
|
2019-04-10 13:38:47 -07:00
|
|
|
assert(std::get<0>(other) == nullptr);
|
2018-03-12 21:17:09 -07:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::~unique_buffer()
|
2018-03-12 21:17:09 -07:00
|
|
|
noexcept
|
|
|
|
{
|
2019-04-10 13:38:47 -07:00
|
|
|
std::free(const_cast<char *>(this->begin()));
|
2018-03-12 21:17:09 -07:00
|
|
|
}
|
2018-08-18 17:43:59 -07:00
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline buffer_type
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::release()
|
2018-08-18 17:43:59 -07:00
|
|
|
{
|
2020-10-29 00:08:06 -07:00
|
|
|
const buffer_type ret{*this};
|
2019-04-18 00:17:58 -07:00
|
|
|
std::get<0>(*this) = nullptr;
|
|
|
|
std::get<1>(*this) = nullptr;
|
2018-08-18 17:43:59 -07:00
|
|
|
return ret;
|
|
|
|
}
|
2019-09-14 17:12:55 -07:00
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline bool
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::operator!()
|
2019-09-14 17:12:55 -07:00
|
|
|
const
|
|
|
|
{
|
2020-10-29 00:08:06 -07:00
|
|
|
return this->buffer_type::empty();
|
2019-09-14 17:12:55 -07:00
|
|
|
}
|
|
|
|
|
2020-10-29 00:08:06 -07:00
|
|
|
template<class buffer_type>
|
|
|
|
inline ircd::buffer::unique_buffer<buffer_type>::operator
|
|
|
|
bool()
|
2019-09-14 17:12:55 -07:00
|
|
|
const
|
|
|
|
{
|
2020-10-29 00:08:06 -07:00
|
|
|
return !this->buffer_type::empty();
|
2019-09-14 17:12:55 -07:00
|
|
|
}
|