src/server/io.c

changeset 4
998844b5ed25
parent 1
3c066d52342d
child 8
f4d56bf9de40
--- a/src/server/io.c	Sun Oct 30 16:26:57 2011 +0100
+++ b/src/server/io.c	Sun Nov 13 13:43:01 2011 +0100
@@ -28,8 +28,10 @@
 
 #include <unistd.h>
 #include <stdlib.h>
+#include <sys/uio.h>
 
 #include "io.h"
+#include "pool.h"
 
 IOStream native_io_funcs = {
     system_write,
@@ -51,3 +53,33 @@
 ssize_t system_read(IOStream *st, void *buf, size_t nbytes) {
     return read(((SystemIOStream*)st)->fd, buf, nbytes);
 }
+
+
+/* iovec buffer */
+iovec_buf_t *iovec_buf_create(pool_handle_t *pool) {
+    iovec_buf_t *buf = pool_malloc(pool, sizeof(iovec_buf_t));
+
+    buf->pool = pool;
+    buf->iov  = pool_calloc(pool, 32, sizeof(struct iovec));
+    buf->maxiovec = 32;
+    buf->iovctn = 0;
+
+    return buf;
+}
+
+void iovec_buf_write(iovec_buf_t *io, void *buf, size_t nbyte) {
+    if(io->iovctn >= io->maxiovec) {
+        io->iov = pool_realloc(
+                io->pool,
+                io->iov,
+                (io->maxiovec + 16) * sizeof(struct iovec));
+    }
+
+    io->iov[io->iovctn].iov_base = buf;
+    io->iov[io->iovctn].iov_len = nbyte;
+    io->iovctn++;
+}
+
+ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) {
+    return writev(fd, io->iov, io->iovctn);
+}

mercurial