changed reading hint
[gromacs/adressmacs.git] / src / gmxlib / smalloc.c
blob83101fffc6b7b1c35ccb270ab607b601078c74b0
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
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
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_smalloc_c = "$Id$";
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "fatal.h"
35 #include "smalloc.h"
36 #include "main.h"
38 #ifdef DEBUG
39 #define NN "NULL"
40 static void log_action(int bMal,char *what,char *file,int line,
41 int nelem,int size,void *ptr)
43 static int btot=0;
44 int bytes;
46 bytes=size*nelem;
47 if (!bMal)
48 bytes=-bytes;
49 btot+=bytes;
51 bytes/=1024;
52 #ifdef _amb_
53 if ((stdlog != NULL) && ((bytes != 0) || ((unsigned long) ptr == 0x5490) )) {
54 long mm=0,mx=0;
56 mm=memavail();
57 mx=maxavail();
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);
63 #else
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);
68 #endif
70 #undef NN
71 #endif
73 void *save_malloc(char *name,char *file,int line,int size)
75 void *p;
77 p=NULL;
78 if (size==0)
79 p=NULL;
80 else
82 if ((p=malloc(size))==NULL)
83 fatal_error(errno,"malloc for %s (%d bytes, file %s, line %d)",
84 name,size,file,line);
85 (void) memset(p,0,size);
87 #ifdef DEBUG
88 log_action(1,name,file,line,1,size,p);
89 #endif
90 return p;
93 void *save_calloc(char *name,char *file,int line,
94 unsigned nelem,unsigned elsize)
96 void *p;
98 p=NULL;
99 if ((nelem==0)||(elsize==0))
100 p=NULL;
101 else
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);
107 #ifdef DEBUG
108 log_action(1,name,file,line,nelem,elsize,p);
109 #endif
110 return p;
113 void *save_realloc(char *name,char *file,int line,void *ptr,unsigned size)
115 void *p;
117 p=NULL;
118 if (size==0)
119 p=NULL;
120 else
122 if (ptr==NULL)
123 p=malloc((size_t)size);
124 else
125 p=realloc(ptr,(size_t)size);
126 if (p==NULL)
127 fatal_error(errno,
128 "realloc for %s (%d bytes, file %s, line %d, %s=0x%8x)",
129 name,size,file,line,name,ptr);
131 #ifdef DEBUG
132 log_action(1,name,file,line,1,size,p);
133 #endif
134 return p;
137 void save_free(char *name,char *file,int line,void *ptr)
139 #ifdef DEBUG
140 log_action(0,name,file,line,0,0,ptr);
141 #endif
142 if (ptr!=NULL)
144 #ifdef _sun_
145 if (free(ptr)==-1)
146 fatal_error(errno,"free for %s (file %s, line %d, %s=0x%8x)",
147 name,file,line,name,ptr);
148 #else
149 free(ptr);
150 #endif
154 unsigned maxavail(void)
156 char *ptr;
157 unsigned low,high,size;
159 low=0;
160 high=256e6;
161 while ((high-low)>4)
163 size=(high+low)/2;
164 if ((ptr=malloc((size_t)size))==NULL)
165 high=size;
166 else
168 free(ptr);
169 low=size;
172 return low;
175 unsigned memavail(void)
177 char *ptr;
178 unsigned size;
180 size=maxavail();
181 if (size!=0)
183 if ((ptr=malloc((size_t)size))!=NULL)
185 size+=memavail();
186 free(ptr);
189 return size;