Little Palm fixes
[MonkeyD.git] / src / iov.c
blobd013defa9eda46abd6713dd05e2f5e01b64637d3
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE
23 #include <fcntl.h>
25 #include <sys/uio.h>
26 #include <sys/mman.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <limits.h>
31 #include "monkey.h"
33 #include "header.h"
34 #include "memory.h"
35 #include "utils.h"
36 #include "iov.h"
38 struct mk_iov *mk_iov_create(int n, int offset)
40 struct mk_iov *iov;
42 iov = mk_mem_malloc(sizeof(struct mk_iov));
43 iov->iov_idx = offset;
44 iov->io = mk_mem_malloc(n * sizeof(struct iovec));
45 iov->buf_to_free = mk_mem_malloc(n * sizeof(char *));
46 iov->buf_idx = 0;
47 iov->total_len = 0;
48 iov->size = n;
50 return iov;
53 int mk_iov_add_entry(struct mk_iov *mk_io, char *buf, int len,
54 mk_pointer sep, int free)
56 if (buf) {
57 mk_io->io[mk_io->iov_idx].iov_base = (unsigned char *) buf;
58 mk_io->io[mk_io->iov_idx].iov_len = len;
59 mk_io->iov_idx++;
60 mk_io->total_len += len;
63 #ifdef DEBUG_IOV
64 if (mk_io->iov_idx > mk_io->size) {
65 printf("\nDEBUG IOV :: ERROR, Broken array size in:");
66 printf("\n '''%s'''", buf);
67 fflush(stdout);
69 #endif
71 /* Add separator */
72 if (sep.len > 0) {
73 mk_io->io[mk_io->iov_idx].iov_base = sep.data;
74 mk_io->io[mk_io->iov_idx].iov_len = sep.len;
75 mk_io->iov_idx++;
76 mk_io->total_len += sep.len;
79 if (free == MK_IOV_FREE_BUF) {
80 _mk_iov_set_free(mk_io, buf);
83 return mk_io->iov_idx;
86 int mk_iov_set_entry(struct mk_iov *mk_io, char *buf, int len,
87 int free, int idx)
89 mk_io->io[idx].iov_base = buf;
90 mk_io->io[idx].iov_len = len;
91 mk_io->total_len += len;
93 if (free == MK_IOV_FREE_BUF) {
94 _mk_iov_set_free(mk_io, buf);
97 return 0;
100 void _mk_iov_set_free(struct mk_iov *mk_io, char *buf)
102 mk_io->buf_to_free[mk_io->buf_idx] = (char *) buf;
103 mk_io->buf_idx++;
106 ssize_t mk_iov_send(int fd, struct mk_iov *mk_io, int to)
108 ssize_t n = -1;
110 if (to == MK_IOV_SEND_TO_SOCKET) {
111 n = writev(fd, mk_io->io, mk_io->iov_idx);
112 if( n < 0 ) {
113 #ifdef TRACE
114 MK_TRACE( "writev() error on FD %i", fd);
115 #endif
116 perror("writev");
119 else if (to == MK_IOV_SEND_TO_PIPE) {
120 /* for some reason, vmsplice is not working as expected for us,
121 * maybe we need to fix something here, at the moment
122 * we will keep using writev to push the iovec struct to the pipe
125 * n = vmsplice(fd,
126 * (const struct iovec *) mk_io->io,
127 * mk_io->iov_idx,
128 * SPLICE_F_GIFT);
129 * return n;
132 n = writev(fd, mk_io->io, mk_io->iov_idx);
134 if (n < 0) {
135 #ifdef TRACE
136 MK_TRACE("writev() error on FD %i", fd);
137 #endif
138 perror("writev");
142 return n;
145 void mk_iov_free(struct mk_iov *mk_io)
147 mk_iov_free_marked(mk_io);
148 mk_mem_free(mk_io->buf_to_free);
149 mk_mem_free(mk_io->io);
150 mk_mem_free(mk_io);
153 void mk_iov_free_marked(struct mk_iov *mk_io)
155 int i, limit = 0;
157 limit = mk_io->buf_idx;
159 for (i = 0; i < limit; i++) {
161 #ifdef DEBUG_IOV
162 printf("\nDEBUG IOV :: going free (idx: %i/%i): %s", i,
163 limit, mk_io->buf_to_free[i]);
164 fflush(stdout);
165 #endif
166 mk_mem_free(mk_io->buf_to_free[i]);
169 mk_io->iov_idx = 0;
170 mk_io->buf_idx = 0;
173 void mk_iov_print(struct mk_iov *mk_io)
175 int i, j;
176 char *c;
178 for (i = 0; i < mk_io->iov_idx; i++) {
179 printf("\n[index=%i len=%i]\n '", i, (int) mk_io->io[i].iov_len);
180 fflush(stdout);
181 for (j=0; j < mk_io->io[i].iov_len; j++) {
182 c = mk_io->io[i].iov_base;
183 printf("%c", c[j]);
184 fflush(stdout);
186 printf("-'\n[end=%i]\n", j);
187 fflush(stdout);
191 void mk_iov_separators_init()
193 mk_pointer_set(&mk_iov_crlf, MK_IOV_CRLF);
194 mk_pointer_set(&mk_iov_lf, MK_IOV_LF);
195 mk_pointer_set(&mk_iov_space, MK_IOV_SPACE);
196 mk_pointer_set(&mk_iov_slash, MK_IOV_SLASH);
197 mk_pointer_set(&mk_iov_none, MK_IOV_NONE);
198 mk_pointer_set(&mk_iov_equal, MK_IOV_EQUAL);