Warn for type mismatch for gmx printf like functions 1/3
[alexxy/gromacs.git] / src / gromacs / utility / smalloc.cpp
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,2015,2016,2017,2018, 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 "gmxpre.h"
38
39 #include "smalloc.h"
40
41 #include "config.h"
42
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 #ifdef WITH_DMALLOC
48 #include <dmalloc.h>
49 #endif
50
51 #include <cstring>
52
53 #include "thread_mpi/threads.h"
54
55 #include "gromacs/utility/alignedallocator.h"
56 #include "gromacs/utility/dir_separator.h"
57 #include "gromacs/utility/fatalerror.h"
58 #ifdef PRINT_ALLOC_KB
59 #include "gromacs/utility/basenetwork.h"
60 #include "gromacs/utility/gmxmpi.h"
61 #endif
62
63 static gmx_bool            g_bOverAllocDD     = FALSE;
64 static tMPI_Thread_mutex_t g_over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
65
66 void *save_malloc(const char *name, const char *file, int line, size_t size)
67 {
68     void *p;
69
70     p = nullptr;
71     if (size == 0)
72     {
73         p = nullptr;
74     }
75     else
76     {
77         if ((p = malloc(size)) == nullptr)
78         {
79             gmx_fatal(errno, __FILE__, __LINE__,
80                       "Not enough memory. Failed to malloc %" GMX_PRId64 " bytes for %s\n"
81                       "(called from file %s, line %d)",
82                       (gmx_int64_t)size, name, file, line);
83         }
84         (void) memset(p, 0, size);
85     }
86     return p;
87 }
88
89 void *save_calloc(const char *name, const char *file, int line,
90                   size_t nelem, size_t elsize)
91 {
92     void *p;
93
94     p = nullptr;
95     if ((nelem == 0) || (elsize == 0))
96     {
97         p = nullptr;
98     }
99     else
100     {
101 #ifdef PRINT_ALLOC_KB
102         if (nelem*elsize >= PRINT_ALLOC_KB*1024)
103         {
104             int rank = gmx_node_rank();
105             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
106                    nelem*elsize/1048576.0, name, file, line, rank);
107         }
108 #endif
109 #if GMX_BROKEN_CALLOC
110         /* emulate calloc(3) with malloc/memset on machines with
111            a broken calloc, e.g. in -lgmalloc on cray xt3. */
112         if ((p = malloc((size_t)nelem*(size_t)elsize)) == NULL)
113         {
114             gmx_fatal(errno, __FILE__, __LINE__,
115                       "Not enough memory. Failed to calloc %" GMX_PRId64
116                       " elements of size %" GMX_PRId64
117                       " for %s\n(called from file %s, line %d)",
118                       (gmx_int64_t)nelem, (gmx_int64_t)elsize,
119                       name, file, line);
120         }
121         memset(p, 0, (size_t) (nelem * elsize));
122 #else
123         if ((p = calloc((size_t)nelem, (size_t)elsize)) == nullptr)
124         {
125             gmx_fatal(errno, __FILE__, __LINE__,
126                       "Not enough memory. Failed to calloc %" GMX_PRId64
127                       " elements of size %" GMX_PRId64
128                       " for %s\n(called from file %s, line %d)",
129                       (gmx_int64_t)nelem, (gmx_int64_t)elsize, name, file, line);
130         }
131 #endif
132     }
133     return p;
134 }
135
136 void *save_realloc(const char *name, const char *file, int line, void *ptr,
137                    size_t nelem, size_t elsize)
138 {
139     void  *p;
140     size_t size = nelem*elsize;
141
142     p = nullptr;
143     if (size == 0)
144     {
145         save_free(name, file, line, ptr);
146     }
147     else
148     {
149 #ifdef PRINT_ALLOC_KB
150         if (size >= PRINT_ALLOC_KB*1024)
151         {
152             int rank = gmx_node_rank();
153             printf("Reallocating %.1f MB for %s (called from file %s, line %d on %d)\n",
154                    size/1048576.0, name, file, line, rank);
155         }
156 #endif
157         if (ptr == nullptr)
158         {
159             p = malloc((size_t)size);
160         }
161         else
162         {
163             p = realloc(ptr, (size_t)size);
164         }
165         if (p == nullptr)
166         {
167             gmx_fatal(errno, __FILE__, __LINE__,
168                       "Not enough memory. Failed to realloc %" GMX_PRId64 " bytes for %s, %s=%x\n"
169                       "(called from file %s, line %d)",
170                       (gmx_int64_t)size, name, name, ptr, file, line);
171         }
172     }
173     return p;
174 }
175
176 void save_free(const char gmx_unused *name, const char gmx_unused *file, int gmx_unused line, void *ptr)
177 {
178     if (ptr != nullptr)
179     {
180         free(ptr);
181     }
182 }
183
184 /* Pointers allocated with this routine should only be freed
185  * with save_free_aligned, however this will only matter
186  * on systems that lack posix_memalign() and memalign() when
187  * freeing memory that needed to be adjusted to achieve
188  * the necessary alignment. */
189 void *save_malloc_aligned(const char *name, const char *file, int line,
190                           size_t nelem, size_t elsize, size_t alignment)
191 {
192     void * p;
193
194     if (alignment == 0)
195     {
196         gmx_fatal(errno, __FILE__, __LINE__,
197                   "Cannot allocate aligned memory with alignment of zero!\n(called from file %s, line %d)", file, line);
198     }
199
200     size_t alignmentSize = gmx::AlignedAllocationPolicy::alignment();
201     if (alignment > alignmentSize)
202     {
203         gmx_fatal(errno, __FILE__, __LINE__,
204                   "Cannot allocate aligned memory with alignment > %zu bytes\n(called from file %s, line %d)", alignmentSize, file, line);
205     }
206
207
208     if (nelem == 0 || elsize == 0)
209     {
210         p = nullptr;
211     }
212     else
213     {
214 #ifdef PRINT_ALLOC_KB
215         if (nelem*elsize >= PRINT_ALLOC_KB*1024)
216         {
217             int rank = gmx_node_rank();
218             printf("Allocating %.1f MB for %s (called from file %s, line %d on %d)\n",
219                    nelem*elsize/1048576.0, name, file, line, rank);
220         }
221 #endif
222
223         p = gmx::AlignedAllocationPolicy::malloc(nelem*elsize);
224
225         if (p == nullptr)
226         {
227             gmx_fatal(errno, __FILE__, __LINE__,
228                       "Not enough memory. Failed to allocate %zu aligned elements of size %zu for %s\n(called from file %s, line %d)", nelem, elsize, name, file, line);
229         }
230     }
231     return p;
232 }
233
234 void *save_calloc_aligned(const char *name, const char *file, int line,
235                           size_t nelem, size_t elsize, size_t alignment)
236 {
237     void *aligned = save_malloc_aligned(name, file, line, nelem, elsize, alignment);
238     if (aligned != nullptr)
239     {
240         memset(aligned, 0, (size_t)(nelem * elsize));
241     }
242     return aligned;
243 }
244
245 /* This routine can NOT be called with any pointer */
246 void save_free_aligned(const char gmx_unused *name, const char gmx_unused *file, int gmx_unused line, void *ptr)
247 {
248     gmx::AlignedAllocationPolicy::free(ptr);
249 }
250
251 void set_over_alloc_dd(gmx_bool set)
252 {
253     tMPI_Thread_mutex_lock(&g_over_alloc_mutex);
254     /* we just make sure that we don't set this at the same time.
255        We don't worry too much about reading this rarely-set variable */
256     g_bOverAllocDD = set;
257     tMPI_Thread_mutex_unlock(&g_over_alloc_mutex);
258 }
259
260 int over_alloc_dd(int n)
261 {
262     if (g_bOverAllocDD)
263     {
264         return static_cast<int>(OVER_ALLOC_FAC*n + 100);
265     }
266     else
267     {
268         return n;
269     }
270 }