1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
5 * Copyright (C) 2008, Eduardo Silva P.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 struct mk_iov
*mk_iov_create(int n
, int offset
)
41 iov
= mk_mem_malloc(sizeof(struct mk_iov
));
42 iov
->iov_idx
= offset
;
43 iov
->io
= mk_mem_malloc(n
*sizeof(struct iovec
));
44 iov
->buf_to_free
= mk_mem_malloc(n
*sizeof(char *));
52 int mk_iov_add_entry(struct mk_iov
*mk_io
, char *buf
, int len
,
53 mk_pointer sep
, int free
)
55 mk_io
->io
[mk_io
->iov_idx
].iov_base
= buf
;
56 mk_io
->io
[mk_io
->iov_idx
].iov_len
= len
;
58 mk_io
->total_len
+= len
;
61 if(mk_io
->iov_idx
> mk_io
->size
){
62 printf("\nDEBUG IOV :: ERROR, Broke array size in:");
63 printf("\n '''%s'''", buf
);
71 mk_io
->io
[mk_io
->iov_idx
].iov_base
= sep
.data
;
72 mk_io
->io
[mk_io
->iov_idx
].iov_len
= sep
.len
;
74 mk_io
->total_len
+= sep
.len
;
77 if(free
== MK_IOV_FREE_BUF
)
79 _mk_iov_set_free(mk_io
, buf
);
82 return mk_io
->iov_idx
;
85 int mk_iov_set_entry(struct mk_iov
*mk_io
, char *buf
, int len
,
88 mk_io
->io
[idx
].iov_base
= buf
;
89 mk_io
->io
[idx
].iov_len
= len
;
90 mk_io
->total_len
+= len
;
92 if(free
== MK_IOV_FREE_BUF
)
94 _mk_iov_set_free(mk_io
, buf
);
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
;
106 ssize_t
mk_iov_send(int fd
, struct mk_iov
*mk_io
, int to
)
110 if(to
==MK_IOV_SEND_TO_SOCKET
){
111 n
= writev(fd
, mk_io
->io
, mk_io
->iov_idx
);
116 else if(to
==MK_IOV_SEND_TO_PIPE
){
117 /* for some reason, vmsplice is not working as expected for us,
118 * maybe we need to fix something here, at the moment
119 * we will keep using writev to push the iovec struct to the pipe
121 * n = vmsplice(fd, mk_io->io, mk_io->iov_idx,
125 * perror("vmsplice");
128 n
= writev(fd
, mk_io
->io
, mk_io
->iov_idx
);
138 void mk_iov_free(struct mk_iov
*mk_io
)
140 mk_iov_free_marked(mk_io
);
141 mk_mem_free(mk_io
->io
);
144 void mk_iov_free_marked(struct mk_iov
*mk_io
)
148 limit
= mk_io
->buf_idx
;
150 for(i
=0; i
<limit
; i
++)
154 printf("\nDEBUG IOV :: going free (idx: %i/%i): %s",i
,
155 limit
, mk_io
->buf_to_free
[i
]);
158 mk_mem_free(mk_io
->buf_to_free
[i
]);
165 void mk_iov_print(struct mk_iov
*mk_io
)
169 for(i
=0; i
<mk_io
->iov_idx
; i
++){
170 printf("\n%i len=%i) '%s'", i
, mk_io
->io
[i
].iov_len
,
171 (char *) mk_io
->io
[i
].iov_base
);
176 void mk_iov_separators_init()
178 mk_pointer_set(&mk_iov_crlf
, MK_IOV_CRLF
);
179 mk_pointer_set(&mk_iov_lf
, MK_IOV_LF
);
180 mk_pointer_set(&mk_iov_space
, MK_IOV_SPACE
);
181 mk_pointer_set(&mk_iov_header_value
, MK_IOV_HEADER_VALUE
);
182 mk_pointer_set(&mk_iov_slash
, MK_IOV_SLASH
);
183 mk_pointer_set(&mk_iov_none
, MK_IOV_NONE
);
184 mk_pointer_set(&mk_iov_equal
, MK_IOV_EQUAL
);