netcdf-c/ncgen/debug.c

88 lines
1.4 KiB
C
Raw Normal View History

2017-10-31 05:52:08 +08:00
/*
Copyright (c) 1998-2017 University Corporation for Atmospheric Research/Unidata
See LICENSE.txt for license information.
*/
2010-06-03 21:24:43 +08:00
#include "includes.h"
extern char* ncclassname(nc_class);
#ifdef DEBUG
int debug = 1;
#else
int debug = 0;
#endif
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");
}
free(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) {
panic("malloc:out of memory");
}
return memory;
}
void*
chkrealloc(void* ptr, size_t size)
{
void* memory = realloc(ptr,size);
if(memory == NULL) {
panic("realloc:out of memory");
}
return memory;
}
2017-10-31 05:52:08 +08:00
char*
chkstrdup(const char* s)
2010-06-03 21:24:43 +08:00
{
char* dup;
2017-10-31 05:52:08 +08:00
if(s == NULL) {
panic("strdup: null argument");
}
dup = strdup(s);
2017-10-31 05:52:08 +08:00
if(dup == NULL) {
panic("strdup: out of memory");
}
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;
}