changed reading hint
[gromacs/adressmacs.git] / include / smalloc.h
blobd5b9f845a0d7e376e905bfa8aadc04a5f4a84863
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
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
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)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Green Red Orange Magenta Azure Cyan Skyblue
30 #ifndef _smalloc_h
31 #define _smalloc_h
33 static char *SRCID_smalloc_h = "$Id$";
35 #ifdef HAVE_IDENT
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:
48 * snew(ptr,nelem)
49 * Allocates memory for nelem elements and returns this in ptr.
50 * The allocated memory is initialized to zeros.
52 * srenew(ptr,nelem)
53 * Reallocates memory for nelem elements and returns this in ptr.
55 * smalloc(ptr,size)
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
60 * this in ptr.
62 * srealloc(ptr,size)
63 * Reallocates memory for size bytes and returns this in ptr.
65 * sfree(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
90 * the free failed.
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__,\
114 (ptr),size)
115 #define sfree(ptr) save_free(#ptr,__FILE__,__LINE__,(ptr))
117 #ifdef CPLUSPLUS
118 extern "C" {
119 #endif
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);
130 #ifdef CPLUSPLUS
132 #endif
134 #endif /* _smalloc_h */