3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Gromacs Runs On Most of All Computer Systems
46 * Memory allocation routines in gromacs:
48 * If an allocation fails, the program is halted by means of the
49 * fatal_error routine, which outputs source file and line number
50 * and the name of the variable involved.
52 * Macro's which can be used:
55 * Allocates memory for nelem elements and returns this in ptr.
56 * The allocated memory is initialized to zeros.
59 * Reallocates memory for nelem elements and returns this in ptr.
62 * Allocates memory for size bytes and returns this in ptr.
64 * scalloc(ptr,nelem,elsize)
65 * Allocates memory for nelem elements of size elsize and returns
69 * Reallocates memory for size bytes and returns this in ptr.
72 * Frees memory referenced by ptr.
74 * snew_aligned(ptr,nelem,alignment)
75 * Allocates memory for nelem elements and returns this in ptr.
76 * The allocated memory is initialized to zeroes.
77 * alignment=n will constrain ptr to be n-byte aligned.
78 * This pointer should only be freed with sfree_aligned, since
79 * it may not be the value returned by the underlying malloc.
82 * Frees aligned memory referenced by ptr.
84 ****************************************************************************
86 * Functions which are used by the macro's:
88 * extern void *save_malloc(char *name,char *file,int line,int size);
89 * Like alloc, returns a pointer to the allocated space, uses name, file
90 * and line to generate an error message when allocation failed.
92 * extern void *save_calloc(char *name,char *file,int line,
93 * size_t nelem,size_t elsize);
94 * Like calloc, returns a pointer to the allocated space, uses name, file
95 * and line to generate an error message when allocation failed.
97 * extern void *save_realloc(char *name,char *file,int line,
98 * void *ptr,size_t size);
99 * Like realloc, returns a pointer to the allocated space, uses name, file
100 * and line to generate an error message when allocation failed.
101 * If ptr equals NULL, malloc is called in stead of realloc, in this way
102 * it is possible to combine first and later allocations.
104 * extern void save_free(char *name,char *file,int line, void *ptr);
105 * Like free, uses name, file and line to generate an error message when
108 * extern size_t maxavail();
109 * Returns the maximum available allocation unit, by applying a binary
110 * search on the largest block of memory available. After allocation
111 * it invokes free to restore the original state. So it is important
112 * that free can undo the effect of a malloc.
114 * extern size_t memavail();
115 * Returns the total of available allocation unit, by applying maxavail
116 * until no space is left, it then frees all allocated space and returns
117 * the sum of the previously allocated space. As mentioned with maxavail,
118 * it is important that free can undo the effect of a malloc.
120 * extern void *save_malloc_aligned(char *name,char *file,int line,size_t size,size_t alignment);
121 * Like alloc, returns a pointer to the allocated space, uses name, file
122 * and line to generate an error message when allocation failed.
123 * The returned pointer will be n-byte aligned, where n=alignment.
124 * The pointer should only be freed with a call to save_free.
126 * extern void save_free_aligned(char *name,char *file,int line, void *ptr);
127 * Like free, uses name, file and line to generate an error message when
128 * the free failed. This function is intended to be called for
129 * pointers allocated with save_malloc_aligned, and may not work
130 * on normal pointers.
137 void *save_malloc(const char *name
,const char *file
,int line
,size_t size
);
138 void *save_calloc(const char *name
,const char *file
,int line
,
139 size_t nelem
,size_t elsize
);
140 void *save_realloc(const char *name
,const char *file
,int line
,
141 void *ptr
,size_t nelem
,size_t elsize
);
142 void save_free(const char *name
,const char *file
,int line
, void *ptr
);
143 size_t maxavail(void);
144 size_t memavail(void);
146 /* Aligned-memory counterparts */
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.
158 * For this reason, the size of an element is passed as a parameter.
160 * The C versions work fine in such cases, but when compiled with a C++
161 * compiler (and if the compiler does not inline the calls), the linker cannot
162 * tell that data structures with identical names are actually different and
163 * links calls to these template functions incorrectly, which can result in
164 * allocation of an incorrect amount of memory if the element size is computed
165 * within the function. Even with the size passed as a parameter, incorrect
166 * linkage will occur, but as the type is now only present in the cast, it
167 * should not cause problems.
169 template <typename T
>
170 void _snew(const char *name
, const char *file
, int line
,
171 T
*&ptr
, size_t nelem
, size_t elsize
)
173 ptr
= (T
*)save_calloc(name
, file
, line
, nelem
, elsize
);
175 template <typename T
>
176 void _srenew(const char *name
, const char *file
, int line
,
177 T
*&ptr
, size_t nelem
, size_t elsize
)
179 ptr
= (T
*)save_realloc(name
, file
, line
, ptr
, nelem
, elsize
);
181 template <typename T
>
182 void _smalloc(const char *name
, const char *file
, int line
, T
*&ptr
, size_t size
)
184 ptr
= (T
*)save_malloc(name
, file
, line
, size
);
186 template <typename T
>
187 void _srealloc(const char *name
, const char *file
, int line
, T
*&ptr
, size_t size
)
189 ptr
= (T
*)save_realloc(name
, file
, line
, ptr
, size
, sizeof(char));
191 template <typename T
>
192 void _snew_aligned(const char *name
, const char *file
, int line
,
193 T
*&ptr
, size_t nelem
, size_t elsize
,size_t alignment
)
195 ptr
= (T
*)save_calloc_aligned(name
, file
, line
, nelem
, elsize
, alignment
);
198 #define snew(ptr,nelem) _snew(#ptr,__FILE__,__LINE__,(ptr),(nelem),sizeof(*(ptr)))
199 #define srenew(ptr,nelem) _srenew(#ptr,__FILE__,__LINE__,(ptr),(nelem),sizeof(*(ptr)))
200 #define smalloc(ptr, size) _smalloc(#ptr,__FILE__,__LINE__,(ptr),(size))
201 #define srealloc(ptr, size) _srealloc(#ptr,__FILE__,__LINE__,(ptr),(size))
202 #define snew_aligned(ptr,nelem,alignment) _snew_aligned(#ptr,__FILE__,__LINE__,(ptr),(nelem),sizeof(*(ptr)),alignment)
206 /* These macros work in C, not in C++ */
207 #define snew(ptr,nelem) (ptr)=save_calloc(#ptr,__FILE__,__LINE__,\
208 (nelem),sizeof(*(ptr)))
209 #define srenew(ptr,nelem) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
210 (ptr),(nelem),sizeof(*(ptr)))
211 #define smalloc(ptr,size) (ptr)=save_malloc(#ptr,__FILE__,__LINE__,size)
212 #define scalloc(ptr,nelem,elsize)\
213 (ptr)=save_calloc(#ptr,__FILE__,__LINE__,nelem,elsize)
214 #define srealloc(ptr,size) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
216 #define snew_aligned(ptr,nelem,alignment) (ptr)=save_calloc_aligned(#ptr,__FILE__,__LINE__,(nelem),sizeof(*(ptr)),alignment)
219 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
221 /* call this ONLY with a pointer obtained through snew_aligned or
223 #define sfree_aligned(ptr) save_free_aligned(#ptr,__FILE__,__LINE__,(ptr))
225 #endif /* _smalloc_h */