rev 740 - in trunk: include/prothon src

SVN User <[email protected]> Thu, 15 Jul 2004 16:34:45 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-07-15 16:34:43 -0400 (Thu, 15 Jul 2004)
New Revision: 740

Modified:
   trunk/include/prothon/prothon.h
   trunk/src/prmalloc.c
Log:
added prmalloc debug flag

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-07-15 08:11:39 UTC (rev 739)
+++ trunk/include/prothon/prothon.h	2004-07-15 20:34:43 UTC (rev 740)
@@ -56,7 +56,9 @@
 #define PROTHON_H
 
 //**************************** DEBUG DEFINITIONS ******************************
- 
+
+// These should be defined for releases
+// they are enabled by command-line options
 #define CMDLINE_FLAGS
 #define TRACE_PARSER
 #define DUMP_MODULE_CODE
@@ -64,6 +66,8 @@
 #define DUMP_OBJECTS_AT_END
 #define DEBUG_THREADS
 
+// These should NOT be defined for releases
+#define DEBUG_PRMALLOC
 //#define DEBUG_MEM_MGR
 //#define PROSIST_DEBUG
 

Modified: trunk/src/prmalloc.c
===================================================================
--- trunk/src/prmalloc.c	2004-07-15 08:11:39 UTC (rev 739)
+++ trunk/src/prmalloc.c	2004-07-15 20:34:43 UTC (rev 740)
@@ -319,6 +319,75 @@
 #endif /* NB_SMALL_SIZE_CLASSES >  8 */
 };
 
+#ifdef DEBUG_PRMALLOC
+
+typedef union {
+	int   num;
+	void* ptr;
+} dlist_item_t;
+
+typedef struct {
+	int				space;
+	int				len;
+	dlist_item_t*	items;
+} dlist_t;
+
+typedef dlist_t* dlist_p;
+
+static dlist_p malloc_list = NULL;
+
+dlist_p new_dlist(int initial_size){
+	dlist_p list;
+	void*   items;
+	list  = malloc(sizeof(dlist_t));
+	items = malloc(initial_size*sizeof(dlist_item_t));
+	list->space = initial_size; 
+	list->len   = 0;
+	list->items = items;
+	return list;
+}
+
+int dlist_in(dlist_p list, void* item) {
+	int i;
+	for(i=0; i < list->len; i++)
+		if (list->items[i].ptr == item) return TRUE;
+	return FALSE;
+}
+
+dlist_p dlist_append(dlist_p list, void* item) {
+	if (list->len == list->space) {
+		list->space *= 2;
+		list->items = realloc(list->items, list->space * sizeof(dlist_item_t));
+	}
+	list->items[list->len++].ptr = item;
+	return list;
+}
+
+int dlist_pop_item(dlist_p list, void* item) {
+	int i;
+	for(i=list->len-1; i >= 0; i--)
+		if (list->items[i].ptr == item) {
+			if (i < list->len-1)
+				memmove( list->items+i, list->items+i+1, 
+					     (list->len-1 - i) * sizeof(dlist_item_t) );
+			list->len--;
+			return FALSE;
+		}
+	return TRUE;
+}
+
+#define debug_add(ptr)							\
+do {											\
+	if (!malloc_list)							\
+		malloc_list = new_dlist(1000);			\
+    if (dlist_in(malloc_list, ptr))	pr_exit(1);	\
+    dlist_append(malloc_list, ptr);				\
+} while(FALSE)
+
+#define debug_del(ptr) if (dlist_pop_item(malloc_list, ptr)) pr_exit(1)
+
+#endif // #ifdef DEBUG_PRMALLOC
+
 /*
  * Free (cached) pools
  */
@@ -387,6 +456,9 @@
 			++pool->ref.count;
 			bp = pool->freeblock;
 			if ((pool->freeblock = *(block **)bp) != NULL) {
+#ifdef DEBUG_PRMALLOC
+				debug_add(bp);
+#endif	
 				UNLOCK();
 				return (void *)bp;
 			}
@@ -403,6 +475,9 @@
 						  POOL_OVERHEAD + \
 						  pool->ref.count * size;
 				*(block **)(pool->freeblock) = NULL;
+#ifdef DEBUG_PRMALLOC
+				debug_add(bp);
+#endif	
 				UNLOCK();
 				return (void *)bp;
 			}
@@ -413,6 +488,9 @@
 			pool = pool->prevpool;
 			next->prevpool = pool;
 			pool->nextpool = next;
+#ifdef DEBUG_PRMALLOC
+			debug_add(bp);
+#endif	
 			UNLOCK();
 			return (void *)bp;
 		}
@@ -443,6 +521,9 @@
 				 */
 				bp = pool->freeblock;
 				pool->freeblock = *(block **)bp;
+#ifdef DEBUG_PRMALLOC
+				debug_add(bp);
+#endif	
 				UNLOCK();
 				return (void *)bp;
 			}
@@ -457,6 +538,9 @@
 			pool->freeblock = bp + size;
 			*(block **)(pool->freeblock) = NULL;
 			pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size;
+#ifdef DEBUG_PRMALLOC
+			debug_add(bp);
+#endif	
 			UNLOCK();
 			return (void *)bp;
 		}
@@ -518,7 +602,13 @@
 	 * last chance to serve the request) or when the max memory limit
 	 * has been reached.
 	 */
-	return (void *)_SYSTEM_MALLOC(nbytes);
+	bp = (void *)_SYSTEM_MALLOC(nbytes);
+#ifdef DEBUG_PRMALLOC
+	LOCK();
+	debug_add(bp);
+	UNLOCK();
+#endif	
+	return bp;
 }
 
 /* free */
@@ -541,6 +631,12 @@
 	if (p == NULL)	/* free(NULL) has no effect */
 		return;
 
+#ifdef DEBUG_PRMALLOC
+	LOCK();
+	debug_del(p);
+	UNLOCK();
+#endif
+
 	offset = (off_t )p & POOL_SIZE_MASK;
 	pool = (poolp )((block *)p - offset);
 	if (pool->pooladdr != pool || pool->magic != (uint )POOL_MAGIC) {
@@ -549,6 +645,7 @@
 	}
 
 	LOCK();
+
 	/*
 	 * At this point, the pool is not empty
 	 */
@@ -627,7 +724,13 @@
 			size = nbytes;
 			goto malloc_copy_free;
 		}
+#ifdef DEBUG_PRMALLOC
+		debug_del(p);
+#endif	
 		bp = (block *)_SYSTEM_REALLOC(p, nbytes);
+#ifdef DEBUG_PRMALLOC
+		debug_add(bp);
+#endif	
 	}
 	else {
 		/* We're in charge of this block */
@@ -645,8 +748,7 @@
 				bp = (block *)p;
 		}
 		else {
-
-		malloc_copy_free:
+malloc_copy_free:
 			UNLOCK();
 			bp = (block *)_THIS_MALLOC(nbytes);
 			if (bp != NULL) {