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;
|
|
|
|
|
2020-08-25 18:14:21 +02:00
|
|
|
// Alignment constant expressions
|
|
|
|
constexpr bool aligned(const uintptr_t &, size_t alignment);
|
|
|
|
constexpr size_t padding(const size_t &size, size_t alignment);
|
2020-07-08 02:27:37 +02:00
|
|
|
constexpr size_t pad_to(const size_t &size, const size_t &alignment);
|
2020-08-25 18:14:21 +02:00
|
|
|
constexpr uintptr_t align(uintptr_t, size_t alignment);
|
|
|
|
constexpr uintptr_t align_up(uintptr_t, size_t alignment);
|
|
|
|
|
|
|
|
// Alignment inline tools
|
2020-07-10 02:06:26 +02:00
|
|
|
bool aligned(const void *const &, const size_t &alignment);
|
2020-08-25 18:14:21 +02:00
|
|
|
template<class T = char> const T *align(const void *const &, const size_t &alignment);
|
|
|
|
template<class T = char> T *align(void *const &, const size_t &alignment);
|
|
|
|
template<class T = char> const T *align_up(const void *const &, const size_t &alignment);
|
|
|
|
template<class T = char> T *align_up(void *const &, const size_t &alignment);
|
2020-07-08 02:27:37 +02:00
|
|
|
|
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
|
|
|
|
2020-07-07 20:58:45 +02:00
|
|
|
// Single buffer observer utils
|
2017-10-17 09:50:07 +02:00
|
|
|
template<class it> bool null(const buffer<it> &buffer);
|
|
|
|
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);
|
2020-07-08 02:01:32 +02:00
|
|
|
template<class it> bool padded(const buffer<it> &buffer, const size_t &alignment);
|
2018-11-17 04:22:44 +01:00
|
|
|
template<class it> bool aligned(const buffer<it> &buffer, const size_t &alignment);
|
2018-02-19 23:01:00 +01:00
|
|
|
template<class it> buffer<it> operator+(const buffer<it> &buffer, const size_t &bytes);
|
2020-06-07 00:48:32 +02:00
|
|
|
size_t overlap_count(const const_buffer &, const const_buffer &);
|
2019-07-11 03:50:27 +02:00
|
|
|
bool overlap(const const_buffer &, const const_buffer &);
|
|
|
|
|
2020-07-07 20:58:45 +02:00
|
|
|
// Single buffer mutator utils
|
2019-07-11 03:50:27 +02:00
|
|
|
template<class it> size_t consume(buffer<it> &buffer, const size_t &bytes);
|
|
|
|
template<class it> buffer<it> &operator+=(buffer<it> &buffer, const size_t &bytes);
|
2020-07-07 20:58:45 +02:00
|
|
|
|
|
|
|
// other tools
|
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"
|
2020-07-07 20:58:45 +02:00
|
|
|
#include "copy.h"
|
|
|
|
#include "move.h"
|
2018-03-13 05:17:09 +01:00
|
|
|
#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
|
|
|
|
2020-09-15 03:42:36 +02:00
|
|
|
using buffer::aligned;
|
|
|
|
using buffer::align;
|
|
|
|
using buffer::align_up;
|
2020-07-08 02:27:37 +02:00
|
|
|
using buffer::padded;
|
|
|
|
using buffer::padding;
|
|
|
|
using buffer::pad_to;
|
|
|
|
|
2017-12-21 22:24:33 +01:00
|
|
|
using buffer::size;
|
|
|
|
using buffer::data;
|
|
|
|
using buffer::copy;
|
2020-08-27 10:04:08 +02:00
|
|
|
using buffer::move;
|
2017-12-21 22:24:33 +01:00
|
|
|
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)
|
2019-07-11 03:50:27 +02:00
|
|
|
ret += copy(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
|
|
|
{
|
2020-08-30 01:47:50 +02:00
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
std::min(size(dst), size(src))
|
|
|
|
};
|
|
|
|
|
2017-12-18 23:05:01 +01:00
|
|
|
std::reverse_copy(data(src), data(src) + ret, data(dst));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-19 23:01:00 +01:00
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline ircd::buffer::buffer<it> &
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2019-07-11 03:50:27 +02:00
|
|
|
ircd::buffer::operator+=(buffer<it> &buffer,
|
|
|
|
const size_t &bytes)
|
2018-02-19 23:01:00 +01:00
|
|
|
{
|
2020-09-13 12:17:54 +02:00
|
|
|
consume(buffer, bytes);
|
2019-07-11 03:50:27 +02:00
|
|
|
return buffer;
|
2018-02-19 23:01:00 +01:00
|
|
|
}
|
|
|
|
|
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));
|
2020-09-13 12:17:54 +02:00
|
|
|
const size_t &advance
|
|
|
|
{
|
|
|
|
std::min(bytes, size(buffer))
|
|
|
|
};
|
|
|
|
|
|
|
|
get<0>(buffer) += advance;
|
2017-11-25 22:01:33 +01:00
|
|
|
assert(get<0>(buffer) <= get<1>(buffer));
|
2020-09-13 12:17:54 +02:00
|
|
|
return advance;
|
2017-03-13 20:00:12 +01:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2019-07-11 03:50:27 +02:00
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
|
|
|
ircd::buffer::overlap(const const_buffer &a,
|
|
|
|
const const_buffer &b)
|
|
|
|
{
|
2020-06-07 00:48:32 +02:00
|
|
|
return overlap_count(a, b) > 0UL;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
|
|
|
__attribute__((always_inline))
|
|
|
|
ircd::buffer::overlap_count(const const_buffer &a,
|
|
|
|
const const_buffer &b)
|
|
|
|
{
|
|
|
|
const char *const res[2]
|
|
|
|
{
|
|
|
|
std::max(begin(a), begin(b)),
|
|
|
|
std::min(end(a), end(b)),
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::max(res[1] - res[0], 0L);
|
2019-07-11 03:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
inline ircd::buffer::buffer<it>
|
|
|
|
__attribute__((always_inline))
|
|
|
|
ircd::buffer::operator+(const buffer<it> &buffer,
|
|
|
|
const size_t &bytes)
|
|
|
|
{
|
2020-08-30 01:47:50 +02:00
|
|
|
auto ret(buffer);
|
|
|
|
ret += bytes;
|
|
|
|
return ret;
|
2019-07-11 03:50:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:22:44 +01:00
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
2018-11-17 04:22:44 +01:00
|
|
|
ircd::buffer::aligned(const buffer<it> &buffer,
|
|
|
|
const size_t &a)
|
|
|
|
{
|
|
|
|
return likely(a)?
|
2020-07-10 02:06:26 +02:00
|
|
|
aligned(data(buffer), a) && padded(buffer, a):
|
2020-07-08 02:01:32 +02:00
|
|
|
true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
|
|
|
ircd::buffer::padded(const buffer<it> &buffer,
|
|
|
|
const size_t &a)
|
|
|
|
{
|
|
|
|
return likely(a)?
|
|
|
|
size(buffer) % a == 0:
|
2018-11-17 04:22:44 +01:00
|
|
|
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>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
2017-10-17 09:50:07 +02:00
|
|
|
ircd::buffer::operator!(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return empty(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
2017-10-17 09:50:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:50:07 +02:00
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
2017-10-17 09:50:07 +02:00
|
|
|
ircd::buffer::null(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<0>(buffer) == nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline std::reverse_iterator<it>
|
|
|
|
__attribute__((always_inline))
|
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>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline std::reverse_iterator<it>
|
|
|
|
__attribute__((always_inline))
|
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>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline it &
|
|
|
|
__attribute__((always_inline))
|
2017-09-08 16:47:51 +02:00
|
|
|
ircd::buffer::end(buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<1>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline it &
|
|
|
|
__attribute__((always_inline))
|
2017-09-08 16:47:51 +02:00
|
|
|
ircd::buffer::begin(buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<0>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline const it &
|
|
|
|
__attribute__((always_inline))
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::buffer::end(const buffer<it> &buffer)
|
|
|
|
{
|
|
|
|
return get<1>(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline const it &
|
|
|
|
__attribute__((always_inline))
|
2017-09-08 16:47:51 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-07-08 02:27:37 +02:00
|
|
|
|
2020-08-25 18:14:21 +02:00
|
|
|
template<class T>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline T *
|
|
|
|
ircd::buffer::align(void *const &ptr,
|
|
|
|
const size_t &alignment)
|
|
|
|
{
|
|
|
|
return align(uintptr_t(ptr), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline const T *
|
|
|
|
ircd::buffer::align(const void *const &ptr,
|
|
|
|
const size_t &alignment)
|
|
|
|
{
|
|
|
|
return align(uintptr_t(ptr), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline T *
|
|
|
|
ircd::buffer::align_up(void *const &ptr,
|
|
|
|
const size_t &alignment)
|
|
|
|
{
|
|
|
|
return align_up(uintptr_t(ptr), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline const T *
|
|
|
|
ircd::buffer::align_up(const void *const &ptr,
|
|
|
|
const size_t &alignment)
|
|
|
|
{
|
|
|
|
return align_up(uintptr_t(ptr), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2020-07-10 02:06:26 +02:00
|
|
|
inline bool
|
|
|
|
ircd::buffer::aligned(const void *const &ptr,
|
|
|
|
const size_t &alignment)
|
|
|
|
{
|
2020-08-25 18:14:21 +02:00
|
|
|
return aligned(uintptr_t(ptr), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr uintptr_t
|
|
|
|
ircd::buffer::align_up(uintptr_t ptr,
|
|
|
|
size_t alignment)
|
|
|
|
{
|
|
|
|
alignment = std::max(alignment, 1UL);
|
|
|
|
ptr += alignment - (ptr % alignment);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr uintptr_t
|
|
|
|
ircd::buffer::align(uintptr_t ptr,
|
|
|
|
size_t alignment)
|
|
|
|
{
|
|
|
|
alignment = std::max(alignment, 1UL);
|
|
|
|
ptr -= (ptr % alignment);
|
|
|
|
return ptr;
|
2020-07-10 02:06:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:27:37 +02:00
|
|
|
constexpr size_t
|
|
|
|
ircd::buffer::pad_to(const size_t &size,
|
|
|
|
const size_t &alignment)
|
|
|
|
{
|
|
|
|
return size + padding(size, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t
|
|
|
|
ircd::buffer::padding(const size_t &size,
|
2020-08-25 18:14:21 +02:00
|
|
|
size_t alignment)
|
|
|
|
{
|
|
|
|
alignment = std::max(alignment, 1UL);
|
|
|
|
return (alignment - (size % alignment)) % alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool
|
|
|
|
ircd::buffer::aligned(const uintptr_t &ptr,
|
|
|
|
size_t alignment)
|
2020-07-08 02:27:37 +02:00
|
|
|
{
|
2020-08-25 18:14:21 +02:00
|
|
|
alignment = std::max(alignment, 1UL);
|
|
|
|
return ptr % alignment == 0;
|
2020-07-08 02:27:37 +02:00
|
|
|
}
|