4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_smalloc_c
= "$Id$";
40 static void log_action(int bMal
,char *what
,char *file
,int line
,
41 int nelem
,int size
,void *ptr
)
53 if ((stdlog
!= NULL
) && ((bytes
!= 0) || ((unsigned long) ptr
== 0x5490) )) {
59 fprintf(stdlog
,"%30s:%4d b (mm:%4d, mx:%4d) [%s, line %d, nelem %d, size %d, ptr: %x]\n",
60 what
? what
: NN
,bytes
,mm
/1024,mx
/1024,
61 file
? file
: NN
,line
,nelem
,size
,ptr
);
64 if ((stdlog
!= NULL
) && (bytes
!= 0))
65 fprintf(stdlog
,"%30s:%6d kb (%7d kb) [%s, line %d, nelem %d, size %d]\n",
66 what
? what
: NN
,bytes
,btot
/1024,
67 file
? file
: NN
,line
,nelem
,size
);
73 void *save_malloc(char *name
,char *file
,int line
,int size
)
82 if ((p
=malloc(size
))==NULL
)
83 fatal_error(errno
,"malloc for %s (%d bytes, file %s, line %d)",
85 (void) memset(p
,0,size
);
88 log_action(1,name
,file
,line
,1,size
,p
);
93 void *save_calloc(char *name
,char *file
,int line
,
94 unsigned nelem
,unsigned elsize
)
99 if ((nelem
==0)||(elsize
==0))
103 if ((p
=calloc((size_t)nelem
,(size_t)elsize
))==NULL
)
104 fatal_error(errno
,"calloc for %s (nelem=%d, elsize=%d, file %s"
105 ", line %d)",name
,nelem
,elsize
,file
,line
);
108 log_action(1,name
,file
,line
,nelem
,elsize
,p
);
113 void *save_realloc(char *name
,char *file
,int line
,void *ptr
,unsigned size
)
123 p
=malloc((size_t)size
);
125 p
=realloc(ptr
,(size_t)size
);
128 "realloc for %s (%d bytes, file %s, line %d, %s=0x%8x)",
129 name
,size
,file
,line
,name
,ptr
);
132 log_action(1,name
,file
,line
,1,size
,p
);
137 void save_free(char *name
,char *file
,int line
,void *ptr
)
140 log_action(0,name
,file
,line
,0,0,ptr
);
146 fatal_error(errno
,"free for %s (file %s, line %d, %s=0x%8x)",
147 name
,file
,line
,name
,ptr
);
154 unsigned maxavail(void)
157 unsigned low
,high
,size
;
164 if ((ptr
=malloc((size_t)size
))==NULL
)
175 unsigned memavail(void)
183 if ((ptr
=malloc((size_t)size
))!=NULL
)