diff options
author | schnetter <schnetter@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2004-12-28 19:04:34 +0000 |
---|---|---|
committer | schnetter <schnetter@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2004-12-28 19:04:34 +0000 |
commit | e475d17c8fa9a47f3a77b3cdb559e84cb81c6a63 (patch) | |
tree | 434af08ecfa19f3db6004dd4d44a4eb61a435c9d /src/util | |
parent | 3abd66b09008b065c9dc815462261b910a6180ae (diff) |
Revert another accidental change.
git-svn-id: http://svn.cactuscode.org/flesh/trunk@3946 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Malloc.c | 115 |
1 files changed, 62 insertions, 53 deletions
diff --git a/src/util/Malloc.c b/src/util/Malloc.c index 17e48608..b0a505c3 100644 --- a/src/util/Malloc.c +++ b/src/util/Malloc.c @@ -9,7 +9,6 @@ @version $Header$ @@*/ -#include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -25,10 +24,25 @@ /* #define MEMDEBUG 1 */ /* Undefine malloc */ +#if defined(malloc) #undef malloc +#define malloc malloc +#endif + +#if defined(realloc) #undef realloc +#define realloc realloc +#endif + +#if defined(calloc) #undef calloc +#define calloc calloc +#endif + +#if defined(free) #undef free +#define free free +#endif static const char *rcsid = "$Header$"; @@ -41,7 +55,8 @@ CCTK_FILEVERSION(util_Malloc_c); /* Datastructure to track the allocation per file */ typedef struct { - size_t size; + unsigned long int size; + unsigned long int tsize; const char *file; } t_memhash; @@ -52,29 +67,21 @@ typedef struct */ typedef struct { - size_t size; /* requested size */ + unsigned long int ok; /* magic number */ + unsigned long int size; /* requested size */ + unsigned long int tsize; /* requested size + database */ unsigned int line; /* allocated on line # */ const char *file; /* allocated by file # */ - unsigned long int ok; /* magic number */ } t_mallocinfo; /* used for integrity checking of the data */ #define OK_INTEGRITY 424242 -/* The t_mallocinfo structure needs to be padded to a multiple of - whatever the architecture requires */ -/* 16 seems a reasonable number; other reasonable numbers might be 32 - or 64. This should really be autodetected, but probably isn't - worth the trouble */ -#define PADDING_BASE 16 -/* We have to pad the t_mallocinfo to this length */ -#define PADDED_MALLOCINFO_LENGTH ((sizeof(t_mallocinfo) + (PADDING_BASE) - 1) / (PADDING_BASE) * (PADDING_BASE)) - /* Datastructure to track the allocation size at ticket request time */ typedef struct { - size_t size; + unsigned long int size; } t_memticket; @@ -87,8 +94,8 @@ static cHandledData *memfileDB = NULL; static int n_tickets = 0; -static size_t totmem=0; -static size_t pastmem=0; +static unsigned long int totmem=0; +static unsigned long int pastmem=0; /******************************************************************** ********************* internal prototypes ******************** @@ -123,7 +130,7 @@ void *CCTKi_Malloc(size_t size, int line, const char *file) t_mallocinfo *info; char *data; - data = (char*)malloc(size+PADDED_MALLOCINFO_LENGTH); + data = (char*)malloc(size+sizeof(t_mallocinfo)); if(!data) { fprintf(stderr, "Allocation error! "); @@ -131,6 +138,7 @@ void *CCTKi_Malloc(size_t size, int line, const char *file) info = (t_mallocinfo*) data; info->ok = OK_INTEGRITY; info->size = size; + info->tsize= size+sizeof(t_mallocinfo); info->line = line; info->file = file; @@ -146,12 +154,12 @@ void *CCTKi_Malloc(size_t size, int line, const char *file) */ #ifdef MEMDEBUG - printf("Allocating %f - by %s in line %ud TOTAL: %f\n", - (double)info->size, info->file, info->line, (double)CCTK_TotalMemory()); + printf("Allocating %lu - by %s in line %d TOTAL: %lu\n", + info->size, info->file, info->line, CCTK_TotalMemory()); #endif /* return the pointer plus an OFFSET (t_mallocinfo) to hide the info*/ - return((void*)(data+PADDED_MALLOCINFO_LENGTH)); + return((void*)(data+sizeof(t_mallocinfo))); } @@ -189,7 +197,7 @@ void *CCTKi_Realloc(void *pointer, size_t size, int line, const char *file) } /* get the info section */ - info = (t_mallocinfo *)((char*)pointer-PADDED_MALLOCINFO_LENGTH); + info = (t_mallocinfo *)((char*)pointer-sizeof(t_mallocinfo)); /* make a sanity check: memory could have be allocated with standard malloc */ if (info->ok!=OK_INTEGRITY) @@ -211,30 +219,31 @@ void *CCTKi_Realloc(void *pointer, size_t size, int line, const char *file) totmem = totmem - info->size + size; /* reallocate starting at info pointer */ - data = (char*)realloc(info, size+padded_mallaocinfo_length); + data = (char*)realloc(info, size+sizeof(t_mallocinfo)); if (!data) { CCTK_VWarn(0,__LINE__,__FILE__,"Cactus", "CCTKi_Realloc: Could not reallocate memory. " - "Reallocation called from %s, line %d.\n",file,line); + "Reallocation called from %s, line %d. \n",file,line); /*$CCTK_Abort(NULL);$*/ } /* and update */ info = (t_mallocinfo*) data; info->size = size; + info->tsize= size+sizeof(t_mallocinfo); /*$CCTKi_UpdateMemByFile(info->size, line, file);$*/ #ifdef MEMDEBUG - printf("ReAllocating %f - by %s in line %ud TOTAL: %f\n", - (double)info->size, info->file, info->line, (double)CCTK_TotalMemory()); + printf("ReAllocating %lu - by %s in line %d TOTAL: %lu\n", + info->size, info->file, info->line, CCTK_TotalMemory()); #endif /* return the pointer starting at the datasection, behind the info section */ - return((void*)(data+PADDED_MALLOCINFO_LENGTH)); + return((void*)(data+sizeof(t_mallocinfo))); } } @@ -286,7 +295,7 @@ void CCTKi_Free(void *pointer, int line, const char *file) return; } - info = (t_mallocinfo *)((char*)pointer-PADDED_MALLOCINFO_LENGTH); + info = (t_mallocinfo *)((char*)pointer-sizeof(t_mallocinfo)); if (info->ok!=OK_INTEGRITY) { @@ -303,8 +312,8 @@ void CCTKi_Free(void *pointer, int line, const char *file) totmem -= info->size; #ifdef MEMDEBUG - printf("Freeing %f - allocated by %s in line %ld TOTAL: %f\n", - (double)info->size, info->file, info->line, (double)CCTK_TotalMemory()); + printf("Freeing %lu - allocated by %s in line %d TOTAL: %lu\n", + info->size, info->file, info->line, CCTK_TotalMemory()); #endif /* invalidate the magic number so that we catch things @@ -333,7 +342,6 @@ void CCTKi_Free(void *pointer, int line, const char *file) int CCTKi_UpdateMemByFile(int size, int line, const char *file) { - void *tmp; t_memhash *memfile; int handle, retval=-1; @@ -341,7 +349,7 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file) /* avoid compiler warning about unused parameter */ line = line; - if ((handle=Util_GetHandle(memfileDB, file, &tmp)) > -1) + if ((handle=Util_GetHandle(memfileDB, file, (void**)&memfile)) > -1) { memfile = (t_memhash*) Util_GetHandledData(memfileDB, handle); @@ -349,6 +357,7 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file) if (memfile) { memfile->size +=size; + memfile->tsize+=size; retval = 0; } else @@ -363,6 +372,7 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file) if (memfile) { memfile->size += size; + memfile->tsize+= size+sizeof(t_memhash); memfile->file = file; retval = Util_NewHandle(&memfileDB, file, memfile); } @@ -400,13 +410,12 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file) int CCTK_MemTicketRequest(void) { int this_ticket; - void *tmp; t_memticket *tmem; char tname[20]; sprintf(tname, "ticket_%d",n_tickets++); - if (Util_GetHandle(ticketDB, tname, &tmp)>-1) + if (Util_GetHandle(ticketDB, tname, (void**)&tmem)>-1) { this_ticket=-3; } @@ -444,23 +453,23 @@ int CCTK_MemTicketRequest(void) @@*/ -ptrdiff_t CCTK_MemTicketCash(int this_ticket) +long int CCTK_MemTicketCash(int this_ticket) { - ptrdiff_t tdiff; - size_t size; + long int tdiff; + unsigned long int tsize; t_memticket *tmem; tmem = (t_memticket*) Util_GetHandledData(ticketDB, this_ticket); if (tmem) { - size = tmem->size; - tdiff = CCTK_TotalMemory() - size; + tsize = tmem->size; + tdiff = CCTK_TotalMemory() - tsize; } else { CCTK_VWarn(1,__LINE__,__FILE__,"Cactus", - "CCTK_MemTicketCash: Cannot find ticket %d\n",this_ticket); + "CCTK_MemTicketCash: Cannot find ticket %d \n",this_ticket); tdiff = 666; } return(tdiff); @@ -508,7 +517,7 @@ int CCTK_MemTicketDelete(int this_ticket) @date Wed Mar 8 12:47:23 2000 @author Gerd Lanfermann @desc - prints a info string, stating current, past total memory + prints a info string, statign current, past total memory and difference. @enddesc @calls @@ -521,8 +530,8 @@ int CCTK_MemTicketDelete(int this_ticket) void CCTK_MemStat(void) { - printf("CCTK_Memstat: total: %f past: %f diff %f\n", - (double)totmem, (double)pastmem, (double)totmem-pastmem); + printf("CCTK_Memstat: total: %lu past: %lu diff %+ld \n", + totmem, pastmem, totmem-pastmem); } /*@@ @@ -540,7 +549,7 @@ void CCTK_MemStat(void) @endhistory @@*/ -size_t CCTK_TotalMemory(void) +unsigned long int CCTK_TotalMemory(void) { return(totmem); } @@ -576,18 +585,18 @@ int main(int argc, char *argv[]) printf("### Start Allocating ...\n"); ticket[0]=CCTKi_MemTicketRequest(); myint = (int*) CCTKi_Malloc(n*sizeof(int),42,"My int"); - printf("check Ticket1: %f\n",(double)CCTK_MemTicketCash(ticket[0])); + printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0])); ticket[1]=CCTKi_MemTicketRequest(); mydouble= (double*)CCTKi_Malloc(n*sizeof(double), 42,"My double"); - printf("check Ticket1: %f\n",(double)CCTK_MemTicketCash(ticket[0])); - printf("check Ticket2: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[1])); + printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0])); + printf("check Ticket2: %d\n",CCTK_MemTicketCash(ticket[1])); ticket[2]=CCTKi_MemTicketRequest(); mychar = (char*) CCTKi_Malloc(n*sizeof(char), 42, "My char"); - printf("check Ticket1: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[0])); - printf("check Ticket2: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[1])); - printf("check Ticket3: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[2])); + printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0])); + printf("check Ticket2: %d\n",CCTK_MemTicketCash(ticket[1])); + printf("check Ticket3: %d\n",CCTK_MemTicketCash(ticket[2])); for (i=0;i<n;i++) { @@ -602,11 +611,11 @@ int main(int argc, char *argv[]) CCTKi_Free(mydouble, 43, "My double"); CCTKi_Free(mychar, 43, "My char"); - printf("check Ticket1: %f\n",(double)CCTK_MemTicketCash(ticket[0])); - printf("check Ticket2: %f\n",(double)CCTK_MemTicketCash(ticket[1])); - printf("check Ticket3: %f\n",(double)CCTK_MemTicketCash(ticket[2])); + printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0])); + printf("check Ticket2: %d\n",CCTK_MemTicketCash(ticket[1])); + printf("check Ticket3: %d\n",CCTK_MemTicketCash(ticket[2])); - printf("Total Memory allocated: %f ", (double)CCTK_TotalMemory()); + printf("Total Memory allocated: %d ", CCTK_TotalMemory); CCTK_MemTicketDelete(ticket[0]); CCTK_MemTicketDelete(ticket[1]); |