// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2018 Jason Volk // // 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_H // 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; } /// Lightweight buffer interface compatible with boost::asio IO buffers and vectors /// /// A const_buffer is a pair of iterators like `const char *` meant for sending /// data; a mutable_buffer is a pair of iterators meant for receiving. /// /// 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. /// namespace ircd::buffer { template struct buffer; struct const_buffer; struct mutable_buffer; struct window_buffer; template struct fixed_buffer; template struct unique_buffer; template using fixed_const_buffer = fixed_buffer; template using fixed_mutable_buffer = fixed_buffer; template class I> using const_buffers = I; template class I> using mutable_buffers = I; // Single buffer iteration of contents template const it &begin(const buffer &buffer); template const it &end(const buffer &buffer); template it &begin(buffer &buffer); template it &end(buffer &buffer); template std::reverse_iterator rbegin(const buffer &buffer); template std::reverse_iterator rend(const buffer &buffer); // Single buffer tools template bool null(const buffer &buffer); template bool full(const buffer &buffer); template bool empty(const buffer &buffer); template bool operator!(const buffer &buffer); template size_t size(const buffer &buffer); template const it &data(const buffer &buffer); template size_t consume(buffer &buffer, const size_t &bytes); template buffer operator+(const buffer &buffer, const size_t &bytes); template it copy(it &dest, const it &stop, const const_buffer &); size_t copy(const mutable_buffer &dst, const const_buffer &src); size_t reverse(const mutable_buffer &dst, const const_buffer &src); void reverse(const mutable_buffer &buf); void zero(const mutable_buffer &buf); // Iterable of buffers tools template class I, class T> size_t size(const I &buffers); template class I, class T> size_t copy(const mutable_buffer &, const I &buffer); template class I, class T> size_t consume(I &buffers, const size_t &bytes); // Convenience copy to std stream template std::ostream &operator<<(std::ostream &s, const buffer &buffer); template class I, class T> std::ostream &operator<<(std::ostream &s, const I &buffers); // Preconstructed null buffers extern const mutable_buffer null_buffer; extern const ilist null_buffers; } // Export these important aliases down to main ircd namespace namespace ircd { using buffer::const_buffer; using buffer::mutable_buffer; using buffer::fixed_buffer; using buffer::unique_buffer; using buffer::null_buffer; using buffer::window_buffer; using buffer::fixed_const_buffer; using buffer::fixed_mutable_buffer; using buffer::const_buffers; using buffer::mutable_buffers; using buffer::size; using buffer::data; using buffer::copy; using buffer::consume; } /// Base for all buffer types /// template struct ircd::buffer::buffer :std::tuple { using iterator = it; using value_type = typename std::remove_pointer::type; operator string_view() const; operator std::string_view() const; explicit operator std::string() const; 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); } auto &operator[](const size_t &i) const { return *(begin() + i); } auto &operator[](const size_t &i) { return *(begin() + i); } buffer(const it &start, const it &stop) :std::tuple{start, stop} {} buffer(const it &start, const size_t &size) :buffer{start, start + size} {} buffer() :buffer{nullptr, nullptr} {} }; /// Base for mutable buffers, or buffers which can be written to because they /// are not const. /// struct ircd::buffer::mutable_buffer :buffer { // Conversion offered for the analogous asio buffer operator boost::asio::mutable_buffer() const; // 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(char *const &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::buffer; mutable_buffer() :buffer{} {} mutable_buffer(const buffer &b) :buffer{b} {} template mutable_buffer(char (&buf)[SIZE]) :buffer{buf, SIZE} {} template mutable_buffer(std::array &buf) :buffer{buf.data(), SIZE} {} // 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(std::string &buf) :mutable_buffer{const_cast(buf.data()), buf.size()} {} mutable_buffer(const std::function &closure) { closure(*this); } }; struct ircd::buffer::const_buffer :buffer { operator boost::asio::const_buffer() const; using buffer::buffer; const_buffer() :buffer{} {} const_buffer(const buffer &b) :buffer{b} {} const_buffer(const buffer &b) :buffer{data(b), size(b)} {} template const_buffer(const char (&buf)[SIZE]) :buffer{buf, SIZE} {} template const_buffer(const std::array &buf) :buffer{reinterpret_cast(buf.data()), SIZE} {} const_buffer(const mutable_buffer &b) :buffer{data(b), size(b)} {} const_buffer(const string_view &s) :buffer{data(s), size(s)} {} }; /// fixed_buffer wraps an std::array with construction and conversions apropos /// the ircd::buffer suite. fixed_buffer should be punnable. Its only memory /// footprint is the array itself and /// template struct ircd::buffer::fixed_buffer :std::array::type, SIZE> { using mutable_type = typename std::remove_const::type; using const_type = typename std::add_const::type; using array_type = std::array; 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}} {} fixed_buffer(const std::function &closure) { closure(mutable_buffer{reinterpret_cast(this->data()), this->size()}); } fixed_buffer(buffer b) :array_type{std::begin(b), std::end(b)} {} fixed_buffer() = default; }; static_assert ( // Assertion over an arbitrary but common template configuration. std::is_standard_layout>::value, "ircd::buffer::fixed_buffer must be standard layout" ); /// The window_buffer is just two mutable_buffers. One of the two buffers /// just spans an underlying space and the other buffer is a window of the /// remaining space which shrinks toward the end as the space is consumed. /// The window_buffer object inherits from the latter, so it always has the /// appearance of a mutable_buffer windowing on the the next place to write. /// /// The recommended usage of this device is actually through the operator() /// closure, which will automatically resize the window based on the return /// value in the closure. /// struct ircd::buffer::window_buffer :mutable_buffer { mutable_buffer base; /// Bytes remaining for writes to the stream buffer (same as size(*this)) size_t remaining() const { assert(begin() <= base.end()); const size_t ret(std::distance(begin(), base.end())); assert(ret == size(*this)); return ret; } /// Bytes used by writes to the stream buffer size_t consumed() const { assert(begin() >= base.begin()); assert(begin() <= base.end()); return std::distance(base.begin(), begin()); } /// View the completed portion of the stream const_buffer completed() const { assert(base.begin() <= begin()); assert(base.begin() + consumed() <= base.end()); return { base.begin(), base.begin() + consumed() }; } /// View the completed portion of the stream mutable_buffer completed() { assert(base.begin() <= begin()); assert(base.begin() + consumed() <= base.end()); return { base.begin(), base.begin() + consumed() }; } /// Convenience conversion to get the completed portion explicit operator const_buffer() const { return completed(); } /// Convenience closure presenting the writable window and advancing the /// window with a consume() for the bytes written in the closure. using closure = std::function; void operator()(const closure &closure) { consume(*this, closure(*this)); } window_buffer(const mutable_buffer &base) :mutable_buffer{base} ,base{base} {} }; /// Like unique_ptr, this template holds ownership of an allocated buffer /// template struct ircd::buffer::unique_buffer :buffer { unique_buffer(std::unique_ptr &&, const size_t &size); unique_buffer(const size_t &size); unique_buffer(); 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; }; template ircd::buffer::unique_buffer::unique_buffer() :buffer { nullptr, nullptr } {} template ircd::buffer::unique_buffer::unique_buffer(std::unique_ptr &&b, const size_t &size) :buffer { typename buffer::iterator(b.release()), size } {} template ircd::buffer::unique_buffer::unique_buffer(const size_t &size) :unique_buffer { std::unique_ptr { //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] }, size } { // Alignment can only be 16 bytes for now assert(alignment == 16); } template ircd::buffer::unique_buffer::unique_buffer(unique_buffer &&other) noexcept :buffer { std::move(static_cast(other)) } { get<0>(other) = nullptr; } template ircd::buffer::unique_buffer & ircd::buffer::unique_buffer::operator=(unique_buffer &&other) noexcept { this->~unique_buffer(); static_cast(*this) = std::move(static_cast(other)); get<0>(other) = nullptr; return *this; } template ircd::buffer::unique_buffer::~unique_buffer() noexcept { delete[] data(*this); } template class buffers, class T> std::ostream & ircd::buffer::operator<<(std::ostream &s, const buffers &b) { using it = typename T::iterator; std::for_each(std::begin(b), std::end(b), [&s] (const buffer &b) { s << b; }); return s; } template class buffers, class T> size_t ircd::buffer::consume(buffers &b, const size_t &bytes) { ssize_t remain(bytes); for(auto it(std::begin(b)); it != std::end(b) && remain > 0; ++it) { using buffer = typename buffers::value_type; using iterator = typename buffer::iterator; buffer &b(const_cast(*it)); const ssize_t bsz(size(b)); const ssize_t csz{std::min(remain, bsz)}; remain -= consume(b, csz); assert(remain >= 0); } assert(ssize_t(bytes) >= remain); return bytes - remain; } template class buffers, class T> size_t ircd::buffer::copy(const mutable_buffer &dest, const buffers &b) { using it = typename T::iterator; size_t ret(0); for(const buffer &b : b) ret += copy(data(dest) + ret, size(dest) - ret, b); return ret; } template class buffers, class T> size_t ircd::buffer::size(const buffers &b) { using it = typename T::iterator; return std::accumulate(std::begin(b), std::end(b), size_t(0), [] (auto ret, const buffer &b) { return ret += size(b); }); } template std::ostream & ircd::buffer::operator<<(std::ostream &s, const buffer &buffer) { assert(!null(buffer) || get<1>(buffer) == nullptr); s.write(data(buffer), size(buffer)); return s; } inline void ircd::buffer::reverse(const mutable_buffer &buf) { std::reverse(data(buf), data(buf) + size(buf)); } inline size_t ircd::buffer::reverse(const mutable_buffer &dst, const const_buffer &src) { const size_t ret{std::min(size(dst), size(src))}; std::reverse_copy(data(src), data(src) + ret, data(dst)); return ret; } inline size_t ircd::buffer::copy(const mutable_buffer &dst, const const_buffer &src) { auto e{begin(dst)}; copy(e, end(dst), src); assert(std::distance(begin(dst), e) >= 0); return std::distance(begin(dst), e); } template it ircd::buffer::copy(it &dest, const it &stop, const const_buffer &src) { const it ret{dest}; const ssize_t srcsz(size(src)); assert(ret <= stop); const ssize_t remain{std::distance(ret, stop)}; const ssize_t cpsz{std::min(srcsz, remain)}; assert(cpsz <= srcsz); assert(cpsz <= remain); assert(remain >= 0); memcpy(ret, data(src), cpsz); dest += cpsz; assert(dest <= stop); return ret; } template ircd::buffer::buffer ircd::buffer::operator+(const buffer &buffer, const size_t &bytes) { const size_t advance { std::min(bytes, size(buffer)) }; return { begin(buffer) + advance, size(buffer) - advance }; } template size_t ircd::buffer::consume(buffer &buffer, const size_t &bytes) { assert(!null(buffer)); assert(bytes <= size(buffer)); get<0>(buffer) += bytes; assert(get<0>(buffer) <= get<1>(buffer)); return size(buffer); } template const it & ircd::buffer::data(const buffer &buffer) { return get<0>(buffer); } template size_t ircd::buffer::size(const buffer &buffer) { assert(get<0>(buffer) <= get<1>(buffer)); assert(!null(buffer) || get<1>(buffer) == nullptr); return std::distance(get<0>(buffer), get<1>(buffer)); } template bool ircd::buffer::operator!(const buffer &buffer) { return empty(buffer); } template bool ircd::buffer::empty(const buffer &buffer) { return null(buffer) || std::distance(get<0>(buffer), get<1>(buffer)) == 0; } template bool ircd::buffer::full(const buffer &buffer) { return std::distance(get<0>(buffer), get<1>(buffer)) == 0; } template bool ircd::buffer::null(const buffer &buffer) { return get<0>(buffer) == nullptr; } template std::reverse_iterator ircd::buffer::rend(const buffer &buffer) { return std::reverse_iterator(get<0>(buffer)); } template std::reverse_iterator ircd::buffer::rbegin(const buffer &buffer) { return std::reverse_iterator(get<0>(buffer) + size(buffer)); } template it & ircd::buffer::end(buffer &buffer) { return get<1>(buffer); } template it & ircd::buffer::begin(buffer &buffer) { return get<0>(buffer); } template const it & ircd::buffer::end(const buffer &buffer) { return get<1>(buffer); } template const it & ircd::buffer::begin(const buffer &buffer) { return get<0>(buffer); } template ircd::buffer::buffer::operator std::string() const { return { reinterpret_cast(data(*this)), size(*this) }; } template ircd::buffer::buffer::operator std::string_view() const { return { reinterpret_cast(data(*this)), size(*this) }; } template ircd::buffer::buffer::operator string_view() const { return { reinterpret_cast(data(*this)), size(*this) }; }