UltraRogue: compile out the built-in memory checking.

Functions in memory.c tracked and instrumented memory allocation.  This
mechanism logged information to a text file and also added a 16KB guard
area to every allocation.  Neither of these are desirable in a multi-
user environment, so the whole memory tracking subsystem has been
disabled.

The behavior can be enabled with a configure flag, but it would be a
better idea to use Valgrind to deal with memory bugs.
This commit is contained in:
John "Elwin" Edwards 2017-11-24 13:22:26 -05:00
parent 359d0987a4
commit 1e2f3f5803
2 changed files with 35 additions and 0 deletions

View file

@ -158,4 +158,15 @@ AC_DEFINE([WIZARD], [], [Define to include wizard mode])
AC_MSG_RESULT([yes]) AC_MSG_RESULT([yes])
fi 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 AC_OUTPUT

View file

@ -15,6 +15,10 @@
#include "memory.h" #include "memory.h"
#include "rogue.h" #include "rogue.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
static char sccsid[] = "%W%\t%G%"; static char sccsid[] = "%W%\t%G%";
/* Debugging memory allocation code that tries to trap common memory problems /* Debugging memory allocation code that tries to trap common memory problems
@ -40,6 +44,7 @@ void mem_debug(const int level)
{ {
memdebug_level = level; memdebug_level = level;
#ifdef MEM_DEBUG
if (trace_file == NULL) if (trace_file == NULL)
trace_file = fopen("trace", "w"); trace_file = fopen("trace", "w");
@ -59,12 +64,14 @@ void mem_debug(const int level)
break; break;
} }
fprintf(trace_file, "fence size = %d\n", FENCE_SIZE); fprintf(trace_file, "fence size = %d\n", FENCE_SIZE);
#endif
} }
/* set memory tracking on or off */ /* set memory tracking on or off */
/* turning it off deletes all tracking data */ /* turning it off deletes all tracking data */
void mem_tracking(int flag) void mem_tracking(int flag)
{ {
#ifdef MEM_DEBUG
/* do nothing if debuglevel is too low */ /* do nothing if debuglevel is too low */
if (memdebug_level < 2) if (memdebug_level < 2)
return; return;
@ -86,6 +93,7 @@ void mem_tracking(int flag)
dict_destroy(allocations); dict_destroy(allocations);
allocations = NULL; allocations = NULL;
} }
#endif
} }
/* go through all pointers and see if they are OK, aborting if not */ /* 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 */ /* if statement boolean expressions */
int mem_check(char *fname, int linenum) int mem_check(char *fname, int linenum)
{ {
#ifdef MEM_DEBUG
STRING_ENTRY *se; STRING_ENTRY *se;
/* scan of a NULL dictionary always succeeds */ /* 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"); fprintf(trace_file, "+++ --- Done pointer scan\n\n");
#endif
/* always return a good value if execution arrives here */ /* always return a good value if execution arrives here */
return 1; return 1;
} }
@ -120,6 +130,7 @@ int mem_check(char *fname, int linenum)
/* allocate some memory and initialize header and trailer */ /* allocate some memory and initialize header and trailer */
void *mem_malloc(const size_t bytes) void *mem_malloc(const size_t bytes)
{ {
#ifdef MEM_DEBUG
char *mem_temp; char *mem_temp;
size_t real_size = bytes + (FENCE_SIZE << 1); size_t real_size = bytes + (FENCE_SIZE << 1);
@ -175,11 +186,15 @@ void *mem_malloc(const size_t bytes)
fflush(trace_file); fflush(trace_file);
} }
return (void *)mem_temp; return (void *)mem_temp;
#else
return malloc(bytes);
#endif
} }
/* release some memory, making sure that it was properly allocated */ /* release some memory, making sure that it was properly allocated */
void mem_free(const void *ptr) void mem_free(const void *ptr)
{ {
#ifdef MEM_DEBUG
char *mem_temp; char *mem_temp;
size_t mem_size; size_t mem_size;
size_t i; size_t i;
@ -238,11 +253,15 @@ void mem_free(const void *ptr)
mem_temp = (char *)ptr - FENCE_SIZE; mem_temp = (char *)ptr - FENCE_SIZE;
free((void *)mem_temp); free((void *)mem_temp);
#else
free((void *) ptr);
#endif
} }
/* reallocate some memory, making sure that it was properly allocated */ /* reallocate some memory, making sure that it was properly allocated */
void *mem_realloc(const void *ptr, const size_t new_size) void *mem_realloc(const void *ptr, const size_t new_size)
{ {
#ifdef MEM_DEBUG
char *mem_temp = (char *)ptr; char *mem_temp = (char *)ptr;
size_t real_size = new_size + (FENCE_SIZE << 1); size_t real_size = new_size + (FENCE_SIZE << 1);
size_t mem_size; size_t mem_size;
@ -334,6 +353,9 @@ void *mem_realloc(const void *ptr, const size_t new_size)
fflush(trace_file); fflush(trace_file);
} }
return (void *)mem_temp; 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 */ /* 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 */ /* if statement boolean expressions */
int mem_validate(const void *ptr) int mem_validate(const void *ptr)
{ {
#ifdef MEM_DEBUG
unsigned char *mem_temp = (unsigned char *)ptr; unsigned char *mem_temp = (unsigned char *)ptr;
size_t mem_size; size_t mem_size;
size_t i; size_t i;
@ -416,5 +439,6 @@ int mem_validate(const void *ptr)
} }
if (memdebug_level > 0) if (memdebug_level > 0)
fflush(trace_file); fflush(trace_file);
#endif
return 1; return 1;
} }