/* This file is part of netcdf-4, a netCDF-like interface for HDF5, or a HDF5 backend for netCDF, depending on your point of view. This file contains functions relating to logging errors. Also it contains the functions nc_malloc, nc_calloc, and nc_free. Copyright 2003, University Corporation for Atmospheric Research. See netcdf-4/docs/COPYRIGHT file for copying and redistribution conditions. $Id: error.c,v 1.5 2005/10/09 13:47:37 ed Exp $ */ #include #include #include #include #include "error.h" #include "assert.h" /* This contents of this file get skipped if LOGGING is not defined * during compile. */ #ifdef LOGGING /* This keeps track of how many bytes of memory are malloced or calloced. */ int nc_allocated_blocks = 0; extern int nc_log_level; /* Extra memory debugging can be turned on with the EXTRA_MEM_DEBUG flag at compile. */ #ifdef EXTRA_MEM_DEBUG NC_MEM_DEBUG_T nc_mem_debug[MEM_DEBUG_MAX_BLOCKS]; int nc_mem_blknum = 0; #endif /* Substitute malloc to keep track of some stuff. Ed Hartnett 11/7/3 */ void * nc_malloc(size_t size) { void *ptr; nc_allocated_blocks++; LOG((6, "nc_malloc called, num_blocks: %d", nc_allocated_blocks)); /* malloc the memory. */ ptr = malloc(size); /* Extra debugging stuff keeps track of mallocs and frees. */ #ifdef EXTRA_MEM_DEBUG if (!ptr) LOG((0, "NULL returned from malloc!")); if (!size) LOG((0, "Malloc called with size = 0!")); LOG((6, "%d bytes of memory allocated, at address 0x%x", size, ptr)); if (nc_mem_blknum > MEM_DEBUG_MAX_BLOCKS) { LOG((0, "Extra memory debugging ran out of blocks!")); return ptr; } nc_mem_debug[nc_mem_blknum].address = ptr; nc_mem_debug[nc_mem_blknum].size = size; nc_mem_blknum++; #endif return ptr; } /* Phoney calloc for debugging. Ed Hartnett 11/7/3 */ void * nc_calloc(size_t nmemb, size_t size) { void *ptr; nc_allocated_blocks++; LOG((6, "nc_calloc called, num_blocks: %d", nc_allocated_blocks)); ptr = calloc(nmemb, size); /* Extra debugging stuff keeps track of mallocs and frees. */ #ifdef EXTRA_MEM_DEBUG if (!ptr) LOG((0, "NULL returned from calloc!")); if (!size) LOG((0, "Calloc called with size = 0!")); LOG((6, "%d bytes of memory calloced, at address 0x%x", nmemb * size, ptr)); if (nc_mem_blknum > MEM_DEBUG_MAX_BLOCKS) { LOG((0, "Extra memory debugging ran out of blocks!")); return ptr; } nc_mem_debug[nc_mem_blknum].address = ptr; nc_mem_debug[nc_mem_blknum].size = nmemb * size; nc_mem_blknum++; #endif return ptr; } /* Phoney free for debugging. */ void nc_free(void *ptr) { nc_allocated_blocks--; LOG((6, "nc_free called, num_blocks: %d", nc_allocated_blocks)); /* Extra debugging stuff keeps track of mallocs and frees. */ #ifdef EXTRA_MEM_DEBUG if (!ptr) LOG((0, "free called on NULL!")); { int i; for (i=0; i nc_log_level) return; /* If the severity is zero, this is an error. Otherwise insert that many tabs before the message. */ if (!severity) fprintf(stdout, "ERROR: "); for (t=0; t