1
0
mirror of git://erdgeist.org/opentracker synced 2025-02-05 03:26:25 +00:00
opentracker/ot_iovec.c

96 lines
2.6 KiB
C
Raw Normal View History

2007-11-12 01:37:47 +00:00
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2007-12-20 05:59:34 +00:00
It is considered beerware. Prost. Skol. Cheers or whatever.
2008-10-28 01:27:22 +00:00
2007-12-20 05:59:34 +00:00
$id$ */
2007-11-12 01:37:47 +00:00
/* System */
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/uio.h>
/* Libowfat */
/* Opentracker */
#include "ot_iovec.h"
void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) {
2021-04-25 18:23:00 +02:00
void *new_data;
int new_entries = 1 + *iovec_entries;
struct iovec *new_vec = realloc( *iovector, new_entries * sizeof( struct iovec ) );
if( !new_vec )
2007-11-12 01:37:47 +00:00
return NULL;
2021-04-25 18:23:00 +02:00
/* Only allocate after we have a place to store the pointer */
new_data = malloc( new_alloc );
if( !new_data )
2007-11-12 01:37:47 +00:00
return NULL;
2021-04-25 18:23:00 +02:00
new_vec[new_entries - 1].iov_base = new_data;
new_vec[new_entries - 1].iov_len = new_alloc;
*iovector = new_vec;
2007-11-12 01:37:47 +00:00
++*iovec_entries;
2021-04-25 18:23:00 +02:00
return new_data;
2007-11-12 01:37:47 +00:00
}
2024-04-13 00:47:29 +02:00
void *iovec_append( int *iovec_entries, struct iovec **iovector, struct iovec *append_iovector) {
int new_entries = *iovec_entries + 1;
struct iovec *new_vec = realloc( *iovector, new_entries * sizeof( struct iovec ) );
if( !new_vec )
return NULL;
/* Take over data from appended iovec */
new_vec[*iovec_entries].iov_base = append_iovector->iov_base;
new_vec[*iovec_entries].iov_len = append_iovector->iov_len;
append_iovector->iov_base = NULL;
append_iovector->iov_len = 0;
*iovector = new_vec;
*iovec_entries = new_entries;
return new_vec;
}
2007-11-12 01:37:47 +00:00
void iovec_free( int *iovec_entries, struct iovec **iovector ) {
int i;
for( i=0; i<*iovec_entries; ++i )
2021-04-25 18:23:00 +02:00
free( ((*iovector)[i]).iov_base );
*iovector = NULL;
2007-11-12 01:37:47 +00:00
*iovec_entries = 0;
}
2021-04-25 18:23:00 +02:00
void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) {
if( *iovec_entries ) {
char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base;
size_t new_alloc = ((char*)last_ptr) - base;
2007-11-12 01:37:47 +00:00
2021-04-25 18:23:00 +02:00
((*iovector)[*iovec_entries - 1 ]).iov_base = realloc( base, new_alloc );
((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc;
}
2007-11-12 01:37:47 +00:00
}
2021-04-25 18:23:00 +02:00
void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) {
void *new_data;
iovec_fixlast( iovec_entries, iovector, last_ptr );
2021-04-25 18:23:00 +02:00
if( !( new_data = iovec_increase( iovec_entries, iovector, new_alloc ) ) )
iovec_free( iovec_entries, iovector );
2021-04-25 18:23:00 +02:00
return new_data;
}
2021-04-25 18:23:00 +02:00
size_t iovec_length( const int *iovec_entries, const struct iovec **iovector ) {
2007-11-12 01:37:47 +00:00
size_t length = 0;
int i;
for( i=0; i<*iovec_entries; ++i )
length += ((*iovector)[i]).iov_len;
return length;
}
2007-12-20 05:59:34 +00:00
const char *g_version_iovec_c = "$Source$: $Revision$\n";