Merge branch release-2016 into release-2018
[gromacs.git] / src / gromacs / utility / smalloc.h
blob465c1a4130e52cac85078f002b2226feb7d8c1ba
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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, 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.
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.
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.
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.
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.
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.
37 /*! \file
38 * \brief
39 * C memory allocation routines for \Gromacs.
41 * This header provides macros snew(), srenew(), smalloc(), and sfree() for
42 * C memory management. Additionally, snew_aligned() and sfree_aligned() are
43 * provided for managing memory with a specified byte alignment.
45 * If an allocation fails, the program is halted by calling gmx_fatal(), which
46 * outputs source file and line number and the name of the variable involved.
47 * This frees calling code from the trouble of checking the result of the
48 * allocations everywhere. It also provides a location for centrally logging
49 * memory allocations for diagnosing memory usage (currently can only enabled
50 * by changing the source code). Additionally, sfree() works also with a
51 * `NULL` parameter, which standard free() does not.
53 * The macros forward the calls to functions save_malloc(), save_calloc(),
54 * save_realloc(), save_free(), save_calloc_aligned(), and save_free_aligned().
55 * There are a few low-level locations in \Gromacs that call these directly,
56 * but generally the macros should be used.
57 * save_malloc_aligned() exists for this purpose, although there is no macro to
58 * invoke it.
60 * \if internal
61 * As an implementation detail, the macros need a different internal
62 * implementation for C and C++ code. This is because C accepts conversions
63 * from `void *` to any pointer type, but C++ doesn't. And in order to cast
64 * the returned pointer to a correct type, a C++ template needs to be used to
65 * get access to the type.
66 * \endif
68 * \inpublicapi
69 * \ingroup module_utility
71 #ifndef GMX_UTILITY_SMALLOC_H
72 #define GMX_UTILITY_SMALLOC_H
74 #include <stddef.h>
76 #include "gromacs/utility/basedefinitions.h"
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
82 /*! \brief
83 * \Gromacs wrapper for malloc().
85 * \param[in] name Variable name identifying the allocation.
86 * \param[in] file Source code file where the allocation originates from.
87 * \param[in] line Source code line where the allocation originates from.
88 * \param[in] size Number of bytes to allocate.
89 * \returns Pointer to the allocated space.
91 * This should generally be called through smalloc(), not directly.
93 void *save_malloc(const char *name, const char *file, int line, size_t size);
94 /*! \brief
95 * \Gromacs wrapper for calloc().
97 * \param[in] name Variable name identifying the allocation.
98 * \param[in] file Source code file where the allocation originates from.
99 * \param[in] line Source code line where the allocation originates from.
100 * \param[in] nelem Number of elements to allocate.
101 * \param[in] elsize Number of bytes per element.
102 * \returns Pointer to the allocated space.
104 * This should generally be called through snew(), not directly.
106 void *save_calloc(const char *name, const char *file, int line,
107 size_t nelem, size_t elsize);
108 /*! \brief
109 * \Gromacs wrapper for realloc().
111 * \param[in] name Variable name identifying the allocation.
112 * \param[in] file Source code file where the allocation originates from.
113 * \param[in] line Source code line where the allocation originates from.
114 * \param[in] ptr Pointer to the previously allocated memory (can be NULL).
115 * \param[in] nelem Number of elements to allocate.
116 * \param[in] elsize Number of bytes per element.
117 * \returns Pointer to the allocated space.
119 * As with realloc(), if \p ptr is NULL, memory is allocated as if malloc() was
120 * called.
121 * This should generally be called through srenew(), not directly.
123 * Note that the allocated memory is not initialized to zero.
125 void *save_realloc(const char *name, const char *file, int line,
126 void *ptr, size_t nelem, size_t elsize);
127 /*! \brief
128 * \Gromacs wrapper for free().
130 * \param[in] name Variable name identifying the deallocation.
131 * \param[in] file Source code file where the deallocation originates from.
132 * \param[in] line Source code line where the deallocation originates from.
133 * \param[in] ptr Pointer to the allocated memory (can be NULL).
135 * If \p ptr is NULL, does nothing.
136 * This should generally be called through sfree(), not directly.
137 * This never fails.
139 void save_free(const char *name, const char *file, int line, void *ptr);
141 /*! \brief
142 * \Gromacs wrapper for allocating aligned memory.
144 * \param[in] name Variable name identifying the allocation.
145 * \param[in] file Source code file where the allocation originates from.
146 * \param[in] line Source code line where the allocation originates from.
147 * \param[in] nelem Number of elements to allocate.
148 * \param[in] elsize Number of bytes per element.
149 * \param[in] alignment Requested alignment in bytes.
150 * \returns Pointer to the allocated space, aligned at `alignment`-byte
151 * boundary.
153 * There is no macro that invokes this function.
155 * The returned pointer should only be freed with a call to save_free_aligned().
157 void *save_malloc_aligned(const char *name, const char *file, int line,
158 size_t nelem, size_t elsize, size_t alignment);
159 /*! \brief
160 * \Gromacs wrapper for allocating zero-initialized aligned memory.
162 * \param[in] name Variable name identifying the allocation.
163 * \param[in] file Source code file where the allocation originates from.
164 * \param[in] line Source code line where the allocation originates from.
165 * \param[in] nelem Number of elements to allocate.
166 * \param[in] elsize Number of bytes per element.
167 * \param[in] alignment Requested alignment in bytes.
168 * \returns Pointer to the allocated space, aligned at `alignment`-byte
169 * boundary.
171 * This should generally be called through snew_aligned(), not directly.
173 * The returned pointer should only be freed with a call to save_free_aligned().
175 void *save_calloc_aligned(const char *name, const char *file, int line,
176 size_t nelem, size_t elsize, size_t alignment);
177 /*! \brief
178 * \Gromacs wrapper for freeing aligned memory.
180 * \param[in] name Variable name identifying the deallocation.
181 * \param[in] file Source code file where the deallocation originates from.
182 * \param[in] line Source code line where the deallocation originates from.
183 * \param[in] ptr Pointer to the allocated memory (can be NULL).
185 * If \p ptr is NULL, does nothing.
186 * \p ptr should have been allocated with save_malloc_aligned() or
187 * save_calloc_aligned().
188 * This should generally be called through sfree_aligned(), not directly.
189 * This never fails.
191 void save_free_aligned(const char *name, const char *file, int line, void *ptr);
193 #ifdef __cplusplus
195 #endif
197 #ifdef __cplusplus
199 #if GMX_CXX11_COMPILATION
200 #include <type_traits>
201 #endif
203 /*! \cond internal */
204 /*! \name Implementation templates for C++ memory allocation macros
206 * These templates are used to implement the snew() etc. macros for C++, where
207 * an explicit cast is needed from `void *` (the return value of the allocation
208 * wrapper functions) to the thpe of \p ptr.
210 * Having these as `static` avoid some obscure bugs if several files define
211 * distinct data structures with identical names and allocate memory for them
212 * using snew(). By the C++ standard, such declarations cause undefined
213 * behavior, but can be difficult to spot in the existing C code.
214 * Without the `static` (and if the compiler does not inline the calls), the
215 * linker cannot that data structures with identical names are actually
216 * different and links calls to these template functions incorrectly, which can
217 * result in allocation of an incorrect amount of memory if the element size is
218 * computed within the function.
220 * The size cannot be passed as a parameter to the function either, since that
221 * provokes warnings from cppcheck for some invocations, where a complex
222 * expression is passed as \p ptr.
224 /*! \{ */
225 /** C++ helper for snew(). */
226 template <typename T> static inline
227 void gmx_snew_impl(const char *name, const char *file, int line,
228 T * &ptr, size_t nelem)
230 #if GMX_CXX11_COMPILATION
231 static_assert(std::is_pod<T>::value, "snew() called on C++ type");
232 #endif
233 ptr = (T *)save_calloc(name, file, line, nelem, sizeof(T));
235 /** C++ helper for srenew(). */
236 template <typename T> static inline
237 void gmx_srenew_impl(const char *name, const char *file, int line,
238 T * &ptr, size_t nelem)
240 #if GMX_CXX11_COMPILATION
241 static_assert(std::is_pod<T>::value, "srenew() called on C++ type");
242 #endif
243 ptr = (T *)save_realloc(name, file, line, ptr, nelem, sizeof(T));
245 /** C++ helper for smalloc(). */
246 template <typename T> static inline
247 void gmx_smalloc_impl(const char *name, const char *file, int line,
248 T * &ptr, size_t size)
250 #if GMX_CXX11_COMPILATION
251 static_assert(std::is_pod<T>::value, "smalloc() called on C++ type");
252 #endif
253 ptr = (T *)save_malloc(name, file, line, size);
255 /** C++ helper for snew_aligned(). */
256 template <typename T> static inline
257 void gmx_snew_aligned_impl(const char *name, const char *file, int line,
258 T * &ptr, size_t nelem, size_t alignment)
260 #if GMX_CXX11_COMPILATION
261 static_assert(std::is_pod<T>::value, "snew_aligned() called on C++ type");
262 #endif
263 ptr = (T *)save_calloc_aligned(name, file, line, nelem, sizeof(T), alignment);
265 /** C++ helper for sfree(). */
266 template <typename T> static inline
267 void gmx_sfree_impl(const char *name, const char *file, int line, T *ptr)
269 #if GMX_CXX11_COMPILATION
270 static_assert(std::is_pod<T>::value || std::is_void<T>::value,
271 "sfree() called on C++ type");
272 #endif
273 save_free(name, file, line, ptr);
275 /** C++ helper for sfree_aligned(). */
276 template <typename T> static inline
277 void gmx_sfree_aligned_impl(const char *name, const char *file, int line, T *ptr)
279 #if GMX_CXX11_COMPILATION
280 static_assert(std::is_pod<T>::value || std::is_void<T>::value,
281 "sfree_aligned() called on C++ type");
282 #endif
283 save_free_aligned(name, file, line, ptr);
285 /*! \} */
286 /*! \endcond */
287 #endif /* __cplusplus */
289 /*! \def snew
290 * \brief
291 * Allocates memory for a given number of elements.
293 * \param[out] ptr Pointer to allocate.
294 * \param[in] nelem Number of elements to allocate.
296 * Allocates memory for \p nelem elements of type \p *ptr and sets this to
297 * \p ptr. The allocated memory is initialized to zeros.
299 * \hideinitializer
301 /*! \def srenew
302 * \brief
303 * Reallocates memory for a given number of elements.
305 * \param[in,out] ptr Pointer to allocate/reallocate.
306 * \param[in] nelem Number of elements to allocate.
308 * (Re)allocates memory for \p ptr such that it can hold \p nelem elements of
309 * type \p *ptr, and sets the new pointer to \p ptr.
310 * If \p ptr is `NULL`, memory is allocated as if it was new.
311 * If \p nelem is zero, \p ptr is freed (if not `NULL`).
312 * Note that the allocated memory is not initialized, unlike with snew().
314 * \hideinitializer
316 /*! \def smalloc
317 * \brief
318 * Allocates memory for a given number of bytes.
320 * \param[out] ptr Pointer to allocate.
321 * \param[in] size Number of bytes to allocate.
323 * Allocates memory for \p size bytes and sets this to \p ptr.
324 * The allocated memory is initialized to zero.
326 * \hideinitializer
328 /*! \def snew_aligned
329 * \brief
330 * Allocates aligned memory for a given number of elements.
332 * \param[out] ptr Pointer to allocate.
333 * \param[in] nelem Number of elements to allocate.
334 * \param[in] alignment Requested alignment in bytes.
336 * Allocates memory for \p nelem elements of type \p *ptr and sets this to
337 * \p ptr. The returned pointer is `alignment`-byte aligned.
338 * The allocated memory is initialized to zeros.
340 * The returned pointer should only be freed with sfree_aligned().
342 * \hideinitializer
344 /*! \def sfree
345 * \brief
346 * Frees memory referenced by \p ptr.
348 * \p ptr is allowed to be NULL, in which case nothing is done.
350 * \hideinitializer
352 /*! \def sfree_aligned
353 * \brief
354 * Frees aligned memory referenced by \p ptr.
356 * This must only be called with a pointer obtained through snew_aligned().
357 * \p ptr is allowed to be NULL, in which case nothing is done.
359 * \hideinitializer
361 #ifdef __cplusplus
363 /* C++ implementation */
364 #define snew(ptr, nelem) \
365 gmx_snew_impl(#ptr, __FILE__, __LINE__, (ptr), (nelem))
366 #define srenew(ptr, nelem) \
367 gmx_srenew_impl(#ptr, __FILE__, __LINE__, (ptr), (nelem))
368 #define smalloc(ptr, size) \
369 gmx_smalloc_impl(#ptr, __FILE__, __LINE__, (ptr), (size))
370 #define snew_aligned(ptr, nelem, alignment) \
371 gmx_snew_aligned_impl(#ptr, __FILE__, __LINE__, (ptr), (nelem), alignment)
372 #define sfree(ptr) \
373 gmx_sfree_impl(#ptr, __FILE__, __LINE__, (ptr))
374 #define sfree_aligned(ptr) \
375 gmx_sfree_aligned_impl(#ptr, __FILE__, __LINE__, (ptr))
377 #else
379 /* C implementation */
380 #define snew(ptr, nelem) \
381 (ptr) = save_calloc(#ptr, __FILE__, __LINE__, (nelem), sizeof(*(ptr)))
382 #define srenew(ptr, nelem) \
383 (ptr) = save_realloc(#ptr, __FILE__, __LINE__, (ptr), (nelem), sizeof(*(ptr)))
384 #define smalloc(ptr, size) \
385 (ptr) = save_malloc(#ptr, __FILE__, __LINE__, size)
386 #define snew_aligned(ptr, nelem, alignment) \
387 (ptr) = save_calloc_aligned(#ptr, __FILE__, __LINE__, (nelem), sizeof(*(ptr)), alignment)
388 #define sfree(ptr) save_free(#ptr, __FILE__, __LINE__, (ptr))
389 #define sfree_aligned(ptr) save_free_aligned(#ptr, __FILE__, __LINE__, (ptr))
391 #endif
393 #ifdef __cplusplus
394 extern "C" {
395 #endif
397 /*! \brief
398 * Over allocation factor for memory allocations.
400 * Memory (re)allocation can be VERY slow, especially with some
401 * MPI libraries that replace the standard malloc and realloc calls.
402 * To avoid slow memory allocation we use over_alloc to set the memory
403 * allocation size for large data blocks. Since this scales the size
404 * with a factor, we use log(n) realloc calls instead of n.
405 * This can reduce allocation times from minutes to seconds.
407 * This factor leads to 4 realloc calls to double the array size.
409 #define OVER_ALLOC_FAC 1.19
411 /*! \brief
412 * Turns over allocation for variable size atoms/cg/top arrays on or off,
413 * default is off.
415 * \todo
416 * This is mdrun-specific, so it might be better to put this and
417 * over_alloc_dd() much higher up.
419 void set_over_alloc_dd(gmx_bool set);
421 /*! \brief
422 * Returns new allocation count for domain decomposition allocations.
424 * Returns n when domain decomposition over allocation is off.
425 * Returns OVER_ALLOC_FAC*n + 100 when over allocation in on.
426 * This is to avoid frequent reallocation during domain decomposition in mdrun.
428 int over_alloc_dd(int n);
430 /** Over allocation for small data types: int, real etc. */
431 #define over_alloc_small(n) (int)(OVER_ALLOC_FAC*(n) + 8000)
433 /** Over allocation for large data types: complex structs */
434 #define over_alloc_large(n) (int)(OVER_ALLOC_FAC*(n) + 1000)
436 #ifdef __cplusplus
438 #endif
440 #endif