- fixed bug in memory reallocation
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
#endif
|
||||
|
||||
#include "allocator.h"
|
||||
#include "types.h"
|
||||
|
||||
#ifdef LEAK_DETECTIVE
|
||||
|
||||
@@ -93,16 +94,29 @@ struct private_allocator_s
|
||||
* Mutex used to make sure, all functions are thread-save
|
||||
*/
|
||||
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.
|
||||
* See #allocator_s.allocate for description.
|
||||
* Implements private_allocator_t's function allocate_special.
|
||||
* 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);
|
||||
|
||||
if (allocated_memory == NULL)
|
||||
@@ -110,7 +124,10 @@ static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line
|
||||
return allocated_memory;
|
||||
}
|
||||
|
||||
pthread_mutex_lock( &(this->mutex));
|
||||
if (use_mutex)
|
||||
{
|
||||
pthread_mutex_lock( &(this->mutex));
|
||||
}
|
||||
|
||||
allocated_memory->info.line = line;
|
||||
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 */
|
||||
memset(allocated_memory+1, '\0', bytes);
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
if (use_mutex)
|
||||
{
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
}
|
||||
|
||||
/* real memory starts after header */
|
||||
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.
|
||||
* 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;
|
||||
}
|
||||
|
||||
pthread_mutex_lock( &(this->mutex));
|
||||
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)
|
||||
{
|
||||
this->public.free_pointer(&(this->public),old);
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
this->public.free_pointer(&(this->public),old);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -237,6 +270,7 @@ static private_allocator_t allocator = {
|
||||
reallocate: reallocate,
|
||||
report_memory_leaks: allocator_report_memory_leaks},
|
||||
allocations: NULL,
|
||||
allocate_special : allocate_special,
|
||||
mutex: PTHREAD_MUTEX_INITIALIZER
|
||||
};
|
||||
|
||||
|
||||
+11
-10
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @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 <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "allocator.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;
|
||||
|
||||
@@ -88,15 +89,13 @@ struct generator_infos_s {
|
||||
*/
|
||||
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 (free_bits < bits)
|
||||
while ((((this->roof_position - this->out_position) * 8) - this->current_bit) < bits)
|
||||
{
|
||||
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 out_position_offset = ((this->out_position) - (this->buffer));
|
||||
u_int8_t *new_buffer;
|
||||
|
||||
|
||||
new_buffer = allocator_realloc(this->buffer,new_buffer_size);
|
||||
if (new_buffer == NULL)
|
||||
{
|
||||
@@ -107,8 +106,9 @@ static status_t generator_info_make_space_available (generator_infos_t *this, si
|
||||
|
||||
this->out_position = (this->buffer + out_position_offset);
|
||||
this->roof_position = (this->buffer + new_buffer_size);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@@ -383,22 +383,23 @@ static status_t generate (private_generator_t *this,void * data_struct,encoding_
|
||||
break;
|
||||
case RESERVED_BIT:
|
||||
{
|
||||
status = infos->make_space_available(infos,1);
|
||||
u_int8_t reserved_bit = ~(1 << (7 - infos->current_bit));
|
||||
|
||||
|
||||
*(infos->out_position) = *(infos->out_position) & reserved_bit;
|
||||
|
||||
infos->current_bit++;
|
||||
if (infos->current_bit >= 8)
|
||||
{
|
||||
infos->current_bit = infos->current_bit % 8;
|
||||
status = infos->make_space_available(infos,8);
|
||||
infos->out_position++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RESERVED_BYTE:
|
||||
{
|
||||
if (infos->current_bit > 0)
|
||||
status = infos->make_space_available(infos,8);
|
||||
if ((status != SUCCESS) || (infos->current_bit > 0))
|
||||
{
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @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"
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
|
||||
@@ -180,8 +180,8 @@ socket_t *global_socket;
|
||||
|
||||
tester_t *tester = tester_create(test_output, FALSE);
|
||||
|
||||
tester->perform_tests(tester,all_tests);
|
||||
/* tester->perform_test(tester,&generator_test2); */
|
||||
/* tester->perform_tests(tester,all_tests);*/
|
||||
tester->perform_test(tester,&generator_test2);
|
||||
|
||||
tester->destroy(tester);
|
||||
|
||||
|
||||
@@ -84,7 +84,9 @@ void test_generator_with_header_payload(tester_t *tester)
|
||||
0x00,0x00,0x00,0x07,
|
||||
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");
|
||||
allocator_free_chunk(generated_data);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user