diff options
| author | erdgeist <> | 2007-11-23 18:12:50 +0000 |
|---|---|---|
| committer | erdgeist <> | 2007-11-23 18:12:50 +0000 |
| commit | 616302c13962e6b9be9b0ca552ded9cd37e85597 (patch) | |
| tree | 2042454ee60444ebf267c0706e97a18c03a754c4 /ot_iovec.c | |
| parent | b1c8723609578b05b999f9cb58b9c90c4787f9d6 (diff) | |
make ot_iovecs fix last interface more sane, also add a convenience function that handles the task of fixing, allocating and - if necessary - freeing
Diffstat (limited to 'ot_iovec.c')
| -rw-r--r-- | ot_iovec.c | 21 |
1 files changed, 17 insertions, 4 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; |
