diff --git a/include/ircd/bufs.h b/include/ircd/bufs.h index ca998de14..988b4f35f 100644 --- a/include/ircd/bufs.h +++ b/include/ircd/bufs.h @@ -49,6 +49,17 @@ size_t size(const const_buffer &buf); template size_t size(const mutable_buffers &iov); template size_t size(const const_buffers &iov); +template T data(const mutable_buffer &buf); +template T begin(const mutable_buffer &buf); +template T end(const mutable_buffer &buf); +template std::reverse_iterator rbegin(const mutable_buffer &buf); +template std::reverse_iterator rend(const mutable_buffer &buf); +template T data(const const_buffer &buf); +template T begin(const const_buffer &buf); +template T end(const const_buffer &buf); +template std::reverse_iterator rbegin(const const_buffer &buf); +template std::reverse_iterator rend(const const_buffer &buf); + size_t copy(const const_buffer &src, const mutable_buffer &dst); template size_t copy(const buffers &iov, const mutable_buffer &buf); template size_t copy(const mutable_buffer &buf, const mutable_buffers &iov); @@ -57,6 +68,61 @@ template size_t copy(const const_buffer &buf, const mutab void fill(const mutable_buffer &buf, const uint8_t &val = 0); template void fill(const mutable_buffers &buf, const uint8_t &val = 0); +std::string string(const mutable_buffer &); +std::string string(const const_buffer &); + +template +struct unique_buffer +:buffer +{ + template unique_buffer(std::unique_ptr &&, const size_t &size); + unique_buffer(const size_t &size); + ~unique_buffer() noexcept; +}; + +template +template +unique_buffer::unique_buffer(std::unique_ptr &&b, + const size_t &size) +:buffer{b.release(), size} +{ +} + +template +unique_buffer::unique_buffer(const size_t &size) +:unique_buffer +{ + std::unique_ptr{new __attribute__((aligned(16))) uint8_t[size]}, + size +} +{ +} + +template +unique_buffer::~unique_buffer() +noexcept +{ + delete[] data(*this); +} + +template +std::string +string(const unique_buffer &buf) +{ + return string(static_cast(buf)); +} + +inline std::string +string(const const_buffer &buf) +{ + return { data(buf), size(buf) }; +} + +inline std::string +string(const mutable_buffer &buf) +{ + return { data(buf), size(buf) }; +} template void @@ -71,7 +137,7 @@ inline void fill(const mutable_buffer &buf, const uint8_t &val) { - memset(buffer_cast(buf), val, size(buf)); + memset(data(buf), val, size(buf)); } template @@ -84,8 +150,8 @@ copy(const const_buffer &buf, { const auto remain(buffer_size(buf) - ret); const auto cp_sz(std::min(buffer_size(dst), remain)); - const auto src(buffer_cast(buf) + ret); - memcpy(buffer_cast(dst), src, cp_sz); + const auto src(data(buf) + ret); + memcpy(data(dst), src, cp_sz); ret += cp_sz; } @@ -102,8 +168,8 @@ copy(const mutable_buffer &buf, { const auto remain(buffer_size(buf) - ret); const auto cp_sz(std::min(buffer_size(dst), remain)); - const auto src(buffer_cast(buf) + ret); - memcpy(buffer_cast(dst), src, cp_sz); + const auto src(data(buf) + ret); + memcpy(data(dst), src, cp_sz); ret += cp_sz; } @@ -120,8 +186,8 @@ copy(const buffers &iov, { const auto remain(buffer_size(buf) - ret); const auto cp_sz(std::min(buffer_size(src), remain)); - const auto dst(buffer_cast(buf) + ret); - memcpy(dst, buffer_cast(src), cp_sz); + const auto dst(data(buf) + ret); + memcpy(dst, data(src), cp_sz); ret += cp_sz; } @@ -133,10 +199,87 @@ copy(const const_buffer &src, const mutable_buffer &dst) { const auto cp_sz(std::min(buffer_size(src), buffer_size(dst))); - memcpy(buffer_cast(dst), buffer_cast(src), cp_sz); + memcpy(data(dst), data(src), cp_sz); return cp_sz; } +template +std::reverse_iterator +rend(const const_buffer &buf) +{ + return begin(buf); +} + +template +std::reverse_iterator +rbegin(const const_buffer &buf) +{ + return std::reverse_iterator(begin(buf) + size(buf)); +} + +template +T +end(const const_buffer &buf) +{ + return begin(buf) + size(buf); +} + +template +T +begin(const const_buffer &buf) +{ + return data(buf); +} + +template +T +data(const const_buffer &buf) +{ + return buffer_cast(buf); +} + +template +std::reverse_iterator +rend(const mutable_buffer &buf) +{ + return begin(buf); +} + +template +std::reverse_iterator +rbegin(const mutable_buffer &buf) +{ + return std::reverse_iterator(begin(buf) + size(buf)); +} + +template +T +end(const mutable_buffer &buf) +{ + return begin(buf) + size(buf); +} + +template +T +begin(const mutable_buffer &buf) +{ + return data(buf); +} + +template +T +data(const mutable_buffer &buf) +{ + return buffer_cast(buf); +} + +template +size_t +size(const unique_buffer &buf) +{ + return size(static_cast(buf)); +} + template size_t size(const buffers &iov) @@ -144,7 +287,7 @@ size(const buffers &iov) return std::accumulate(begin(iov), end(iov), size_t(0), [] (auto ret, const auto &buffer) { - return ret += buffer_size(buffer); + return ret += size(buffer); }); }