From 6e1d58c2e6d06c5436ffeef5846d4242f78925e0 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 6 Jun 2020 15:48:32 -0700 Subject: [PATCH] ircd::buffer: Improve the overlap tool. --- include/ircd/buffer/buffer.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/include/ircd/buffer/buffer.h b/include/ircd/buffer/buffer.h index ad07e7fbf..11573bed1 100644 --- a/include/ircd/buffer/buffer.h +++ b/include/ircd/buffer/buffer.h @@ -75,6 +75,7 @@ namespace ircd::buffer template const it &data(const buffer &buffer); template bool aligned(const buffer &buffer, const size_t &alignment); template buffer operator+(const buffer &buffer, const size_t &bytes); + size_t overlap_count(const const_buffer &, const const_buffer &); bool overlap(const const_buffer &, const const_buffer &); // Single buffer mutators @@ -408,8 +409,21 @@ __attribute__((always_inline)) ircd::buffer::overlap(const const_buffer &a, const const_buffer &b) { - return data(a) + size(a) > data(b) && - data(a) + size(a) < data(b) + size(b); + 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); } template