2016-11-29 16:23:38 +01:00
|
|
|
/*
|
|
|
|
* charybdis5
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_BUFFER_H
|
|
|
|
|
2017-09-21 04:31:54 +02:00
|
|
|
// Forward declarations from boost::asio because it is not included here. IRCd
|
|
|
|
// buffers are not based directly on the boost ones but are easily converted
|
|
|
|
// when passing our buffer to an asio function.
|
|
|
|
namespace boost::asio
|
|
|
|
{
|
|
|
|
struct const_buffer;
|
|
|
|
struct mutable_buffer;
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
/// Lightweight buffer interface compatible with boost::asio IO buffers and vectors
|
2017-09-14 20:28:38 +02:00
|
|
|
///
|
|
|
|
/// A const_buffer is a pair of iterators like `const char *` meant for sending
|
2017-09-23 01:53:46 +02:00
|
|
|
/// data; a mutable_buffer is a pair of iterators meant for receiving. In fact,
|
|
|
|
/// those types store signed char* and our convention is to represent readable
|
|
|
|
/// data with them. The const_raw_buffer and mutable_raw_buffer use unsigned
|
|
|
|
/// char* pairs and our convention is to represent unreadable/binary data with
|
|
|
|
/// them. Remember, these are conventions, not guarantees from these types. The
|
|
|
|
/// const_buffer is analogous (but doesn't inherit from) a string_view, so they
|
|
|
|
/// play well and convert easily between each other.
|
2017-09-14 20:28:38 +02:00
|
|
|
///
|
2017-09-23 01:53:46 +02:00
|
|
|
/// These templates offer tools for individual buffers as well as tools for
|
|
|
|
/// iterations of buffers. An iteration of buffers is an iovector that is
|
|
|
|
/// passed to our sockets etc. The ircd::iov template can host an iteration of
|
|
|
|
/// buffers. The `template template` functions are tools for a container of
|
|
|
|
/// buffers of any permutation.
|
2017-09-14 20:28:38 +02:00
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::buffer
|
|
|
|
{
|
|
|
|
template<class it> struct buffer;
|
|
|
|
struct const_buffer;
|
|
|
|
struct mutable_buffer;
|
2017-09-23 01:16:01 +02:00
|
|
|
struct const_raw_buffer;
|
|
|
|
struct mutable_raw_buffer;
|
2017-10-06 03:32:48 +02:00
|
|
|
template<class buffer, size_t SIZE> struct fixed_buffer;
|
2017-10-02 06:31:13 +02:00
|
|
|
template<class buffer, uint align = 16> struct unique_buffer;
|
2017-09-23 01:16:01 +02:00
|
|
|
|
2017-10-06 03:32:48 +02:00
|
|
|
template<size_t SIZE> using fixed_const_buffer = fixed_buffer<const_buffer, SIZE>;
|
|
|
|
template<size_t SIZE> using fixed_mutable_buffer = fixed_buffer<mutable_buffer, SIZE>;
|
|
|
|
template<size_t SIZE> using fixed_const_raw_buffer = fixed_buffer<const_raw_buffer, SIZE>;
|
|
|
|
template<size_t SIZE> using fixed_mutable_raw_buffer = fixed_buffer<mutable_raw_buffer, SIZE>;
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
template<template<class> class I> using const_buffers = I<const_buffer>;
|
|
|
|
template<template<class> class I> using mutable_buffers = I<mutable_buffer>;
|
2017-09-23 01:16:01 +02:00
|
|
|
template<template<class> class I> using const_raw_buffers = I<const_raw_buffer>;
|
|
|
|
template<template<class> class I> using mutable_raw_buffers = I<mutable_raw_buffer>;
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2017-10-01 04:11:09 +02:00
|
|
|
// Preconstructed null buffers
|
|
|
|
extern const mutable_buffer null_buffer;
|
|
|
|
extern const ilist<mutable_buffer> null_buffers;
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
// Single buffer iteration of contents
|
2017-09-08 16:47:51 +02:00
|
|
|
template<class it> const it &begin(const buffer<it> &buffer);
|
|
|
|
template<class it> const it &end(const buffer<it> &buffer);
|
|
|
|
template<class it> it &begin(buffer<it> &buffer);
|
|
|
|
template<class it> it &end(buffer<it> &buffer);
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class it> it rbegin(const buffer<it> &buffer);
|
2017-09-08 16:47:51 +02:00
|
|
|
template<class it> it rend(const buffer<it> &buffer);
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
// Single buffer tools
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class it> size_t size(const buffer<it> &buffer);
|
2017-09-08 16:47:51 +02:00
|
|
|
template<class it> const it &data(const buffer<it> &buffer);
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class it> size_t consume(buffer<it> &buffer, const size_t &bytes);
|
2017-10-01 04:11:09 +02:00
|
|
|
template<class it> it copy(it &dest, const it &stop, const const_raw_buffer &);
|
|
|
|
template<class it> size_t copy(const it &dest, const size_t &max, const const_raw_buffer &buffer);
|
|
|
|
size_t copy(const mutable_raw_buffer &dst, const const_raw_buffer &src);
|
2017-10-01 04:14:45 +02:00
|
|
|
void zero(const mutable_raw_buffer &buf);
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
// Iterable of buffers tools
|
|
|
|
template<template<class> class I, class T> size_t size(const I<T> &buffers);
|
2017-10-01 04:11:09 +02:00
|
|
|
template<template<class> class I, class T> size_t copy(const mutable_raw_buffer &, const I<T> &buffer);
|
2017-09-12 18:28:41 +02:00
|
|
|
template<template<class> class I, class T> size_t consume(I<T> &buffers, const size_t &bytes);
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2017-10-01 04:11:09 +02:00
|
|
|
// Convenience copy to stream
|
|
|
|
template<class it> std::ostream &operator<<(std::ostream &s, const buffer<it> &buffer);
|
|
|
|
template<template<class> class I, class T> std::ostream &operator<<(std::ostream &s, const I<T> &buffers);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Export these important aliases down to main ircd namespace
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
using buffer::const_buffer;
|
|
|
|
using buffer::mutable_buffer;
|
2017-09-23 01:16:01 +02:00
|
|
|
using buffer::const_raw_buffer;
|
|
|
|
using buffer::mutable_raw_buffer;
|
2017-10-06 03:32:48 +02:00
|
|
|
using buffer::fixed_buffer;
|
2017-08-28 23:51:22 +02:00
|
|
|
using buffer::unique_buffer;
|
2017-09-12 18:28:41 +02:00
|
|
|
using buffer::null_buffer;
|
2017-10-06 03:32:48 +02:00
|
|
|
using buffer::fixed_const_buffer;
|
|
|
|
using buffer::fixed_mutable_buffer;
|
|
|
|
using buffer::fixed_const_raw_buffer;
|
|
|
|
using buffer::fixed_mutable_raw_buffer;
|
2017-09-12 18:28:41 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
using buffer::const_buffers;
|
|
|
|
using buffer::mutable_buffers;
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-30 08:05:24 +02:00
|
|
|
/// Base for all buffer types
|
|
|
|
///
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class it>
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::buffer::buffer
|
2016-11-29 16:23:38 +01:00
|
|
|
:std::tuple<it, it>
|
|
|
|
{
|
2017-09-29 23:47:14 +02:00
|
|
|
using iterator = it;
|
|
|
|
using value_type = typename std::remove_pointer<iterator>::type;
|
2017-03-13 20:00:12 +01:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
operator string_view() const;
|
|
|
|
operator std::string_view() const;
|
|
|
|
explicit operator std::string() const;
|
|
|
|
|
2017-09-14 22:19:39 +02:00
|
|
|
auto &begin() const { return std::get<0>(*this); }
|
|
|
|
auto &begin() { return std::get<0>(*this); }
|
|
|
|
auto &end() const { return std::get<1>(*this); }
|
|
|
|
auto &end() { return std::get<1>(*this); }
|
|
|
|
|
2017-09-23 00:49:32 +02:00
|
|
|
auto &operator[](const size_t &i) const
|
|
|
|
{
|
|
|
|
return *(begin() + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &operator[](const size_t &i)
|
|
|
|
{
|
|
|
|
return *(begin() + i);
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:51 +02:00
|
|
|
buffer(const it &start, const it &stop)
|
2016-11-29 16:23:38 +01:00
|
|
|
:std::tuple<it, it>{start, stop}
|
|
|
|
{}
|
|
|
|
|
|
|
|
buffer(const it &start, const size_t &size)
|
|
|
|
:buffer{start, start + size}
|
|
|
|
{}
|
2017-09-08 16:47:51 +02:00
|
|
|
|
|
|
|
buffer()
|
|
|
|
:buffer{nullptr, nullptr}
|
|
|
|
{}
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
namespace ircd::buffer
|
|
|
|
{
|
|
|
|
template<class T> struct mutable_buffer_base;
|
|
|
|
}
|
|
|
|
|
2017-09-30 08:05:24 +02:00
|
|
|
/// Base for mutable buffers, or buffers which can be written to because they
|
|
|
|
/// are not const.
|
|
|
|
///
|
2017-09-29 23:47:14 +02:00
|
|
|
template<class T>
|
|
|
|
struct ircd::buffer::mutable_buffer_base
|
|
|
|
:buffer<T>
|
|
|
|
{
|
|
|
|
using iterator = typename buffer<T>::iterator;
|
|
|
|
using value_type = typename buffer<T>::value_type;
|
|
|
|
|
|
|
|
// Allows boost::spirit to append to the buffer; this means the size() of
|
|
|
|
// this buffer becomes a consumption counter and the real size of the buffer
|
|
|
|
// must be kept separately. This is the lowlevel basis for a stream buffer.
|
|
|
|
void insert(const iterator &it, const value_type &v)
|
|
|
|
{
|
|
|
|
assert(it >= this->begin() && it <= this->end());
|
|
|
|
memmove(it + 1, it, std::distance(it, this->end()));
|
|
|
|
*it = v;
|
|
|
|
++std::get<1>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
using buffer<T>::buffer;
|
2017-10-06 03:32:48 +02:00
|
|
|
|
|
|
|
mutable_buffer_base()
|
|
|
|
:buffer<iterator>{} {}
|
2017-10-03 12:55:01 +02:00
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
mutable_buffer_base(value_type (&buf)[SIZE])
|
|
|
|
:buffer<iterator>{buf, SIZE}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
mutable_buffer_base(std::array<value_type, SIZE> &buf)
|
|
|
|
:buffer<iterator>{reinterpret_cast<iterator>(buf.data()), SIZE}
|
|
|
|
{}
|
2017-09-29 23:47:14 +02:00
|
|
|
|
|
|
|
// lvalue string reference offered to write through to a std::string as
|
|
|
|
// the buffer. not explicit; should be hard to bind by accident...
|
|
|
|
mutable_buffer_base(std::string &buf)
|
2017-09-30 08:05:24 +02:00
|
|
|
:mutable_buffer_base<iterator>{const_cast<iterator>(buf.data()), buf.size()}
|
2017-09-29 23:47:14 +02:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-09-30 08:05:24 +02:00
|
|
|
/// A writable buffer of signed char data. Convention is for this buffer to
|
|
|
|
/// represent readable strings, which may or may not be null terminated. This
|
|
|
|
/// is just a convention; not a gurantee of the type.
|
|
|
|
///
|
2017-09-23 01:45:47 +02:00
|
|
|
struct ircd::buffer::mutable_buffer
|
2017-09-29 23:47:14 +02:00
|
|
|
:mutable_buffer_base<char *>
|
2017-09-23 01:45:47 +02:00
|
|
|
{
|
2017-09-29 23:47:14 +02:00
|
|
|
// Conversion offered for the analogous asio buffer
|
2017-09-23 01:45:47 +02:00
|
|
|
operator boost::asio::mutable_buffer() const;
|
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
using mutable_buffer_base<char *>::mutable_buffer_base;
|
2017-10-06 03:32:48 +02:00
|
|
|
|
|
|
|
mutable_buffer(const std::function<void (const mutable_buffer &)> &closure)
|
|
|
|
{
|
|
|
|
closure(*this);
|
|
|
|
}
|
2017-09-23 01:45:47 +02:00
|
|
|
};
|
|
|
|
|
2017-09-30 08:05:24 +02:00
|
|
|
/// A writable buffer of unsigned signed char data. Convention is for this
|
|
|
|
/// buffer to represent unreadable binary data. It may also be constructed
|
|
|
|
/// from a mutable_buffer because a buffer of any data (this one) can also
|
|
|
|
/// be readable data. The inverse is not true, mutable_buffer cannot be
|
|
|
|
/// constructed from this class.
|
|
|
|
///
|
2017-09-23 01:45:47 +02:00
|
|
|
struct ircd::buffer::mutable_raw_buffer
|
2017-09-29 23:47:14 +02:00
|
|
|
:mutable_buffer_base<unsigned char *>
|
2017-09-23 01:45:47 +02:00
|
|
|
{
|
2017-09-29 23:47:14 +02:00
|
|
|
// Conversion offered for the analogous asio buffer
|
2017-09-23 01:45:47 +02:00
|
|
|
operator boost::asio::mutable_buffer() const;
|
|
|
|
|
2017-10-03 12:55:01 +02:00
|
|
|
using mutable_buffer_base<unsigned char *>::mutable_buffer_base;
|
2017-09-23 01:45:47 +02:00
|
|
|
|
|
|
|
mutable_raw_buffer(const mutable_buffer &b)
|
2017-09-29 23:47:14 +02:00
|
|
|
:mutable_buffer_base<iterator>{reinterpret_cast<iterator>(data(b)), size(b)}
|
2017-09-23 01:45:47 +02:00
|
|
|
{}
|
2017-10-06 03:32:48 +02:00
|
|
|
|
|
|
|
mutable_raw_buffer(const std::function<void (const mutable_raw_buffer &)> &closure)
|
|
|
|
{
|
|
|
|
closure(*this);
|
|
|
|
}
|
2017-09-23 01:45:47 +02:00
|
|
|
};
|
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
namespace ircd::buffer
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-29 23:47:14 +02:00
|
|
|
template<class T> struct const_buffer_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct ircd::buffer::const_buffer_base
|
|
|
|
:buffer<T>
|
|
|
|
{
|
|
|
|
using iterator = typename buffer<T>::iterator;
|
|
|
|
using value_type = typename buffer<T>::value_type;
|
2017-10-03 12:55:01 +02:00
|
|
|
using mutable_value_type = typename std::remove_const<value_type>::type;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-10-03 12:55:01 +02:00
|
|
|
using buffer<T>::buffer;
|
2017-10-06 03:32:48 +02:00
|
|
|
|
|
|
|
const_buffer_base()
|
|
|
|
:buffer<iterator>{}
|
|
|
|
{}
|
2017-10-03 12:55:01 +02:00
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
const_buffer_base(const value_type (&buf)[SIZE])
|
|
|
|
:buffer<iterator>{buf, SIZE}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
const_buffer_base(const std::array<mutable_value_type, SIZE> &buf)
|
|
|
|
:buffer<iterator>{reinterpret_cast<iterator>(buf.data()), SIZE}
|
|
|
|
{}
|
2017-09-23 01:45:47 +02:00
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
const_buffer_base(const mutable_buffer &b)
|
2017-09-30 08:05:24 +02:00
|
|
|
:buffer<iterator>{reinterpret_cast<iterator>(data(b)), size(b)}
|
2017-03-11 04:31:20 +01:00
|
|
|
{}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
const_buffer_base(const string_view &s)
|
2017-09-30 08:05:24 +02:00
|
|
|
:buffer<iterator>{reinterpret_cast<iterator>(s.data()), s.size()}
|
2017-09-23 01:45:47 +02:00
|
|
|
{}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
explicit const_buffer_base(const std::string &s)
|
2017-09-30 08:05:24 +02:00
|
|
|
:buffer<iterator>{reinterpret_cast<iterator>(s.data()), s.size()}
|
2017-09-23 01:45:47 +02:00
|
|
|
{}
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
struct ircd::buffer::const_buffer
|
|
|
|
:const_buffer_base<const char *>
|
|
|
|
{
|
|
|
|
operator boost::asio::const_buffer() const;
|
|
|
|
|
|
|
|
using const_buffer_base<iterator>::const_buffer_base;
|
2017-10-03 12:55:01 +02:00
|
|
|
|
|
|
|
explicit const_buffer(const std::string &s)
|
|
|
|
:const_buffer_base{s}
|
|
|
|
{}
|
2017-09-29 23:47:14 +02:00
|
|
|
};
|
|
|
|
|
2017-09-23 01:16:01 +02:00
|
|
|
struct ircd::buffer::const_raw_buffer
|
2017-09-29 23:47:14 +02:00
|
|
|
:const_buffer_base<const unsigned char *>
|
2017-09-23 01:16:01 +02:00
|
|
|
{
|
|
|
|
operator boost::asio::const_buffer() const;
|
|
|
|
|
2017-09-29 23:47:14 +02:00
|
|
|
using const_buffer_base<iterator>::const_buffer_base;
|
2017-09-23 01:16:01 +02:00
|
|
|
|
2017-09-23 01:45:47 +02:00
|
|
|
const_raw_buffer(const const_buffer &b)
|
2017-10-03 12:55:01 +02:00
|
|
|
:const_buffer_base{reinterpret_cast<iterator>(data(b)), size(b)}
|
2017-09-23 01:45:47 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
const_raw_buffer(const mutable_raw_buffer &b)
|
2017-10-03 12:55:01 +02:00
|
|
|
:const_buffer_base{reinterpret_cast<iterator>(data(b)), size(b)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
explicit const_raw_buffer(const std::string &s)
|
|
|
|
:const_buffer_base{reinterpret_cast<iterator>(s.data()), s.size()}
|
2017-09-23 01:45:47 +02:00
|
|
|
{}
|
2017-09-23 01:16:01 +02:00
|
|
|
};
|
|
|
|
|
2017-10-06 03:32:48 +02:00
|
|
|
/// fixed_buffer wraps an std::array with construction and conversions apropos
|
2017-10-12 02:43:11 +02:00
|
|
|
/// the ircd::buffer suite. fixed_buffer should be punnable. Its only memory
|
|
|
|
/// footprint is the array itself and
|
2017-10-06 03:32:48 +02:00
|
|
|
///
|
|
|
|
template<class buffer,
|
|
|
|
size_t SIZE>
|
|
|
|
struct ircd::buffer::fixed_buffer
|
|
|
|
:std::array<typename std::remove_const<typename buffer::value_type>::type, SIZE>
|
|
|
|
{
|
|
|
|
using mutable_type = typename std::remove_const<typename buffer::value_type>::type;
|
|
|
|
using const_type = typename std::add_const<mutable_type>::type;
|
|
|
|
using array_type = std::array<mutable_type, SIZE>;
|
|
|
|
|
|
|
|
operator buffer() const
|
|
|
|
{
|
|
|
|
return { std::begin(*this), std::end(*this) };
|
|
|
|
}
|
|
|
|
|
|
|
|
operator buffer()
|
|
|
|
{
|
|
|
|
return { std::begin(*this), std::end(*this) };
|
|
|
|
}
|
|
|
|
|
|
|
|
using array_type::array_type;
|
|
|
|
fixed_buffer(const nullptr_t &)
|
|
|
|
:array_type{{0}}
|
|
|
|
{}
|
|
|
|
|
2017-10-12 02:51:43 +02:00
|
|
|
fixed_buffer(const std::function<void (const mutable_buffer &)> &closure)
|
2017-10-06 03:32:48 +02:00
|
|
|
{
|
2017-10-12 02:51:43 +02:00
|
|
|
closure(mutable_buffer{reinterpret_cast<mutable_buffer::iterator>(this->data()), this->size()});
|
2017-10-06 03:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fixed_buffer(buffer b)
|
|
|
|
:array_type{std::begin(b), std::end(b)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
fixed_buffer() = default;
|
|
|
|
};
|
|
|
|
|
2017-10-12 02:51:43 +02:00
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
// Assertion over an arbitrary but common template configuration.
|
|
|
|
std::is_standard_layout<ircd::buffer::fixed_buffer<ircd::buffer::const_buffer, 32>>::value,
|
|
|
|
"ircd::buffer::fixed_buffer must be standard layout"
|
|
|
|
);
|
|
|
|
|
2017-09-23 04:29:51 +02:00
|
|
|
/// Like unique_ptr, this template holds ownership of an allocated buffer
|
|
|
|
///
|
|
|
|
///
|
|
|
|
template<class buffer,
|
2017-10-02 06:31:13 +02:00
|
|
|
uint alignment>
|
2017-09-23 04:29:51 +02:00
|
|
|
struct ircd::buffer::unique_buffer
|
|
|
|
:buffer
|
|
|
|
{
|
|
|
|
unique_buffer(std::unique_ptr<uint8_t[]> &&, const size_t &size);
|
|
|
|
unique_buffer(const size_t &size);
|
|
|
|
unique_buffer() = default;
|
|
|
|
unique_buffer(unique_buffer &&) noexcept;
|
|
|
|
unique_buffer(const unique_buffer &) = delete;
|
|
|
|
~unique_buffer() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class buffer,
|
2017-10-02 06:31:13 +02:00
|
|
|
uint alignment>
|
2017-09-23 04:29:51 +02:00
|
|
|
ircd::buffer::unique_buffer<buffer, alignment>::unique_buffer(std::unique_ptr<uint8_t[]> &&b,
|
|
|
|
const size_t &size)
|
|
|
|
:buffer
|
|
|
|
{
|
2017-09-29 23:47:14 +02:00
|
|
|
typename buffer::iterator(b.release()), size
|
2017-09-23 04:29:51 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class buffer,
|
2017-10-02 06:31:13 +02:00
|
|
|
uint alignment>
|
2017-09-23 04:29:51 +02:00
|
|
|
ircd::buffer::unique_buffer<buffer, alignment>::unique_buffer(const size_t &size)
|
|
|
|
:unique_buffer<buffer, alignment>
|
|
|
|
{
|
|
|
|
std::unique_ptr<uint8_t[]>
|
|
|
|
{
|
2017-10-02 06:31:13 +02:00
|
|
|
//TODO: Can't use a template parameter to the attribute even though
|
|
|
|
// it's known at compile time. Hardcoding this until fixed with better
|
|
|
|
// aligned dynamic memory.
|
|
|
|
//new __attribute__((aligned(alignment))) uint8_t[size]
|
|
|
|
new __attribute__((aligned(16))) uint8_t[size]
|
2017-09-23 04:29:51 +02:00
|
|
|
},
|
|
|
|
size
|
|
|
|
}
|
|
|
|
{
|
2017-10-02 06:31:13 +02:00
|
|
|
// Alignment can only be 16 bytes for now
|
|
|
|
assert(alignment == 16);
|
2017-09-23 04:29:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class buffer,
|
2017-10-02 06:31:13 +02:00
|
|
|
uint alignment>
|
2017-09-23 04:29:51 +02:00
|
|
|
ircd::buffer::unique_buffer<buffer, alignment>::unique_buffer(unique_buffer &&other)
|
|
|
|
noexcept
|
|
|
|
:buffer
|
|
|
|
{
|
|
|
|
std::move(other)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
get<0>(other) = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class buffer,
|
2017-10-02 06:31:13 +02:00
|
|
|
uint alignment>
|
2017-09-23 04:29:51 +02:00
|
|
|
ircd::buffer::unique_buffer<buffer, alignment>::~unique_buffer()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
delete[] data(*this);
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
template<template<class>
|
|
|
|
class buffers,
|
|
|
|
class T>
|
2017-04-04 07:42:16 +02:00
|
|
|
std::ostream &
|
2017-09-12 18:28:41 +02:00
|
|
|
ircd::buffer::operator<<(std::ostream &s, const buffers<T> &b)
|
2017-04-04 07:42:16 +02:00
|
|
|
{
|
2017-09-12 18:28:41 +02:00
|
|
|
std::for_each(std::begin(b), std::end(b), [&s]
|
2017-09-14 20:28:38 +02:00
|
|
|
(const T &b)
|
2017-04-04 07:42:16 +02:00
|
|
|
{
|
2017-09-12 18:28:41 +02:00
|
|
|
s << b;
|
2017-04-04 07:42:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
template<template<class>
|
|
|
|
class buffers,
|
|
|
|
class T>
|
|
|
|
size_t
|
|
|
|
ircd::buffer::consume(buffers<T> &b,
|
|
|
|
const size_t &bytes)
|
2017-04-04 07:42:16 +02:00
|
|
|
{
|
2017-09-12 18:28:41 +02:00
|
|
|
size_t remain(bytes);
|
|
|
|
for(auto it(std::begin(b)); it != std::end(b) && remain > 0; ++it)
|
|
|
|
{
|
|
|
|
using buffer = typename buffers<T>::value_type;
|
2017-09-29 23:47:14 +02:00
|
|
|
using iterator = typename buffer::iterator;
|
2017-09-12 18:28:41 +02:00
|
|
|
|
|
|
|
buffer &b(const_cast<buffer &>(*it));
|
|
|
|
remain -= consume(b, remain);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes - remain;
|
2017-04-04 07:42:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
template<template<class>
|
|
|
|
class buffers,
|
|
|
|
class T>
|
2017-03-20 07:58:26 +01:00
|
|
|
size_t
|
2017-10-01 04:11:09 +02:00
|
|
|
ircd::buffer::copy(const mutable_raw_buffer &dest,
|
2017-09-12 18:28:41 +02:00
|
|
|
const buffers<T> &b)
|
2017-03-20 07:58:26 +01:00
|
|
|
{
|
|
|
|
size_t ret(0);
|
2017-09-14 20:28:38 +02:00
|
|
|
for(const T &b : b)
|
2017-10-01 04:11:09 +02:00
|
|
|
ret += copy(data(dest) + ret, size(dest) - ret, b);
|
2017-03-20 07:58:26 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
template<template<class>
|
|
|
|
class buffers,
|
|
|
|
class T>
|
|
|
|
size_t
|
|
|
|
ircd::buffer::size(const buffers<T> &b)
|
|
|
|
{
|
|
|
|
return std::accumulate(std::begin(b), std::end(b), size_t(0), []
|
2017-09-14 20:28:38 +02:00
|
|
|
(auto ret, const T &b)
|
2017-09-12 18:28:41 +02:00
|
|
|
{
|
|
|
|
return ret += size(b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
std::ostream &
|
|
|
|
ircd::buffer::operator<<(std::ostream &s, const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
s.write(data(buffer), size(buffer));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:28:11 +02:00
|
|
|
inline size_t
|
2017-10-01 04:11:09 +02:00
|
|
|
ircd::buffer::copy(const mutable_raw_buffer &dst,
|
|
|
|
const const_raw_buffer &src)
|
2017-09-14 20:28:11 +02:00
|
|
|
{
|
2017-10-01 04:11:09 +02:00
|
|
|
auto e{begin(dst)};
|
|
|
|
copy(e, end(dst), src);
|
|
|
|
return std::distance(begin(dst), e);
|
2017-09-14 20:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
size_t
|
2017-10-01 04:11:09 +02:00
|
|
|
ircd::buffer::copy(const it &dest,
|
2017-03-20 07:58:26 +01:00
|
|
|
const size_t &max,
|
2017-10-01 04:11:09 +02:00
|
|
|
const const_raw_buffer &src)
|
2017-03-20 07:58:26 +01:00
|
|
|
{
|
|
|
|
if(!max)
|
|
|
|
return 0;
|
|
|
|
|
2017-10-01 04:11:09 +02:00
|
|
|
it out{dest};
|
|
|
|
const it stop{dest + max - 1};
|
|
|
|
copy(out, stop, const_raw_buffer{src});
|
2017-03-20 07:58:26 +01:00
|
|
|
*out = '\0';
|
|
|
|
return std::distance(dest, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2017-10-01 04:11:09 +02:00
|
|
|
it
|
|
|
|
ircd::buffer::copy(it &dest,
|
|
|
|
const it &stop,
|
|
|
|
const const_raw_buffer &src)
|
2017-03-20 07:58:26 +01:00
|
|
|
{
|
2017-10-01 04:11:09 +02:00
|
|
|
const it ret{dest};
|
2017-03-20 07:58:26 +01:00
|
|
|
const size_t remain(stop - dest);
|
2017-10-01 04:11:09 +02:00
|
|
|
dest += std::min(size(src), remain);
|
|
|
|
memcpy(ret, data(src), dest - ret);
|
2017-03-20 07:58:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-13 20:00:12 +01:00
|
|
|
template<class it>
|
|
|
|
size_t
|
|
|
|
ircd::buffer::consume(buffer<it> &buffer,
|
|
|
|
const size_t &bytes)
|
|
|
|
{
|
|
|
|
get<0>(buffer) += std::min(size(buffer), bytes);
|
2017-09-14 20:28:38 +02:00
|
|
|
return size(buffer);
|
2017-03-13 20:00:12 +01:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
template<class it>
|
2017-09-08 16:47:51 +02:00
|
|
|
const it &
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::buffer::data(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<0>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
size_t
|
|
|
|
ircd::buffer::size(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
assert(get<0>(buffer) <= get<1>(buffer));
|
|
|
|
return std::distance(get<0>(buffer), get<1>(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
it
|
2017-09-08 16:47:51 +02:00
|
|
|
ircd::buffer::rend(const buffer<it> &buffer)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 16:47:51 +02:00
|
|
|
return std::reverse_iterator<it>(get<0>(buffer));
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
it
|
|
|
|
ircd::buffer::rbegin(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return std::reverse_iterator<it>(get<0>(buffer) + size(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2017-09-08 16:47:51 +02:00
|
|
|
it &
|
|
|
|
ircd::buffer::end(buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<1>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
it &
|
|
|
|
ircd::buffer::begin(buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<0>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
const it &
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::buffer::end(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<1>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2017-09-08 16:47:51 +02:00
|
|
|
const it &
|
|
|
|
ircd::buffer::begin(const buffer<it> &buffer)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 16:47:51 +02:00
|
|
|
return get<0>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
ircd::buffer::buffer<it>::operator std::string()
|
|
|
|
const
|
|
|
|
{
|
2017-10-01 04:11:09 +02:00
|
|
|
return { reinterpret_cast<const char *>(data(*this)), size(*this) };
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:51 +02:00
|
|
|
template<class it>
|
|
|
|
ircd::buffer::buffer<it>::operator std::string_view()
|
|
|
|
const
|
|
|
|
{
|
2017-10-01 04:11:09 +02:00
|
|
|
return { reinterpret_cast<const char *>(data(*this)), size(*this) };
|
2017-09-08 16:47:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
ircd::buffer::buffer<it>::operator string_view()
|
|
|
|
const
|
|
|
|
{
|
2017-10-01 04:11:09 +02:00
|
|
|
return { reinterpret_cast<const char *>(data(*this)), size(*this) };
|
2017-09-08 16:47:51 +02:00
|
|
|
}
|