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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/*@@
@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 <string.h>
#include "cctk_Malloc.h"
#include "StoreHandledData.h"
static char *rcsid = "$Header$";
/* #define MEMDEBUG */
/********************************************************************
********************* Local Data Types ***********************
********************************************************************/
typedef struct
{
unsigned long int size;
unsigned long int tsize;
const char *file;
} 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 **********************
********************************************************************/
/*@@
@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)
{
t_mallocinfo *info;
char *data;
int retval;
data = (char*)malloc(size+sizeof(t_mallocinfo));
if(!data)
{
fprintf(stderr, "Allocation error! ");
}
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",
info->size, info->file, info->line, CCTK_TotalMemory());
#endif
return((void*)(data+sizeof(t_mallocinfo)));
}
/*@@
@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)
{
t_mallocinfo *info;
info = (t_mallocinfo *)((char*)pointer-sizeof(t_mallocinfo));
#ifdef MEMDEBUG
printf("Freeing %lu - allocated by %s in line %d TOTAL: %lu\n",
info->size, info->file, info->line, CCTK_TotalMemory());
#endif
pastmem = totmem;
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;
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
@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[1024];
sprintf(mess,"total: %ld past: %ld diff %+ld \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);
}
/*@@
@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
|