Commit 6774fa6e authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

buffer: Refactor

parent 7baf6f78
...@@ -29,21 +29,21 @@ ...@@ -29,21 +29,21 @@
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>
#include <array>
namespace nghttp2 { namespace nghttp2 {
template <size_t N> struct Buffer { template <size_t N> struct Buffer {
Buffer() : pos(begin), last(begin) {} Buffer() : pos(std::begin(buf)), last(pos) {}
// Returns the number of bytes to read. // Returns the number of bytes to read.
size_t rleft() const { return last - pos; } size_t rleft() const { return last - pos; }
// Returns the number of bytes this buffer can store. // Returns the number of bytes this buffer can store.
size_t wleft() const { return begin + N - last; } size_t wleft() const { return std::end(buf) - last; }
// Writes up to min(wleft(), |count|) bytes from buffer pointed by // Writes up to min(wleft(), |count|) bytes from buffer pointed by
// |buf|. Returns number of bytes written. // |src|. Returns number of bytes written.
size_t write(const void *buf, size_t count) { size_t write(const void *src, size_t count) {
count = std::min(count, wleft()); count = std::min(count, wleft());
memcpy(last, buf, count); last = std::copy_n(static_cast<const uint8_t *>(src), count, last);
last += count;
return count; return count;
} }
size_t write(size_t count) { size_t write(size_t count) {
...@@ -57,8 +57,8 @@ template <size_t N> struct Buffer { ...@@ -57,8 +57,8 @@ template <size_t N> struct Buffer {
pos += count; pos += count;
return count; return count;
} }
void reset() { pos = last = begin; } void reset() { pos = last = std::begin(buf); }
uint8_t begin[N]; std::array<uint8_t, N> buf;
uint8_t *pos, *last; uint8_t *pos, *last;
}; };
......
...@@ -45,13 +45,13 @@ void test_buffer_write(void) { ...@@ -45,13 +45,13 @@ void test_buffer_write(void) {
CU_ASSERT(3 == b.rleft()); CU_ASSERT(3 == b.rleft());
CU_ASSERT(13 == b.wleft()); CU_ASSERT(13 == b.wleft());
CU_ASSERT(0 == b.pos - b.begin); CU_ASSERT(0 == b.pos - std::begin(b.buf));
b.drain(3); b.drain(3);
CU_ASSERT(0 == b.rleft()); CU_ASSERT(0 == b.rleft());
CU_ASSERT(13 == b.wleft()); CU_ASSERT(13 == b.wleft());
CU_ASSERT(3 == b.pos - b.begin); CU_ASSERT(3 == b.pos - std::begin(b.buf));
auto n = b.write("0123456789ABCDEF", 16); auto n = b.write("0123456789ABCDEF", 16);
...@@ -59,20 +59,20 @@ void test_buffer_write(void) { ...@@ -59,20 +59,20 @@ void test_buffer_write(void) {
CU_ASSERT(13 == b.rleft()); CU_ASSERT(13 == b.rleft());
CU_ASSERT(0 == b.wleft()); CU_ASSERT(0 == b.wleft());
CU_ASSERT(3 == b.pos - b.begin); CU_ASSERT(3 == b.pos - std::begin(b.buf));
CU_ASSERT(0 == memcmp(b.pos, "0123456789ABC", 13)); CU_ASSERT(0 == memcmp(b.pos, "0123456789ABC", 13));
b.reset(); b.reset();
CU_ASSERT(0 == b.rleft()); CU_ASSERT(0 == b.rleft());
CU_ASSERT(0 == b.wleft()); CU_ASSERT(0 == b.wleft());
CU_ASSERT(0 == b.pos - b.begin); CU_ASSERT(0 == b.pos - std::begin(b.buf));
b.write(5); b.write(5);
CU_ASSERT(5 == b.rleft()); CU_ASSERT(5 == b.rleft());
CU_ASSERT(11 == b.wleft()); CU_ASSERT(11 == b.wleft());
CU_ASSERT(0 == b.pos - b.begin); CU_ASSERT(0 == b.pos - std::begin(b.buf));
} }
} // namespace nghttp2 } // namespace nghttp2
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment