- added clone functionality and chunk_t support
This commit is contained in:
@@ -26,6 +26,8 @@
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
|
||||
/**
|
||||
* Macro to allocate a special type
|
||||
@@ -35,6 +37,16 @@
|
||||
* - Pointer to allocated memory if successful
|
||||
* - 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)))
|
||||
|
||||
#ifdef LEAK_DETECTIVE
|
||||
@@ -63,6 +75,23 @@
|
||||
* - NULL otherwise
|
||||
*/
|
||||
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
|
||||
@@ -80,6 +109,24 @@
|
||||
* - NULL otherwise
|
||||
*/
|
||||
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
|
||||
*
|
||||
@@ -118,12 +165,28 @@
|
||||
* @see #allocator_s.allocate for description
|
||||
*/
|
||||
#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
|
||||
*
|
||||
* @see #allocator_s.reallocate for description
|
||||
*/
|
||||
#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
|
||||
*
|
||||
@@ -146,8 +209,20 @@
|
||||
#define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
|
||||
#else
|
||||
#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_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){ \
|
||||
free(chunk.ptr); \
|
||||
chunk.ptr = NULL; \
|
||||
|
||||
Reference in New Issue
Block a user