rsl_malloc.c

References to this file elsewhere.
1 /***********************************************************************
2      
3                               COPYRIGHT
4      
5      The following is a notice of limited availability of the code and 
6      Government license and disclaimer which must be included in the 
7      prologue of the code and in all source listings of the code.
8      
9      Copyright notice
10        © 1977  University of Chicago
11      
12      Permission is hereby granted to use, reproduce, prepare 
13      derivative works, and to redistribute to others at no charge.  If 
14      you distribute a copy or copies of the Software, or you modify a 
15      copy or copies of the Software or any portion of it, thus forming 
16      a work based on the Software and make and/or distribute copies of 
17      such work, you must meet the following conditions:
18      
19           a) If you make a copy of the Software (modified or verbatim) 
20              it must include the copyright notice and Government       
21              license and disclaimer.
22      
23           b) You must cause the modified Software to carry prominent   
24              notices stating that you changed specified portions of    
25              the Software.
26      
27      This software was authored by:
28      
29      Argonne National Laboratory
30      J. Michalakes: (630) 252-6646; email: michalak@mcs.anl.gov
31      Mathematics and Computer Science Division
32      Argonne National Laboratory, Argonne, IL  60439
33      
34      ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES 
35      OF ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT, 
36      AND OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A 
37      CONTRACT WITH THE DEPARTMENT OF ENERGY.
38      
39                       GOVERNMENT LICENSE AND DISCLAIMER
40      
41      This computer code material was prepared, in part, as an account 
42      of work sponsored by an agency of the United States Government.
43      The Government is granted for itself and others acting on its 
44      behalf a paid-up, nonexclusive, irrevocable worldwide license in 
45      this data to reproduce, prepare derivative works, distribute 
46      copies to the public, perform publicly and display publicly, and 
47      to permit others to do so.  NEITHER THE UNITED STATES GOVERNMENT 
48      NOR ANY AGENCY THEREOF, NOR THE UNIVERSITY OF CHICAGO, NOR ANY OF 
49      THEIR EMPLOYEES, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR 
50      ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY, 
51      COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, APPARATUS, 
52      PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD 
53      NOT INFRINGE PRIVATELY OWNED RIGHTS.
54 
55 ***************************************************************************/
56 
57 #if 0
58 #define PADIT		/* add page at beginning and end of allocation */
59 #endif
60 #if 0
61 # define BASE_MALLOC   fence_malloc
62 # define BASE_FREE     fence_free
63 #else
64 # define BASE_MALLOC   malloc
65 # define BASE_FREE     free
66 #endif
67 
68 #ifndef MS_SUA
69 # include <stdio.h>
70 #endif
71 #include <stdlib.h>
72 #ifndef MACOS
73 # include <malloc.h>
74 #else
75 # include <malloc/malloc.h>
76 #endif
77 #ifdef T3D
78 #include <errno.h>
79 #endif
80 #ifndef STUBMPI
81 # include "mpi.h"
82 #endif
83 #include "rsl_lite.h"
84 
85 /*
86 extern int EF_ALIGNMENT;
87 extern int EF_PROTECT_BELOW;
88 extern int EF_PROTECT_FREE;
89 */
90 
91 /* define STUG to enable tracking of allocs and frees (performance and space penalty) */
92 #ifdef STUG
93 #define MAXSTUG  1000000
94 struct stugtype {
95    char *  ddr ;
96    int     sz  ;
97 } stug[MAXSTUG] ;
98 static int stugfirst =1 ;
99 int outy = 0 ;
100 int nouty = 0 ;
101 int maxstug = 0 ;
102 int maxouty = 0 ;
103 int bbb ;
104 #endif
105 
106 
107 static char zero_length_storage[] = "" ;
108 
109 #if !(defined(vpp) || defined(vpp2) || defined(SUN) || defined(XT3_Catamount) || defined(crayx1) || defined(MACOS) || defined(MS_SUA) )
110 static struct mallinfo minf ;
111 #endif
112 
113 static char *last_f ;
114 static int last_l ;
115 static int last_s ;
116 
117 #ifdef O2K
118 static struct mallinfo mallinfo() {} ;
119 #endif
120 
121 void * rsl_malloc(f,l,s)
122    char * f ;
123    int l, s ;
124 {
125    char mess[128] ;
126    void *retval ;
127    int s2, tries ;
128 
129 /*
130 EF_PROTECT_BELOW = 0 ;
131 EF_PROTECT_FREE = 1 ;
132 */
133 
134 #ifdef STUG
135    if ( stugfirst == 1 ) {
136       stugfirst = 0 ;
137       for ( bbb = 0 ; bbb < MAXSTUG ; bbb++ ) {
138          stug[bbb].ddr = 0L ;
139          stug[bbb].sz  = 0 ;
140       }
141    }
142 #endif
143 
144    if ( s == 0 )
145    {
146      retval = (void *) zero_length_storage ;
147    }
148    else 
149    {
150 #ifdef PADIT
151      s2 = s + 1024 ;
152 #else
153      s2 = s ;
154 #endif
155      tries = 0 ;
156      while ((retval=(void *)BASE_MALLOC(s2))==(void *)NULL)
157      {
158        tries++ ;
159        sprintf(mess,
160 "rsl_malloc failed allocating %d bytes, called %s, line %d, try %d\n",
161        s,f,l,tries) ;
162        perror(mess) ;
163 #if !(defined(vpp) || defined(vpp2) || defined(SUN) || defined(XT3_Catamount) || defined(crayx1) || defined(MACOS) || defined(MS_SUA) )
164        minf = mallinfo() ;
165        fprintf(stderr,"mallinfo: arena %d\n",minf.arena)  ;
166        fprintf(stderr,"mallinfo: ordblks %d\n",minf.ordblks)  ;
167        fprintf(stderr,"mallinfo: smblks %d\n",minf.smblks)  ;
168        fprintf(stderr,"mallinfo: hblks %d\n",minf.hblks)  ;
169        fprintf(stderr,"mallinfo: hblkhd %d\n",minf.hblkhd)  ;
170        fprintf(stderr,"mallinfo: usmblks %d\n",minf.usmblks)  ;
171        fprintf(stderr,"mallinfo: fsmblks %d\n",minf.fsmblks)  ;
172        fprintf(stderr,"mallinfo: uordblks %d\n",minf.uordblks)  ;
173        fprintf(stderr,"mallinfo: fordblks %d\n",minf.fordblks)  ;
174        fprintf(stderr,"mallinfo: keepcost %d\n",minf.keepcost)  ;
175 #ifdef SUNINFO
176        fprintf(stderr,"mallinfo: mkfast %d\n",minf.mkfast)  ;
177        fprintf(stderr,"mallinfo: nblks %d\n",minf.nblks)  ;
178        fprintf(stderr,"mallinfo: grain %d\n",minf.grain)  ;
179        fprintf(stderr,"mallinfo: uordbytes %d\n",minf.uordbytes)  ;
180        fprintf(stderr,"mallinfo: allocated %d\n",minf.allocated)  ;
181        fprintf(stderr,"mallinfo: treeoverhead %d\n",minf.treeoverhead)  ;
182 #endif
183 #endif
184        if ( tries >= 2 )
185        { 
186 	 system("lsps -a") ;
187 #ifndef MS_SUA
188 	 sleep(1) ;
189 #endif
190        }
191        if ( tries >= 3 ) 
192        {
193 	 system("lsps -a") ;
194 	 RSL_FATAL(2) ;
195        }
196      }
197    }
198 #if !(defined(vpp)||defined(vpp2)) || defined(sx) || defined(alphavector)
199    if ( s > 0 )
200      bzero( retval, s2 ) ;	/* return zero'd storage always */
201 #else
202    if ( s > 0 )
203    { int l, lb ;
204      l = s2/sizeof(int) ;
205      lb = l*sizeof(int) ;
206      vizero_( retval, &l  ) ;
207      l = s2-lb ;
208      vbzero_( retval+lb, &l ) ;	/* return zero'd storage always */
209    }
210 #endif
211 
212 #ifdef PADIT
213    retval = retval + 512 ;
214 #endif
215 
216 #ifdef STUG
217 for ( bbb = 0 ; bbb < MAXSTUG ; bbb++ )
218 {
219    if ( stug[bbb].ddr == 0 ) break ;
220 }
221 if ( bbb < MAXSTUG ) {
222    stug[bbb].ddr = retval ;
223    stug[bbb].sz  = s ;
224    outy += stug[bbb].sz ;
225 /* fprintf(stderr,"+ %10d.  %08x   %10d  %10d\n", bbb, stug[bbb].ddr, stug[bbb].sz, outy ) ; */
226    nouty ++ ;
227    if ( nouty > maxstug ) maxstug = nouty ;
228    if ( outy > maxouty ) maxouty = outy ;
229 }else{
230 #ifndef MS_SUA
231 fprintf(stderr,"stug full %d\n",bbb) ;
232 #endif
233 RSL_FATAL(2) ;
234 }
235 #endif
236    return(retval) ;
237 }
238 
239 rsl_free( p )
240    char * p ;
241 {
242    if ( p == zero_length_storage ) return ;    /* fix from ANU */
243 
244 #ifdef STUG
245 for ( bbb = 0 ; bbb < MAXSTUG ; bbb++ ) 
246 {
247    if ( stug[bbb].ddr == p ) {
248       outy -= stug[bbb].sz ;
249 /* fprintf(stderr,"- %10d.  %08x   %10d  %10d\n", bbb, stug[bbb].ddr, stug[bbb].sz, outy ) ; */
250       nouty -- ;
251       stug[bbb].ddr = 0L ;
252       break ;
253    }
254 }
255 #endif
256 
257 #ifdef PADIT
258    BASE_FREE ( p-512 ) ;
259 #else
260    BASE_FREE ( p ) ;
261 #endif
262    p = NULL ;
263 }
264 
265 #ifdef MS_SUA
266 bzero( char *buf, int l )
267 {
268    int i ;
269    char * p ;
270    for ( p = buf, i=0 ; i < l ; i++ ) *p = '\0' ;
271 }
272 #endif
273