diff options
Diffstat (limited to 'ot_iovec.c')
| -rw-r--r-- | ot_iovec.c | 71 |
1 files changed, 35 insertions, 36 deletions
| @@ -4,90 +4,89 @@ | |||
| 4 | $id$ */ | 4 | $id$ */ |
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <sys/types.h> | ||
| 8 | #include <stdlib.h> | 7 | #include <stdlib.h> |
| 9 | #include <unistd.h> | 8 | #include <sys/types.h> |
| 10 | #include <sys/uio.h> | 9 | #include <sys/uio.h> |
| 10 | #include <unistd.h> | ||
| 11 | 11 | ||
| 12 | /* Libowfat */ | 12 | /* Libowfat */ |
| 13 | 13 | ||
| 14 | /* Opentracker */ | 14 | /* Opentracker */ |
| 15 | #include "ot_iovec.h" | 15 | #include "ot_iovec.h" |
| 16 | 16 | ||
| 17 | void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) { | 17 | void *iovec_increase(int *iovec_entries, struct iovec **iovector, size_t new_alloc) { |
| 18 | void *new_data; | 18 | void *new_data; |
| 19 | int new_entries = 1 + *iovec_entries; | 19 | int new_entries = 1 + *iovec_entries; |
| 20 | struct iovec *new_vec = realloc( *iovector, new_entries * sizeof( struct iovec ) ); | 20 | struct iovec *new_vec = realloc(*iovector, new_entries * sizeof(struct iovec)); |
| 21 | 21 | ||
| 22 | if( !new_vec ) | 22 | if (!new_vec) |
| 23 | return NULL; | 23 | return NULL; |
| 24 | 24 | ||
| 25 | /* Only allocate after we have a place to store the pointer */ | 25 | /* Only allocate after we have a place to store the pointer */ |
| 26 | new_data = malloc( new_alloc ); | 26 | new_data = malloc(new_alloc); |
| 27 | if( !new_data ) | 27 | if (!new_data) |
| 28 | return NULL; | 28 | return NULL; |
| 29 | 29 | ||
| 30 | new_vec[new_entries - 1].iov_base = new_data; | 30 | new_vec[new_entries - 1].iov_base = new_data; |
| 31 | new_vec[new_entries - 1].iov_len = new_alloc; | 31 | new_vec[new_entries - 1].iov_len = new_alloc; |
| 32 | 32 | ||
| 33 | *iovector = new_vec; | 33 | *iovector = new_vec; |
| 34 | ++*iovec_entries; | 34 | ++*iovec_entries; |
| 35 | return new_data; | 35 | return new_data; |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | void *iovec_append( int *iovec_entries, struct iovec **iovector, struct iovec *append_iovector) { | 38 | void *iovec_append(int *iovec_entries, struct iovec **iovector, struct iovec *append_iovector) { |
| 39 | int new_entries = *iovec_entries + 1; | 39 | int new_entries = *iovec_entries + 1; |
| 40 | struct iovec *new_vec = realloc( *iovector, new_entries * sizeof( struct iovec ) ); | 40 | struct iovec *new_vec = realloc(*iovector, new_entries * sizeof(struct iovec)); |
| 41 | if( !new_vec ) | 41 | if (!new_vec) |
| 42 | return NULL; | 42 | return NULL; |
| 43 | 43 | ||
| 44 | /* Take over data from appended iovec */ | 44 | /* Take over data from appended iovec */ |
| 45 | new_vec[*iovec_entries].iov_base = append_iovector->iov_base; | 45 | new_vec[*iovec_entries].iov_base = append_iovector->iov_base; |
| 46 | new_vec[*iovec_entries].iov_len = append_iovector->iov_len; | 46 | new_vec[*iovec_entries].iov_len = append_iovector->iov_len; |
| 47 | 47 | ||
| 48 | append_iovector->iov_base = NULL; | 48 | append_iovector->iov_base = NULL; |
| 49 | append_iovector->iov_len = 0; | 49 | append_iovector->iov_len = 0; |
| 50 | 50 | ||
| 51 | *iovector = new_vec; | 51 | *iovector = new_vec; |
| 52 | *iovec_entries = new_entries; | 52 | *iovec_entries = new_entries; |
| 53 | 53 | ||
| 54 | return new_vec; | 54 | return new_vec; |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | 57 | void iovec_free(int *iovec_entries, struct iovec **iovector) { | |
| 58 | void iovec_free( int *iovec_entries, struct iovec **iovector ) { | ||
| 59 | int i; | 58 | int i; |
| 60 | for( i=0; i<*iovec_entries; ++i ) | 59 | for (i = 0; i < *iovec_entries; ++i) |
| 61 | free( ((*iovector)[i]).iov_base ); | 60 | free(((*iovector)[i]).iov_base); |
| 62 | *iovector = NULL; | 61 | *iovector = NULL; |
| 63 | *iovec_entries = 0; | 62 | *iovec_entries = 0; |
| 64 | } | 63 | } |
| 65 | 64 | ||
| 66 | void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) { | 65 | void iovec_fixlast(int *iovec_entries, struct iovec **iovector, void *last_ptr) { |
| 67 | if( *iovec_entries ) { | 66 | if (*iovec_entries) { |
| 68 | char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base; | 67 | char *base = (char *)((*iovector)[*iovec_entries - 1]).iov_base; |
| 69 | size_t new_alloc = ((char*)last_ptr) - base; | 68 | size_t new_alloc = ((char *)last_ptr) - base; |
| 70 | 69 | ||
| 71 | ((*iovector)[*iovec_entries - 1 ]).iov_base = realloc( base, new_alloc ); | 70 | ((*iovector)[*iovec_entries - 1]).iov_base = realloc(base, new_alloc); |
| 72 | ((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc; | 71 | ((*iovector)[*iovec_entries - 1]).iov_len = new_alloc; |
| 73 | } | 72 | } |
| 74 | } | 73 | } |
| 75 | 74 | ||
| 76 | void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) { | 75 | void *iovec_fix_increase_or_free(int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc) { |
| 77 | void *new_data; | 76 | void *new_data; |
| 78 | 77 | ||
| 79 | iovec_fixlast( iovec_entries, iovector, last_ptr ); | 78 | iovec_fixlast(iovec_entries, iovector, last_ptr); |
| 80 | 79 | ||
| 81 | if( !( new_data = iovec_increase( iovec_entries, iovector, new_alloc ) ) ) | 80 | if (!(new_data = iovec_increase(iovec_entries, iovector, new_alloc))) |
| 82 | iovec_free( iovec_entries, iovector ); | 81 | iovec_free(iovec_entries, iovector); |
| 83 | 82 | ||
| 84 | return new_data; | 83 | return new_data; |
| 85 | } | 84 | } |
| 86 | 85 | ||
| 87 | size_t iovec_length( const int *iovec_entries, const struct iovec **iovector ) { | 86 | size_t iovec_length(const int *iovec_entries, const struct iovec **iovector) { |
| 88 | size_t length = 0; | 87 | size_t length = 0; |
| 89 | int i; | 88 | int i; |
| 90 | for( i=0; i<*iovec_entries; ++i ) | 89 | for (i = 0; i < *iovec_entries; ++i) |
| 91 | length += ((*iovector)[i]).iov_len; | 90 | length += ((*iovector)[i]).iov_len; |
| 92 | return length; | 91 | return length; |
| 93 | } | 92 | } |
