diff options
author | lanfer <lanfer@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2000-03-08 09:13:04 +0000 |
---|---|---|
committer | lanfer <lanfer@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2000-03-08 09:13:04 +0000 |
commit | 8fef8afb8c8b33c7041b9f5c09c106a36d44f89f (patch) | |
tree | 035b618e11cd1bf8c14d4910f7ab03d2827cf368 /src/util/Malloc.c | |
parent | f18425bffac176469a7a922e6cf5527abada416b (diff) |
memtracing
git-svn-id: http://svn.cactuscode.org/flesh/trunk@1445 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/util/Malloc.c')
-rw-r--r-- | src/util/Malloc.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/util/Malloc.c b/src/util/Malloc.c new file mode 100644 index 00000000..85339e99 --- /dev/null +++ b/src/util/Malloc.c @@ -0,0 +1,97 @@ + /*@@ + @file MemAllocate.c + @date Tue Mar 7 11:58:03 2000 + @author Gerd lanfermann + @desc + + @enddesc + @version $Header$ + @@*/ + +#include <stdio.h> +#include <stdlib.h> + +#include "cctk_Config.h" +#include "cctk_Malloc.h" + +static char *rcsid = "$Header$"; + +#define MEMDEBUG + +/******************************************************************** + ********************* Local Data Types *********************** + ********************************************************************/ + +typedef struct +{ + unsigned long int size; + int line; + const char *file; +} iMemData; + + +/******************************************************************** + ********************* Local Data ***************************** + ********************************************************************/ + +static unsigned long int totmem=0; +static unsigned long int pastmem=0; + +/******************************************************************** + ********************* External Routines ********************** + ********************************************************************/ + +void *CCTKi_Malloc(size_t size, int line, const char *file) +{ + iMemData *memdata; + char *data; + + data = (char*)malloc(size+sizeof(iMemData)); + if(!data) + { + fprintf(stderr, "Allocation error! "); + } + memdata = (iMemData *)data; + memdata->size = size; + memdata->line = line; + memdata->file = file; + + pastmem = totmem; + totmem += size; + +#ifdef MEMDEBUG + printf("Allocating %lu - by %s in line %d TOTAL: %lu\n", + memdata->size,memdata->file,memdata->line, CCTK_TotalMemory()); +#endif + + return((void*)(data+sizeof(iMemData))); +} + +void CCTKi_Free(void *pointer) +{ + iMemData *memdata; + + memdata = (iMemData *)((char*)pointer-sizeof(iMemData)); +#ifdef MEMDEBUG + printf("Freeing %lu - allocated by %s in line %d TOTAL: %lu\n", + memdata->size,memdata->file,memdata->line, CCTK_TotalMemory()); +#endif + pastmem = totmem; + totmem -= memdata->size; + + free(memdata); +} + +void CCTK_MemStat(void) +{ + char mess[138]; + sprintf(mess,"total: %d past: %d diff %d \n", + totmem, pastmem, totmem-pastmem); + printf("CCTK_Memstat: %s ",mess); +} + +unsigned long int CCTK_TotalMemory(void) +{ + return(totmem); +} + |