Fix undefined behavior in RSL_LITE (#1765)
[WRF.git] / external / RSL_LITE / rsl_malloc.c
blob4a4cdd100134bac1ea94e1b503a65dd0e61c63a5
1 /***********************************************************************
3 COPYRIGHT
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.
9 Copyright notice
10 (c) 1977 University of Chicago
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:
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.
23 b) You must cause the modified Software to carry prominent
24 notices stating that you changed specified portions of
25 the Software.
27 This software was authored by:
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
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.
39 GOVERNMENT LICENSE AND DISCLAIMER
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.
55 ***************************************************************************/
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
68 #ifdef _WIN32
69 #define bzero(X,Y) memset(X,0,Y)
70 #endif
72 #ifndef MS_SUA
73 # include <stdio.h>
74 #endif
75 #include <stdlib.h>
76 #ifndef MACOS
77 # include <malloc.h>
78 #else
79 # include <malloc/malloc.h>
80 #endif
81 #ifdef T3D
82 #include <errno.h>
83 #endif
84 #ifndef STUBMPI
85 # include "mpi.h"
86 #endif
87 #include "rsl_lite.h"
90 extern int EF_ALIGNMENT;
91 extern int EF_PROTECT_BELOW;
92 extern int EF_PROTECT_FREE;
95 /* define STUG to enable tracking of allocs and frees (performance and space penalty) */
96 #ifdef STUG
97 #define MAXSTUG 1000000
98 struct stugtype {
99 char * ddr ;
100 int sz ;
101 } stug[MAXSTUG] ;
102 static int stugfirst =1 ;
103 int outy = 0 ;
104 int nouty = 0 ;
105 int maxstug = 0 ;
106 int maxouty = 0 ;
107 int bbb ;
108 #endif
111 static char zero_length_storage[] = "" ;
113 #if !(defined(vpp) || defined(vpp2) || defined(SUN) || defined(XT3_Catamount) || defined(crayx1) || defined(MACOS) || defined(MS_SUA) )
114 static struct mallinfo minf ;
115 #endif
117 static char *last_f ;
118 static int last_l ;
119 static int last_s ;
121 #ifdef O2K
122 static struct mallinfo mallinfo() {} ;
123 #endif
125 void * rsl_malloc(f,l,s)
126 char * f ;
127 int l, s ;
129 char mess[128] ;
130 void *retval ;
131 int s2, tries ;
134 EF_PROTECT_BELOW = 0 ;
135 EF_PROTECT_FREE = 1 ;
138 #ifdef STUG
139 if ( stugfirst == 1 ) {
140 stugfirst = 0 ;
141 for ( bbb = 0 ; bbb < MAXSTUG ; bbb++ ) {
142 stug[bbb].ddr = 0L ;
143 stug[bbb].sz = 0 ;
146 #endif
148 if ( s == 0 )
150 retval = (void *) zero_length_storage ;
152 else
154 #ifdef PADIT
155 s2 = s + 1024 ;
156 #else
157 s2 = s ;
158 #endif
159 tries = 0 ;
160 while ((retval=(void *)BASE_MALLOC(s2))==(void *)NULL)
162 tries++ ;
163 sprintf(mess,
164 "rsl_malloc failed allocating %d bytes, called %s, line %d, try %d\n",
165 s,f,l,tries) ;
166 perror(mess) ;
167 #if !(defined(vpp) || defined(vpp2) || defined(SUN) || defined(XT3_Catamount) || defined(crayx1) || defined(MACOS) || defined(MS_SUA) || defined(_WIN32))
168 minf = mallinfo() ;
169 fprintf(stderr,"mallinfo: arena %d\n",minf.arena) ;
170 fprintf(stderr,"mallinfo: ordblks %d\n",minf.ordblks) ;
171 fprintf(stderr,"mallinfo: smblks %d\n",minf.smblks) ;
172 fprintf(stderr,"mallinfo: hblks %d\n",minf.hblks) ;
173 fprintf(stderr,"mallinfo: hblkhd %d\n",minf.hblkhd) ;
174 fprintf(stderr,"mallinfo: usmblks %d\n",minf.usmblks) ;
175 fprintf(stderr,"mallinfo: fsmblks %d\n",minf.fsmblks) ;
176 fprintf(stderr,"mallinfo: uordblks %d\n",minf.uordblks) ;
177 fprintf(stderr,"mallinfo: fordblks %d\n",minf.fordblks) ;
178 fprintf(stderr,"mallinfo: keepcost %d\n",minf.keepcost) ;
179 #ifdef SUNINFO
180 fprintf(stderr,"mallinfo: mkfast %d\n",minf.mkfast) ;
181 fprintf(stderr,"mallinfo: nblks %d\n",minf.nblks) ;
182 fprintf(stderr,"mallinfo: grain %d\n",minf.grain) ;
183 fprintf(stderr,"mallinfo: uordbytes %d\n",minf.uordbytes) ;
184 fprintf(stderr,"mallinfo: allocated %d\n",minf.allocated) ;
185 fprintf(stderr,"mallinfo: treeoverhead %d\n",minf.treeoverhead) ;
186 #endif
187 #endif
188 if ( tries >= 2 )
190 system("lsps -a") ;
191 #if !defined (MS_SUA) && !defined(_WIN32)
192 sleep(1) ;
193 #endif
195 if ( tries >= 3 )
197 system("lsps -a") ;
198 RSL_FATAL(2) ;
202 #if !(defined(vpp)||defined(vpp2)) || defined(sx) || defined(alphavector)
203 if ( s > 0 )
204 bzero( retval, s2 ) ; /* return zero'd storage always */
205 #else
206 if ( s > 0 )
207 { int l, lb ;
208 l = s2/sizeof(int) ;
209 lb = l*sizeof(int) ;
210 vizero_( retval, &l ) ;
211 l = s2-lb ;
212 vbzero_( retval+lb, &l ) ; /* return zero'd storage always */
214 #endif
216 #ifdef PADIT
217 retval = retval + 512 ;
218 #endif
220 #ifdef STUG
221 for ( bbb = 0 ; bbb < MAXSTUG ; bbb++ )
223 if ( stug[bbb].ddr == 0 ) break ;
225 if ( bbb < MAXSTUG ) {
226 stug[bbb].ddr = retval ;
227 stug[bbb].sz = s ;
228 outy += stug[bbb].sz ;
229 /* fprintf(stderr,"+ %10d. %08x %10d %10d\n", bbb, stug[bbb].ddr, stug[bbb].sz, outy ) ; */
230 nouty ++ ;
231 if ( nouty > maxstug ) maxstug = nouty ;
232 if ( outy > maxouty ) maxouty = outy ;
233 }else{
234 #ifndef MS_SUA
235 fprintf(stderr,"stug full %d\n",bbb) ;
236 #endif
237 RSL_FATAL(2) ;
239 #endif
240 return(retval) ;
243 void rsl_free( p )
244 char **p ;
246 if ( *p == zero_length_storage ) return ; /* fix from ANU */
248 #ifdef STUG
249 for ( bbb = 0 ; bbb < MAXSTUG ; bbb++ )
251 if ( stug[bbb].ddr == *p ) {
252 outy -= stug[bbb].sz ;
253 /* fprintf(stderr,"- %10d. %08x %10d %10d\n", bbb, stug[bbb].ddr, stug[bbb].sz, outy ) ; */
254 nouty -- ;
255 stug[bbb].ddr = 0L ;
256 break ;
259 #endif
261 #ifdef PADIT
262 BASE_FREE ( *p-512 ) ;
263 #else
264 BASE_FREE ( *p ) ;
265 #endif
266 *p = NULL ;
269 #ifdef MS_SUA
270 bzero( char *buf, int l )
272 int i ;
273 char * p ;
274 for ( p = buf, i=0 ; i < l ; i++ ) *p = '\0' ;
276 #endif