4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-2001
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
34 * Good ROcking Metal Altar for Chronical Sinners
40 static char *SRCID_smalloc_h = "$Id$";
46 #ident "@(#) smalloc.h 1.14 11/23/92"
47 #endif /* HAVE_IDENT */
50 * Memory allocation routines in gromacs:
52 * If an allocation fails, the program is halted by means of the
53 * fatal_error routine, which outputs source file and line number
54 * and the name of the variable involved.
56 * Macro's which can be used:
59 * Allocates memory for nelem elements and returns this in ptr.
60 * The allocated memory is initialized to zeros.
63 * Reallocates memory for nelem elements and returns this in ptr.
66 * Allocates memory for size bytes and returns this in ptr.
68 * scalloc(ptr,nelem,elsize)
69 * Allocates memory for nelem elements of size elsize and returns
73 * Reallocates memory for size bytes and returns this in ptr.
76 * Frees memory referenced by ptr.
78 ****************************************************************************
80 * Functions which are used by the macro's:
82 * extern void *save_malloc(char *name,char *file,int line,int size);
83 * Like alloc, returns a pointer to the allocated space, uses name, file
84 * and line to generate an error message when allocation failed.
86 * extern void *save_calloc(char *name,char *file,int line,
87 * unsigned nelem,unsigned elsize);
88 * Like calloc, returns a pointer to the allocated space, uses name, file
89 * and line to generate an error message when allocation failed.
91 * extern void *save_realloc(char *name,char *file,int line,
92 * void *ptr,unsigned size);
93 * Like realloc, returns a pointer to the allocated space, uses name, file
94 * and line to generate an error message when allocation failed.
95 * If ptr equals NULL, malloc is called in stead of realloc, in this way
96 * it is possible to combine first and later allocations.
98 * extern void save_free(char *name,char *file,int line,void *ptr);
99 * Like free, uses name, file and line to generate an error message when
102 * extern unsigned maxavail();
103 * Returns the maximum available allocation unit, by applying a binary
104 * search on the largest block of memory available. After allocation
105 * it invokes free to restore the original state. So it is important
106 * that free can undo the effect of a malloc.
108 * extern unsigned memavail();
109 * Returns the total of available allocation unit, by applying maxavail
110 * until no space is left, it then frees all allocated space and returns
111 * the sum of the previously allocated space. As mentioned with maxavail,
112 * it is important that free can undo the effect of a malloc.
116 #define snew(ptr,nelem) (ptr)=save_calloc(#ptr,__FILE__,__LINE__,\
117 (nelem),sizeof(*(ptr)))
118 #define srenew(ptr,nelem) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
119 (ptr),(nelem)*sizeof(*(ptr)))
120 #define smalloc(ptr,size) (ptr)=save_malloc(#ptr,__FILE__,__LINE__,size)
121 #define scalloc(ptr,nelem,elsize)\
122 (ptr)=save_calloc(#ptr,__FILE__,__LINE__,nelem,elsize)
123 #define srealloc(ptr,size) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
125 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
131 void *save_malloc(char *name,char *file,int line,int size);
132 void *save_calloc(char *name,char *file,int line,
133 unsigned nelem,unsigned elsize);
134 void *save_realloc(char *name,char *file,int line,
135 void *ptr,unsigned size);
136 void save_free(char *name,char *file,int line,void *ptr);
137 unsigned maxavail(void);
138 unsigned memavail(void);
144 #endif /* _smalloc_h */