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
|
|
|
{
|
2022-05-03 04:20:53 +02:00
|
|
|
[[using gnu: malloc, alloc_align(1), alloc_size(2), returns_nonnull, warn_unused_result]]
|
|
|
|
char *allocate(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
|
|
|
|
///
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
2018-03-13 05:17:09 +01:00
|
|
|
struct ircd::buffer::unique_buffer
|
2020-10-29 08:08:06 +01:00
|
|
|
:buffer_type
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
2019-09-15 02:12:55 +02:00
|
|
|
explicit operator bool() const;
|
2020-10-29 08:08:06 +01:00
|
|
|
bool operator!() const;
|
2019-09-15 02:12:55 +02:00
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
buffer_type release();
|
2018-08-19 02:43:59 +02:00
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::unique_buffer(const const_buffer &src)
|
2022-05-03 04:20:53 +02:00
|
|
|
:unique_buffer
|
2018-10-18 18:01:25 +02:00
|
|
|
{
|
2022-05-03 04:20:53 +02:00
|
|
|
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));
|
2022-03-26 03:02:02 +01:00
|
|
|
const auto ptr(std::get<0>(*this));
|
2018-08-19 02:41:19 +02:00
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
2022-03-26 03:02:02 +01:00
|
|
|
const_cast<char *>(ptr), size(src)
|
2018-08-19 02:41:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
2018-03-13 05:17:09 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::unique_buffer(const size_t &size,
|
2022-05-03 04:20:53 +02:00
|
|
|
const size_t &a)
|
2020-10-29 08:08:06 +01:00
|
|
|
:buffer_type
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
2022-05-03 04:20:53 +02:00
|
|
|
allocator::allocate(a?: sizeof(void *), pad_to(size, a?: sizeof(void *))),
|
|
|
|
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
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::unique_buffer(unique_buffer &&other)
|
2018-03-13 05:17:09 +01:00
|
|
|
noexcept
|
2020-10-29 08:08:06 +01:00
|
|
|
:buffer_type
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01: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 18:57:57 +02:00
|
|
|
& noexcept
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
|
|
|
this->~unique_buffer();
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
static_cast<buffer_type &>(*this) = other.release();
|
2019-04-10 22:38:47 +02:00
|
|
|
assert(std::get<0>(other) == nullptr);
|
2018-03-13 05:17:09 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
|
|
|
inline
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::~unique_buffer()
|
2018-03-13 05:17:09 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2022-03-26 03:02:02 +01:00
|
|
|
const auto ptr(std::get<0>(*this));
|
|
|
|
std::free(const_cast<char *>(ptr));
|
2018-03-13 05:17:09 +01:00
|
|
|
}
|
2018-08-19 02:43:59 +02:00
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
|
|
|
inline buffer_type
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::release()
|
2018-08-19 02:43:59 +02:00
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
const buffer_type 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
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
|
|
|
inline bool
|
|
|
|
ircd::buffer::unique_buffer<buffer_type>::operator!()
|
2019-09-15 02:12:55 +02:00
|
|
|
const
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
return this->buffer_type::empty();
|
2019-09-15 02:12:55 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type>
|
|
|
|
inline ircd::buffer::unique_buffer<buffer_type>::operator
|
|
|
|
bool()
|
2019-09-15 02:12:55 +02:00
|
|
|
const
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
return !this->buffer_type::empty();
|
2019-09-15 02:12:55 +02:00
|
|
|
}
|