ecoMalloc.c 1.54 KB
Newer Older
Eric Coissac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include "ecoPCR.h"
#include <stdlib.h>

static int eco_log_malloc = 0;

void    eco_trace_memory_allocation()
{
	eco_log_malloc=1;
}

void    eco_untrace_memory_allocation()
{
	eco_log_malloc=0;
}


void   *eco_malloc(int32_t chunksize,
                   const char *error_message,
                   const char *filename,
                   int32_t    line)
{
	void * chunk;
	
	chunk = calloc(1,chunksize);
	
	if (!chunk)
		ecoError(ECO_MEM_ERROR,error_message,filename,line);
		
	if (eco_log_malloc)
		REprintf("Memory segment located at %p of size %d is allocated (file : %s [%d])",
			       chunk,
			       chunksize,
			       filename,
			       line);
		
	return chunk;
}

void   *eco_realloc(void *chunk,
                    int32_t newsize,
                    const char *error_message,
                    const char *filename,
                    int32_t    line)
{
	void *newchunk;
	
	newchunk = realloc(chunk,newsize);
	
	if (!newchunk)
		ecoError(ECO_MEM_ERROR,error_message,filename,line);

	if (eco_log_malloc)
		REprintf("Old memory segment %p is reallocated at %p with a size of %d (file : %s [%d])",
			        chunk,
			        newchunk,
			        newsize,
			        filename,
			        line);
		
	return newchunk;	
}

void    eco_free(void *chunk,
                 const char *error_message,
                 const char *filename,
                 int32_t    line)
{
	free(chunk);
	
	if (eco_log_malloc)
		REprintf("Memory segment %p is released => %s (file : %s [%d])",
			       chunk,
			       error_message,
			       filename,
			       line);
}