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
|
|
|
|
|
2018-11-02 07:27:51 +01:00
|
|
|
namespace ircd::buffer
|
|
|
|
{
|
|
|
|
std::unique_ptr<char, decltype(&std::free)> aligned_alloc(const size_t &align, const size_t &size);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2018-08-19 02:43:59 +02:00
|
|
|
buffer release();
|
|
|
|
|
2018-11-10 00:55:20 +01:00
|
|
|
unique_buffer(const size_t &size, const size_t &align = 0);
|
2018-08-19 02:41:19 +02:00
|
|
|
explicit unique_buffer(const buffer &);
|
2019-04-10 22:38:47 +02:00
|
|
|
unique_buffer() = default;
|
2018-03-13 05:17:09 +01:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2018-11-10 00:55:20 +01:00
|
|
|
template<class buffer>
|
|
|
|
ircd::buffer::unique_buffer<buffer>::unique_buffer(const buffer &src)
|
2018-10-18 18:01:25 +02:00
|
|
|
:unique_buffer
|
|
|
|
{
|
|
|
|
size(src)
|
|
|
|
}
|
2018-08-19 02:41:19 +02:00
|
|
|
{
|
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
|
|
|
{
|
2018-11-10 00:55:20 +01:00
|
|
|
aligned_alloc(align, size).release(), size
|
2018-03-13 05:17:09 +01: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)
|
2018-03-13 05:17:09 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
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};
|
|
|
|
this->begin() = nullptr;
|
2018-08-19 02:43:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2018-10-18 18:01:25 +02:00
|
|
|
|
2018-11-02 07:27:51 +01:00
|
|
|
inline std::unique_ptr<char, decltype(&std::free)>
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-10-18 18:01:25 +02:00
|
|
|
int errc;
|
|
|
|
void *ret;
|
2018-11-13 01:49:06 +01:00
|
|
|
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()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-02 07:27:51 +01:00
|
|
|
return std::unique_ptr<char, decltype(&std::free)>
|
|
|
|
{
|
2019-04-10 22:38:47 +02:00
|
|
|
reinterpret_cast<char *>(ret), &std::free
|
2018-11-02 07:27:51 +01:00
|
|
|
};
|
2018-10-18 18:01:25 +02:00
|
|
|
}
|