From 6250a2b746aee1937c475d6ff56fe45f0b984d6e Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sat, 14 Jan 2017 09:42:46 +0100 Subject: [PATCH] fix(streambuf): Buffer size --- include/utils/file.hpp | 7 ++++--- src/utils/file.cpp | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/utils/file.hpp b/include/utils/file.hpp index 2ca2ff47..840bc8fa 100644 --- a/include/utils/file.hpp +++ b/include/utils/file.hpp @@ -56,7 +56,7 @@ class fd_streambuf : public std::streambuf { template explicit fd_streambuf(Args&&... args) : m_fd(forward(args)...) { setg(m_in, m_in, m_in); - setp(m_out, m_out + sizeof(m_in)); + setp(m_out, m_out + bufsize - 1); } ~fd_streambuf(); @@ -73,8 +73,9 @@ class fd_streambuf : public std::streambuf { private: file_descriptor m_fd; - char m_out[BUFSIZ]{}; - char m_in[BUFSIZ - 1]{}; + enum { bufsize = 1024 }; + char m_out[bufsize]; + char m_in[bufsize - 1]; }; template diff --git a/src/utils/file.cpp b/src/utils/file.cpp index 8ecc0c1a..9f94f761 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -118,7 +119,7 @@ void fd_streambuf::open(int fd) { } m_fd = fd; setg(m_in, m_in, m_in); - setp(m_out, m_out + sizeof(m_in)); + setp(m_out, m_out + bufsize - 1); } void fd_streambuf::close() { @@ -131,7 +132,7 @@ void fd_streambuf::close() { int fd_streambuf::sync() { if (pbase() != pptr()) { auto size = pptr() - pbase(); - auto bytes = ::write(m_fd, m_out, size); + auto bytes = write(m_fd, m_out, size); if (bytes > 0) { std::copy(pbase() + bytes, pptr(), pbase()); setp(pbase(), epptr()); @@ -151,9 +152,9 @@ int fd_streambuf::overflow(int c) { int fd_streambuf::underflow() { if (gptr() == egptr()) { - std::streamsize pback(std::min(gptr() - eback(), std::ptrdiff_t(m_in))); + std::streamsize pback(std::min(gptr() - eback(), std::ptrdiff_t(16 - sizeof(int)))); std::copy(egptr() - pback, egptr(), eback()); - int bytes(::read(m_fd, eback() + pback, BUFSIZ)); + int bytes(read(m_fd, eback() + pback, bufsize)); setg(eback(), eback() + pback, eback() + pback + std::max(0, bytes)); } return gptr() == egptr() ? traits_type::eof() : traits_type::to_int_type(*gptr());