changeset 288:b8919055c2fc

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.
author John "Elwin" Edwards
date Fri, 24 Nov 2017 13:22:26 -0500
parents 0b3d1b38998f
children d815c40c3753
files urogue/configure.ac urogue/memory.c
diffstat 2 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/urogue/configure.ac	Mon Oct 16 19:53:38 2017 -0400
+++ b/urogue/configure.ac	Fri Nov 24 13:22:26 2017 -0500
@@ -158,4 +158,15 @@
 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
--- a/urogue/memory.c	Mon Oct 16 19:53:38 2017 -0400
+++ b/urogue/memory.c	Fri Nov 24 13:22:26 2017 -0500
@@ -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 @@
 {
 	memdebug_level = level;
 
+#ifdef MEM_DEBUG
 	if (trace_file == NULL)
 		trace_file = fopen("trace", "w");
 
@@ -59,12 +64,14 @@
 		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 @@
 		dict_destroy(allocations);
 		allocations = NULL;
 	}
+#endif
 }
 
 /* go through all pointers and see if they are OK, aborting if not */
@@ -93,6 +101,7 @@
 /* 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 @@
 
 	fprintf(trace_file, "+++ --- Done pointer scan\n\n");
 
+#endif
 	/* always return a good value if execution arrives here */
 	return 1;
 }
@@ -120,6 +130,7 @@
 /* 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 @@
 		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 @@
 
 	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 @@
 		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 @@
 /* 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 @@
 		}
 	if (memdebug_level > 0)
 		fflush(trace_file);
+#endif
 	return 1;
 }