From 9ae328fd8467ba9d05d4193f906e1808074f0ffd Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 28 Feb 2024 11:19:56 -0800 Subject: [PATCH] Further reduce code size --- native/src/base/include/stream.hpp | 10 ++++- native/src/base/stream.cpp | 62 ++++++++++++++++-------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/native/src/base/include/stream.hpp b/native/src/base/include/stream.hpp index 631cfd14b..027de1c1f 100644 --- a/native/src/base/include/stream.hpp +++ b/native/src/base/include/stream.hpp @@ -6,9 +6,13 @@ #include "../files.hpp" +#define ENABLE_IOV 0 + struct out_stream { virtual bool write(const void *buf, size_t len) = 0; +#if ENABLE_IOV virtual ssize_t writev(const iovec *iov, int iovcnt); +#endif virtual ~out_stream() = default; }; @@ -48,8 +52,10 @@ private: struct in_stream { virtual ssize_t read(void *buf, size_t len) = 0; - virtual ssize_t readFully(void *buf, size_t len); + ssize_t readFully(void *buf, size_t len); +#if ENABLE_IOV virtual ssize_t readv(const iovec *iov, int iovcnt); +#endif virtual ~in_stream() = default; }; @@ -102,8 +108,10 @@ class fd_stream : public file_stream { public: fd_stream(int fd) : fd(fd) {} ssize_t read(void *buf, size_t len) override; +#if ENABLE_IOV ssize_t readv(const iovec *iov, int iovcnt) override; ssize_t writev(const iovec *iov, int iovcnt) override; +#endif protected: ssize_t do_write(const void *buf, size_t len) override; private: diff --git a/native/src/base/stream.cpp b/native/src/base/stream.cpp index 2a03b2bef..f86e10cc1 100644 --- a/native/src/base/stream.cpp +++ b/native/src/base/stream.cpp @@ -45,27 +45,6 @@ ssize_t in_stream::readFully(void *buf, size_t len) { return read_sz; } -ssize_t in_stream::readv(const iovec *iov, int iovcnt) { - size_t read_sz = 0; - for (int i = 0; i < iovcnt; ++i) { - auto ret = readFully(iov[i].iov_base, iov[i].iov_len); - if (ret < 0) - return ret; - read_sz += ret; - } - return read_sz; -} - -ssize_t out_stream::writev(const iovec *iov, int iovcnt) { - size_t write_sz = 0; - for (int i = 0; i < iovcnt; ++i) { - if (!write(iov[i].iov_base, iov[i].iov_len)) - return write_sz; - write_sz += iov[i].iov_len; - } - return write_sz; -} - ssize_t fp_stream::read(void *buf, size_t len) { auto ret = fread(buf, 1, len, fp.get()); return ret ? ret : (ferror(fp.get()) ? -1 : 0); @@ -178,18 +157,10 @@ ssize_t fd_stream::read(void *buf, size_t len) { return ::read(fd, buf, len); } -ssize_t fd_stream::readv(const iovec *iov, int iovcnt) { - return ::readv(fd, iov, iovcnt); -} - ssize_t fd_stream::do_write(const void *buf, size_t len) { return ::write(fd, buf, len); } -ssize_t fd_stream::writev(const iovec *iov, int iovcnt) { - return ::writev(fd, iov, iovcnt); -} - bool file_stream::write(const void *buf, size_t len) { size_t write_sz = 0; ssize_t ret; @@ -204,3 +175,36 @@ bool file_stream::write(const void *buf, size_t len) { } while (write_sz != len && ret != 0); return true; } + +#if ENABLE_IOV + +ssize_t in_stream::readv(const iovec *iov, int iovcnt) { + size_t read_sz = 0; + for (int i = 0; i < iovcnt; ++i) { + auto ret = readFully(iov[i].iov_base, iov[i].iov_len); + if (ret < 0) + return ret; + read_sz += ret; + } + return read_sz; +} + +ssize_t out_stream::writev(const iovec *iov, int iovcnt) { + size_t write_sz = 0; + for (int i = 0; i < iovcnt; ++i) { + if (!write(iov[i].iov_base, iov[i].iov_len)) + return write_sz; + write_sz += iov[i].iov_len; + } + return write_sz; +} + +ssize_t fd_stream::readv(const iovec *iov, int iovcnt) { + return ::readv(fd, iov, iovcnt); +} + +ssize_t fd_stream::writev(const iovec *iov, int iovcnt) { + return ::writev(fd, iov, iovcnt); +} + +#endif // ENABLE_IOV