From 7baf6f781e6d0396c4a96824bdce0fe4b6e6558e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 5 Feb 2015 22:45:17 +0900 Subject: [PATCH] memchunk: Refactor --- src/memchunk.h | 54 ++++++++++++++++++++------------------------ src/memchunk_test.cc | 6 ++--- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/memchunk.h b/src/memchunk.h index 58600bb7..76334a4b 100644 --- a/src/memchunk.h +++ b/src/memchunk.h @@ -31,6 +31,7 @@ #include #include +#include #include "util.h" @@ -38,21 +39,20 @@ namespace nghttp2 { template struct Memchunk { Memchunk(std::unique_ptr next_chunk) - : knext(std::move(next_chunk)), kprev(nullptr), next(nullptr), pos(begin), - last(begin), end(begin + N) { + : pos(std::begin(buf)), last(pos), knext(std::move(next_chunk)), + kprev(nullptr), next(nullptr) { if (knext) { knext->kprev = this; } } size_t len() const { return last - pos; } - size_t left() const { return end - last; } - void reset() { pos = last = begin; } + size_t left() const { return std::end(buf) - last; } + void reset() { pos = last = std::begin(buf); } + std::array buf; + uint8_t *pos, *last; std::unique_ptr knext; Memchunk *kprev; Memchunk *next; - uint8_t *pos, *last; - uint8_t *end; - uint8_t begin[N]; static const size_t size = N; }; @@ -106,11 +106,6 @@ template struct Pool { size_t poolsize; }; -inline void *cpymem(void *dest, const void *src, size_t count) { - memcpy(dest, src, count); - return reinterpret_cast(dest) + count; -} - template struct Memchunks { Memchunks(Pool *pool) : pool(pool), head(nullptr), tail(nullptr), len(0) {} @@ -124,54 +119,53 @@ template struct Memchunks { m = next; } } - size_t append(const void *data, size_t count) { + size_t append(const void *src, size_t count) { if (count == 0) { return 0; } - auto p = reinterpret_cast(data); + auto first = static_cast(src); + auto last = first + count; if (!tail) { head = tail = pool->get(); } - auto all = count; - while (count > 0) { - auto n = std::min(count, tail->left()); - tail->last = reinterpret_cast(cpymem(tail->last, p, n)); - p += n; - count -= n; + for (;;) { + auto n = std::min(static_cast(last - first), tail->left()); + tail->last = std::copy_n(first, n, tail->last); + first += n; len += n; - if (count == 0) { + if (first == last) { break; } tail->next = pool->get(); - - assert(tail != tail->next); tail = tail->next; } - return all; + return count; } template size_t append(const char (&s)[N]) { return append(s, N - 1); } - size_t remove(void *data, size_t count) { + size_t remove(void *dest, size_t count) { if (!tail || count == 0) { return 0; } - auto ndata = count; + + auto first = static_cast(dest); + auto last = first + count; + auto m = head; while (m) { auto next = m->next; - auto n = std::min(count, m->len()); + auto n = std::min(static_cast(last - first), m->len()); assert(m->len()); - data = cpymem(data, m->pos, n); + first = std::copy_n(m->pos, n, first); m->pos += n; - count -= n; len -= n; if (m->len() > 0) { break; @@ -184,7 +178,7 @@ template struct Memchunks { tail = nullptr; } - return ndata - count; + return first - static_cast(dest); } size_t drain(size_t count) { auto ndata = count; diff --git a/src/memchunk_test.cc b/src/memchunk_test.cc index 31761282..4d90553c 100644 --- a/src/memchunk_test.cc +++ b/src/memchunk_test.cc @@ -159,12 +159,12 @@ void test_memchunks_riovec(void) { auto m = chunks.head; CU_ASSERT(2 == iovcnt); - CU_ASSERT(m->begin == iov[0].iov_base); + CU_ASSERT(m->buf.data() == iov[0].iov_base); CU_ASSERT(m->len() == iov[0].iov_len); m = m->next; - CU_ASSERT(m->begin == iov[1].iov_base); + CU_ASSERT(m->buf.data() == iov[1].iov_base); CU_ASSERT(m->len() == iov[1].iov_len); chunks.drain(2 * 16); @@ -174,7 +174,7 @@ void test_memchunks_riovec(void) { CU_ASSERT(1 == iovcnt); m = chunks.head; - CU_ASSERT(m->begin == iov[0].iov_base); + CU_ASSERT(m->buf.data() == iov[0].iov_base); CU_ASSERT(m->len() == iov[0].iov_len); }