[sheepdog] [PATCH v7 4/6] lib: add wrappers do_writev2() and writev2() for typical usage of writev()

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Mar 11 04:48:56 CET 2013


This patch adds 2 new wrappers: do_write2v() and writev2() for
enshorting typical usage of writev().

Typical usage of writev() is sending an array of struct iovec with 2
elements, one for a header and another for a message body.
With do_writev2(), declaring the array in caller side can be removed.

writev2() is a wrapper for do_writev2(). Typical headers have fixed
length, so writev2() obtains their length with sizeof() (so writev2()
is defined as a macro function).

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 include/net.h |    5 +++++
 lib/net.c     |   15 +++++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/include/net.h b/include/net.h
index 63771a9..61bd6aa 100644
--- a/include/net.h
+++ b/include/net.h
@@ -72,5 +72,10 @@ int set_snd_timeout(int fd);
 int set_rcv_timeout(int fd);
 int get_local_addr(uint8_t *bytes);
 bool inetaddr_is_valid(char *addr);
+int do_writev2(int fd, void *hdr, size_t hdr_len, void *body, size_t body_len);
+
+/* for typical usage of do_writev2() */
+#define writev2(fd, hdr, body, body_len)	\
+	do_writev2(fd, hdr, sizeof(*hdr), body, body_len)
 
 #endif
diff --git a/lib/net.c b/lib/net.c
index 116e55a..f25e733 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -25,6 +25,8 @@
 #include <sys/un.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/uio.h>
+
 #include "sheepdog_proto.h"
 #include "sheep.h"
 #include "util.h"
@@ -648,3 +650,16 @@ bool inetaddr_is_valid(char *addr)
 	}
 	return true;
 }
+
+int do_writev2(int fd, void *hdr, size_t hdr_len, void *body, size_t body_len)
+{
+	struct iovec iov[2];
+
+	iov[0].iov_base = hdr;
+	iov[0].iov_len = hdr_len;
+
+	iov[1].iov_base = body;
+	iov[1].iov_len = body_len;
+
+	return writev(fd, iov, 2);
+}
-- 
1.7.2.5




More information about the sheepdog mailing list