2017-10-31 05:52:08 +08:00
|
|
|
/*
|
2018-12-07 06:40:43 +08:00
|
|
|
Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
|
|
|
|
See COPYRIGHT for license information.
|
2017-10-31 05:52:08 +08:00
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
#define TRACE
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
extern char* ncclassname(nc_class);
|
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
#ifdef TRACE
|
|
|
|
#define T(fcn,mem) {if(trace) {fprintf(stderr,"X: %s: %p\n", fcn,mem);}}
|
2010-06-03 21:24:43 +08:00
|
|
|
#else
|
2018-11-16 01:00:38 +08:00
|
|
|
#define T(fcn,mem)
|
2010-06-03 21:24:43 +08:00
|
|
|
#endif
|
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
int debug = 0;
|
|
|
|
static int trace = 0;
|
|
|
|
|
|
|
|
int
|
|
|
|
settrace(int tf)
|
|
|
|
{
|
|
|
|
trace = tf;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
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*/
|
2017-10-31 05:52:08 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
chkfree(void* memory)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2017-10-31 05:52:08 +08:00
|
|
|
if(memory == NULL) {
|
|
|
|
panic("free: null memory");
|
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
T("free",memory);
|
2017-10-31 05:52:08 +08:00
|
|
|
free(memory);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
void*
|
|
|
|
chkmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void* memory = malloc(size);
|
|
|
|
if(memory == NULL) {
|
|
|
|
panic("malloc:out of memory");
|
|
|
|
}
|
|
|
|
T("malloc",memory);
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
void*
|
2017-10-31 05:52:08 +08:00
|
|
|
chkcalloc(size_t size)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
void* memory = calloc(size,1); /* use calloc to zero memory*/
|
|
|
|
if(memory == NULL) {
|
2018-11-16 01:00:38 +08:00
|
|
|
panic("calloc:out of memory");
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
T("calloc",memory);
|
2010-06-03 21:24:43 +08:00
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
chkrealloc(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
void* memory = realloc(ptr,size);
|
|
|
|
if(memory == NULL) {
|
|
|
|
panic("realloc:out of memory");
|
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
if(ptr != memory) {T("free",memory); T("realloc",memory);}
|
2010-06-03 21:24:43 +08:00
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
2017-10-31 05:52:08 +08:00
|
|
|
char*
|
|
|
|
chkstrdup(const char* s)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2018-04-24 00:54:44 +08:00
|
|
|
char* dup;
|
2017-10-31 05:52:08 +08:00
|
|
|
if(s == NULL) {
|
|
|
|
panic("strdup: null argument");
|
|
|
|
}
|
2018-04-24 00:54:44 +08:00
|
|
|
dup = strdup(s);
|
2017-10-31 05:52:08 +08:00
|
|
|
if(dup == NULL) {
|
|
|
|
panic("strdup: out of memory");
|
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
T("strdup",dup);
|
2018-04-24 00:54:44 +08:00
|
|
|
return dup;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|