2018-03-13 19:21:19 +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_SHARED_BUFFER_H
|
|
|
|
|
2018-10-05 00:37:03 +02:00
|
|
|
/// Like shared_ptr, this template shares ownership of an allocated buffer
|
|
|
|
///
|
2018-03-13 19:21:19 +01:00
|
|
|
template<class buffer>
|
|
|
|
struct ircd::buffer::shared_buffer
|
2019-09-03 02:24:39 +02:00
|
|
|
:buffer
|
2018-03-13 19:21:19 +01:00
|
|
|
{
|
2019-09-03 02:24:39 +02:00
|
|
|
std::shared_ptr<char> sp;
|
|
|
|
|
2019-09-02 01:15:34 +02:00
|
|
|
shared_buffer(unique_buffer<buffer> &&) noexcept;
|
2019-09-03 02:24:39 +02:00
|
|
|
explicit shared_buffer(const size_t &size, const size_t &align = 0);
|
2018-10-05 00:37:03 +02:00
|
|
|
explicit shared_buffer(const const_buffer &);
|
2019-09-03 02:24:39 +02:00
|
|
|
shared_buffer() = default;
|
2018-10-05 00:37:03 +02:00
|
|
|
shared_buffer(shared_buffer &&) = default;
|
|
|
|
shared_buffer(const shared_buffer &) = default;
|
|
|
|
shared_buffer &operator=(shared_buffer &&) = default;
|
|
|
|
shared_buffer &operator=(const shared_buffer &) = default;
|
2019-09-02 01:15:34 +02:00
|
|
|
shared_buffer &operator=(unique_buffer<buffer> &&) noexcept;
|
|
|
|
~shared_buffer() = default;
|
2018-03-13 19:21:19 +01:00
|
|
|
};
|
|
|
|
|
2019-09-02 01:15:34 +02:00
|
|
|
template<class buffer>
|
2019-09-03 02:24:39 +02:00
|
|
|
ircd::buffer::shared_buffer<buffer>::shared_buffer(const const_buffer &src)
|
|
|
|
:shared_buffer
|
2019-09-02 01:15:34 +02:00
|
|
|
{
|
2019-09-03 02:24:39 +02:00
|
|
|
unique_buffer<buffer>
|
|
|
|
{
|
|
|
|
src
|
|
|
|
}
|
2019-09-02 01:15:34 +02:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2018-03-13 19:21:19 +01:00
|
|
|
template<class buffer>
|
2019-09-03 02:24:39 +02:00
|
|
|
ircd::buffer::shared_buffer<buffer>::shared_buffer(const size_t &size,
|
|
|
|
const size_t &align)
|
2019-09-01 06:52:54 +02:00
|
|
|
:shared_buffer
|
|
|
|
{
|
2019-09-03 02:24:39 +02:00
|
|
|
unique_buffer<buffer>
|
|
|
|
{
|
|
|
|
size, align
|
|
|
|
}
|
2019-09-01 06:52:54 +02:00
|
|
|
}
|
2019-09-03 02:24:39 +02:00
|
|
|
{}
|
2019-09-01 06:52:54 +02:00
|
|
|
|
|
|
|
template<class buffer>
|
|
|
|
ircd::buffer::shared_buffer<buffer>::shared_buffer(unique_buffer<buffer> &&buf)
|
2019-09-02 01:15:34 +02:00
|
|
|
noexcept
|
2019-09-03 02:24:39 +02:00
|
|
|
:buffer
|
2018-03-13 19:21:19 +01:00
|
|
|
{
|
2019-09-03 02:24:39 +02:00
|
|
|
data(buf), size(buf)
|
2018-03-13 19:21:19 +01:00
|
|
|
}
|
2019-09-03 02:24:39 +02:00
|
|
|
,sp
|
2018-03-13 19:21:19 +01:00
|
|
|
{
|
2019-09-03 02:24:39 +02:00
|
|
|
data(buf), &std::free
|
2019-09-01 06:52:54 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
buf.release();
|
2018-03-13 19:21:19 +01:00
|
|
|
}
|
2018-10-05 00:37:03 +02:00
|
|
|
|
2019-09-02 01:15:34 +02:00
|
|
|
template<class buffer>
|
|
|
|
ircd::buffer::shared_buffer<buffer> &
|
|
|
|
ircd::buffer::shared_buffer<buffer>::operator=(unique_buffer<buffer> &&buf)
|
|
|
|
noexcept
|
|
|
|
{
|
2019-09-03 02:24:39 +02:00
|
|
|
sp.reset(data(buf), &std::free);
|
|
|
|
*static_cast<buffer *>(this) = buffer
|
2019-09-02 01:15:34 +02:00
|
|
|
{
|
2019-09-03 02:24:39 +02:00
|
|
|
sp.get(), size(buf)
|
2019-09-02 01:15:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
buf.release();
|
|
|
|
return *this;
|
|
|
|
}
|