From 3cd26dec6471bfae937c217d99370d249d97b34c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 4 Apr 2009 08:23:53 +0000 Subject: [PATCH] compile time options to use readv/writev or not --- include/libtorrent/config.hpp | 3 +- src/file.cpp | 57 +++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index 9b503a599..6b3de8dbb 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -85,8 +85,9 @@ POSSIBILITY OF SUCH DAMAGE. #endif #define TORRENT_USE_IPV6 1 - #define TORRENT_USE_MLOCK 1 +#define TORRENT_USE_READV 1 +#define TORRENT_USE_WRITEV 1 // should wpath or path be used? #if defined UNICODE && !defined BOOST_FILESYSTEM_NARROW_ONLY \ diff --git a/src/file.cpp b/src/file.cpp index 04ba62156..b09f1f5e1 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -492,13 +492,17 @@ namespace libtorrent } CloseHandle(ol.hEvent); return ret; -#else + +#else // TORRENT_WINDOWS + size_type ret = lseek(m_fd, file_offset, SEEK_SET); if (ret < 0) { ec = error_code(errno, get_posix_category()); return -1; } +#if TORRENT_USE_READV + #ifdef TORRENT_LINUX bool aligned = false; int size = 0; @@ -510,7 +514,7 @@ namespace libtorrent if (size & (size_alignment()-1) == 0) aligned = true; } if (aligned) -#endif +#endif // TORRENT_LINUX { ret = ::readv(m_fd, bufs, num_bufs); if (ret < 0) @@ -532,8 +536,27 @@ namespace libtorrent return -1; } return (std::min)(ret, size_type(size)); -#endif -#endif +#endif // TORRENT_LINUX + +#else // TORRENT_USE_READV + + ret = 0; + for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i) + { + int tmp = read(m_fd, i->iov_base, i->iov_len); + if (tmp < 0) + { + ec = error_code(errno, get_posix_category()); + return -1; + } + ret += tmp; + if (tmp < i->iov_len) break; + } + return ret; + +#endif // TORRENT_USE_READV + +#endif // TORRENT_WINDOWS } size_type file::writev(size_type file_offset, iovec_t const* bufs, int num_bufs, error_code& ec) @@ -705,6 +728,9 @@ namespace libtorrent ec = error_code(errno, get_posix_category()); return -1; } + +#if TORRENT_USE_WRITEV + #ifdef TORRENT_LINUX bool aligned = false; int size = 0; @@ -743,8 +769,27 @@ namespace libtorrent return -1; } return (std::min)(ret, size_type(size)); -#endif -#endif +#endif // TORRENT_LINUX + +#else // TORRENT_USE_WRITEV + + ret = 0; + for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i) + { + int tmp = write(m_fd, i->iov_base, i->iov_len); + if (tmp < 0) + { + ec = error_code(errno, get_posix_category()); + return -1; + } + ret += tmp; + if (tmp < i->iov_len) break; + } + return ret; + +#endif // TORRENT_USE_WRITEV + +#endif // TORRENT_WINDOWS } bool file::set_size(size_type s, error_code& ec)