Fixing copyright issues and code contributors
[alexxy/gromacs.git] / src / gmxlib / smalloc.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012,2013, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 /* This file is completely threadsafe - keep it that way! */
43
44 #ifdef GMX_THREAD_MPI
45 #include "thread_mpi/threads.h"
46 #endif 
47
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include "gmx_fatal.h"
53 #include "smalloc.h"
54 #include "main.h"
55 #ifdef WITH_DMALLOC
56 #include "dmalloc.h"
57 #endif
58
59 #ifdef DEBUG
60 static void log_action(int bMal,const char *what,const char *file,int line,
61                        int nelem,int size,void *ptr)
62 {
63   static int btot=0;
64   char *NN = "NULL";
65   int        bytes;
66   
67   bytes=size*nelem;
68   if (!bMal)
69     bytes=-bytes;
70   
71 #ifdef GMX_THREAD_MPI
72   tMPI_Thread_mutex_lock(&gmx_logfile_mtx);
73 #endif
74
75   /* This total memory count is not correct, since with realloc
76    * it adds the whole size again, not just the increment.
77    */
78   /* This static variable is protected by the mutex too... */
79   btot+=bytes;
80     
81   bytes/=1024;
82   if (debug && (bytes != 0)) {
83     fprintf(debug,"%s:%d kB (%7d kB) [%s, line %d, nelem %d, size %d]\n",
84             what ? what : NN,bytes,btot/1024,
85             file ? file : NN,line,nelem,size);
86   }
87   /* Print to stderr for things larger than 1 MB */
88   if (bytes >= 1024 || bytes <= -1024) {
89     char *fname=NULL;
90     if (file) {
91       fname = strrchr(file,DIR_SEPARATOR);
92       if (fname) {
93         fname++;
94       } else {
95         fname = file;
96       }
97     }
98     printf("%s: %.1f MB [%s, line %d, nelem %d, size %d]\n",
99            what ? what  : NN,bytes/1024.0,
100            file ? fname : NN,line,nelem,size);
101   }
102 #ifdef GMX_THREAD_MPI
103   tMPI_Thread_mutex_unlock(&gmx_logfile_mtx);
104 #endif
105 }
106 #endif
107
108 static char *gmx_large_int_str(gmx_large_int_t i,char *buf)
109 {
110   sprintf(buf,gmx_large_int_pfmt,i);
111
112   return buf;
113 }
114
115 void *save_malloc(const char *name,const char *file,int line,size_t size)
116 {
117   void *p;
118   
119   p=NULL;
120   if (size==0)
121     p=NULL;
122   else
123     {
124       if ((p=malloc(size))==NULL) {
125         char cbuf[22];
126         gmx_fatal(errno,__FILE__,__LINE__,
127                   "Not enough memory. Failed to malloc %s bytes for %s\n"
128                   "(called from file %s, line %d)",
129                   gmx_large_int_str((gmx_large_int_t)size,cbuf),
130                   name,file,line);
131       }
132       (void) memset(p,0,size);
133     }
134 #ifdef DEBUG
135   log_action(1,name,file,line,1,size,p);
136 #endif
137   return p;
138 }
139
140 void *save_calloc(const char *name,const char *file,int line,
141                   size_t nelem,size_t elsize)
142 {
143   void *p;
144   
145   p=NULL;
146   if ((nelem==0)||(elsize==0))
147     p=NULL;
148   else
149     {
150 #ifdef PRINT_ALLOC_KB
151       int rank=0;
152       if (nelem*elsize >= PRINT_ALLOC_KB*1024) {
153 #ifdef GMX_MPI
154 #include <mpi.h>
155         MPI_Comm_rank(MPI_COMM_WORLD,&rank);
156 #endif
157         printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
158                nelem*elsize/1048576.0,name,file,line,rank);
159       }
160 #endif
161 #ifdef GMX_BROKEN_CALLOC
162       /* emulate calloc(3) with malloc/memset on machines with 
163          a broken calloc, e.g. in -lgmalloc on cray xt3. */
164       if ((p=malloc((size_t)nelem*(size_t)elsize))==NULL) 
165         gmx_fatal(errno,__FILE__,__LINE__,
166                   "Not enough memory. Failed to calloc %"gmx_large_int_fmt
167                   " elements of size %"gmx_large_int_fmt
168                   " for %s\n(called from file %s, line %d)",
169                   (gmx_large_int_t)nelem,(gmx_large_int_t)elsize,
170                   name,file,line);
171       memset(p, 0,(size_t) (nelem * elsize));
172 #else
173       if ((p=calloc((size_t)nelem,(size_t)elsize))==NULL) 
174         gmx_fatal(errno,__FILE__,__LINE__,
175                   "Not enough memory. Failed to calloc %"gmx_large_int_fmt
176                   " elements of size %"gmx_large_int_fmt
177                   " for %s\n(called from file %s, line %d)",
178                   (gmx_large_int_t)nelem,(gmx_large_int_t)elsize,name,file,line);
179 #endif
180     }
181 #ifdef DEBUG
182   log_action(1,name,file,line,nelem,elsize,p);
183 #endif
184   return p;
185 }
186
187 void *save_realloc(const char *name,const char *file,int line,void *ptr,
188                    size_t nelem,size_t elsize)
189 {
190   void *p;
191   size_t size = nelem*elsize;
192   
193   p=NULL;
194   if (size==0)
195     {
196       save_free(name, file, line, ptr);
197     }
198   else
199     {
200 #ifdef PRINT_ALLOC_KB
201       int rank=0;
202       if (size >= PRINT_ALLOC_KB*1024) {
203 #ifdef GMX_MPI
204 #include <mpi.h>
205         MPI_Comm_rank(MPI_COMM_WORLD,&rank);
206 #endif
207         printf("Reallocating %.1f MB for %s (called from file %s, line %d on %d)\n",
208                size/1048576.0,name,file,line,rank);
209       }
210 #endif
211       if (ptr==NULL) 
212         p=malloc((size_t)size); 
213       else 
214         p=realloc(ptr,(size_t)size);
215       if (p == NULL) {
216         char cbuf[22];
217         gmx_fatal(errno,__FILE__,__LINE__,
218                   "Not enough memory. Failed to realloc %s bytes for %s, %s=%x\n"
219                   "(called from file %s, line %d)",
220                   gmx_large_int_str((gmx_large_int_t)size,cbuf),
221                   name,name,ptr,file,line);
222       }
223 #ifdef DEBUG
224       log_action(1,name,file,line,1,size,p);
225 #endif
226     }
227   return p;
228 }
229
230 void save_free(const char *name,const char *file,int line, void *ptr)
231 {
232 #ifdef DEBUG
233   log_action(0,name,file,line,0,0,ptr);
234 #endif
235   if (ptr != NULL)
236     free(ptr);
237 }
238
239 size_t maxavail(void)
240 {
241   char *ptr;
242   size_t low,high,size;
243   
244   low=0;
245   high=256e6;
246   while ((high-low) > 4) {
247     size=(high+low)/2;
248     if ((ptr=(char *)malloc((size_t)size))==NULL)
249       high=size;
250     else {
251       free(ptr);
252       low=size;
253     }
254   }
255   return low;
256 }
257
258 size_t memavail(void)
259 {
260   char *ptr;
261   size_t size;
262   
263   size = maxavail(); 
264   if (size != 0) { 
265     if ((ptr=(char *)malloc((size_t)size)) != NULL) {
266       size += memavail();
267       free(ptr);
268     }
269   }
270   return size;
271 }
272
273 /* If we don't have useful routines for allocating aligned memory,
274  * then we have to use the old-style GROMACS approach bitwise-ANDing
275  * pointers to ensure alignment. We store the pointer to the originally
276  * allocated region in the space before the returned pointer */
277
278 /* we create a positive define for the absence of an system-provided memalign */
279 #if (!defined HAVE_POSIX_MEMALIGN && !defined HAVE_MEMALIGN && \
280      !defined HAVE__ALIGNED_MALLOC)
281 #define GMX_OWN_MEMALIGN
282 #endif
283
284
285 /* Pointers allocated with this routine should only be freed
286  * with save_free_aligned, however this will only matter
287  * on systems that lack posix_memalign() and memalign() when 
288  * freeing memory that needed to be adjusted to achieve
289  * the necessary alignment. */
290 void *save_malloc_aligned(const char *name,const char *file,int line,
291                           unsigned nelem,size_t elsize,size_t alignment)
292 {
293     void **aligned=NULL;
294     void *malloced=NULL;
295     gmx_bool allocate_fail;
296
297     if (alignment == 0)
298     {
299         gmx_fatal(errno,__FILE__,__LINE__,
300                   "Cannot allocate aligned memory with alignment of zero!\n(called from file %s, line %d)",file,line);
301     }
302
303     
304     if (nelem ==0 || elsize == 0)
305     {
306         aligned  = NULL;
307     }
308     else
309     {
310 #ifdef PRINT_ALLOC_KB
311         if (nelem*elsize >= PRINT_ALLOC_KB*1024)
312         {
313             printf("Allocating %.1f MB for %s\n",
314                    nelem*elsize/(PRINT_ALLOC_KB*1024.0),name);
315         }
316 #endif
317
318         allocate_fail = FALSE; /* stop compiler warnings */
319 #ifdef HAVE_POSIX_MEMALIGN
320         allocate_fail = (0!=posix_memalign(&malloced, alignment, nelem*elsize));
321 #elif defined HAVE_MEMALIGN
322         allocate_fail = ((malloced=memalign(alignment, nelem*elsize)) == NULL);
323 #elif defined HAVE__ALIGNED_MALLOC
324         allocate_fail = ((malloced=_aligned_malloc(nelem*elsize, alignment)) 
325                          == NULL);
326 #else
327         allocate_fail = ((malloced = malloc(nelem*elsize+alignment+
328                                             sizeof(void*)))==NULL);
329 #endif
330         if (allocate_fail)
331         {
332             gmx_fatal(errno,__FILE__,__LINE__,
333                       "Not enough memory. Failed to allocate %u aligned elements of size %u for %s\n(called from file %s, line %d)",nelem,elsize,name,file,line);
334         }
335         /* we start with the original pointer */
336         aligned=(void**)malloced;
337   
338 #ifdef GMX_OWN_MEMALIGN
339         /* Make the aligned pointer, and save the underlying pointer that
340          * we're allowed to free(). */
341
342         /* we first make space to store that underlying pointer: */
343         aligned = aligned + 1; 
344         /* then we apply a bit mask */
345         aligned = (void *) (((size_t) aligned + alignment - 1) & 
346                             (~((size_t) (alignment-1))));
347         /* and we store the original pointer in the area just before the 
348            pointer we're going to return */
349         aligned[-1] = malloced;
350 #endif
351     }
352     return (void*)aligned;
353 }
354
355 void *save_calloc_aligned(const char *name,const char *file,int line,
356                           unsigned nelem,size_t elsize,size_t alignment)
357 {
358     void *aligned = save_malloc_aligned(name, file, line, nelem, elsize, alignment);
359     if (aligned != NULL)
360     {
361         memset(aligned, 0, (size_t)(nelem * elsize));
362     }
363     return aligned;
364 }
365
366 /* This routine can NOT be called with any pointer */
367 void save_free_aligned(const char *name,const char *file,int line,void *ptr)
368 {
369     int i, j;
370     void *free=ptr;
371
372     if (NULL != ptr)
373     {
374 #ifdef GMX_OWN_MEMALIGN 
375         /* we get the pointer from just before the memaligned pointer */
376         free= ((void**)ptr)[-1];
377 #endif
378
379 #ifndef HAVE__ALIGNED_MALLOC
380         /* (Now) we're allowed to use a normal free() on this pointer. */
381         save_free(name,file,line,free);
382 #else
383         _aligned_free(free);
384 #endif
385     }
386 }
387