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));
+ 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;
}
/*
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));
+ 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;
}
*/
static private_allocator_t allocator = {
public: {allocate: allocate,
+ allocate_as_chunk: allocate_as_chunk,
free_pointer: free_pointer,
reallocate: reallocate,
+ clone_bytes : clone_bytes,
report_memory_leaks: allocator_report_memory_leaks},
allocations: NULL,
allocate_special : allocate_special,
#include <stddef.h>
+#include "types.h"
+
/**
* Macro to allocate a special type
* - 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
* - 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
* - 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
*
* @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
*
#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; \