4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
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)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Green Red Orange Magenta Azure Cyan Skyblue
33 static char *SRCID_smalloc_h = "$Id$";
40 #ident "@(#) smalloc.h 1.14 11/23/92"
41 #endif /* HAVE_IDENT */
44 * Memory allocation routines in gromacs:
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.
50 * Macro's which can be used:
53 * Allocates memory for nelem elements and returns this in ptr.
54 * The allocated memory is initialized to zeros.
57 * Reallocates memory for nelem elements and returns this in ptr.
60 * Allocates memory for size bytes and returns this in ptr.
62 * scalloc(ptr,nelem,elsize)
63 * Allocates memory for nelem elements of size elsize and returns
67 * Reallocates memory for size bytes and returns this in ptr.
70 * Frees memory referenced by ptr.
72 ****************************************************************************
74 * Functions which are used by the macro's:
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.
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.
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.
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
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.
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.
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__,\
119 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
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);
138 #endif /* _smalloc_h */