Merge branch 'master' of git@git.gromacs.org:gromacs
[gromacs/rigid-bodies.git] / include / smalloc.h
blob1cce2353114d841450e567a212d1faec5a82a817
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
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
32 * And Hey:
33 * Gromacs Runs On Most of All Computer Systems
36 #ifndef _smalloc_h
37 #define _smalloc_h
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
43 #include <stdlib.h>
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:
54 * snew(ptr,nelem)
55 * Allocates memory for nelem elements and returns this in ptr.
56 * The allocated memory is initialized to zeros.
58 * srenew(ptr,nelem)
59 * Reallocates memory for nelem elements and returns this in ptr.
61 * smalloc(ptr,size)
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
66 * this in ptr.
68 * srealloc(ptr,size)
69 * Reallocates memory for size bytes and returns this in ptr.
71 * sfree(ptr)
72 * Frees memory referenced by ptr.
74 ****************************************************************************
76 * Functions which are used by the macro's:
78 * extern void *save_malloc(char *name,char *file,int line,int size);
79 * Like alloc, returns a pointer to the allocated space, uses name, file
80 * and line to generate an error message when allocation failed.
82 * extern void *save_calloc(char *name,char *file,int line,
83 * size_t nelem,size_t elsize);
84 * Like calloc, returns a pointer to the allocated space, uses name, file
85 * and line to generate an error message when allocation failed.
87 * extern void *save_realloc(char *name,char *file,int line,
88 * void *ptr,size_t size);
89 * Like realloc, returns a pointer to the allocated space, uses name, file
90 * and line to generate an error message when allocation failed.
91 * If ptr equals NULL, malloc is called in stead of realloc, in this way
92 * it is possible to combine first and later allocations.
94 * extern void save_free(char *name,char *file,int line, void *ptr);
95 * Like free, uses name, file and line to generate an error message when
96 * the free failed.
98 * extern size_t maxavail();
99 * Returns the maximum available allocation unit, by applying a binary
100 * search on the largest block of memory available. After allocation
101 * it invokes free to restore the original state. So it is important
102 * that free can undo the effect of a malloc.
104 * extern size_t memavail();
105 * Returns the total of available allocation unit, by applying maxavail
106 * until no space is left, it then frees all allocated space and returns
107 * the sum of the previously allocated space. As mentioned with maxavail,
108 * it is important that free can undo the effect of a malloc.
112 #ifdef __cplusplus
113 extern "C" {
114 #endif
116 void *save_malloc(const char *name,const char *file,int line,size_t size);
117 void *save_calloc(const char *name,const char *file,int line,
118 size_t nelem,size_t elsize);
119 void *save_realloc(const char *name,const char *file,int line,
120 void *ptr,size_t nelem,size_t elsize);
121 void save_free(const char *name,const char *file,int line, void *ptr);
122 size_t maxavail(void);
123 size_t memavail(void);
125 #ifdef __cplusplus
128 /* Use of sizeof(T) in _snew() and _srenew() can cause obscure bugs if
129 * several files define distinct data structures with identical names and
130 * allocate memory for them using the macros below.
131 * For this reason, the size of an element is passed as a parameter.
133 * The C versions work fine in such cases, but when compiled with a C++
134 * compiler (and if the compiler does not inline the calls), the linker cannot
135 * tell that data structures with identical names are actually different and
136 * links calls to these template functions incorrectly, which can result in
137 * allocation of an incorrect amount of memory if the element size is computed
138 * within the function. Even with the size passed as a parameter, incorrect
139 * linkage will occur, but as the type is now only present in the cast, it
140 * should not cause problems.
142 template <typename T>
143 void _snew(const char *name, const char *file, int line,
144 T *&ptr, size_t nelem, size_t elsize)
146 ptr = (T *)save_calloc(name, file, line, nelem, elsize);
148 template <typename T>
149 void _srenew(const char *name, const char *file, int line,
150 T *&ptr, size_t nelem, size_t elsize)
152 ptr = (T *)save_realloc(name, file, line, ptr, nelem, elsize);
154 template <typename T>
155 void _smalloc(const char *name, const char *file, int line, T *&ptr, size_t size)
157 ptr = (T *)save_malloc(name, file, line, size);
159 template <typename T>
160 void _srealloc(const char *name, const char *file, int line, T *&ptr, size_t size)
162 ptr = (T *)save_realloc(name, file, line, ptr, size, sizeof(char));
165 #define snew(ptr,nelem) _snew(#ptr,__FILE__,__LINE__,(ptr),(nelem),sizeof(*(ptr)))
166 #define srenew(ptr,nelem) _srenew(#ptr,__FILE__,__LINE__,(ptr),(nelem),sizeof(*(ptr)))
167 #define smalloc(ptr, size) _smalloc(#ptr,__FILE__,__LINE__,(ptr),(size))
168 #define srealloc(ptr, size) _srealloc(#ptr,__FILE__,__LINE__,(ptr),(size))
170 #else
172 /* These macros work in C, not in C++ */
173 #define snew(ptr,nelem) (ptr)=save_calloc(#ptr,__FILE__,__LINE__,\
174 (nelem),sizeof(*(ptr)))
175 #define srenew(ptr,nelem) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
176 (ptr),(nelem),sizeof(*(ptr)))
177 #define smalloc(ptr,size) (ptr)=save_malloc(#ptr,__FILE__,__LINE__,size)
178 #define scalloc(ptr,nelem,elsize)\
179 (ptr)=save_calloc(#ptr,__FILE__,__LINE__,nelem,elsize)
180 #define srealloc(ptr,size) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
181 (ptr),size,1)
182 #endif
184 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
186 #endif /* _smalloc_h */