Turn mmaps into malloc

dynamic-accesslists
Dirk Engling 4 years ago
parent 95f1780f0b
commit 397cd035a4

@ -125,7 +125,7 @@ ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iove
struct http_data *cookie = io_getcookie( sock ); struct http_data *cookie = io_getcookie( sock );
char *header; char *header;
int i; int i;
size_t header_size, size = iovec_length( &iovec_entries, &iovector ); size_t header_size, size = iovec_length( &iovec_entries, (const struct iovec **)&iovector );
tai6464 t; tai6464 t;
/* No cookie? Bad socket. Leave. */ /* No cookie? Bad socket. Leave. */

@ -16,56 +16,57 @@
#include "ot_iovec.h" #include "ot_iovec.h"
void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) { void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) {
void *new_ptr = realloc( *iovector, (1 + *iovec_entries ) * sizeof( struct iovec ) ); void *new_data;
if( !new_ptr ) int new_entries = 1 + *iovec_entries;
struct iovec *new_vec = realloc( *iovector, new_entries * sizeof( struct iovec ) );
if( !new_vec )
return NULL; return NULL;
*iovector = new_ptr;
new_ptr = mmap( NULL, new_alloc, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ); /* Only allocate after we have a place to store the pointer */
if( !new_ptr ) new_data = malloc( new_alloc );
if( !new_data )
return NULL; return NULL;
((*iovector)[*iovec_entries]).iov_base = new_ptr;
((*iovector)[*iovec_entries]).iov_len = new_alloc; new_vec[new_entries - 1].iov_base = new_data;
new_vec[new_entries - 1].iov_len = new_alloc;
*iovector = new_vec;
++*iovec_entries; ++*iovec_entries;
return new_ptr; return new_data;
} }
void iovec_free( int *iovec_entries, struct iovec **iovector ) { void iovec_free( int *iovec_entries, struct iovec **iovector ) {
int i; int i;
for( i=0; i<*iovec_entries; ++i ) for( i=0; i<*iovec_entries; ++i )
munmap( ((*iovector)[i]).iov_base, ((*iovector)[i]).iov_len ); free( ((*iovector)[i]).iov_base );
*iovector = NULL;
*iovec_entries = 0; *iovec_entries = 0;
} }
void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) { void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) {
int page_size = getpagesize(); if( *iovec_entries ) {
size_t old_alloc, new_alloc, old_pages, new_pages; char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base;
char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base; size_t new_alloc = ((char*)last_ptr) - base;
if( !*iovec_entries ) return;
old_alloc = ((*iovector)[ *iovec_entries - 1 ]).iov_len;
new_alloc = ((char*)last_ptr) - base;
old_pages = 1 + old_alloc / page_size;
new_pages = 1 + new_alloc / page_size;
if( old_pages != new_pages ) ((*iovector)[*iovec_entries - 1 ]).iov_base = realloc( base, new_alloc );
munmap( base + new_pages * page_size, old_alloc - new_pages * page_size ); ((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc;
((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc; }
} }
void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) { void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) {
void *new_ptr; void *new_data;
iovec_fixlast( iovec_entries, iovector, last_ptr ); iovec_fixlast( iovec_entries, iovector, last_ptr );
if( !( new_ptr = iovec_increase( iovec_entries, iovector, new_alloc ) ) ) if( !( new_data = iovec_increase( iovec_entries, iovector, new_alloc ) ) )
iovec_free( iovec_entries, iovector ); iovec_free( iovec_entries, iovector );
return new_ptr; return new_data;
} }
size_t iovec_length( int *iovec_entries, struct iovec **iovector ) { size_t iovec_length( const int *iovec_entries, const struct iovec **iovector ) {
size_t length = 0; size_t length = 0;
int i; int i;
for( i=0; i<*iovec_entries; ++i ) for( i=0; i<*iovec_entries; ++i )

@ -12,7 +12,7 @@ void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_a
void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ); void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr );
void iovec_free( int *iovec_entries, struct iovec **iovector ); void iovec_free( int *iovec_entries, struct iovec **iovector );
size_t iovec_length( int *iovec_entries, struct iovec **iovector ); size_t iovec_length( const int *iovec_entries, const struct iovec **iovector );
void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ); void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc );

@ -126,7 +126,7 @@ void mutex_workqueue_canceltask( int64 sock ) {
/* Free task's iovec */ /* Free task's iovec */
for( i=0; i<(*task)->iovec_entries; ++i ) for( i=0; i<(*task)->iovec_entries; ++i )
munmap( iovec[i].iov_base, iovec[i].iov_len ); free( iovec[i].iov_base );
*task = (*task)->next; *task = (*task)->next;
free( ptask ); free( ptask );

Loading…
Cancel
Save