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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*@@
@file MemAllocate.c
@date Tue Mar 7 11:58:03 2000
@author Gerd lanfermann
@desc
Cactus memory allocation routines to monitor memory consumption
by C routines. No fortran support.
@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 **********************
********************************************************************/
/*@@
@routine CCTKi_Malloc
@date Wed Mar 8 12:46:06 2000
@author Gerd Lanfermann
@desc
Allocates memory, updates the total memory variable (static)
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
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)));
}
/*@@
@routine CCTKi_Free
@date Wed Mar 8 12:46:55 2000
@author Gerd Lanfermann
@desc
Frees memory, updates the total memory variable (static)
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
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);
}
/*@@
@routine CCTK_MemStat
@date Wed Mar 8 12:47:23 2000
@author Gerd Lanfermann
@desc
prints a info string, statign current, past total memory
and difference.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
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);
}
/*@@
@routine CCTK_MemStat
@date Wed Mar 8 12:47:23 2000
@author Gerd Lanfermann
@desc
returns total memory allocated by C routines
(which use CCTK_MALLOC)
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
unsigned long int CCTK_TotalMemory(void)
{
return(totmem);
}
|