Index: /trunk/archive/pslib/src/Utils/memory.c
===================================================================
--- /trunk/archive/pslib/src/Utils/memory.c	(revision 263)
+++ /trunk/archive/pslib/src/Utils/memory.c	(revision 264)
@@ -2,10 +2,7 @@
 #include <stdio.h>
 #include <assert.h>
+#define PS_ALLOW_MALLOC			// we're allowed to call malloc()
 #include "psUtils.h"
 #include "Private/p_psMemory.h"
-
-#undef malloc				// _We_ are allowed to call these
-#undef realloc
-#undef free
 
 #undef psAlloc				// don't get the __FILE__ versions
@@ -15,8 +12,9 @@
 static int bad_memblock(const psMemBlock *m, int quiet);
 /*
- * This strawman allocation package only handles up to NMEMBLOCK allocations
- */
-#define NMEMBLOCK 10000			// max. number of calls to psAlloc
-static psMemBlock *memBlocks[NMEMBLOCK];
+ * This strawman allocation package uses an array to track calls to psAlloc;
+ * not nearly good enough for production work!
+ */
+static int nMemBlock = 0;		// size of memBlocks
+static psMemBlock **memBlocks = NULL;
 
 /*****************************************************************************/
@@ -77,8 +75,8 @@
 	}
 	fprintf(stderr, "\n");
-    } else if(ptr->refCounter <= 0) {
+    } else if (ptr->refCounter <= 0) {
 	psError(__func__, "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
 		ptr->id, ptr->file, ptr->lineno, file, lineno);
-    } else if(ptr->refCounter > 1) {
+    } else if (ptr->refCounter > 1) {
 	psError(__func__, "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
 		ptr->id, ptr->file, ptr->lineno, file, lineno);
@@ -138,9 +136,18 @@
 }
 
+static int memFreeCB0(const psMemBlock *ptr)
+{
+    static int incr = 0;		// "p_psMemFreeID += incr"
+
+    assert (ptr != NULL);		// prevent compiler warnings
+    
+    return incr;
+}
+
 /*
  * The default callbacks, and the routines to change them
  */
 static psMemCallback memAllocateCB = memAllocateCB0;
-static psMemCallback memFreeCB = memAllocateCB0;
+static psMemCallback memFreeCB = memFreeCB0;
 
 psMemCallback psMemAllocateSetCB(psMemCallback func)
@@ -157,5 +164,5 @@
     psMemCallback old = memFreeCB;
 
-    memFreeCB = (func != NULL) ? func : memAllocateCB0;
+    memFreeCB = (func != NULL) ? func : memFreeCB0;
 
     return old;
@@ -183,5 +190,5 @@
 	     int quiet)			// be quiet?
 {
-    if(m == NULL) {
+    if (m == NULL) {
 	if (!quiet) {
 	    psError(__func__, "psMemCheckCorruption: NULL memory block\n");
@@ -190,5 +197,5 @@
     }
     
-    if(!ALIGNED(m)) {
+    if (!ALIGNED(m)) {
 	if (!quiet) {
 	    psError(__func__, "psMemCheckCorruption: non-aligned memory block\n");
@@ -197,5 +204,5 @@
     }
     
-    if(m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) {
+    if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) {
 	if (!quiet) {
 	    psError(__func__,
@@ -211,4 +218,8 @@
 {
     int nbad = 0;			// number of bad blocks
+
+    if (memBlocks == NULL) {
+	return 0;			// no memory is allocated to be corrupted
+    }
 
     for (int i = 1; i <= memid; i++) {
@@ -252,13 +263,14 @@
      * Did the user ask to be informed about this allocation?
      */
-    if(ptr->id == p_psMemAllocateID) {
+    
+    if (ptr->id == p_psMemAllocateID && p_psMemAllocateID > 0) {
 	p_psMemAllocateID += memAllocateCB(ptr);	
     }
     /*
-     * Register the allocation.  This is not production quality!
+     * Register the allocation.  This is not a production quality algorithm!
      */
-    if (memid == NMEMBLOCK) {
-	psAbort(__func__, "I'm sorry, but I can only handle %d allocations",
-		NMEMBLOCK);
+    if (memid >= nMemBlock) {
+	nMemBlock = (nMemBlock == 0) ? 10000 : 2*nMemBlock;
+	memBlocks = realloc(memBlocks, nMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
     }
     memBlocks[ptr->id] = ptr;
@@ -292,5 +304,5 @@
      * Did the user ask to be informed about this deallocation?
      */
-    if (ptr->id == p_psMemFreeID) {
+    if (ptr->id == p_psMemFreeID && p_psMemFreeID > 0) {
 	p_psMemFreeID += memFreeCB(ptr);	
     }
@@ -303,7 +315,9 @@
 	}
 	ptr->refCounter--;
+#if 1
 	memBlocks[ptr->id] = NULL;	// XXX sub-optimal! Can't check
-					// freed blocks for corruption
+					// freed blocks for corruption/double frees
 	free(ptr);
+#endif
     }
 }
@@ -320,5 +334,9 @@
 {
     int nleak = 0;
-    
+
+    if (memBlocks == NULL) {		// nothing was allocated
+	return 0;
+    }
+
     for (int i = id0; i <= memid; i++) {
 	if (memBlocks[i] != NULL) {
@@ -408,172 +426,2 @@
     return vptr;
 }
-
-/*****************************************************************************/
-#if 0					// Testing
-#include <signal.h>
-/*
- * These are usually defined in psMemory.h, but in this file (only!)
- * they are undefined explicitly, so let's get them back
- */
-#define psAlloc(size) p_psAlloc(size, __FILE__, __LINE__)
-#define psRealloc(ptr, size) p_psRealloc(ptr, size, __FILE__, __LINE__)
-#define psFree(size) p_psFree(size, __FILE__, __LINE__)
-
-/*****************************************************************************/
-/*
- * My callbacks
- */
-static int my_MemAllocateCB(const psMemBlock *ptr)
-{
-    static int incr = 0;		// "p_psMemAllocateID += incr"
-
-    assert (ptr != NULL);		// prevent compiler warnings
-    
-    fprintf(stderr, "Allocating block %ld (%s:%d)\n",
-	    ptr->id, ptr->file, ptr->lineno);
-    
-    incr = 2;				// See every other allocation
-
-    return incr;
-}
-
-static void *my_MemExhaustedCB(size_t size)
-{
-    psError(__func__, "Bye bye sweet world\n");
-    
-    return NULL;
-}
-
-static psMemProblemCallback default_MemProblemCB = NULL;
-
-static void my_MemProblemCB(const psMemBlock *ptr,
-			    const char *file, int lineno)
-{
-    fprintf(stderr, "I foresee trouble:\n");
-
-    default_MemProblemCB(ptr, file, lineno);
-}
-    
-/*****************************************************************************/
-
-int main(void)
-{
-    /*
-     * Survive any aborts that are generated
-     * This doesn't seem to work on mac os/x 10.2.8
-     */
-    if (signal(SIGABRT, SIG_IGN) == SIG_ERR) {
-	perror("Installing a SIGABRT handler");
-    }
-    /*
-     * Install my callbacks
-     */
-    (void)psMemAllocateSetCB(my_MemAllocateCB);
-    default_MemProblemCB = psMemProblemSetCB(my_MemProblemCB);
-    (void)psMemExhaustedSetCB(my_MemExhaustedCB);
-    /*
-     * Request callback on first allocation
-     */
-    (void)psMemSetAllocateID(1);
-    /*
-     * Start allocating
-     */
-    char *str = psAlloc(10);
-    double *x = psAlloc(1);
-    int *i = psAlloc(1);
-    *i = 1;
-    *x = 1.234;
-
-    psFree(i);
-    
-    str = psRealloc(str, 20);
-    psFree(str);
-#if 1
-    /*
-     * An illegal double-free
-     */
-    psFree(str);
-#endif
-
-#if 1
-    /*
-     * Free memory not allocated with psAlloc
-     */
-    psFree(malloc(1));
-#endif
-
-#if 0
-    /*
-     * Ask for more memory than we can get
-     */
-    psAlloc((size_t)-1 - sizeof(psMemBlock));
-#endif
-
-#if 0
-    /*
-     * Increment refCounter on non-malloced pointer
-     */
-    psMemGetRefCounter(malloc(1));
-#endif
-
-#if 0
-    /*
-     * Mess with the refCounter
-     */
-    (void)psMemIncrRefCounter(x);
-    (void)psMemIncrRefCounter(x);
-    (void)psMemDecrRefCounter(x);	// now == 2
-#endif
-
-#if 0
-    /*
-     * A memory leak
-     */
-    {
-	char *str2 = psAlloc(20);
-	
-	sprintf(str2, "XXX");
-    }
-#else
-    psFree(x);
-#endif
-
-#if 1
-    /*
-     * Corrupt some memory
-     */
-    char *c = psAlloc(1);
-    c[-4] = 0;
-#endif
-
-    /*************************************************************************/
-    /*
-     * Check for memory corruption
-     */
-    if(psMemCheckCorruption(1)) {
-	psError(__func__, "Detected corrupted memory\n\n");
-    }
-    /*
-     * Check for memory leaks
-     */
-    psMemBlock **leaks = NULL;
-    int nleak = psMemCheckLeaks(0, &leaks, NULL);
-
-    if (nleak > 0) {
-	fprintf(stderr, "Memory is leaking (%d blocks):\n", nleak);
-	fprintf(stderr, "   %20s:line ID\n", "file");
-	for (int i = 0; i < nleak; i++) {
-	    fprintf(stderr, "   %20s:%-4d %ld\n",
-		    leaks[i]->file, leaks[i]->lineno, leaks[i]->id);
-	}
-	psFree(leaks);
-    }
-
-    if ((nleak = psMemCheckLeaks(0, NULL, NULL)) > 0) {
-	fprintf(stderr, "Memory is still leaking (%d blocks):\n", nleak);
-	psMemCheckLeaks(0, NULL, stderr);
-    }
-
-    return 0;
-}
-#endif
