address linter warnings. Thanks to gagath@debian.org
[opentracker.git] / ot_iovec.c
blob8e94c523e1f26de4023b140eee12eb8e7c8b92e3
1 /* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever.
4 $id$ */
6 /* System */
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/uio.h>
10 #include <unistd.h>
12 /* Libowfat */
14 /* Opentracker */
15 #include "ot_iovec.h"
17 void *iovec_increase(int *iovec_entries, struct iovec **iovector, size_t new_alloc) {
18 void *new_data;
19 int new_entries = 1 + *iovec_entries;
20 struct iovec *new_vec = realloc(*iovector, new_entries * sizeof(struct iovec));
22 if (!new_vec)
23 return NULL;
25 /* Only allocate after we have a place to store the pointer */
26 new_data = malloc(new_alloc);
27 if (!new_data)
28 return NULL;
30 new_vec[new_entries - 1].iov_base = new_data;
31 new_vec[new_entries - 1].iov_len = new_alloc;
33 *iovector = new_vec;
34 ++*iovec_entries;
35 return new_data;
38 void *iovec_append(int *iovec_entries, struct iovec **iovector, struct iovec *append_iovector) {
39 int new_entries = *iovec_entries + 1;
40 struct iovec *new_vec = realloc(*iovector, new_entries * sizeof(struct iovec));
41 if (!new_vec)
42 return NULL;
44 /* Take over data from appended iovec */
45 new_vec[*iovec_entries].iov_base = append_iovector->iov_base;
46 new_vec[*iovec_entries].iov_len = append_iovector->iov_len;
48 append_iovector->iov_base = NULL;
49 append_iovector->iov_len = 0;
51 *iovector = new_vec;
52 *iovec_entries = new_entries;
54 return new_vec;
57 void iovec_free(int *iovec_entries, struct iovec **iovector) {
58 int i;
59 for (i = 0; i < *iovec_entries; ++i)
60 free(((*iovector)[i]).iov_base);
61 *iovector = NULL;
62 *iovec_entries = 0;
65 void iovec_fixlast(int *iovec_entries, struct iovec **iovector, void *last_ptr) {
66 if (*iovec_entries) {
67 char *base = (char *)((*iovector)[*iovec_entries - 1]).iov_base;
68 size_t new_alloc = ((char *)last_ptr) - base;
70 ((*iovector)[*iovec_entries - 1]).iov_base = realloc(base, new_alloc);
71 ((*iovector)[*iovec_entries - 1]).iov_len = new_alloc;
75 void *iovec_fix_increase_or_free(int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc) {
76 void *new_data;
78 iovec_fixlast(iovec_entries, iovector, last_ptr);
80 if (!(new_data = iovec_increase(iovec_entries, iovector, new_alloc)))
81 iovec_free(iovec_entries, iovector);
83 return new_data;
86 size_t iovec_length(const int *iovec_entries, const struct iovec **iovector) {
87 size_t length = 0;
88 int i;
89 for (i = 0; i < *iovec_entries; ++i)
90 length += ((*iovector)[i]).iov_len;
91 return length;