- added clone functionality and chunk_t support
This commit is contained in:
parent
b92be4f06d
commit
87e7f9d740
|
@ -158,7 +158,20 @@ static void *allocate_special(private_allocator_t *this,size_t bytes, char * fil
|
||||||
static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line)
|
static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line)
|
||||||
{
|
{
|
||||||
private_allocator_t *this = (private_allocator_t *) allocator;
|
private_allocator_t *this = (private_allocator_t *) allocator;
|
||||||
return (allocate_special(this,bytes, file,line,TRUE));
|
return (this->allocate_special(this,bytes, file,line,TRUE));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements allocator_t's function allocate_as_chunk.
|
||||||
|
* See #allocator_s.allocate_as_chunk for description.
|
||||||
|
*/
|
||||||
|
static chunk_t allocate_as_chunk(allocator_t *allocator,size_t bytes, char * file,int line)
|
||||||
|
{
|
||||||
|
private_allocator_t *this = (private_allocator_t *) allocator;
|
||||||
|
chunk_t new_chunk;
|
||||||
|
new_chunk.ptr = this->allocate_special(this,bytes, file,line,TRUE);
|
||||||
|
new_chunk.len = (new_chunk.ptr == NULL) ? 0 : bytes;
|
||||||
|
return new_chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -222,8 +235,32 @@ static void * reallocate(allocator_t *allocator, void * old, size_t bytes, char
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(new_space,old,allocated_memory->info.size_of_memory);
|
|
||||||
|
/* the smaller size is copied to avoid overflows */
|
||||||
|
memcpy(new_space,old,(allocated_memory->info.size_of_memory < bytes) ? allocated_memory->info.size_of_memory : bytes);
|
||||||
pthread_mutex_unlock(&(this->mutex));
|
pthread_mutex_unlock(&(this->mutex));
|
||||||
|
this->public.free_pointer(&(this->public),old);
|
||||||
|
|
||||||
|
return new_space;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void * clone_bytes(allocator_t *allocator,void * to_clone, size_t bytes, char * file, int line)
|
||||||
|
{
|
||||||
|
private_allocator_t *this = (private_allocator_t *) allocator;
|
||||||
|
|
||||||
|
if (to_clone == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *new_space = this->allocate_special(this,bytes,file,line,TRUE);
|
||||||
|
|
||||||
|
if (new_space == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(new_space,to_clone,bytes);
|
||||||
|
|
||||||
return new_space;
|
return new_space;
|
||||||
}
|
}
|
||||||
|
@ -266,8 +303,10 @@ static void allocator_report_memory_leaks(allocator_t *allocator)
|
||||||
*/
|
*/
|
||||||
static private_allocator_t allocator = {
|
static private_allocator_t allocator = {
|
||||||
public: {allocate: allocate,
|
public: {allocate: allocate,
|
||||||
|
allocate_as_chunk: allocate_as_chunk,
|
||||||
free_pointer: free_pointer,
|
free_pointer: free_pointer,
|
||||||
reallocate: reallocate,
|
reallocate: reallocate,
|
||||||
|
clone_bytes : clone_bytes,
|
||||||
report_memory_leaks: allocator_report_memory_leaks},
|
report_memory_leaks: allocator_report_memory_leaks},
|
||||||
allocations: NULL,
|
allocations: NULL,
|
||||||
allocate_special : allocate_special,
|
allocate_special : allocate_special,
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Macro to allocate a special type
|
* Macro to allocate a special type
|
||||||
|
@ -35,6 +37,16 @@
|
||||||
* - Pointer to allocated memory if successful
|
* - Pointer to allocated memory if successful
|
||||||
* - NULL otherwise
|
* - NULL otherwise
|
||||||
*/
|
*/
|
||||||
|
#define allocator_alloc_thing_as_chunk(thing) (allocator_alloc_as_chunk(sizeof(thing)))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Macro to allocate a special type as chunk_t
|
||||||
|
*
|
||||||
|
* @param thing object on which a sizeof is performed
|
||||||
|
* @return
|
||||||
|
* - chunk_t pointing to allocated memory if successful
|
||||||
|
* - chunk_t containing empty pointer
|
||||||
|
*/
|
||||||
#define allocator_alloc_thing(thing) (allocator_alloc(sizeof(thing)))
|
#define allocator_alloc_thing(thing) (allocator_alloc(sizeof(thing)))
|
||||||
|
|
||||||
#ifdef LEAK_DETECTIVE
|
#ifdef LEAK_DETECTIVE
|
||||||
|
@ -64,6 +76,23 @@
|
||||||
*/
|
*/
|
||||||
void * (*allocate) (allocator_t *this,size_t bytes, char * file,int line);
|
void * (*allocate) (allocator_t *this,size_t bytes, char * file,int line);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocates memory with LEAK_DETECTION and
|
||||||
|
* returns an chunk pointing to an empy data area filled with zeros.
|
||||||
|
*
|
||||||
|
* @warning Use this function not directly, only with assigned macros
|
||||||
|
* #allocator_alloc_as_chunk and #allocator_alloc_thing_as_chunk.
|
||||||
|
*
|
||||||
|
* @param this 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
|
||||||
|
* @return
|
||||||
|
* - pointer to allocated memory area if successful
|
||||||
|
* - NULL otherwise
|
||||||
|
*/
|
||||||
|
chunk_t (*allocate_as_chunk) (allocator_t *this,size_t bytes, char * file,int line);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reallocates memory with LEAK_DETECTION and
|
* Reallocates memory with LEAK_DETECTION and
|
||||||
* returns an empty data area filled with zeros
|
* returns an empty data area filled with zeros
|
||||||
|
@ -80,6 +109,24 @@
|
||||||
* - NULL otherwise
|
* - NULL otherwise
|
||||||
*/
|
*/
|
||||||
void * (*reallocate) (allocator_t *this,void * old, size_t bytes, char * file, int line);
|
void * (*reallocate) (allocator_t *this,void * old, size_t bytes, char * file, int line);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones memory with LEAK_DETECTION and
|
||||||
|
* returns a cloned data area.
|
||||||
|
*
|
||||||
|
* @warning Use this function not directly, only with assigned macro
|
||||||
|
* #allocator_clone_bytes
|
||||||
|
*
|
||||||
|
* @param this allocator_t object
|
||||||
|
* @param old pointer to the old data area
|
||||||
|
* @param bytes number of bytes to allocate
|
||||||
|
* @param file filename from which the memory is allocated
|
||||||
|
* @param line line number in specific file
|
||||||
|
* @return - pointer to reallocated memory area if successful
|
||||||
|
* - NULL otherwise
|
||||||
|
*/
|
||||||
|
void * (*clone_bytes) (allocator_t *this,void * to_clone, size_t bytes, char * file, int line);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Frees memory with LEAK_DETECTION
|
* Frees memory with LEAK_DETECTION
|
||||||
*
|
*
|
||||||
|
@ -118,12 +165,28 @@
|
||||||
* @see #allocator_s.allocate for description
|
* @see #allocator_s.allocate for description
|
||||||
*/
|
*/
|
||||||
#define allocator_alloc(bytes) (global_allocator->allocate(global_allocator,bytes,__FILE__,__LINE__))
|
#define allocator_alloc(bytes) (global_allocator->allocate(global_allocator,bytes,__FILE__,__LINE__))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Macro to allocate some memory for a chunk_t
|
||||||
|
*
|
||||||
|
* @see #allocator_s.allocate_as_chunk for description
|
||||||
|
*/
|
||||||
|
#define allocator_alloc_as_chunk(bytes) (global_allocator->allocate_as_chunk(global_allocator,bytes,__FILE__,__LINE__))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Macro to reallocate some memory
|
* Macro to reallocate some memory
|
||||||
*
|
*
|
||||||
* @see #allocator_s.reallocate for description
|
* @see #allocator_s.reallocate for description
|
||||||
*/
|
*/
|
||||||
#define allocator_realloc(old,bytes) (global_allocator->reallocate(global_allocator,old,bytes,__FILE__, __LINE__))
|
#define allocator_realloc(old,bytes) (global_allocator->reallocate(global_allocator,old,bytes,__FILE__, __LINE__))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Macro to clone some memory
|
||||||
|
*
|
||||||
|
* @see #allocator_s.*clone_bytes for description
|
||||||
|
*/
|
||||||
|
#define allocator_clone_bytes(old,bytes) (global_allocator->clone_bytes(global_allocator,old,bytes,__FILE__, __LINE__))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Macro to free some memory
|
* Macro to free some memory
|
||||||
*
|
*
|
||||||
|
@ -146,8 +209,20 @@
|
||||||
#define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
|
#define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
|
||||||
#else
|
#else
|
||||||
#define allocator_alloc(bytes) (malloc(bytes))
|
#define allocator_alloc(bytes) (malloc(bytes))
|
||||||
|
#define allocator_alloc_as_chunk(bytes){\
|
||||||
|
chunk_t new_chunk; \
|
||||||
|
new_chunk.ptr = malloc(bytes); \
|
||||||
|
new_chunk.len = (new_chunk.ptr == NULL) ? 0 : bytes; \
|
||||||
|
return new_chunk;
|
||||||
|
}
|
||||||
#define allocator_realloc(old,bytes) (realloc(old,bytes))
|
#define allocator_realloc(old,bytes) (realloc(old,bytes))
|
||||||
#define allocator_free(pointer) (free(pointer))
|
#define allocator_free(pointer) (free(pointer))
|
||||||
|
#define allocator_clone_bytes(pointer,size){\
|
||||||
|
void *new_data = malloc(size)\
|
||||||
|
if (new_data == NULL) return NULL; \
|
||||||
|
memcpy(new_data,pointer,size)\
|
||||||
|
return new_data; \
|
||||||
|
}
|
||||||
#define allocator_free_chunk(chunk){ \
|
#define allocator_free_chunk(chunk){ \
|
||||||
free(chunk.ptr); \
|
free(chunk.ptr); \
|
||||||
chunk.ptr = NULL; \
|
chunk.ptr = NULL; \
|
||||||
|
|
Loading…
Reference in New Issue