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