Index: trunk/psLib/src/sys/psMemory.c
===================================================================
--- trunk/psLib/src/sys/psMemory.c	(revision 803)
+++ trunk/psLib/src/sys/psMemory.c	(revision 830)
@@ -8,6 +8,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-28 03:17:39 $
+ *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-06-02 20:03:22 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -16,5 +16,4 @@
 #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,4 +24,5 @@
 #include "psError.h"
 #include "psAbort.h"
+#include "psLogMsg.h"
 
 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header
@@ -67,8 +67,4 @@
         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) {
-        psError(__func__,
-                "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
                 ptr->id, ptr->file, ptr->lineno, file, lineno);
     }
@@ -268,8 +264,10 @@
     ptr->startblock = P_PS_MEMMAGIC;
     ptr->endblock = P_PS_MEMMAGIC;
-    ptr->refCounter = 1;                // one user so far
     ptr->userMemorySize = size;
     *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC;
     ptr->previousBlock = NULL;
+
+    pthread_mutex_init(&ptr->refCounterMutex, NULL);
+    ptr->refCounter = 1;                // one user so far
 
     // need exclusive access of the memory block list now...
@@ -347,36 +345,9 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    // Did the user ask to be informed about this deallocation?
-    if (ptr->id == p_psMemFreeID) {
-        p_psMemFreeID += memFreeCallback(ptr);
-    }
-
-    if (checkMemBlock(ptr, "psFree") != 0) {
+    if (checkMemBlock(ptr, __func__) != 0) {
         memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
-    } else {
-        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);
-        }
-    }
+    }
+
+    psMemDecrRefCounter(vptr);          // this handles the free, if required.
 }
 
@@ -397,5 +368,5 @@
     for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
     {
-        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
+        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
             nleak++;
 
@@ -422,5 +393,5 @@
     for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
     {
-        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
+        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
             (*arr)[j++] = iter;
             if (j == nleak) { // found them all
@@ -440,49 +411,97 @@
 psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter
 {
+    psMemBlock *ptr;
+    unsigned int refCount;
+
+    if (vptr == NULL)
+    {
+        return 0;
+    }
+
+    ptr = ((psMemBlock *)vptr) - 1;
+
+    if (checkMemBlock(ptr, __func__) != 0)
+    {
+        memProblemCallback(ptr, __func__, __LINE__);
+    }
+
+    pthread_mutex_lock(&ptr->refCounterMutex);
+    refCount = ptr->refCounter;
+    pthread_mutex_unlock(&ptr->refCounterMutex);
+
+    return refCount;
+}
+
+void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
+{
+    psMemBlock *ptr;
+
+    if (vptr == NULL)
+    {
+        return vptr;
+    }
+
+    ptr = ((psMemBlock *)vptr) - 1;
+
+    if (checkMemBlock(ptr, __func__))
+    {
+        memProblemCallback(ptr, __func__, __LINE__);
+    }
+
+    pthread_mutex_lock(&ptr->refCounterMutex);
+    ptr->refCounter++;
+    pthread_mutex_unlock(&ptr->refCounterMutex);
+
+    return vptr;
+}
+
+void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
+{
+    if (vptr == NULL)
+    {
+        return NULL;
+    }
+
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (checkMemBlock(ptr, __func__) != 0)
-    {
-        memProblemCallback(ptr, __func__, __LINE__);
-    }
-
-    return ptr->refCounter;
-}
-
-void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
-{
-    if (vptr == NULL)
-    {
-        return vptr;
-    }
-
-    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
-
-    if (checkMemBlock(ptr, __func__))
-    {
-        memProblemCallback(ptr, __func__, __LINE__);
-    }
-
-    ptr->refCounter++;
+
+    pthread_mutex_lock(&ptr->refCounterMutex);
+
+    if (ptr->refCounter > 1)
+    {
+        /// XXX - Probably should have another mutex here.
+        ptr->refCounter--;          // multiple references, just decrement the count.
+        pthread_mutex_unlock(&ptr->refCounterMutex);
+
+    } else
+    {
+        pthread_mutex_unlock(&ptr->refCounterMutex);
+
+        // Did the user ask to be informed about this deallocation?
+        if (ptr->id == p_psMemFreeID) {
+            p_psMemFreeID += memFreeCallback(ptr);
+        }
+
+        pthread_mutex_lock(&memBlockListMutex);
+
+        // cut the memBlock out of the memBlock list
+        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);
+
+        vptr = NULL;    // since we freed it, make sure we return NULL.
+    }
 
     return vptr;
 }
 
-void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
-{
-    if (vptr == NULL)
-    {
-        return vptr;
-    }
-
-    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
-
-    if (checkMemBlock(ptr, __func__))
-    {
-        memProblemCallback(ptr, __func__, __LINE__);
-    }
-
-    ptr->refCounter--;
-
-    return vptr;
-}
