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 ****************************************************************************
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
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.
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);
128 template <typename T
>
129 void _snew(const char *name
, T
*&ptr
, int nelem
)
131 ptr
= (T
*)save_calloc(name
, __FILE__
, __LINE__
, nelem
, sizeof(T
));
133 template <typename T
>
134 void _srenew(const char *name
, T
*&ptr
, int nelem
)
136 ptr
= (T
*)save_realloc(name
, __FILE__
, __LINE__
, ptr
, nelem
, sizeof(T
));
138 template <typename T
>
139 void _smalloc(const char *name
, T
*&ptr
, size_t size
)
141 ptr
= (T
*)save_malloc(name
, __FILE__
, __LINE__
, size
);
143 template <typename T
>
144 void _srealloc(const char *name
, T
*&ptr
, size_t size
)
146 ptr
= (T
*)save_realloc(name
, __FILE__
, __LINE__
, ptr
, size
, sizeof(char));
151 #define snew(ptr,nelem) _snew(#ptr,(ptr),(nelem))
152 #define srenew(ptr,nelem) _srenew(#ptr,(ptr),(nelem))
153 #define smalloc(ptr, size) _smalloc(#ptr,(ptr),(size))
154 #define srealloc(ptr, size) _srealloc(#ptr,(ptr),(size))
158 /* These macros work in C, not in C++ */
159 #define snew(ptr,nelem) (ptr)=save_calloc(#ptr,__FILE__,__LINE__,\
160 (nelem),sizeof(*(ptr)))
161 #define srenew(ptr,nelem) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
162 (ptr),(nelem),sizeof(*(ptr)))
163 #define smalloc(ptr,size) (ptr)=save_malloc(#ptr,__FILE__,__LINE__,size)
164 #define scalloc(ptr,nelem,elsize)\
165 (ptr)=save_calloc(#ptr,__FILE__,__LINE__,nelem,elsize)
166 #define srealloc(ptr,size) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
170 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
172 #endif /* _smalloc_h */