summaryrefslogtreecommitdiff
path: root/src/util/Malloc.c
diff options
context:
space:
mode:
authorlanfer <lanfer@17b73243-c579-4c4c-a9d2-2d5706c11dac>2000-03-13 10:42:10 +0000
committerlanfer <lanfer@17b73243-c579-4c4c-a9d2-2d5706c11dac>2000-03-13 10:42:10 +0000
commit0a4ba3e0266556527b50cbf8b8f2b0f4fb557e86 (patch)
tree38dca304f0c68482626bd623ecde874b44afa428 /src/util/Malloc.c
parent6568e251220619ef18278df69a114256e89b267a (diff)
Memory tickets
git-svn-id: http://svn.cactuscode.org/flesh/trunk@1461 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/util/Malloc.c')
-rw-r--r--src/util/Malloc.c314
1 files changed, 295 insertions, 19 deletions
diff --git a/src/util/Malloc.c b/src/util/Malloc.c
index 5f46383a..33505a37 100644
--- a/src/util/Malloc.c
+++ b/src/util/Malloc.c
@@ -11,13 +11,14 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
-#include "cctk_Config.h"
#include "cctk_Malloc.h"
+#include "StoreHandledData.h"
static char *rcsid = "$Header$";
-#define MEMDEBUG
+/* #define MEMDEBUG */
/********************************************************************
********************* Local Data Types ***********************
@@ -26,19 +27,44 @@ static char *rcsid = "$Header$";
typedef struct
{
unsigned long int size;
- int line;
+ unsigned long int tsize;
const char *file;
-} iMemData;
+} t_memhash;
+typedef struct
+{
+ unsigned long int size;
+ unsigned long int tsize;
+ unsigned int line;
+ const char *file;
+} t_mallocinfo;
+
+typedef struct
+{
+ unsigned long int size;
+} t_memticket;
+
/********************************************************************
********************* Local Data *****************************
********************************************************************/
+static cHandledData *ticketDB = NULL;
+static cHandledData *memfileDB = NULL;
+
+static int n_tickets = 0;
+
static unsigned long int totmem=0;
static unsigned long int pastmem=0;
/********************************************************************
+ ********************* internal prototypes ********************
+ ********************************************************************/
+
+int CCTKi_UpdateMemByFile(int size, int line, const char *file);
+
+
+/********************************************************************
********************* External Routines **********************
********************************************************************/
@@ -59,28 +85,32 @@ static unsigned long int pastmem=0;
void *CCTKi_Malloc(size_t size, int line, const char *file)
{
- iMemData *memdata;
+ t_mallocinfo *info;
char *data;
-
- data = (char*)malloc(size+sizeof(iMemData));
+ int retval;
+
+ data = (char*)malloc(size+sizeof(t_mallocinfo));
if(!data)
{
fprintf(stderr, "Allocation error! ");
}
- memdata = (iMemData *)data;
- memdata->size = size;
- memdata->line = line;
- memdata->file = file;
+ info = (t_mallocinfo*) data;
+ info->size = size;
+ info->tsize= size+sizeof(t_mallocinfo);
+ info->line = line;
+ info->file = file;
pastmem = totmem;
totmem += size;
+ retval = CCTKi_UpdateMemByFile(info->size, line, file);
+
#ifdef MEMDEBUG
printf("Allocating %lu - by %s in line %d TOTAL: %lu\n",
- memdata->size,memdata->file,memdata->line, CCTK_TotalMemory());
+ info->size, info->file, info->line, CCTK_TotalMemory());
#endif
- return((void*)(data+sizeof(iMemData)));
+ return((void*)(data+sizeof(t_mallocinfo)));
}
/*@@
@@ -100,19 +130,190 @@ void *CCTKi_Malloc(size_t size, int line, const char *file)
void CCTKi_Free(void *pointer)
{
- iMemData *memdata;
+ t_mallocinfo *info;
- memdata = (iMemData *)((char*)pointer-sizeof(iMemData));
+ info = (t_mallocinfo *)((char*)pointer-sizeof(t_mallocinfo));
#ifdef MEMDEBUG
printf("Freeing %lu - allocated by %s in line %d TOTAL: %lu\n",
- memdata->size,memdata->file,memdata->line, CCTK_TotalMemory());
+ info->size, info->file, info->line, CCTK_TotalMemory());
#endif
pastmem = totmem;
- totmem -= memdata->size;
+ totmem -= info->size;
+
+ free(info);
+}
+
+/*@@
+ @routine CCTKi_Malloc
+ @date Wed Mar 8 12:46:06 2000
+ @author Gerd Lanfermann
+ @desc
+ Keeps track of the allocated memory on a per file bases.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+@@*/
+
+int CCTKi_UpdateMemByFile(int size, int line, const char *file)
+{
+ t_memhash *memfile;
+ int handle, retval=-1;
- free(memdata);
+ if ((handle=Util_GetHandle(memfileDB, file, (void**)&memfile)) > -1)
+ {
+ /* Memory entry under <file> exists */
+ if (memfile = (t_memhash*) Util_GetHandledData(memfileDB, handle))
+ {
+ memfile->size +=size;
+ memfile->tsize+=size;
+ retval = 0;
+ }
+ else {
+ retval = -3;
+ }
+ }
+ else
+ {
+ /* Memory entry under <file> has to be created */
+ memfile = (t_memhash*) malloc( sizeof(t_memhash));
+ if (memfile)
+ {
+ memfile->size += size;
+ memfile->tsize+= size+sizeof(t_memhash);
+ memfile->file = file;
+ retval = Util_NewHandle(&memfileDB, file, memfile);
+ }
+ else
+ {
+ retval = -1;
+ }
+ }
+ return retval;
+}
+
+/*@@
+ @routine CCTK_MemTicketRequest
+ @date Sun Mar 12 17:22:08 2000
+ @author Gerd Lanfermann
+ @desc
+ Request a ticket: save the current total memory to a database.
+ Return an integer (ticket). Use the ticket to calculate the
+ difference in memory allocation between the two instances in
+ CCTK_MemTicketCash.
+
+ This only tracks the real data memory, which is the same as in
+ undebug mode. It does not keep track of the internal allocations
+ done to provide the database, bc. this is not allocated either if
+ you compile undebugged.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+@@*/
+
+int CCTK_MemTicketRequest(void)
+{
+ int this_ticket;
+ t_memticket *tmem;
+ char tname[20];
+
+ sprintf(tname, "ticket_%d",n_tickets++);
+
+ if (Util_GetHandle(ticketDB, tname, (void**)&tmem)>-1)
+ {
+ this_ticket=-3;
+ }
+ else {
+ tmem =(t_memticket*) malloc(sizeof(t_memticket));
+
+ if (tmem)
+ {
+ tmem->size = CCTK_TotalMemory();
+ this_ticket = Util_NewHandle(&ticketDB, tname, tmem);
+ }
+ else {
+ this_ticket = -2;
+ }
+ }
+ return(this_ticket);
}
+/*@@
+ @routine CCTK_MemTicketCash
+ @date Sun Mar 12 17:22:08 2000
+ @author Gerd Lanfermann
+ @desc
+ Cash in your ticket: return the memory difference between now and the
+ time the ticket was requested.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+@@*/
+
+long int CCTK_MemTicketCash(int this_ticket)
+{
+ long int tdiff;
+ unsigned long int tsize;
+ t_memticket *tmem;
+
+ if (tmem = (t_memticket*) Util_GetHandledData(ticketDB, this_ticket))
+ {
+ tsize = tmem->size;
+ tdiff = CCTK_TotalMemory() - tsize;
+ }
+ else {
+ printf("CCTK_MemTicketCash: Cannot find ticket %d \n", this_ticket);
+ tdiff = 42;
+ }
+ return(tdiff);
+}
+
+/*@@
+ @routine CCTK_MemTicketDelete
+ @date Sun Mar 12 17:22:08 2000
+ @author Gerd Lanfermann
+ @desc
+ Delete the memory ticket. The ticket-id will not be reused, since
+ it's incremented with every ticket request, but the memory
+ is freed.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+@@*/
+
+int CCTK_MemTicketDelete(int this_ticket)
+{
+ int ret_val;
+ t_memticket *tmem;
+
+ if (tmem = (t_memticket*)Util_GetHandledData(ticketDB, this_ticket))
+ {
+ Util_DeleteHandle(ticketDB, this_ticket);
+ ret_val = 0;
+ }
+ else
+ {
+ ret_val = -1;
+ }
+ return(ret_val);
+}
+
+
/*@@
@routine CCTK_MemStat
@date Wed Mar 8 12:47:23 2000
@@ -132,7 +333,7 @@ void CCTKi_Free(void *pointer)
void CCTK_MemStat(void)
{
char mess[138];
- sprintf(mess,"total: %d past: %d diff %d \n",
+ sprintf(mess,"total: %ld past: %ld diff %+ld \n",
totmem, pastmem, totmem-pastmem);
printf("CCTK_Memstat: %s ",mess);
}
@@ -156,4 +357,79 @@ unsigned long int CCTK_TotalMemory(void)
{
return(totmem);
}
+
+
+
+
+
+
+ /*@@
+ @routine testmalloc
+ @date Sun Mar 12 17:31:44 2000
+ @author Gerd Lanfermann
+ @desc
+ test routine
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+@@*/
+#ifdef TESTMALLOC
+
+int main(int argc, char *argv[])
+{
+ int *myint;
+ double *mydouble;
+ char *mychar;
+
+ int i,n,ticket[3];
+
+ n = 10;
+
+ printf("### Start Allocating ...\n");
+ ticket[0]=CCTKi_MemTicketRequest();
+ myint = (int*) CCTKi_Malloc(n*sizeof(int),42,"My int");
+ 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: %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: %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++)
+ {
+ myint[i] = i;
+ mydouble[i]= i/3.1415;
+ mychar[i] = "i";
+ }
+
+ printf("Total Memory allocated: %d ", CCTK_TotalMemory);
+ printf("### Start Freeing ...\n");
+ CCTKi_Free(myint, 43, "Myn int");
+ CCTKi_Free(mydouble, 43, "My double");
+ CCTKi_Free(mychar, 43, "My char");
+
+ 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: %d ", CCTK_TotalMemory);
+
+ CCTK_MemTicketDelete(ticket[0]);
+ CCTK_MemTicketDelete(ticket[1]);
+ CCTK_MemTicketDelete(ticket[2]);
+
+ return(0);
+}
+
+#endif TESTMALLOC