- fixed bug in memory reallocation

This commit is contained in:
Jan Hutter
2005-11-10 09:31:07 +00:00
parent 67a57dc6f4
commit c7748338f2
5 changed files with 63 additions and 25 deletions
+43 -9
View File
@@ -33,6 +33,7 @@
#endif #endif
#include "allocator.h" #include "allocator.h"
#include "types.h"
#ifdef LEAK_DETECTIVE #ifdef LEAK_DETECTIVE
@@ -93,16 +94,29 @@ struct private_allocator_s
* Mutex used to make sure, all functions are thread-save * Mutex used to make sure, all functions are thread-save
*/ */
pthread_mutex_t mutex; pthread_mutex_t mutex;
/**
* Allocates memory with LEAK_DETECTION and
* returns an empty data area filled with zeros.
*
* @param this private_allocator_t object
* @param bytes number of bytes to allocate
* @param file filename from which the memory is allocated
* @param line line number in specific file
* @param use_mutex If FALSE no mutex is used for allocation
* @return
* - pointer to allocated memory area if successful
* - NULL otherwise
*/
void * (*allocate_special) (private_allocator_t *this,size_t bytes, char * file,int line, bool use_mutex);
}; };
/** /**
* Implements allocator_t's function allocate. * Implements private_allocator_t's function allocate_special.
* See #allocator_s.allocate for description. * See #private_allocator_s.allocate_special for description.
*/ */
static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line) static void *allocate_special(private_allocator_t *this,size_t bytes, char * file,int line, bool use_mutex)
{ {
private_allocator_t *this = (private_allocator_t *) allocator;
memory_hdr_t *allocated_memory = malloc(sizeof(memory_hdr_t) + bytes); memory_hdr_t *allocated_memory = malloc(sizeof(memory_hdr_t) + bytes);
if (allocated_memory == NULL) if (allocated_memory == NULL)
@@ -110,7 +124,10 @@ static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line
return allocated_memory; return allocated_memory;
} }
pthread_mutex_lock( &(this->mutex)); if (use_mutex)
{
pthread_mutex_lock( &(this->mutex));
}
allocated_memory->info.line = line; allocated_memory->info.line = line;
allocated_memory->info.filename = file; allocated_memory->info.filename = file;
@@ -125,11 +142,25 @@ static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line
/* fill memory with zero's */ /* fill memory with zero's */
memset(allocated_memory+1, '\0', bytes); memset(allocated_memory+1, '\0', bytes);
pthread_mutex_unlock(&(this->mutex)); if (use_mutex)
{
pthread_mutex_unlock(&(this->mutex));
}
/* real memory starts after header */ /* real memory starts after header */
return (allocated_memory+1); return (allocated_memory+1);
} }
/**
* Implements allocator_t's function allocate.
* See #allocator_s.allocate for description.
*/
static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line)
{
private_allocator_t *this = (private_allocator_t *) allocator;
return (allocate_special(this,bytes, file,line,TRUE));
}
/* /*
* Implements allocator_t's free_pointer allocate. * Implements allocator_t's free_pointer allocate.
* See #allocator_s.free_pointer for description. * See #allocator_s.free_pointer for description.
@@ -178,14 +209,16 @@ static void * reallocate(allocator_t *allocator, void * old, size_t bytes, char
{ {
return NULL; return NULL;
} }
pthread_mutex_lock( &(this->mutex)); pthread_mutex_lock( &(this->mutex));
allocated_memory = ((memory_hdr_t *)old) - 1; allocated_memory = ((memory_hdr_t *)old) - 1;
void *new_space = this->public.allocate(&(this->public),bytes,file,line); void *new_space = this->allocate_special(this,bytes,file,line,FALSE);
if (new_space == NULL) if (new_space == NULL)
{ {
this->public.free_pointer(&(this->public),old);
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
this->public.free_pointer(&(this->public),old);
return NULL; return NULL;
} }
@@ -237,6 +270,7 @@ static private_allocator_t allocator = {
reallocate: reallocate, reallocate: reallocate,
report_memory_leaks: allocator_report_memory_leaks}, report_memory_leaks: allocator_report_memory_leaks},
allocations: NULL, allocations: NULL,
allocate_special : allocate_special,
mutex: PTHREAD_MUTEX_INITIALIZER mutex: PTHREAD_MUTEX_INITIALIZER
}; };
+8 -7
View File
@@ -1,7 +1,7 @@
/** /**
* @file generator.c * @file generator.c
* *
* @brief Generic generator class used to generate IKEv2-Header and Payload * @brief Generic generator class used to generate IKEv2-header and payloads.
* *
*/ */
@@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include "allocator.h" #include "allocator.h"
#include "types.h" #include "types.h"
@@ -29,7 +30,7 @@
/** /**
* buffer_t: used for geneartor operations * Uused for geneartor operations
*/ */
typedef struct generator_infos_s generator_infos_t; typedef struct generator_infos_s generator_infos_t;
@@ -88,9 +89,7 @@ struct generator_infos_s {
*/ */
static status_t generator_info_make_space_available (generator_infos_t *this, size_t bits) static status_t generator_info_make_space_available (generator_infos_t *this, size_t bits)
{ {
size_t free_bits = ((this->roof_position - this->out_position) * 8) - this->current_bit; while ((((this->roof_position - this->out_position) * 8) - this->current_bit) < bits)
while (free_bits < bits)
{ {
size_t old_buffer_size = ((this->roof_position) - ( this->buffer)); size_t old_buffer_size = ((this->roof_position) - ( this->buffer));
size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE; size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
@@ -107,6 +106,7 @@ static status_t generator_info_make_space_available (generator_infos_t *this, si
this->out_position = (this->buffer + out_position_offset); this->out_position = (this->buffer + out_position_offset);
this->roof_position = (this->buffer + new_buffer_size); this->roof_position = (this->buffer + new_buffer_size);
} }
return SUCCESS; return SUCCESS;
@@ -383,6 +383,7 @@ static status_t generate (private_generator_t *this,void * data_struct,encoding_
break; break;
case RESERVED_BIT: case RESERVED_BIT:
{ {
status = infos->make_space_available(infos,1);
u_int8_t reserved_bit = ~(1 << (7 - infos->current_bit)); u_int8_t reserved_bit = ~(1 << (7 - infos->current_bit));
*(infos->out_position) = *(infos->out_position) & reserved_bit; *(infos->out_position) = *(infos->out_position) & reserved_bit;
@@ -391,14 +392,14 @@ static status_t generate (private_generator_t *this,void * data_struct,encoding_
if (infos->current_bit >= 8) if (infos->current_bit >= 8)
{ {
infos->current_bit = infos->current_bit % 8; infos->current_bit = infos->current_bit % 8;
status = infos->make_space_available(infos,8);
infos->out_position++; infos->out_position++;
} }
break; break;
} }
case RESERVED_BYTE: case RESERVED_BYTE:
{ {
if (infos->current_bit > 0) status = infos->make_space_available(infos,8);
if ((status != SUCCESS) || (infos->current_bit > 0))
{ {
return FAILED; return FAILED;
} }
+4 -3
View File
@@ -1,7 +1,7 @@
/** /**
* @file generator.h * @file generator.h
* *
* @brief Generic generator class used to generate IKEv2-Header and Payload * @brief Generic generator class used to generate IKEv2-header and payloads.
* *
*/ */
@@ -27,12 +27,13 @@
#include "encodings.h" #include "encodings.h"
/** /**
* Data Buffer for generating has start size of 3000 Bytes * Generating is done in a data buffer.
* This is thehe start size of this buffer in Bytes.
*/ */
#define GENERATOR_DATA_BUFFER_SIZE 3000 #define GENERATOR_DATA_BUFFER_SIZE 3000
/** /**
* Number of Bytes to increase the buffer if it is to small * Number of bytes to increase the buffer, if it is to small.
*/ */
#define GENERATOR_DATA_BUFFER_INCREASE_VALUE 1000 #define GENERATOR_DATA_BUFFER_INCREASE_VALUE 1000
+2 -2
View File
@@ -180,8 +180,8 @@ socket_t *global_socket;
tester_t *tester = tester_create(test_output, FALSE); tester_t *tester = tester_create(test_output, FALSE);
tester->perform_tests(tester,all_tests); /* tester->perform_tests(tester,all_tests);*/
/* tester->perform_test(tester,&generator_test2); */ tester->perform_test(tester,&generator_test2);
tester->destroy(tester); tester->destroy(tester);
+2
View File
@@ -85,6 +85,8 @@ void test_generator_with_header_payload(tester_t *tester)
0x00,0x00,0x00,0x08, 0x00,0x00,0x00,0x08,
}; };
tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data"); tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
allocator_free_chunk(generated_data); allocator_free_chunk(generated_data);