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