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