Index: /trunk/psLib/src/sys/psMemory.c
===================================================================
--- /trunk/psLib/src/sys/psMemory.c	(revision 450)
+++ /trunk/psLib/src/sys/psMemory.c	(revision 451)
@@ -8,6 +8,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-04-16 02:18:57 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-04-19 20:14:04 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -16,4 +16,5 @@
 #define PS_ALLOW_MALLOC                 // we're allowed to call malloc()
 
+#include <pthread.h>                   // we need a mutex to make this stuff thread safe.
 #include <stdlib.h>
 #include <stdio.h>
@@ -25,6 +26,6 @@
 
 static int checkMemBlock(const psMemBlock *m, const char* funcName);
-static int numMemBlock = 0;             // size of memBlocks
-static psMemBlock **memBlocks = NULL;
+static psMemBlock *lastMemBlockAllocated = NULL;
+pthread_mutex_t   memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
 
 /**
@@ -183,7 +184,10 @@
 
 static int
-checkMemBlock(const psMemBlock *m, // block to check
-             const char* funcName)   // be quiet?
-{
+checkMemBlock(const psMemBlock *m,
+             const char* funcName)
+{
+    // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked,
+    // we shouldn't call such things as p_psAlloc/p_psFree here.
+
     if (m == NULL) {
         psError(funcName,"Memory Corruption: NULL memory block found.");
@@ -206,24 +210,27 @@
 int psMemCheckCorruption(int abort_on_error)
 {
-    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++) {
-        if (memBlocks[i] != NULL) {
-            if (checkMemBlock(memBlocks[i], __func__)) {
-                nbad++;
-
-                memProblemCallback(memBlocks[i], __func__, __LINE__);
-
-                if (abort_on_error) {
-                    psAbort(__func__, "Detected memory corruption");
-                }
-            }
+    int nbad = 0;                       // number of bad blocks
+
+	// get exclusive access to the memBlock list to avoid it changing on us while we use it.
+	pthread_mutex_lock(&memBlockListMutex);
+
+    for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock)
+{
+        if (checkMemBlock(iter, __func__)) {
+			nbad++;
+
+			memProblemCallback(iter, __func__, __LINE__);
+
+			if (abort_on_error) {
+				// release the lock on the memblock list
+            	pthread_mutex_unlock(&memBlockListMutex);
+				psAbort(__func__, "Detected memory corruption");
+				return nbad;
+			}
         }
     }
 
+	// release the lock on the memblock list
+	pthread_mutex_unlock(&memBlockListMutex);
     return nbad;
 }
@@ -243,27 +250,31 @@
     }
 
-    *(unsigned long *)&ptr->id = ++memid;
+    *(unsigned long*)&ptr->id = ++memid;
     ptr->file = file;
-    *(int *)&ptr->lineno = lineno;
+    *(unsigned int*)&ptr->lineno = lineno;
     ptr->startblock = P_PS_MEMMAGIC;
 	ptr->endblock = P_PS_MEMMAGIC;
-
-    ptr->refCounter = 1;  // one user so far
-    /*
-     * Did the user ask to be informed about this allocation?
-     */
-
-    if (ptr->id == p_psMemAllocateID && p_psMemAllocateID > 0) {
+    ptr->refCounter = 1;                // one user so far
+
+	// need exclusive access of the memory block list now...
+	pthread_mutex_lock(&memBlockListMutex);
+
+	// insert the new block to the front of the memBlock linked-list
+	ptr->previousBlock = NULL;
+	ptr->nextBlock = lastMemBlockAllocated;
+	if (ptr->nextBlock != NULL) {
+	    ptr->nextBlock->previousBlock = ptr;
+	}
+	lastMemBlockAllocated = ptr;
+
+	pthread_mutex_unlock(&memBlockListMutex);
+
+    //  Did the user ask to be informed about this allocation?
+    if (ptr->id == p_psMemAllocateID) {
         p_psMemAllocateID += memAllocateCallback(ptr);
     }
-    if (memid >= numMemBlock) {
-        numMemBlock = (numMemBlock == 0) ? 100000 : 2*numMemBlock;
-        memBlocks = realloc(memBlocks, numMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
-    }
-    memBlocks[ptr->id] = ptr;
-    /*
-     * And return the user the memory that they allocated
-     */
-    return ptr + 1;   // usr memory
+
+	// And return the user the memory that they allocated
+    return ptr + 1;   // user memory
 }
 
@@ -274,8 +285,20 @@
     } else {
         psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
-        ptr = (psMemBlock *)realloc(ptr, sizeof(psMemBlock) + size);
-        memBlocks[ptr->id] = ptr;
-
-        return ptr + 1;   // usr memory
+
+    	pthread_mutex_lock(&memBlockListMutex);
+
+		ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);
+
+		// the block location may have changed, so fix the linked list addresses.
+		if (ptr->nextBlock != NULL) {
+		    ptr->nextBlock->previousBlock = ptr;
+		}
+		if (ptr->previousBlock != NULL) {
+		    ptr->previousBlock->nextBlock = ptr;
+		}
+
+	    pthread_mutex_unlock(&memBlockListMutex);
+
+		return ptr + 1;   // usr memory
     }
 }
@@ -288,8 +311,7 @@
 
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
-    /*
-     * Did the user ask to be informed about this deallocation?
-     */
-    if (ptr->id == p_psMemFreeID && p_psMemFreeID > 0) {
+
+	// Did the user ask to be informed about this deallocation?
+    if (ptr->id == p_psMemFreeID) {
         p_psMemFreeID += memFreeCallback(ptr);
     }
@@ -298,73 +320,80 @@
         memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
     } else {
-        if (ptr->refCounter != 1) {
+        if (ptr->refCounter > 1) {
+		    psError(__func__,"The buffer being freed is referenced elsewhere. "
+			        "Buffer's reference count was decremented instead.");
+            ptr->refCounter--;
             memProblemCallback(ptr, file, lineno);
+        } else {
+
+			// cut the memBlock out of the memBlock list
+        	pthread_mutex_lock(&memBlockListMutex);
+			if (ptr->nextBlock != NULL) {
+			    ptr->nextBlock->previousBlock = ptr->previousBlock;
+			}
+			if (ptr->previousBlock != NULL) {
+			    ptr->previousBlock->nextBlock = ptr->nextBlock;
+			}
+			if (lastMemBlockAllocated == ptr) {
+			    lastMemBlockAllocated = ptr->nextBlock;
+			}
+
+        	pthread_mutex_unlock(&memBlockListMutex);
+
+		    free(ptr);
+		}
+    }
+}
+
+/*****************************************************************************/
+/*
+ * Check for memory leaks. Not production quality code
+ */
+int psMemCheckLeaks(
+    int id0,                            // don't list blocks with id < id0
+    psMemBlock ***arr,                  // pointer to array of pointers to leaked blocks, or NULL
+    FILE *fd)                           // print list of leaks to fd (or NULL)
+{
+    int nleak = 0;
+	int j = 0;
+
+	pthread_mutex_lock(&memBlockListMutex);
+
+    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) {
+        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
+			nleak++;
+
+			if (fd != NULL) {
+				if (nleak == 1) {
+					fprintf(fd, "   %20s:line ID\n", "file");
+				}
+
+				fprintf(fd, "   %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id);
+			}
+		}
+    }
+
+	pthread_mutex_unlock(&memBlockListMutex);
+
+	if (nleak == 0 || arr == NULL) {
+        return nleak;
+    }
+
+    p_psFree(*arr,__FILE__, __LINE__);  // Don't generate a memory leak!
+
+    *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
+
+	pthread_mutex_lock(&memBlockListMutex);
+
+    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) {
+        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
+			(*arr)[j++] = iter;
+			if (j == nleak) { // found them all
+				break;
+			}
         }
-        ptr->refCounter--;
-        #if 1
-
-        memBlocks[ptr->id] = NULL; // XXX sub-optimal! Can't check
-        // freed blocks for corruption/double frees
-        free(ptr);
-        #endif
-
-    }
-}
-
-/*****************************************************************************/
-/*
- * Check for memory leaks. Not production quality code
- */
-int psMemCheckLeaks(
-    int id0,    // don't list blocks with id < id0
-    psMemBlock ***arr,   // pointer to array of pointers to
-    // leaked blocks, or NULL
-    FILE *fd)    // print list of leaks to fd (or NULL)
-{
-    int nleak = 0;
-
-    if (memBlocks == NULL)
-    {  // nothing was allocated
-        return 0;
-    }
-
-    for (int i = id0; i <= memid; i++)
-    {
-        if (memBlocks[i] != NULL) {
-            if (memBlocks[i]->refCounter > 0) {
-                nleak++;
-
-                if (fd != NULL) {
-                    if (nleak == 1) {
-                        fprintf(fd, "   %20s:line ID\n", "file");
-                    }
-
-                    fprintf(fd, "   %20s:%-4d %ld\n",
-                            memBlocks[i]->file, memBlocks[i]->lineno,
-                            memBlocks[i]->id);
-                }
-            }
-        }
-    }
-
-    if (nleak == 0 || arr == NULL)
-    {
-        return nleak;
-    }
-
-    p_psFree(*arr,__FILE__, __LINE__);  // Don't generate a memory leak!
-
-    *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
-    for (int i = id0, j = 0; i < memid; i++)
-    {
-        if (memBlocks[i] != NULL) {
-            if (memBlocks[i]->refCounter > 0) {
-                (*arr)[j++] = memBlocks[i];
-                if (j == nleak) { // found them all
-                    break;
-                }
-            }
-        }
-    }
+    }
+
+    pthread_mutex_unlock(&memBlockListMutex);
 
     return nleak;
@@ -389,6 +418,5 @@
 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
 {
-    if (vptr == NULL)
-    {
+    if (vptr == NULL) {
         return vptr;
     }
@@ -396,6 +424,5 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (checkMemBlock(ptr, __func__))
-    {
+    if (checkMemBlock(ptr, __func__)) {
         memProblemCallback(ptr, __func__, __LINE__);
     }
@@ -408,6 +435,5 @@
 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
 {
-    if (vptr == NULL)
-    {
+    if (vptr == NULL) {
         return vptr;
     }
@@ -415,6 +441,5 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (checkMemBlock(ptr, __func__))
-    {
+    if (checkMemBlock(ptr, __func__)) {
         memProblemCallback(ptr, __func__, __LINE__);
     }
Index: /trunk/psLib/src/sys/psMemory.h
===================================================================
--- /trunk/psLib/src/sys/psMemory.h	(revision 450)
+++ /trunk/psLib/src/sys/psMemory.h	(revision 451)
@@ -14,6 +14,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-04-16 02:18:57 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-04-19 20:14:04 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -25,13 +25,14 @@
  *  aligned for all storage types.
  */
-typedef struct
+typedef struct psMemBlock
 {
-    const void *startblock;             ///< initialised to p_psMEMMAGIC
+    const void* startblock;             ///< initialised to p_psMEMMAGIC
+	struct psMemBlock* previousBlock;   ///< previous block in allocation list
+	struct psMemBlock* nextBlock;       ///< next block allocation list
     const unsigned long id;             ///< a unique ID for this allocation
-    const char *file;                   ///< set from __FILE__ in e.g. p_psAlloc
+    const char* file;                   ///< set from __FILE__ in e.g. p_psAlloc
     const int lineno;                   ///< set from __LINE__ in e.g. p_psAlloc
-    int refCounter;                     ///< how many times pointer is referenced
-    const void **endpost;               ///< initialised to p_psMEMMAGIC
-    const void *endblock;               ///< initialised to p_psMEMMAGIC
+    unsigned int refCounter;            ///< how many times pointer is referenced
+    const void* endblock;               ///< initialised to p_psMEMMAGIC
 }
 psMemBlock;
@@ -43,5 +44,9 @@
 typedef long (*psMemFreeCallback)(const psMemBlock *ptr);
 
-/// prototype of a callback used in error conditions
+/** prototype of a callback used in error conditions
+ *
+ *  This callback should never try to call psAlloc or psFree.
+ *
+ */
 typedef void (*psMemProblemCallback)(const psMemBlock *ptr, const char *file, int lineno);
 
Index: /trunk/psLib/src/sysUtils/psMemory.c
===================================================================
--- /trunk/psLib/src/sysUtils/psMemory.c	(revision 450)
+++ /trunk/psLib/src/sysUtils/psMemory.c	(revision 451)
@@ -8,6 +8,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-04-16 02:18:57 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-04-19 20:14:04 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -16,4 +16,5 @@
 #define PS_ALLOW_MALLOC                 // we're allowed to call malloc()
 
+#include <pthread.h>                   // we need a mutex to make this stuff thread safe.
 #include <stdlib.h>
 #include <stdio.h>
@@ -25,6 +26,6 @@
 
 static int checkMemBlock(const psMemBlock *m, const char* funcName);
-static int numMemBlock = 0;             // size of memBlocks
-static psMemBlock **memBlocks = NULL;
+static psMemBlock *lastMemBlockAllocated = NULL;
+pthread_mutex_t   memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
 
 /**
@@ -183,7 +184,10 @@
 
 static int
-checkMemBlock(const psMemBlock *m, // block to check
-             const char* funcName)   // be quiet?
-{
+checkMemBlock(const psMemBlock *m,
+             const char* funcName)
+{
+    // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked,
+    // we shouldn't call such things as p_psAlloc/p_psFree here.
+
     if (m == NULL) {
         psError(funcName,"Memory Corruption: NULL memory block found.");
@@ -206,24 +210,27 @@
 int psMemCheckCorruption(int abort_on_error)
 {
-    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++) {
-        if (memBlocks[i] != NULL) {
-            if (checkMemBlock(memBlocks[i], __func__)) {
-                nbad++;
-
-                memProblemCallback(memBlocks[i], __func__, __LINE__);
-
-                if (abort_on_error) {
-                    psAbort(__func__, "Detected memory corruption");
-                }
-            }
+    int nbad = 0;                       // number of bad blocks
+
+	// get exclusive access to the memBlock list to avoid it changing on us while we use it.
+	pthread_mutex_lock(&memBlockListMutex);
+
+    for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock)
+{
+        if (checkMemBlock(iter, __func__)) {
+			nbad++;
+
+			memProblemCallback(iter, __func__, __LINE__);
+
+			if (abort_on_error) {
+				// release the lock on the memblock list
+            	pthread_mutex_unlock(&memBlockListMutex);
+				psAbort(__func__, "Detected memory corruption");
+				return nbad;
+			}
         }
     }
 
+	// release the lock on the memblock list
+	pthread_mutex_unlock(&memBlockListMutex);
     return nbad;
 }
@@ -243,27 +250,31 @@
     }
 
-    *(unsigned long *)&ptr->id = ++memid;
+    *(unsigned long*)&ptr->id = ++memid;
     ptr->file = file;
-    *(int *)&ptr->lineno = lineno;
+    *(unsigned int*)&ptr->lineno = lineno;
     ptr->startblock = P_PS_MEMMAGIC;
 	ptr->endblock = P_PS_MEMMAGIC;
-
-    ptr->refCounter = 1;  // one user so far
-    /*
-     * Did the user ask to be informed about this allocation?
-     */
-
-    if (ptr->id == p_psMemAllocateID && p_psMemAllocateID > 0) {
+    ptr->refCounter = 1;                // one user so far
+
+	// need exclusive access of the memory block list now...
+	pthread_mutex_lock(&memBlockListMutex);
+
+	// insert the new block to the front of the memBlock linked-list
+	ptr->previousBlock = NULL;
+	ptr->nextBlock = lastMemBlockAllocated;
+	if (ptr->nextBlock != NULL) {
+	    ptr->nextBlock->previousBlock = ptr;
+	}
+	lastMemBlockAllocated = ptr;
+
+	pthread_mutex_unlock(&memBlockListMutex);
+
+    //  Did the user ask to be informed about this allocation?
+    if (ptr->id == p_psMemAllocateID) {
         p_psMemAllocateID += memAllocateCallback(ptr);
     }
-    if (memid >= numMemBlock) {
-        numMemBlock = (numMemBlock == 0) ? 100000 : 2*numMemBlock;
-        memBlocks = realloc(memBlocks, numMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
-    }
-    memBlocks[ptr->id] = ptr;
-    /*
-     * And return the user the memory that they allocated
-     */
-    return ptr + 1;   // usr memory
+
+	// And return the user the memory that they allocated
+    return ptr + 1;   // user memory
 }
 
@@ -274,8 +285,20 @@
     } else {
         psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
-        ptr = (psMemBlock *)realloc(ptr, sizeof(psMemBlock) + size);
-        memBlocks[ptr->id] = ptr;
-
-        return ptr + 1;   // usr memory
+
+    	pthread_mutex_lock(&memBlockListMutex);
+
+		ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);
+
+		// the block location may have changed, so fix the linked list addresses.
+		if (ptr->nextBlock != NULL) {
+		    ptr->nextBlock->previousBlock = ptr;
+		}
+		if (ptr->previousBlock != NULL) {
+		    ptr->previousBlock->nextBlock = ptr;
+		}
+
+	    pthread_mutex_unlock(&memBlockListMutex);
+
+		return ptr + 1;   // usr memory
     }
 }
@@ -288,8 +311,7 @@
 
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
-    /*
-     * Did the user ask to be informed about this deallocation?
-     */
-    if (ptr->id == p_psMemFreeID && p_psMemFreeID > 0) {
+
+	// Did the user ask to be informed about this deallocation?
+    if (ptr->id == p_psMemFreeID) {
         p_psMemFreeID += memFreeCallback(ptr);
     }
@@ -298,73 +320,80 @@
         memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
     } else {
-        if (ptr->refCounter != 1) {
+        if (ptr->refCounter > 1) {
+		    psError(__func__,"The buffer being freed is referenced elsewhere. "
+			        "Buffer's reference count was decremented instead.");
+            ptr->refCounter--;
             memProblemCallback(ptr, file, lineno);
+        } else {
+
+			// cut the memBlock out of the memBlock list
+        	pthread_mutex_lock(&memBlockListMutex);
+			if (ptr->nextBlock != NULL) {
+			    ptr->nextBlock->previousBlock = ptr->previousBlock;
+			}
+			if (ptr->previousBlock != NULL) {
+			    ptr->previousBlock->nextBlock = ptr->nextBlock;
+			}
+			if (lastMemBlockAllocated == ptr) {
+			    lastMemBlockAllocated = ptr->nextBlock;
+			}
+
+        	pthread_mutex_unlock(&memBlockListMutex);
+
+		    free(ptr);
+		}
+    }
+}
+
+/*****************************************************************************/
+/*
+ * Check for memory leaks. Not production quality code
+ */
+int psMemCheckLeaks(
+    int id0,                            // don't list blocks with id < id0
+    psMemBlock ***arr,                  // pointer to array of pointers to leaked blocks, or NULL
+    FILE *fd)                           // print list of leaks to fd (or NULL)
+{
+    int nleak = 0;
+	int j = 0;
+
+	pthread_mutex_lock(&memBlockListMutex);
+
+    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) {
+        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
+			nleak++;
+
+			if (fd != NULL) {
+				if (nleak == 1) {
+					fprintf(fd, "   %20s:line ID\n", "file");
+				}
+
+				fprintf(fd, "   %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id);
+			}
+		}
+    }
+
+	pthread_mutex_unlock(&memBlockListMutex);
+
+	if (nleak == 0 || arr == NULL) {
+        return nleak;
+    }
+
+    p_psFree(*arr,__FILE__, __LINE__);  // Don't generate a memory leak!
+
+    *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
+
+	pthread_mutex_lock(&memBlockListMutex);
+
+    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) {
+        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
+			(*arr)[j++] = iter;
+			if (j == nleak) { // found them all
+				break;
+			}
         }
-        ptr->refCounter--;
-        #if 1
-
-        memBlocks[ptr->id] = NULL; // XXX sub-optimal! Can't check
-        // freed blocks for corruption/double frees
-        free(ptr);
-        #endif
-
-    }
-}
-
-/*****************************************************************************/
-/*
- * Check for memory leaks. Not production quality code
- */
-int psMemCheckLeaks(
-    int id0,    // don't list blocks with id < id0
-    psMemBlock ***arr,   // pointer to array of pointers to
-    // leaked blocks, or NULL
-    FILE *fd)    // print list of leaks to fd (or NULL)
-{
-    int nleak = 0;
-
-    if (memBlocks == NULL)
-    {  // nothing was allocated
-        return 0;
-    }
-
-    for (int i = id0; i <= memid; i++)
-    {
-        if (memBlocks[i] != NULL) {
-            if (memBlocks[i]->refCounter > 0) {
-                nleak++;
-
-                if (fd != NULL) {
-                    if (nleak == 1) {
-                        fprintf(fd, "   %20s:line ID\n", "file");
-                    }
-
-                    fprintf(fd, "   %20s:%-4d %ld\n",
-                            memBlocks[i]->file, memBlocks[i]->lineno,
-                            memBlocks[i]->id);
-                }
-            }
-        }
-    }
-
-    if (nleak == 0 || arr == NULL)
-    {
-        return nleak;
-    }
-
-    p_psFree(*arr,__FILE__, __LINE__);  // Don't generate a memory leak!
-
-    *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
-    for (int i = id0, j = 0; i < memid; i++)
-    {
-        if (memBlocks[i] != NULL) {
-            if (memBlocks[i]->refCounter > 0) {
-                (*arr)[j++] = memBlocks[i];
-                if (j == nleak) { // found them all
-                    break;
-                }
-            }
-        }
-    }
+    }
+
+    pthread_mutex_unlock(&memBlockListMutex);
 
     return nleak;
@@ -389,6 +418,5 @@
 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
 {
-    if (vptr == NULL)
-    {
+    if (vptr == NULL) {
         return vptr;
     }
@@ -396,6 +424,5 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (checkMemBlock(ptr, __func__))
-    {
+    if (checkMemBlock(ptr, __func__)) {
         memProblemCallback(ptr, __func__, __LINE__);
     }
@@ -408,6 +435,5 @@
 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
 {
-    if (vptr == NULL)
-    {
+    if (vptr == NULL) {
         return vptr;
     }
@@ -415,6 +441,5 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (checkMemBlock(ptr, __func__))
-    {
+    if (checkMemBlock(ptr, __func__)) {
         memProblemCallback(ptr, __func__, __LINE__);
     }
Index: /trunk/psLib/src/sysUtils/psMemory.h
===================================================================
--- /trunk/psLib/src/sysUtils/psMemory.h	(revision 450)
+++ /trunk/psLib/src/sysUtils/psMemory.h	(revision 451)
@@ -14,6 +14,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-04-16 02:18:57 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-04-19 20:14:04 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -25,13 +25,14 @@
  *  aligned for all storage types.
  */
-typedef struct
+typedef struct psMemBlock
 {
-    const void *startblock;             ///< initialised to p_psMEMMAGIC
+    const void* startblock;             ///< initialised to p_psMEMMAGIC
+	struct psMemBlock* previousBlock;   ///< previous block in allocation list
+	struct psMemBlock* nextBlock;       ///< next block allocation list
     const unsigned long id;             ///< a unique ID for this allocation
-    const char *file;                   ///< set from __FILE__ in e.g. p_psAlloc
+    const char* file;                   ///< set from __FILE__ in e.g. p_psAlloc
     const int lineno;                   ///< set from __LINE__ in e.g. p_psAlloc
-    int refCounter;                     ///< how many times pointer is referenced
-    const void **endpost;               ///< initialised to p_psMEMMAGIC
-    const void *endblock;               ///< initialised to p_psMEMMAGIC
+    unsigned int refCounter;            ///< how many times pointer is referenced
+    const void* endblock;               ///< initialised to p_psMEMMAGIC
 }
 psMemBlock;
@@ -43,5 +44,9 @@
 typedef long (*psMemFreeCallback)(const psMemBlock *ptr);
 
-/// prototype of a callback used in error conditions
+/** prototype of a callback used in error conditions
+ *
+ *  This callback should never try to call psAlloc or psFree.
+ *
+ */
 typedef void (*psMemProblemCallback)(const psMemBlock *ptr, const char *file, int lineno);
 
