Further reduce code size

This commit is contained in:
topjohnwu 2024-02-28 11:19:56 -08:00
parent 625a1d6f44
commit 9ae328fd84
2 changed files with 42 additions and 30 deletions

View File

@ -6,9 +6,13 @@
#include "../files.hpp" #include "../files.hpp"
#define ENABLE_IOV 0
struct out_stream { struct out_stream {
virtual bool write(const void *buf, size_t len) = 0; virtual bool write(const void *buf, size_t len) = 0;
#if ENABLE_IOV
virtual ssize_t writev(const iovec *iov, int iovcnt); virtual ssize_t writev(const iovec *iov, int iovcnt);
#endif
virtual ~out_stream() = default; virtual ~out_stream() = default;
}; };
@ -48,8 +52,10 @@ private:
struct in_stream { struct in_stream {
virtual ssize_t read(void *buf, size_t len) = 0; 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); virtual ssize_t readv(const iovec *iov, int iovcnt);
#endif
virtual ~in_stream() = default; virtual ~in_stream() = default;
}; };
@ -102,8 +108,10 @@ class fd_stream : public file_stream {
public: public:
fd_stream(int fd) : fd(fd) {} fd_stream(int fd) : fd(fd) {}
ssize_t read(void *buf, size_t len) override; ssize_t read(void *buf, size_t len) override;
#if ENABLE_IOV
ssize_t readv(const iovec *iov, int iovcnt) override; ssize_t readv(const iovec *iov, int iovcnt) override;
ssize_t writev(const iovec *iov, int iovcnt) override; ssize_t writev(const iovec *iov, int iovcnt) override;
#endif
protected: protected:
ssize_t do_write(const void *buf, size_t len) override; ssize_t do_write(const void *buf, size_t len) override;
private: private:

View File

@ -45,27 +45,6 @@ ssize_t in_stream::readFully(void *buf, size_t len) {
return read_sz; 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) { ssize_t fp_stream::read(void *buf, size_t len) {
auto ret = fread(buf, 1, len, fp.get()); auto ret = fread(buf, 1, len, fp.get());
return ret ? ret : (ferror(fp.get()) ? -1 : 0); 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); 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) { ssize_t fd_stream::do_write(const void *buf, size_t len) {
return ::write(fd, buf, 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) { bool file_stream::write(const void *buf, size_t len) {
size_t write_sz = 0; size_t write_sz = 0;
ssize_t ret; ssize_t ret;
@ -204,3 +175,36 @@ bool file_stream::write(const void *buf, size_t len) {
} while (write_sz != len && ret != 0); } while (write_sz != len && ret != 0);
return true; 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