Skip to content

Commit

Permalink
buffer: revert write_fd to original impl
Browse files Browse the repository at this point in the history
Signed-off-by: Haomai Wang <haomai@xsky.com>
  • Loading branch information
yuyuyu101 committed Jan 29, 2016
1 parent 39c05ab commit 92c81a8
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions src/common/buffer.cc
Expand Up @@ -1994,14 +1994,61 @@ static int do_writev(int fd, struct iovec *vec, uint64_t offset, unsigned veclen

int buffer::list::write_fd(int fd) const
{
return write_fd(fd, 0);
if (can_zero_copy())
return write_fd_zero_copy(fd);

// use writev!
iovec iov[IOV_MAX];
int iovlen = 0;
ssize_t bytes = 0;

std::list<ptr>::const_iterator p = _buffers.begin();
while (p != _buffers.end()) {
if (p->length() > 0) {
iov[iovlen].iov_base = (void *)p->c_str();
iov[iovlen].iov_len = p->length();
bytes += p->length();
iovlen++;
}
++p;

if (iovlen == IOV_MAX-1 ||
p == _buffers.end()) {
iovec *start = iov;
int num = iovlen;
ssize_t wrote;
retry:
wrote = ::writev(fd, start, num);
if (wrote < 0) {
int err = errno;
if (err == EINTR)
goto retry;
return -err;
}
if (wrote < bytes) {
// partial write, recover!
while ((size_t)wrote >= start[0].iov_len) {
wrote -= start[0].iov_len;
bytes -= start[0].iov_len;
start++;
num--;
}
if (wrote > 0) {
start[0].iov_len -= wrote;
start[0].iov_base = (char *)start[0].iov_base + wrote;
bytes -= wrote;
}
goto retry;
}
iovlen = 0;
bytes = 0;
}
}
return 0;
}

int buffer::list::write_fd(int fd, uint64_t offset) const
{
if (can_zero_copy())
return write_fd_zero_copy(fd);

iovec iov[IOV_MAX];
unsigned iovlen = 0;
ssize_t bytes = 0;
Expand Down

0 comments on commit 92c81a8

Please sign in to comment.