Skip to content

Commit 397cd03

Browse files
committed
Turn mmaps into malloc
1 parent 95f1780 commit 397cd03

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

ot_http.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iove
125125
struct http_data *cookie = io_getcookie( sock );
126126
char *header;
127127
int i;
128-
size_t header_size, size = iovec_length( &iovec_entries, &iovector );
128+
size_t header_size, size = iovec_length( &iovec_entries, (const struct iovec **)&iovector );
129129
tai6464 t;
130130

131131
/* No cookie? Bad socket. Leave. */

ot_iovec.c

+29-28
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,57 @@
1616
#include "ot_iovec.h"
1717

1818
void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) {
19-
void *new_ptr = realloc( *iovector, (1 + *iovec_entries ) * sizeof( struct iovec ) );
20-
if( !new_ptr )
19+
void *new_data;
20+
int new_entries = 1 + *iovec_entries;
21+
struct iovec *new_vec = realloc( *iovector, new_entries * sizeof( struct iovec ) );
22+
23+
if( !new_vec )
2124
return NULL;
22-
*iovector = new_ptr;
23-
new_ptr = mmap( NULL, new_alloc, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 );
24-
if( !new_ptr )
25+
26+
/* Only allocate after we have a place to store the pointer */
27+
new_data = malloc( new_alloc );
28+
if( !new_data )
2529
return NULL;
26-
((*iovector)[*iovec_entries]).iov_base = new_ptr;
27-
((*iovector)[*iovec_entries]).iov_len = new_alloc;
30+
31+
new_vec[new_entries - 1].iov_base = new_data;
32+
new_vec[new_entries - 1].iov_len = new_alloc;
33+
34+
*iovector = new_vec;
2835
++*iovec_entries;
29-
return new_ptr;
36+
return new_data;
3037
}
3138

3239
void iovec_free( int *iovec_entries, struct iovec **iovector ) {
3340
int i;
3441
for( i=0; i<*iovec_entries; ++i )
35-
munmap( ((*iovector)[i]).iov_base, ((*iovector)[i]).iov_len );
42+
free( ((*iovector)[i]).iov_base );
43+
*iovector = NULL;
3644
*iovec_entries = 0;
3745
}
3846

39-
void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) {
40-
int page_size = getpagesize();
41-
size_t old_alloc, new_alloc, old_pages, new_pages;
42-
char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base;
43-
44-
if( !*iovec_entries ) return;
45-
46-
old_alloc = ((*iovector)[ *iovec_entries - 1 ]).iov_len;
47-
new_alloc = ((char*)last_ptr) - base;
48-
old_pages = 1 + old_alloc / page_size;
49-
new_pages = 1 + new_alloc / page_size;
47+
void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) {
48+
if( *iovec_entries ) {
49+
char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base;
50+
size_t new_alloc = ((char*)last_ptr) - base;
5051

51-
if( old_pages != new_pages )
52-
munmap( base + new_pages * page_size, old_alloc - new_pages * page_size );
53-
((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc;
52+
((*iovector)[*iovec_entries - 1 ]).iov_base = realloc( base, new_alloc );
53+
((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc;
54+
}
5455
}
5556

56-
void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) {
57-
void *new_ptr;
57+
void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) {
58+
void *new_data;
5859

5960
iovec_fixlast( iovec_entries, iovector, last_ptr );
6061

61-
if( !( new_ptr = iovec_increase( iovec_entries, iovector, new_alloc ) ) )
62+
if( !( new_data = iovec_increase( iovec_entries, iovector, new_alloc ) ) )
6263
iovec_free( iovec_entries, iovector );
6364

64-
return new_ptr;
65+
return new_data;
6566
}
6667

6768

68-
size_t iovec_length( int *iovec_entries, struct iovec **iovector ) {
69+
size_t iovec_length( const int *iovec_entries, const struct iovec **iovector ) {
6970
size_t length = 0;
7071
int i;
7172
for( i=0; i<*iovec_entries; ++i )

ot_iovec.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_a
1212
void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr );
1313
void iovec_free( int *iovec_entries, struct iovec **iovector );
1414

15-
size_t iovec_length( int *iovec_entries, struct iovec **iovector );
15+
size_t iovec_length( const int *iovec_entries, const struct iovec **iovector );
1616

1717
void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc );
1818

ot_mutex.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void mutex_workqueue_canceltask( int64 sock ) {
126126

127127
/* Free task's iovec */
128128
for( i=0; i<(*task)->iovec_entries; ++i )
129-
munmap( iovec[i].iov_base, iovec[i].iov_len );
129+
free( iovec[i].iov_base );
130130

131131
*task = (*task)->next;
132132
free( ptask );

0 commit comments

Comments
 (0)