From de2a32be7bb4422748adfa28e4531d2c0905c16d Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Sat, 16 Jan 2016 12:54:05 +0000 Subject: [PATCH] Make CPBuffer a template, parameterized by buffer size --- src/control.h | 2 +- src/cpbuffer.h | 26 +++++++++++++------------- src/dinitctl.cc | 8 ++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/control.h b/src/control.h index f027f01..9f10fed 100644 --- a/src/control.h +++ b/src/control.h @@ -60,7 +60,7 @@ class ControlConn : private ServiceListener int chklen; // Receive buffer - CPBuffer rbuf; + CPBuffer<1024> rbuf; template using list = std::list; template using vector = std::vector; diff --git a/src/cpbuffer.h b/src/cpbuffer.h index 94132ca..751cbf8 100644 --- a/src/cpbuffer.h +++ b/src/cpbuffer.h @@ -4,9 +4,9 @@ #include // control protocol buffer, a circular buffer with 1024-byte capacity. -class CPBuffer +template class CPBuffer { - char buf[1024]; + char buf[SIZE]; int cur_idx = 0; int length = 0; // number of elements in the buffer @@ -20,8 +20,8 @@ class CPBuffer int fill(int fd) noexcept { int pos = cur_idx + length; - if (pos >= 1024) pos -= 1024; - int max_count = std::min(1024 - pos, 1024 - length); + if (pos >= SIZE) pos -= SIZE; + int max_count = std::min(SIZE - pos, SIZE - length); ssize_t r = read(fd, buf + pos, max_count); if (r >= 0) { length += r; @@ -43,24 +43,24 @@ class CPBuffer int operator[](int idx) noexcept { int dest_idx = cur_idx + idx; - if (dest_idx > 1024) dest_idx -= 1024; + if (dest_idx > SIZE) dest_idx -= SIZE; return buf[dest_idx]; } void consume(int amount) noexcept { cur_idx += amount; - if (cur_idx >= 1024) cur_idx -= 1024; + if (cur_idx >= SIZE) cur_idx -= SIZE; length -= amount; } void extract(char *dest, int index, int length) noexcept { index += cur_idx; - if (index >= 1024) index -= 1024; - if (index + length > 1024) { + if (index >= SIZE) index -= SIZE; + if (index + length > SIZE) { // wrap-around copy - int half = 1024 - index; + int half = SIZE - index; std::memcpy(dest, buf + index, half); std::memcpy(dest + half, buf, length - half); } @@ -74,10 +74,10 @@ class CPBuffer std::string extract_string(int index, int length) { index += cur_idx; - if (index >= 1024) index -= 1024; - if (index + length > 1024) { - std::string r(buf + index, 1024 - index); - r.insert(r.end(), buf, buf + length - (1024 - index)); + if (index >= SIZE) index -= SIZE; + if (index + length > SIZE) { + std::string r(buf + index, SIZE - index); + r.insert(r.end(), buf, buf + length - (SIZE - index)); return r; } else { diff --git a/src/dinitctl.cc b/src/dinitctl.cc index 0928bb8..1e5c2fe 100644 --- a/src/dinitctl.cc +++ b/src/dinitctl.cc @@ -30,7 +30,7 @@ class ReadCPException ReadCPException(int err) : errcode(err) { } }; -static void fillBufferTo(CPBuffer *buf, int fd, int rlength) +static void fillBufferTo(CPBuffer<1024> *buf, int fd, int rlength) { int r = buf->fillTo(fd, rlength); if (r == -1) { @@ -53,7 +53,7 @@ static const char * describeVerb(bool stop) // Wait for a reply packet, skipping over any information packets // that are received in the meantime. -static void wait_for_reply(CPBuffer &rbuffer, int fd) +static void wait_for_reply(CPBuffer<1024> &rbuffer, int fd) { fillBufferTo(&rbuffer, fd, 1); @@ -263,7 +263,7 @@ int main(int argc, char **argv) // Now we expect a reply: try { - CPBuffer rbuffer; + CPBuffer<1024> rbuffer; wait_for_reply(rbuffer, socknum); //ServiceState state; @@ -443,7 +443,7 @@ static int unpinService(int socknum, const char *service_name) // Now we expect a reply: try { - CPBuffer rbuffer; + CPBuffer<1024> rbuffer; wait_for_reply(rbuffer, socknum); //ServiceState state; -- 2.25.1