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, 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.
44 * Memory allocation routines in gromacs:
46 * If an allocation fails, the program is halted by means of the
47 * fatal_error routine, which outputs source file and line number
48 * and the name of the variable involved.
50 * Macro's which can be used:
53 * Allocates memory for nelem elements and returns this in ptr.
54 * The allocated memory is initialized to zeros.
57 * Reallocates memory for nelem elements and returns this in ptr.
60 * Allocates memory for size bytes and returns this in ptr.
62 * scalloc(ptr,nelem,elsize)
63 * Allocates memory for nelem elements of size elsize and returns
67 * Reallocates memory for size bytes and returns this in ptr.
70 * Frees memory referenced by ptr.
72 * snew_aligned(ptr,nelem,alignment)
73 * Allocates memory for nelem elements and returns this in ptr.
74 * The allocated memory is initialized to zeroes.
75 * alignment=n will constrain ptr to be n-byte aligned.
76 * This pointer should only be freed with sfree_aligned, since
77 * it may not be the value returned by the underlying malloc.
80 * Frees aligned memory referenced by ptr.
82 ****************************************************************************
84 * Functions which are used by the macro's:
86 * extern void *save_malloc(char *name,char *file,int line,int size);
87 * Like alloc, returns a pointer to the allocated space, uses name, file
88 * and line to generate an error message when allocation failed.
90 * extern void *save_calloc(char *name,char *file,int line,
91 * size_t nelem,size_t elsize);
92 * Like calloc, returns a pointer to the allocated space, uses name, file
93 * and line to generate an error message when allocation failed.
95 * extern void *save_realloc(char *name,char *file,int line,
96 * void *ptr,size_t size);
97 * Like realloc, returns a pointer to the allocated space, uses name, file
98 * and line to generate an error message when allocation failed.
99 * If ptr equals NULL, malloc is called in stead of realloc, in this way
100 * it is possible to combine first and later allocations.
102 * extern void save_free(char *name,char *file,int line, void *ptr);
103 * Like free, uses name, file and line to generate an error message when
106 * extern size_t maxavail();
107 * Returns the maximum available allocation unit, by applying a binary
108 * search on the largest block of memory available. After allocation
109 * it invokes free to restore the original state. So it is important
110 * that free can undo the effect of a malloc.
112 * extern size_t memavail();
113 * Returns the total of available allocation unit, by applying maxavail
114 * until no space is left, it then frees all allocated space and returns
115 * the sum of the previously allocated space. As mentioned with maxavail,
116 * it is important that free can undo the effect of a malloc.
118 * extern void *save_malloc_aligned(char *name,char *file,int line,size_t size,size_t alignment);
119 * Like alloc, returns a pointer to the allocated space, uses name, file
120 * and line to generate an error message when allocation failed.
121 * The returned pointer will be n-byte aligned, where n=alignment.
122 * The pointer should only be freed with a call to save_free.
124 * extern void save_free_aligned(char *name,char *file,int line, void *ptr);
125 * Like free, uses name, file and line to generate an error message when
126 * the free failed. This function is intended to be called for
127 * pointers allocated with save_malloc_aligned, and may not work
128 * on normal pointers.
135 void *save_malloc(const char *name, const char *file, int line, size_t size);
136 void *save_calloc(const char *name, const char *file, int line,
137 size_t nelem, size_t elsize);
138 void *save_realloc(const char *name, const char *file, int line,
139 void *ptr, size_t nelem, size_t elsize);
140 void save_free(const char *name, const char *file, int line, void *ptr);
141 size_t maxavail(void);
142 size_t memavail(void);
144 /* Aligned-memory counterparts */
146 void *save_malloc_aligned(const char *name, const char *file, int line,
147 unsigned nelem, size_t elsize, size_t alignment);
148 void *save_calloc_aligned(const char *name, const char *file, int line,
149 unsigned nelem, size_t elsize, size_t alignment);
150 void save_free_aligned(const char *name, const char *file, int line, void *ptr);
155 /* Use of sizeof(T) in _snew() and _srenew() can cause obscure bugs if
156 * several files define distinct data structures with identical names and
157 * allocate memory for them using the macros below. Note that by the standard,
158 * such declarations cause undefined behavior.
159 * The C versions work fine in such cases, but when compiled with a C++
160 * compiler (and if the compiler does not inline the calls), the linker cannot
161 * tell that data structures with identical names are actually different and
162 * links calls to these template functions incorrectly, which can result in
163 * allocation of an incorrect amount of memory if the element size is computed
164 * within the function.
166 * This could be solved by passing the size as a parameter, but this has other
167 * issues: it provokes warnings from cppcheck for some invokations.
168 * Even with the size passed as a parameter, incorrect linkage will occur.
169 * When converting files to C++, locally declared structs should be enclosed in
170 * anonymous namespaces or some other means taken to ensure they are unique.
172 template <typename T> inline
173 void _snew(const char *name, const char *file, int line,
174 T * &ptr, size_t nelem)
176 ptr = (T *)save_calloc(name, file, line, nelem, sizeof(T));
178 template <typename T> inline
179 void _srenew(const char *name, const char *file, int line,
180 T * &ptr, size_t nelem)
182 ptr = (T *)save_realloc(name, file, line, ptr, nelem, sizeof(T));
184 template <typename T> inline
185 void _smalloc(const char *name, const char *file, int line, T * &ptr, size_t size)
187 ptr = (T *)save_malloc(name, file, line, size);
189 template <typename T> inline
190 void _srealloc(const char *name, const char *file, int line, T * &ptr, size_t size)
192 ptr = (T *)save_realloc(name, file, line, ptr, size, sizeof(char));
194 template <typename T> inline
195 void _snew_aligned(const char *name, const char *file, int line,
196 T * &ptr, size_t nelem, size_t alignment)
198 ptr = (T *)save_calloc_aligned(name, file, line, nelem, sizeof(T), alignment);
201 #define snew(ptr, nelem) _snew(#ptr, __FILE__, __LINE__, (ptr), (nelem))
202 #define srenew(ptr, nelem) _srenew(#ptr, __FILE__, __LINE__, (ptr), (nelem))
203 #define smalloc(ptr, size) _smalloc(#ptr, __FILE__, __LINE__, (ptr), (size))
204 #define srealloc(ptr, size) _srealloc(#ptr, __FILE__, __LINE__, (ptr), (size))
205 #define snew_aligned(ptr, nelem, alignment) _snew_aligned(#ptr, __FILE__, __LINE__, (ptr), (nelem), alignment)
207 #else /* __cplusplus */
209 /* These macros work in C, not in C++ */
210 #define snew(ptr, nelem) (ptr) = save_calloc(#ptr, __FILE__, __LINE__, \
211 (nelem), sizeof(*(ptr)))
212 #define srenew(ptr, nelem) (ptr) = save_realloc(#ptr, __FILE__, __LINE__, \
213 (ptr), (nelem), sizeof(*(ptr)))
214 #define smalloc(ptr, size) (ptr) = save_malloc(#ptr, __FILE__, __LINE__, size)
215 #define scalloc(ptr, nelem, elsize) \
216 (ptr) = save_calloc(#ptr, __FILE__, __LINE__, nelem, elsize)
217 #define srealloc(ptr, size) (ptr) = save_realloc(#ptr, __FILE__, __LINE__, \
219 #define snew_aligned(ptr, nelem, alignment) (ptr) = save_calloc_aligned(#ptr, __FILE__, __LINE__, (nelem), sizeof(*(ptr)), alignment)
220 #endif /* __cplusplus */
222 #define sfree(ptr) save_free(#ptr, __FILE__, __LINE__, (ptr))
224 /* call this ONLY with a pointer obtained through snew_aligned or
226 #define sfree_aligned(ptr) save_free_aligned(#ptr, __FILE__, __LINE__, (ptr))
230 #include "../utility/common.h"
236 * Stripped-down version of scoped_ptr that uses sfree().
238 * Currently only implements constructor from a pointer value and destructor;
239 * other operations can be added if they become necessary.
241 * This is currently in smalloc.h, as this header also declares sfree().
242 * If more flexible guards/smart pointers are needed for C pointers, this class
243 * should be moved to a separate header under src/gromacs/utility/ together
244 * with that more flexible implementation.
245 * Currently, boost::shared_ptr is used in a few locations, but is not suitable
246 * for all cases. A scoped_ptr with deleter support would be a general enough
247 * implementation for all uses. C++11 unique_ptr has this, but for non-C++11
248 * suppoer we need something else.
250 * Methods in this class do not throw.
252 class scoped_ptr_sfree
256 * Initializes a scoped_ptr that frees \p ptr on scope exit.
258 * \param[in] ptr Pointer to use for initialization.
260 explicit scoped_ptr_sfree(void *ptr) : ptr_(ptr) {}
261 //! Frees the pointer passed to the constructor.
262 ~scoped_ptr_sfree() { sfree(ptr_); }
267 GMX_DISALLOW_COPY_AND_ASSIGN(scoped_ptr_sfree);
271 #endif /* __cplusplus */
273 #endif /* _smalloc_h */