bfb5e12ef876a7dba78f3c9efaab8b3b44493c0f
[alexxy/gromacs.git] / include / smalloc.h
1 /*
2  * $Id$
3  * 
4  *       This source code is part of
5  * 
6  *        G   R   O   M   A   C   S
7  * 
8  * GROningen MAchine for Chemical Simulations
9  * 
10  *               VERSION 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Green Red Orange Magenta Azure Cyan Skyblue
28  */
29
30 #ifndef _smalloc_h
31 #define _smalloc_h
32
33 static char *SRCID_smalloc_h = "$Id$";
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifdef HAVE_IDENT
40 #ident  "@(#) smalloc.h 1.14 11/23/92"
41 #endif /* HAVE_IDENT */
42
43 /*
44  * Memory allocation routines in gromacs:
45  *
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.
49  *
50  * Macro's which can be used:
51  *
52  * snew(ptr,nelem)
53  *    Allocates memory for nelem elements and returns this in ptr.
54  *    The allocated memory is initialized to zeros.
55  *
56  * srenew(ptr,nelem)
57  *    Reallocates memory for nelem elements and returns this in ptr.
58  *
59  * smalloc(ptr,size)
60  *    Allocates memory for size bytes and returns this in ptr.
61  *
62  * scalloc(ptr,nelem,elsize)
63  *    Allocates memory for nelem elements of size elsize and returns 
64  *    this in ptr.
65  *
66  * srealloc(ptr,size)
67  *    Reallocates memory for size bytes and returns this in ptr.
68  *
69  * sfree(ptr)
70  *    Frees memory referenced by ptr.
71  *
72  ****************************************************************************
73  *
74  * Functions which are used by the macro's:
75  *
76  * extern void *save_malloc(char *name,char *file,int line,int size);
77  *    Like alloc, returns a pointer to the allocated space, uses name, file
78  *    and line to generate an error message when allocation failed.
79  *
80  * extern void *save_calloc(char *name,char *file,int line, 
81  *                          unsigned nelem,unsigned elsize);
82  *    Like calloc, returns a pointer to the allocated space, uses name, file
83  *    and line to generate an error message when allocation failed.
84  *
85  * extern void *save_realloc(char *name,char *file,int line,
86  *                           void *ptr,unsigned size);
87  *    Like realloc, returns a pointer to the allocated space, uses name, file
88  *    and line to generate an error message when allocation failed.
89  *    If ptr equals NULL, malloc is called in stead of realloc, in this way
90  *    it is possible to combine first and later allocations.
91  *
92  * extern void save_free(char *name,char *file,int line,void *ptr);
93  *    Like free, uses name, file and line to generate an error message when 
94  *    the free failed.
95  *
96  * extern unsigned maxavail();
97  *    Returns the maximum available allocation unit, by applying a binary
98  *    search on the largest block of memory available. After allocation
99  *    it invokes free to restore the original state. So it is important
100  *    that free can undo the effect of a malloc.
101  * 
102  * extern unsigned memavail();
103  *    Returns the total of available allocation unit, by applying maxavail
104  *    until no space is left, it then frees all allocated space and returns
105  *    the sum of the previously allocated space. As mentioned with maxavail,
106  *    it is important that free can undo the effect of a malloc.
107  * 
108  */
109
110 #define snew(ptr,nelem) (ptr)=save_calloc(#ptr,__FILE__,__LINE__,\
111                         (nelem),sizeof(*(ptr)))
112 #define srenew(ptr,nelem) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
113                         (ptr),(nelem)*sizeof(*(ptr)))
114 #define smalloc(ptr,size) (ptr)=save_malloc(#ptr,__FILE__,__LINE__,size)
115 #define scalloc(ptr,nelem,elsize)\
116                 (ptr)=save_calloc(#ptr,__FILE__,__LINE__,nelem,elsize)
117 #define srealloc(ptr,size) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
118                         (ptr),size)
119 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
120
121 #ifdef CPLUSPLUS 
122 extern "C" { 
123 #endif
124
125 void *save_malloc(char *name,char *file,int line,int size); 
126 void *save_calloc(char *name,char *file,int line,
127                   unsigned nelem,unsigned elsize); 
128 void *save_realloc(char *name,char *file,int line,
129                    void *ptr,unsigned size);
130 void save_free(char *name,char *file,int line,void *ptr);
131 unsigned maxavail(void);
132 unsigned memavail(void);
133
134 #ifdef CPLUSPLUS
135 }
136 #endif
137
138 #endif  /* _smalloc_h */