src/server/io.c

changeset 4
998844b5ed25
parent 1
3c066d52342d
child 8
f4d56bf9de40
equal deleted inserted replaced
3:137197831306 4:998844b5ed25
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include <unistd.h> 29 #include <unistd.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <sys/uio.h>
31 32
32 #include "io.h" 33 #include "io.h"
34 #include "pool.h"
33 35
34 IOStream native_io_funcs = { 36 IOStream native_io_funcs = {
35 system_write, 37 system_write,
36 system_read 38 system_read
37 }; 39 };
49 } 51 }
50 52
51 ssize_t system_read(IOStream *st, void *buf, size_t nbytes) { 53 ssize_t system_read(IOStream *st, void *buf, size_t nbytes) {
52 return read(((SystemIOStream*)st)->fd, buf, nbytes); 54 return read(((SystemIOStream*)st)->fd, buf, nbytes);
53 } 55 }
56
57
58 /* iovec buffer */
59 iovec_buf_t *iovec_buf_create(pool_handle_t *pool) {
60 iovec_buf_t *buf = pool_malloc(pool, sizeof(iovec_buf_t));
61
62 buf->pool = pool;
63 buf->iov = pool_calloc(pool, 32, sizeof(struct iovec));
64 buf->maxiovec = 32;
65 buf->iovctn = 0;
66
67 return buf;
68 }
69
70 void iovec_buf_write(iovec_buf_t *io, void *buf, size_t nbyte) {
71 if(io->iovctn >= io->maxiovec) {
72 io->iov = pool_realloc(
73 io->pool,
74 io->iov,
75 (io->maxiovec + 16) * sizeof(struct iovec));
76 }
77
78 io->iov[io->iovctn].iov_base = buf;
79 io->iov[io->iovctn].iov_len = nbyte;
80 io->iovctn++;
81 }
82
83 ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) {
84 return writev(fd, io->iov, io->iovctn);
85 }

mercurial