diff --git a/urogue/configure.ac b/urogue/configure.ac index 32f80fb..4b9dfbd 100644 --- a/urogue/configure.ac +++ b/urogue/configure.ac @@ -158,4 +158,15 @@ AC_DEFINE([WIZARD], [], [Define to include wizard mode]) AC_MSG_RESULT([yes]) fi +AC_ARG_ENABLE([memdebug],[AC_HELP_STRING([--enable-memdebug], [enable built-in memory checking @<:@default=no@:>@])],[],[]) +AC_MSG_CHECKING([if memory checking is enabled]) +if test "x$enable_memdebug" = "xno" ; then +AC_MSG_RESULT([no]) +elif test "x$enable_memdebug" = "x" ; then +AC_MSG_RESULT([no]) +else +AC_DEFINE([MEM_DEBUG], [], [Define to enable built-in memory checking]) +AC_MSG_RESULT([yes]) +fi + AC_OUTPUT diff --git a/urogue/memory.c b/urogue/memory.c index 412da50..12b70f4 100644 --- a/urogue/memory.c +++ b/urogue/memory.c @@ -15,6 +15,10 @@ #include "memory.h" #include "rogue.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + static char sccsid[] = "%W%\t%G%"; /* Debugging memory allocation code that tries to trap common memory problems @@ -40,6 +44,7 @@ void mem_debug(const int level) { memdebug_level = level; +#ifdef MEM_DEBUG if (trace_file == NULL) trace_file = fopen("trace", "w"); @@ -59,12 +64,14 @@ void mem_debug(const int level) break; } fprintf(trace_file, "fence size = %d\n", FENCE_SIZE); +#endif } /* set memory tracking on or off */ /* turning it off deletes all tracking data */ void mem_tracking(int flag) { +#ifdef MEM_DEBUG /* do nothing if debuglevel is too low */ if (memdebug_level < 2) return; @@ -86,6 +93,7 @@ void mem_tracking(int flag) dict_destroy(allocations); allocations = NULL; } +#endif } /* go through all pointers and see if they are OK, aborting if not */ @@ -93,6 +101,7 @@ void mem_tracking(int flag) /* if statement boolean expressions */ int mem_check(char *fname, int linenum) { +#ifdef MEM_DEBUG STRING_ENTRY *se; /* scan of a NULL dictionary always succeeds */ @@ -113,6 +122,7 @@ int mem_check(char *fname, int linenum) fprintf(trace_file, "+++ --- Done pointer scan\n\n"); +#endif /* always return a good value if execution arrives here */ return 1; } @@ -120,6 +130,7 @@ int mem_check(char *fname, int linenum) /* allocate some memory and initialize header and trailer */ void *mem_malloc(const size_t bytes) { +#ifdef MEM_DEBUG char *mem_temp; size_t real_size = bytes + (FENCE_SIZE << 1); @@ -175,11 +186,15 @@ void *mem_malloc(const size_t bytes) fflush(trace_file); } return (void *)mem_temp; +#else + return malloc(bytes); +#endif } /* release some memory, making sure that it was properly allocated */ void mem_free(const void *ptr) { +#ifdef MEM_DEBUG char *mem_temp; size_t mem_size; size_t i; @@ -238,11 +253,15 @@ void mem_free(const void *ptr) mem_temp = (char *)ptr - FENCE_SIZE; free((void *)mem_temp); +#else + free((void *) ptr); +#endif } /* reallocate some memory, making sure that it was properly allocated */ void *mem_realloc(const void *ptr, const size_t new_size) { +#ifdef MEM_DEBUG char *mem_temp = (char *)ptr; size_t real_size = new_size + (FENCE_SIZE << 1); size_t mem_size; @@ -334,6 +353,9 @@ void *mem_realloc(const void *ptr, const size_t new_size) fflush(trace_file); } return (void *)mem_temp; +#else + return realloc((void *) ptr, new_size); +#endif } /* check a pointer to be sure all check bytes are OK. abort if not */ @@ -341,6 +363,7 @@ void *mem_realloc(const void *ptr, const size_t new_size) /* if statement boolean expressions */ int mem_validate(const void *ptr) { +#ifdef MEM_DEBUG unsigned char *mem_temp = (unsigned char *)ptr; size_t mem_size; size_t i; @@ -416,5 +439,6 @@ int mem_validate(const void *ptr) } if (memdebug_level > 0) fflush(trace_file); +#endif return 1; }