Original WRF subgrid support version from John Michalakes without fire
[wrffire.git] / wrfv2_fire / external / RSL / RSL / debug.c
blob50e26e6f0d3f9f2de9c81c919269de054ab9b495
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 ***************************************************************************/
58 * debug.c
60 * Various debugging things, including malloc debugging stuff
63 dumdebug(j)
65 return ;
68 /* #define NEXUS_MALLOC_DEBUG */
69 #ifdef NEXUS_MALLOC_DEBUG
71 #include <stdio.h>
72 #include <ctype.h>
73 #include <errno.h>
74 #include <sys/param.h>
75 #include <sys/access.h>
78 * Memory allocation debugging and diagnostics code.
81 #define NEXUS_MALLOC_PAD 512
82 #define NEXUS_N_MALLOC_RECS 20000
84 typedef struct _malloc_rec_t
86 char *addr;
87 char *file;
88 char *free_file;
89 int size;
90 #ifdef crayx1
91 int line;
92 int free_line;
93 int freed;
94 #else
95 short line;
96 short free_line;
97 short freed;
98 #endif
99 } *malloc_rec_t;
101 static struct _malloc_rec_t malloc_recs[NEXUS_N_MALLOC_RECS];
102 static int next_malloc_rec = 0;
104 static char last_successful_file[1024];
105 static int last_successful_line;
107 static int initialized = 0;
110 #define START_MAGIC 0xf00dface
111 #define END_MAGIC 0xeeaaddff
114 * nexus_debug_malloc()
116 * Malloc wrapper that can print out the size and location
117 * of allocations when the -Dmalloc argument has been given.
119 * The intent is to define a macro of the form
121 * #ifdef NEXUS_DEBUG
122 * #define malloc(size) nexus_debug_malloc(size, __FILE__, __LINE__)
123 * #endif
125 * in order to trace memory allocation in detail.
128 void *nexus_debug_malloc(int size, char *file, int line)
130 void *rc, *addr;
131 malloc_rec_t rec;
132 int *p;
134 nexus_debug_malloc_check(file, line);
136 while ((size & 0x07) != 0)
137 size++;
139 if (next_malloc_rec >= NEXUS_N_MALLOC_RECS)
141 fprintf(stderr,"Too many malloc recs\n");
142 rc = malloc(size);
144 else
146 rec = &malloc_recs[next_malloc_rec];
148 addr = malloc(size + 2 * NEXUS_MALLOC_PAD);
149 rc = (char *) addr + NEXUS_MALLOC_PAD;
150 bzero( rc, size ) ; /* zero storage */
151 if (0 )
153 printf("malloc(%d) at %s:%d returns %x idx=%d\n",
154 size, file, line, rc, next_malloc_rec);
157 rec->addr = addr;
158 rec->file = file;
159 rec->line = line;
160 rec->size = size;
161 rec->free_file = (char *) NULL;
162 rec->free_line = -1;
163 rec->freed = 0;
165 if (NEXUS_MALLOC_PAD >= 4)
167 *((int *) addr) = next_malloc_rec;
169 for (p = (int *) addr + 1; p < (int *) rc; p++)
171 *p = START_MAGIC;
173 for (p = (int *) ((char *) addr + size + NEXUS_MALLOC_PAD);
174 p < (int *) ((char *) addr + size + 2 * NEXUS_MALLOC_PAD); p++)
176 *p = END_MAGIC;
180 next_malloc_rec++;
183 return rc;
184 } /* nexus_debug_malloc() */
188 * nexus_debug_malloc_check()
190 * Walk the list of allocated blocks looking for munged memory.
192 nexus_debug_malloc_check(char *file, int line)
194 int i;
195 malloc_rec_t rec;
196 int *p;
198 if (NEXUS_MALLOC_PAD < 4)
199 return;
201 for (i = 0; i < next_malloc_rec; i++)
203 rec = &malloc_recs[i];
205 if (rec->freed)
206 continue;
208 if (*((int *) rec->addr) != i)
210 fprintf(stderr,"Malloc check (start) failed for idx %d at %s:%d for allocation at %s:%d of size %d. Last successful check was %s:%d\n",
212 file, line,
213 rec->file, rec->line,
214 rec->size,
215 last_successful_file,
216 last_successful_line);
217 exit(2) ;
220 for (p = (int *) rec->addr + 1; p < (int *) ((char *) rec->addr + NEXUS_MALLOC_PAD); p++)
222 if (*p != START_MAGIC)
224 fprintf(stderr,"Malloc check (start) failed for idx %d at %s:%d for allocation at %s:%d of size %d Last successful check was %s:%d\n",
226 file, line,
227 rec->file, rec->line,
228 rec->size,
229 last_successful_file,
230 last_successful_line);
231 exit(2) ;
235 for (p = (int *) ((char *) rec->addr + rec->size + NEXUS_MALLOC_PAD);
236 p < (int *) ((char *) rec->addr + rec->size + 2 * NEXUS_MALLOC_PAD); p++)
238 if (*p != END_MAGIC)
240 fprintf(stderr,"Malloc check (end) failed for idx %d at %s:%d for allocation at %s:%d of size %d Last successful check was %s:%d\n",
242 file, line,
243 rec->file, rec->line,
244 rec->size,
245 last_successful_file,
246 last_successful_line);
247 exit(2) ;
251 strcpy(last_successful_file, file);
252 last_successful_line = line;
253 } /* nexus_debug_malloc_check() */
255 void nexus_debug_mem_check(int size, void *address)
257 int i;
258 malloc_rec_t rec;
259 char *pad1_start, *pad1_end, *pad2_start, *pad2_end, *a_start, *a_end;
261 for (i = 0; i < next_malloc_rec; i++)
263 rec = &malloc_recs[i];
265 if (rec->freed)
266 continue;
268 pad1_start = rec->addr;
269 pad1_end = pad1_start + NEXUS_MALLOC_PAD;
271 pad2_start = rec->addr + rec->size + NEXUS_MALLOC_PAD;
272 pad2_end = pad2_start + NEXUS_MALLOC_PAD;
274 a_start = address;
275 a_end = a_start + size - 1;
277 if ((a_start >= pad1_start && a_start < pad1_end) ||
278 (a_end >= pad1_start && a_end < pad1_end) ||
279 (a_start >= pad2_start && a_start < pad2_end) ||
280 (a_end >= pad2_start && a_end < pad2_end) ||
281 (a_start < pad1_start && a_end > pad1_end) ||
282 (a_start < pad2_start && a_end > pad2_end))
284 fprintf(stderr,"Malloc memory check for address %x length %s failed for idx %d for allocation at %s:%d of size %d.\n",
285 address,
286 size,
288 rec->file, rec->line,
289 rec->size);
290 exit(2) ;
293 } /* nexus_debug_mem_check() */
297 * nexus_debug_show_freed_blocks()
299 * Walk the list of allocated blocks looking blocks that
300 * were never freed.
302 void nexus_debug_show_freed_blocks()
304 int i;
305 malloc_rec_t rec;
307 for (i = 0; i < next_malloc_rec; i++)
309 rec = &malloc_recs[i];
311 if (!rec->freed)
313 fprintf(stderr,"Unfreed block %d size=%5d at %s:%d\n",
314 i, rec->size, rec->file, rec->line);
317 } /* nexus_debug_show_freed_blocks() */
321 * nexus_debug_show_malloc_stats()
323 * Print a summary of memory allocation statistics.
325 void nexus_debug_show_malloc_stats()
327 int i;
328 malloc_rec_t rec;
329 int bytes, bytes_freed;
330 int n_blocks_freed;
332 bytes = bytes_freed = n_blocks_freed = 0;
334 for (i = 0; i < next_malloc_rec; i++)
336 rec = &malloc_recs[i];
338 bytes += rec->size;
340 if (rec->freed)
342 bytes_freed += rec->size;
343 n_blocks_freed++;
347 fprintf(stderr,"Malloc statistics:\n");
348 fprintf(stderr,"\tbytes allocated: %d\n", bytes);
349 fprintf(stderr,"\tbytes freed: %d\n", bytes_freed);
350 fprintf(stderr,"\tbytes unfreed: %d\n", bytes - bytes_freed);
351 fprintf(stderr,"\tblocks allocated: %d\n", next_malloc_rec);
352 fprintf(stderr,"\tblocks freed: %d\n", n_blocks_freed);
353 fprintf(stderr,"\tblocks unfreed: %d\n", next_malloc_rec - n_blocks_freed);
354 } /* nexus_debug_show_malloc_stats() */
358 * nexus_debug_free()
360 void nexus_debug_free(void *ptr, char *file, int line)
362 void *addr;
363 int idx;
365 malloc_rec_t rec;
367 if (NEXUS_MALLOC_PAD >= 4)
369 addr = (char *) ptr - NEXUS_MALLOC_PAD;
371 idx = *((int *) addr);
373 if ( idx < 0 || idx >= NEXUS_N_MALLOC_RECS )
375 fprintf(stderr,"nexus_debug_free(): bad idx = %d; \n possibly corrupt ptr %08x at %s line %d\n",
376 idx, ptr, file, line ) ;
379 rec = &(malloc_recs[idx]);
381 if (rec->freed == 1 )
383 fprintf(stderr,"nexus_debug_free(): block %x idx %d allocated at %s:%d was freed twice at %s:%d and %s:%d\n",
384 ptr, idx,
385 rec->file, rec->line,
386 rec->free_file, rec->free_line,
387 file, line);
388 exit(2) ;
391 rec->freed = 1;
392 rec->free_file = file;
393 rec->free_line = line;
394 free(addr);
396 else
398 idx = -1;
399 free(ptr);
401 if (0)
403 nexus_printf("free(%x) at %s:%d index=%d\n",
404 ptr, file, line, idx);
406 } /* nexus_debug_free() */
410 #endif /* NEXUS_DEBUG */