/* * void dmalloc_error * * DESCRIPTION: * * Handler of error codes. The caller should have set the errno already * * RETURNS: * * None. * * ARGUMENTS: * * func -> Function name for the logs. */ void dmalloc_error(const char *func) { /* do we need to log or print the error? */ if (dmalloc_logpath != NULL || BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) { /* default str value */ if (func == NULL) { func = "_malloc_error"; } /* print the malloc error message */ dmalloc_message("ERROR: %s: %s (err %d)", func, dmalloc_strerror(dmalloc_errno), dmalloc_errno); } /* do I need to abort? */ if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_ABORT)) { _dmalloc_die(0); } #if HAVE_FORK /* how about just drop core? */ if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_DUMP)) { if (fork() == 0) { _dmalloc_die(1); } } #endif }
/* * static void *heap_extend * * DESCRIPTION: * * Get more bytes from the system functions. * * RETURNS: * * Success - Valid pointer. * * Failure - NULL * * ARGUMENTS: * * incr -> Number of bytes we need. */ static void *heap_extend(const int incr) { void *ret = SBRK_ERROR; char *high; #if INTERNAL_MEMORY_SPACE { static char block_o_bytes[INTERNAL_MEMORY_SPACE]; static char *bounds_p = block_o_bytes + sizeof(block_o_bytes); static char *block_p = block_o_bytes; if (block_p + incr >= bounds_p) { ret = SBRK_ERROR; } else { ret = block_p; block_p += incr; } } #else #if HAVE_MMAP && USE_MMAP #if MAP_ANON /* if we have and can use mmap, then do so */ ret = mmap(0L, incr, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1 /* no fd */, 0 /* no offset */); #else #endif if (ret == MAP_FAILED) { ret = SBRK_ERROR; } #else #if HAVE_SBRK ret = sbrk(incr); #endif /* if HAVE_SBRK */ #endif /* if not HAVE_MMAP && USE_MMAP */ #endif /* if not INTERNAL_MEMORY_SPACE */ if (ret == SBRK_ERROR) { if (BIT_IS_SET(_dmalloc_flags, DEBUG_CATCH_NULL)) { char str[128]; int len; len = loc_snprintf(str, sizeof(str), "\r\ndmalloc: critical error: could not extend heap %u more bytes\r\n", incr); (void)write(STDERR, str, len); _dmalloc_die(0); } dmalloc_errno = ERROR_ALLOC_FAILED; dmalloc_error("heap_extend"); } if (_dmalloc_heap_low == NULL || (char *)ret < (char *)_dmalloc_heap_low) { _dmalloc_heap_low = ret; } high = (char *)ret + incr; if (high > (char *)_dmalloc_heap_high) { _dmalloc_heap_high = high; } if (BIT_IS_SET(_dmalloc_flags, DEBUG_LOG_ADMIN)) { dmalloc_message("extended heap space by %d bytes [%#lx, %#lx]", incr, (unsigned long)_dmalloc_heap_low, (unsigned long)_dmalloc_heap_high); } return ret; }