2018-02-04 03:22:01 +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.
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
#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
|
2018-02-03 08:20:26 +01:00
|
|
|
/// data; a mutable_buffer is a pair of iterators meant for receiving.
|
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;
|
2019-06-14 21:53:49 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct const_buffer;
|
|
|
|
struct mutable_buffer;
|
2018-02-08 06:30:20 +01:00
|
|
|
struct window_buffer;
|
2019-02-28 19:10:43 +01:00
|
|
|
struct parse_buffer;
|
2019-06-14 21:53:49 +02:00
|
|
|
|
2017-10-06 03:32:48 +02:00
|
|
|
template<class buffer, size_t SIZE> struct fixed_buffer;
|
2018-11-10 00:55:20 +01:00
|
|
|
template<class buffer> struct unique_buffer;
|
2018-03-13 19:21:19 +01:00
|
|
|
template<class buffer> struct shared_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>;
|
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-08-28 23:51:22 +02:00
|
|
|
|
2019-06-14 21:53:49 +02:00
|
|
|
using unique_const_buffer = unique_buffer<const_buffer>;
|
|
|
|
using unique_mutable_buffer = unique_buffer<mutable_buffer>;
|
|
|
|
using shared_const_buffer = shared_buffer<const_buffer>;
|
|
|
|
using shared_mutable_buffer = shared_buffer<mutable_buffer>;
|
|
|
|
|
2018-03-13 05:17:09 +01: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);
|
2018-01-20 13:11:37 +01:00
|
|
|
template<class it> std::reverse_iterator<it> rbegin(const buffer<it> &buffer);
|
|
|
|
template<class it> std::reverse_iterator<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-10-17 09:50:07 +02:00
|
|
|
template<class it> bool null(const buffer<it> &buffer);
|
2017-11-25 22:01:33 +01:00
|
|
|
template<class it> bool full(const buffer<it> &buffer);
|
2017-10-17 09:50:07 +02:00
|
|
|
template<class it> bool empty(const buffer<it> &buffer);
|
|
|
|
template<class it> bool operator!(const buffer<it> &buffer);
|
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);
|
2018-11-17 04:22:44 +01:00
|
|
|
template<class it> bool aligned(const buffer<it> &buffer, const size_t &alignment);
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class it> size_t consume(buffer<it> &buffer, const size_t &bytes);
|
2018-02-19 23:01:00 +01:00
|
|
|
template<class it> buffer<it> operator+(const buffer<it> &buffer, const size_t &bytes);
|
2018-02-03 08:20:26 +01:00
|
|
|
template<class it> it copy(it &dest, const it &stop, const const_buffer &);
|
2018-03-20 10:18:26 +01:00
|
|
|
template<class it> it move(it &dest, const it &stop, const const_buffer &);
|
2018-02-20 01:47:17 +01:00
|
|
|
template<size_t SIZE> size_t copy(const mutable_buffer &dst, const char (&buf)[SIZE]);
|
2018-03-20 10:18:26 +01:00
|
|
|
template<size_t SIZE> size_t move(const mutable_buffer &dst, const char (&buf)[SIZE]);
|
2018-02-03 08:20:26 +01:00
|
|
|
size_t copy(const mutable_buffer &dst, const const_buffer &src);
|
2018-03-20 10:18:26 +01:00
|
|
|
size_t move(const mutable_buffer &dst, const const_buffer &src);
|
2018-02-03 08:20:26 +01:00
|
|
|
size_t reverse(const mutable_buffer &dst, const const_buffer &src);
|
|
|
|
void reverse(const mutable_buffer &buf);
|
2018-11-16 23:13:34 +01:00
|
|
|
size_t zero(const mutable_buffer &buf);
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2018-09-13 12:22:21 +02:00
|
|
|
// Convenience copy to std stream
|
|
|
|
template<class it> std::ostream &operator<<(std::ostream &s, const buffer<it> &buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ircd::buffer::buffers
|
|
|
|
{
|
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);
|
2018-02-03 08:20:26 +01:00
|
|
|
template<template<class> class I, class T> size_t copy(const mutable_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-10-01 04:11:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-13 05:17:09 +01:00
|
|
|
#include "buffer_base.h"
|
|
|
|
#include "mutable_buffer.h"
|
|
|
|
#include "const_buffer.h"
|
|
|
|
#include "fixed_buffer.h"
|
|
|
|
#include "window_buffer.h"
|
2019-02-28 19:10:43 +01:00
|
|
|
#include "parse_buffer.h"
|
2018-03-13 05:17:09 +01:00
|
|
|
#include "unique_buffer.h"
|
2018-03-13 19:21:19 +01:00
|
|
|
#include "shared_buffer.h"
|
2018-03-13 05:17:09 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
// Export these important aliases down to main ircd namespace
|
|
|
|
namespace ircd
|
|
|
|
{
|
2018-12-18 22:48:56 +01:00
|
|
|
namespace buffers = buffer::buffers;
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
using buffer::const_buffer;
|
|
|
|
using buffer::mutable_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;
|
2018-03-13 19:21:19 +01:00
|
|
|
using buffer::shared_buffer;
|
2017-09-12 18:28:41 +02:00
|
|
|
using buffer::null_buffer;
|
2018-02-08 06:30:20 +01:00
|
|
|
using buffer::window_buffer;
|
2017-10-06 03:32:48 +02:00
|
|
|
using buffer::fixed_const_buffer;
|
|
|
|
using buffer::fixed_mutable_buffer;
|
2019-06-14 21:53:49 +02:00
|
|
|
using buffer::unique_const_buffer;
|
|
|
|
using buffer::unique_mutable_buffer;
|
|
|
|
using buffer::shared_const_buffer;
|
|
|
|
using buffer::shared_mutable_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;
|
2017-12-21 22:24:33 +01:00
|
|
|
|
|
|
|
using buffer::size;
|
|
|
|
using buffer::data;
|
|
|
|
using buffer::copy;
|
|
|
|
using buffer::consume;
|
2018-07-05 06:48:15 +02:00
|
|
|
using buffer::begin;
|
|
|
|
using buffer::end;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
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 &
|
2018-09-13 12:22:21 +02:00
|
|
|
ircd::buffer::buffers::operator<<(std::ostream &s, const buffers<T> &b)
|
2017-04-04 07:42:16 +02:00
|
|
|
{
|
2017-12-23 01:29:21 +01:00
|
|
|
using it = typename T::iterator;
|
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
std::for_each(std::begin(b), std::end(b), [&s]
|
2017-12-23 01:29:21 +01:00
|
|
|
(const buffer<it> &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
|
2018-09-13 12:22:21 +02:00
|
|
|
ircd::buffer::buffers::consume(buffers<T> &b,
|
|
|
|
const size_t &bytes)
|
2017-04-04 07:42:16 +02:00
|
|
|
{
|
2017-11-25 22:01:33 +01:00
|
|
|
ssize_t remain(bytes);
|
2017-09-12 18:28:41 +02:00
|
|
|
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;
|
2018-09-13 12:22:21 +02:00
|
|
|
using ircd::buffer::size;
|
|
|
|
using ircd::buffer::consume;
|
2017-09-12 18:28:41 +02:00
|
|
|
|
|
|
|
buffer &b(const_cast<buffer &>(*it));
|
2017-11-25 22:01:33 +01:00
|
|
|
const ssize_t bsz(size(b));
|
|
|
|
const ssize_t csz{std::min(remain, bsz)};
|
|
|
|
remain -= consume(b, csz);
|
|
|
|
assert(remain >= 0);
|
2017-09-12 18:28:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 22:01:33 +01:00
|
|
|
assert(ssize_t(bytes) >= remain);
|
2017-09-12 18:28:41 +02:00
|
|
|
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
|
2018-09-13 12:22:21 +02:00
|
|
|
ircd::buffer::buffers::copy(const mutable_buffer &dest,
|
|
|
|
const buffers<T> &b)
|
2017-03-20 07:58:26 +01:00
|
|
|
{
|
2017-12-23 01:29:21 +01:00
|
|
|
using it = typename T::iterator;
|
2018-09-13 12:22:21 +02:00
|
|
|
using ircd::buffer::copy;
|
|
|
|
using ircd::buffer::size;
|
2017-12-23 01:29:21 +01:00
|
|
|
|
2017-03-20 07:58:26 +01:00
|
|
|
size_t ret(0);
|
2017-12-23 01:29:21 +01:00
|
|
|
for(const buffer<it> &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
|
2018-09-13 12:22:21 +02:00
|
|
|
ircd::buffer::buffers::size(const buffers<T> &b)
|
2017-09-12 18:28:41 +02:00
|
|
|
{
|
2017-12-23 01:29:21 +01:00
|
|
|
using it = typename T::iterator;
|
2018-09-13 12:22:21 +02:00
|
|
|
using ircd::buffer::size;
|
2017-12-23 01:29:21 +01:00
|
|
|
|
2017-09-12 18:28:41 +02:00
|
|
|
return std::accumulate(std::begin(b), std::end(b), size_t(0), []
|
2017-12-23 01:29:21 +01:00
|
|
|
(auto ret, const buffer<it> &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)
|
|
|
|
{
|
2017-11-30 19:24:51 +01:00
|
|
|
assert(!null(buffer) || get<1>(buffer) == nullptr);
|
2017-09-12 18:28:41 +02:00
|
|
|
s.write(data(buffer), size(buffer));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:13:34 +01:00
|
|
|
// We use the sodium_memzero() from libsodium in ircd/sodium.cc if available
|
|
|
|
// to ensure cross-platform guarantees the zero'ing doesn't get optimized away.
|
2019-06-06 12:54:37 +02:00
|
|
|
#ifndef HAVE_SODIUM
|
2018-11-16 23:13:34 +01:00
|
|
|
inline size_t
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-11-16 23:13:34 +01:00
|
|
|
ircd::buffer::zero(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
std::memset(data(buf), 0x0, size(buf));
|
|
|
|
return size(buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-18 23:05:01 +01:00
|
|
|
inline void
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::buffer::reverse(const mutable_buffer &buf)
|
2017-12-18 23:05:01 +01:00
|
|
|
{
|
|
|
|
std::reverse(data(buf), data(buf) + size(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::buffer::reverse(const mutable_buffer &dst,
|
|
|
|
const const_buffer &src)
|
2017-12-18 23:05:01 +01:00
|
|
|
{
|
|
|
|
const size_t ret{std::min(size(dst), size(src))};
|
|
|
|
std::reverse_copy(data(src), data(src) + ret, data(dst));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-20 10:18:26 +01:00
|
|
|
template<size_t SIZE>
|
|
|
|
__attribute__((error
|
|
|
|
(
|
|
|
|
"Move source is an array. Is this a string literal? Do you want to move the \\0?"
|
|
|
|
" Disambiguate this by typing the source string_view or const_buffer."
|
|
|
|
)))
|
|
|
|
inline size_t
|
|
|
|
ircd::buffer::move(const mutable_buffer &dst,
|
|
|
|
const char (&buf)[SIZE])
|
|
|
|
{
|
|
|
|
return move(dst, const_buffer{buf});
|
|
|
|
}
|
|
|
|
|
2018-02-20 01:47:17 +01:00
|
|
|
template<size_t SIZE>
|
|
|
|
__attribute__((error
|
|
|
|
(
|
|
|
|
"Copy source is an array. Is this a string literal? Do you want to copy the \\0?"
|
|
|
|
" Disambiguate this by typing the source string_view or const_buffer."
|
|
|
|
)))
|
|
|
|
inline size_t
|
|
|
|
ircd::buffer::copy(const mutable_buffer &dst,
|
|
|
|
const char (&buf)[SIZE])
|
|
|
|
{
|
|
|
|
return copy(dst, const_buffer{buf});
|
|
|
|
}
|
|
|
|
|
2018-03-20 10:18:26 +01:00
|
|
|
inline size_t
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-20 10:18:26 +01:00
|
|
|
ircd::buffer::move(const mutable_buffer &dst,
|
|
|
|
const const_buffer &src)
|
|
|
|
{
|
|
|
|
auto e{begin(dst)};
|
|
|
|
move(e, end(dst), src);
|
|
|
|
assert(std::distance(begin(dst), e) >= 0);
|
|
|
|
return std::distance(begin(dst), e);
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:28:11 +02:00
|
|
|
inline size_t
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::buffer::copy(const mutable_buffer &dst,
|
|
|
|
const const_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);
|
2017-11-25 22:01:33 +01:00
|
|
|
assert(std::distance(begin(dst), e) >= 0);
|
2017-10-01 04:11:09 +02:00
|
|
|
return std::distance(begin(dst), e);
|
2017-09-14 20:28:11 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 10:18:26 +01:00
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline it
|
|
|
|
__attribute__((always_inline))
|
2018-03-20 10:18:26 +01:00
|
|
|
ircd::buffer::move(it &dest,
|
|
|
|
const it &stop,
|
|
|
|
const const_buffer &src)
|
|
|
|
{
|
2018-05-29 18:31:30 +02:00
|
|
|
assert(dest <= stop);
|
|
|
|
const it ret(dest);
|
2019-03-01 20:35:07 +01:00
|
|
|
const size_t &srcsz(size(src));
|
|
|
|
const size_t &remain(std::distance(ret, stop));
|
|
|
|
const size_t &mvsz(std::min(srcsz, remain));
|
2018-05-29 18:31:30 +02:00
|
|
|
dest += mvsz;
|
|
|
|
assert(dest <= stop);
|
2018-03-20 10:18:26 +01:00
|
|
|
assert(mvsz <= srcsz);
|
|
|
|
assert(mvsz <= remain);
|
|
|
|
assert(remain >= 0);
|
|
|
|
memmove(ret, data(src), mvsz);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:58:26 +01:00
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline it
|
|
|
|
__attribute__((always_inline))
|
2017-10-01 04:11:09 +02:00
|
|
|
ircd::buffer::copy(it &dest,
|
|
|
|
const it &stop,
|
2018-02-03 08:20:26 +01:00
|
|
|
const const_buffer &src)
|
2017-03-20 07:58:26 +01:00
|
|
|
{
|
2018-05-29 18:31:30 +02:00
|
|
|
assert(dest <= stop);
|
|
|
|
const it ret(dest);
|
2019-03-01 20:35:07 +01:00
|
|
|
const size_t &srcsz(size(src));
|
|
|
|
const size_t &remain(std::distance(ret, stop));
|
|
|
|
const size_t &cpsz(std::min(srcsz, remain));
|
2018-05-29 18:31:30 +02:00
|
|
|
dest += cpsz;
|
|
|
|
assert(dest <= stop);
|
2017-11-25 22:01:33 +01:00
|
|
|
assert(cpsz <= srcsz);
|
|
|
|
assert(cpsz <= remain);
|
|
|
|
assert(remain >= 0);
|
2017-11-18 08:38:39 +01:00
|
|
|
memcpy(ret, data(src), cpsz);
|
2017-03-20 07:58:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-19 23:01:00 +01:00
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline ircd::buffer::buffer<it>
|
|
|
|
__attribute__((always_inline))
|
2018-02-19 23:01:00 +01:00
|
|
|
ircd::buffer::operator+(const buffer<it> &buffer,
|
|
|
|
const size_t &bytes)
|
|
|
|
{
|
|
|
|
const size_t advance
|
|
|
|
{
|
|
|
|
std::min(bytes, size(buffer))
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
begin(buffer) + advance, size(buffer) - advance
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-13 20:00:12 +01:00
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline size_t
|
|
|
|
__attribute__((always_inline))
|
2017-03-13 20:00:12 +01:00
|
|
|
ircd::buffer::consume(buffer<it> &buffer,
|
|
|
|
const size_t &bytes)
|
|
|
|
{
|
2017-11-25 22:01:33 +01:00
|
|
|
assert(!null(buffer));
|
|
|
|
assert(bytes <= size(buffer));
|
|
|
|
get<0>(buffer) += bytes;
|
|
|
|
assert(get<0>(buffer) <= get<1>(buffer));
|
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
|
|
|
|
2018-11-17 04:22:44 +01:00
|
|
|
template<class it>
|
|
|
|
bool
|
|
|
|
ircd::buffer::aligned(const buffer<it> &buffer,
|
|
|
|
const size_t &a)
|
|
|
|
{
|
|
|
|
return likely(a)?
|
|
|
|
uintptr_t(data(buffer)) % a == 0 && size(buffer) % a == 0:
|
|
|
|
true;
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline const it &
|
|
|
|
__attribute__((always_inline))
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::buffer::data(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<0>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline size_t
|
|
|
|
__attribute__((always_inline))
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::buffer::size(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
assert(get<0>(buffer) <= get<1>(buffer));
|
2017-11-25 22:01:33 +01:00
|
|
|
assert(!null(buffer) || get<1>(buffer) == nullptr);
|
2016-11-29 16:23:38 +01:00
|
|
|
return std::distance(get<0>(buffer), get<1>(buffer));
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:50:07 +02:00
|
|
|
template<class it>
|
|
|
|
bool
|
|
|
|
ircd::buffer::operator!(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return empty(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
bool
|
|
|
|
ircd::buffer::empty(const buffer<it> &buffer)
|
|
|
|
{
|
2017-11-25 22:01:33 +01:00
|
|
|
return null(buffer) || std::distance(get<0>(buffer), get<1>(buffer)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
bool
|
|
|
|
ircd::buffer::full(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return std::distance(get<0>(buffer), get<1>(buffer)) == 0;
|
2017-10-17 09:50:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
bool
|
|
|
|
ircd::buffer::null(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<0>(buffer) == nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class it>
|
2018-01-20 13:11:37 +01:00
|
|
|
std::reverse_iterator<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>
|
2018-01-20 13:11:37 +01:00
|
|
|
std::reverse_iterator<it>
|
2016-11-29 16:23:38 +01:00
|
|
|
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);
|
|
|
|
}
|