From 54bdad552e5c997258467492c0c7d022fb428090 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Mon, 12 Nov 2007 01:37:47 +0000 Subject: [PATCH] New large chunk allocation code --- ot_iovec.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ot_iovec.h | 14 +++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 ot_iovec.c create mode 100644 ot_iovec.h diff --git a/ot_iovec.c b/ot_iovec.c new file mode 100644 index 0000000..ea3dd23 --- /dev/null +++ b/ot_iovec.c @@ -0,0 +1,60 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. */ + +/* System */ +#include +#include +#include +#include +#include + +/* Libowfat */ + +/* Opentracker */ +#include "ot_iovec.h" + +void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) { + void *new_ptr = realloc( *iovector, 1 + *iovec_entries * sizeof( struct iovec ) ); + if( !new_ptr ) + return NULL; + *iovector = new_ptr; + new_ptr = mmap( NULL, new_alloc, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ); + if( !new_ptr ) + return NULL; + ((*iovector)[*iovec_entries]).iov_base = new_ptr; + ((*iovector)[*iovec_entries]).iov_len = new_alloc; + ++*iovec_entries; + return new_ptr; +} + +void iovec_free( int *iovec_entries, struct iovec **iovector ) { + int i; + for( i=0; i<*iovec_entries; ++i ) + munmap( ((*iovector)[i]).iov_base, ((*iovector)[i]).iov_len ); + *iovec_entries = 0; +} + +void iovec_fixlast( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) { + int page_size = getpagesize(); + size_t old_alloc, old_pages, new_pages; + + if( !*iovec_entries ) return; + + old_alloc = ((*iovector)[ *iovec_entries - 1 ]).iov_len; + old_pages = 1 + old_alloc / page_size; + new_pages = 1 + new_alloc / page_size; + + if( old_pages != new_pages ) { + munmap( ((char*)((*iovector)[ *iovec_entries - 1 ]).iov_base ) + new_pages * page_size, + old_alloc - new_pages * page_size ); + } + ((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc; +} + +size_t iovec_length( int *iovec_entries, struct iovec **iovector ) { + size_t length = 0; + int i; + for( i=0; i<*iovec_entries; ++i ) + length += ((*iovector)[i]).iov_len; + return length; +} diff --git a/ot_iovec.h b/ot_iovec.h new file mode 100644 index 0000000..7bf3046 --- /dev/null +++ b/ot_iovec.h @@ -0,0 +1,14 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. */ + +#ifndef __OT_IOVEC_H__ +#define __OT_IOVEC_H__ + +#include + +void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ); +void iovec_fixlast( int *iovec_entries, struct iovec **iovector, size_t new_alloc ); +void iovec_free( int *iovec_entries, struct iovec **iovector ); +size_t iovec_length( int *iovec_entries, struct iovec **iovector ); + +#endif \ No newline at end of file