File: debug.c

package info (click to toggle)
netcdf-parallel 1%3A4.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,668 kB
  • sloc: ansic: 200,241; sh: 10,807; yacc: 2,522; makefile: 1,306; lex: 1,153; xml: 173; awk: 2
file content (115 lines) | stat: -rw-r--r-- 1,918 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright (c) 1998-2017 University Corporation for Atmospheric Research/Unidata
See LICENSE.txt for license information.
*/

#include "includes.h"

#define TRACE

extern char* ncclassname(nc_class);

#ifdef TRACE
#define T(fcn,mem) {if(trace) {fprintf(stderr,"X: %s: %p\n", fcn,mem);}}
#else
#define T(fcn,mem)
#endif

int debug = 0;
static int trace = 0;

int
settrace(int tf)
{
    trace = tf;
    return 1;
}


void fdebug(const char *fmt, ...)
{
    va_list argv;
    if(debug == 0) return;
    va_start(argv,fmt);
    (void)vfprintf(stderr,fmt,argv) ;
}

/**************************************************/

/* Support debugging of memory*/

void
chkfree(void* memory)
{
    if(memory == NULL) {
	panic("free: null memory");
    }
T("free",memory);
    free(memory);
}

void*
chkmalloc(size_t size)
{
    void* memory = malloc(size);
    if(memory == NULL) {
	panic("malloc:out of memory");
    }
T("malloc",memory);
    return memory;
}

void*
chkcalloc(size_t size)
{
    void* memory = calloc(size,1); /* use calloc to zero memory*/
    if(memory == NULL) {
	panic("calloc:out of memory");
    }
T("calloc",memory);
    return memory;
}

void*
chkrealloc(void* ptr, size_t size)
{
    void* memory = realloc(ptr,size);
    if(memory == NULL) {
	panic("realloc:out of memory");
    }
if(ptr != memory) {T("free",memory); T("realloc",memory);}
    return memory;
}

char*
chkstrdup(const char* s)
{
    char* dup;
    if(s == NULL) {
	panic("strdup: null argument");
    }
    dup = strdup(s);
    if(dup == NULL) {
	panic("strdup: out of memory");
    }
T("strdup",dup);
    return dup;
}

int
panic(const char* fmt, ...)
{
    va_list args;
    if(fmt != NULL) {
      va_start(args, fmt);
      vfprintf(stderr, fmt, args);
      fprintf(stderr, "\n" );
      va_end( args );
    } else {
      fprintf(stderr, "panic" );
    }
    fprintf(stderr, "\n" );
    fflush(stderr);
    abort();
    return 0;
}