-
Notifications
You must be signed in to change notification settings - Fork 3
/
ot_iovec.c
76 lines (57 loc) · 1.99 KB
/
ot_iovec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This software was written by Dirk Engling <[email protected]>
It is considered beerware. Prost. Skol. Cheers or whatever.
$id$ */
/* System */
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/uio.h>
/* Libowfat */
/* Opentracker */
#include "ot_iovec.h"
void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) {
void *new_data;
int new_entries = 1 + *iovec_entries;
struct iovec *new_vec = realloc( *iovector, new_entries * sizeof( struct iovec ) );
if( !new_vec )
return NULL;
/* Only allocate after we have a place to store the pointer */
new_data = malloc( new_alloc );
if( !new_data )
return NULL;
new_vec[new_entries - 1].iov_base = new_data;
new_vec[new_entries - 1].iov_len = new_alloc;
*iovector = new_vec;
++*iovec_entries;
return new_data;
}
void iovec_free( int *iovec_entries, struct iovec **iovector ) {
int i;
for( i=0; i<*iovec_entries; ++i )
free( ((*iovector)[i]).iov_base );
*iovector = NULL;
*iovec_entries = 0;
}
void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) {
if( *iovec_entries ) {
char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base;
size_t new_alloc = ((char*)last_ptr) - base;
((*iovector)[*iovec_entries - 1 ]).iov_base = realloc( base, new_alloc );
((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc;
}
}
void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) {
void *new_data;
iovec_fixlast( iovec_entries, iovector, last_ptr );
if( !( new_data = iovec_increase( iovec_entries, iovector, new_alloc ) ) )
iovec_free( iovec_entries, iovector );
return new_data;
}
size_t iovec_length( const int *iovec_entries, const struct iovec **iovector ) {
size_t length = 0;
int i;
for( i=0; i<*iovec_entries; ++i )
length += ((*iovector)[i]).iov_len;
return length;
}
const char *g_version_iovec_c = "$Source$: $Revision$\n";