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$";
36 #ident "@(#) smalloc.h 1.14 11/23/92"
37 #endif /* HAVE_IDENT */
40 * Memory allocation routines in gromacs:
42 * If an allocation fails, the program is halted by means of the
43 * fatal_error routine, which outputs source file and line number
44 * and the name of the variable involved.
46 * Macro's which can be used:
49 * Allocates memory for nelem elements and returns this in ptr.
50 * The allocated memory is initialized to zeros.
53 * Reallocates memory for nelem elements and returns this in ptr.
56 * Allocates memory for size bytes and returns this in ptr.
58 * scalloc(ptr,nelem,elsize)
59 * Allocates memory for nelem elements of size elsize and returns
63 * Reallocates memory for size bytes and returns this in ptr.
66 * Frees memory referenced by ptr.
68 ****************************************************************************
70 * Functions which are used by the macro's:
72 * extern void *save_malloc(char *name,char *file,int line,int size);
73 * Like alloc, returns a pointer to the allocated space, uses name, file
74 * and line to generate an error message when allocation failed.
76 * extern void *save_calloc(char *name,char *file,int line,
77 * unsigned nelem,unsigned elsize);
78 * Like calloc, returns a pointer to the allocated space, uses name, file
79 * and line to generate an error message when allocation failed.
81 * extern void *save_realloc(char *name,char *file,int line,
82 * void *ptr,unsigned size);
83 * Like realloc, returns a pointer to the allocated space, uses name, file
84 * and line to generate an error message when allocation failed.
85 * If ptr equals NULL, malloc is called in stead of realloc, in this way
86 * it is possible to combine first and later allocations.
88 * extern void save_free(char *name,char *file,int line,void *ptr);
89 * Like free, uses name, file and line to generate an error message when
92 * extern unsigned maxavail();
93 * Returns the maximum available allocation unit, by applying a binary
94 * search on the largest block of memory available. After allocation
95 * it invokes free to restore the original state. So it is important
96 * that free can undo the effect of a malloc.
98 * extern unsigned memavail();
99 * Returns the total of available allocation unit, by applying maxavail
100 * until no space is left, it then frees all allocated space and returns
101 * the sum of the previously allocated space. As mentioned with maxavail,
102 * it is important that free can undo the effect of a malloc.
106 #define snew(ptr,nelem) (ptr)=save_calloc(#ptr,__FILE__,__LINE__,\
107 (nelem),sizeof(*(ptr)))
108 #define srenew(ptr,nelem) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
109 (ptr),(nelem)*sizeof(*(ptr)))
110 #define smalloc(ptr,size) (ptr)=save_malloc(#ptr,__FILE__,__LINE__,size)
111 #define scalloc(ptr,nelem,elsize)\
112 (ptr)=save_calloc(#ptr,__FILE__,__LINE__,nelem,elsize)
113 #define srealloc(ptr,size) (ptr)=save_realloc(#ptr,__FILE__,__LINE__,\
115 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
121 void *save_malloc(char *name
,char *file
,int line
,int size
);
122 void *save_calloc(char *name
,char *file
,int line
,
123 unsigned nelem
,unsigned elsize
);
124 void *save_realloc(char *name
,char *file
,int line
,
125 void *ptr
,unsigned size
);
126 void save_free(char *name
,char *file
,int line
,void *ptr
);
127 unsigned maxavail(void);
128 unsigned memavail(void);
134 #endif /* _smalloc_h */