summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2007-11-23 18:12:50 +0000
committererdgeist <>2007-11-23 18:12:50 +0000
commit616302c13962e6b9be9b0ca552ded9cd37e85597 (patch)
tree2042454ee60444ebf267c0706e97a18c03a754c4
parentb1c8723609578b05b999f9cb58b9c90c4787f9d6 (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
-rw-r--r--ot_iovec.c21
-rw-r--r--ot_iovec.h5
2 files changed, 21 insertions, 5 deletions
diff --git a/ot_iovec.c b/ot_iovec.c
index a098733..f71d4d7 100644
--- a/ot_iovec.c
+++ b/ot_iovec.c
@@ -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
37void iovec_fixlast( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) { 37void 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
55void *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
54size_t iovec_length( int *iovec_entries, struct iovec **iovector ) { 67size_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;
diff --git a/ot_iovec.h b/ot_iovec.h
index a2d329d..d52a167 100644
--- a/ot_iovec.h
+++ b/ot_iovec.h
@@ -7,8 +7,11 @@
7#include <sys/uio.h> 7#include <sys/uio.h>
8 8
9void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ); 9void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc );
10void iovec_fixlast( int *iovec_entries, struct iovec **iovector, size_t new_alloc ); 10void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr );
11void iovec_free( int *iovec_entries, struct iovec **iovector ); 11void iovec_free( int *iovec_entries, struct iovec **iovector );
12
12size_t iovec_length( int *iovec_entries, struct iovec **iovector ); 13size_t iovec_length( int *iovec_entries, struct iovec **iovector );
13 14
15void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc );
16
14#endif 17#endif