diff options
| -rw-r--r-- | ot_iovec.c | 21 | ||||
| -rw-r--r-- | ot_iovec.h | 5 |
2 files changed, 21 insertions, 5 deletions
| @@ -34,23 +34,36 @@ void iovec_free( int *iovec_entries, struct iovec **iovector ) { | |||
| 34 | *iovec_entries = 0; | 34 | *iovec_entries = 0; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | void iovec_fixlast( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) { | 37 | void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) { |
| 38 | int page_size = getpagesize(); | 38 | int page_size = getpagesize(); |
| 39 | size_t old_alloc, old_pages, new_pages; | 39 | size_t old_alloc, new_alloc, old_pages, new_pages; |
| 40 | char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base; | ||
| 40 | 41 | ||
| 41 | if( !*iovec_entries ) return; | 42 | if( !*iovec_entries ) return; |
| 42 | 43 | ||
| 43 | old_alloc = ((*iovector)[ *iovec_entries - 1 ]).iov_len; | 44 | old_alloc = ((*iovector)[ *iovec_entries - 1 ]).iov_len; |
| 45 | new_alloc = ((char*)last_ptr) - base; | ||
| 44 | old_pages = 1 + old_alloc / page_size; | 46 | old_pages = 1 + old_alloc / page_size; |
| 45 | new_pages = 1 + new_alloc / page_size; | 47 | new_pages = 1 + new_alloc / page_size; |
| 46 | 48 | ||
| 47 | if( old_pages != new_pages ) { | 49 | if( old_pages != new_pages ) { |
| 48 | munmap( ((char*)((*iovector)[ *iovec_entries - 1 ]).iov_base ) + new_pages * page_size, | 50 | munmap( base + new_pages * page_size, old_alloc - new_pages * page_size ); |
| 49 | old_alloc - new_pages * page_size ); | ||
| 50 | } | 51 | } |
| 51 | ((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc; | 52 | ((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc; |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 55 | void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) { | ||
| 56 | void *new_ptr; | ||
| 57 | |||
| 58 | iovec_fixlast( iovec_entries, iovector, last_ptr ); | ||
| 59 | |||
| 60 | if( !( new_ptr = iovec_increase( iovec_entries, iovector, new_alloc ) ) ) | ||
| 61 | iovec_free( iovec_entries, iovector ); | ||
| 62 | |||
| 63 | return new_ptr; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 54 | size_t iovec_length( int *iovec_entries, struct iovec **iovector ) { | 67 | size_t iovec_length( int *iovec_entries, struct iovec **iovector ) { |
| 55 | size_t length = 0; | 68 | size_t length = 0; |
| 56 | int i; | 69 | int i; |
| @@ -7,8 +7,11 @@ | |||
| 7 | #include <sys/uio.h> | 7 | #include <sys/uio.h> |
| 8 | 8 | ||
| 9 | void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ); | 9 | void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ); |
| 10 | void iovec_fixlast( int *iovec_entries, struct iovec **iovector, size_t new_alloc ); | 10 | void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ); |
| 11 | void iovec_free( int *iovec_entries, struct iovec **iovector ); | 11 | void iovec_free( int *iovec_entries, struct iovec **iovector ); |
| 12 | |||
| 12 | size_t iovec_length( int *iovec_entries, struct iovec **iovector ); | 13 | size_t iovec_length( int *iovec_entries, struct iovec **iovector ); |
| 13 | 14 | ||
| 15 | void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ); | ||
| 16 | |||
| 14 | #endif | 17 | #endif |
