Index: /branches/jch-memory/psLib/src/sys/psMemory.c
===================================================================
--- /branches/jch-memory/psLib/src/sys/psMemory.c	(revision 10885)
+++ /branches/jch-memory/psLib/src/sys/psMemory.c	(revision 10886)
@@ -8,6 +8,6 @@
 *  @author Robert Lupton, Princeton University
 *
-*  @version $Revision: 1.88.2.1 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2007-01-03 03:36:38 $
+*  @version $Revision: 1.88.2.2 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2007-01-03 04:29:10 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -38,4 +38,14 @@
 #include "psHistogram.h"
 
+#define MUTEX_LOCK(mutexPtr) \
+if (safeThreads) { \
+    pthread_mutex_lock(mutexPtr); \
+}
+
+#define MUTEX_UNLOCK(mutexPtr) \
+if (safeThreads) { \
+    pthread_mutex_unlock(mutexPtr); \
+}
+
 #define P_PS_LARGE_BLOCK_SIZE 65536        // size where under, we try to recycle
 
@@ -262,13 +272,9 @@
 psMemId psMemGetLastId(void)
 {
-    if (safeThreads) {
-        pthread_mutex_lock(&memIdMutex);
-    }
+    MUTEX_LOCK(&memIdMutex);
 
     psMemId id = memid;
 
-    if (safeThreads) {
-        pthread_mutex_unlock(&memIdMutex);
-    }
+    MUTEX_UNLOCK(&memIdMutex);
 
     return id;
@@ -281,14 +287,10 @@
 
     // get exclusive access to the memBlock list to avoid it changing on us while we use it.
-    //    pthread_mutex_lock(&memBlockListMutex);
+    //    MUTEX_LOCK(&memBlockListMutex);
 
     for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) {
-        if (safeThreads) {
-            pthread_mutex_unlock(&memBlockListMutex);
-        }
+        MUTEX_UNLOCK(&memBlockListMutex);
         failure = checkMemBlock(iter, __func__);
-        if (safeThreads) {
-            pthread_mutex_lock(&memBlockListMutex);
-        }
+        MUTEX_LOCK(&memBlockListMutex);
         if ( failure ) {
             nbad++;
@@ -298,7 +300,5 @@
             if (abort_on_error) {
                 // release the lock on the memblock list
-                if (safeThreads) {
-                    pthread_mutex_unlock(&memBlockListMutex);
-                }
+                MUTEX_UNLOCK(&memBlockListMutex);
                 psAbort(__func__, "Detected memory corruption");
                 return nbad;
@@ -308,7 +308,6 @@
 
     // release the lock on the memblock list
-    if (safeThreads) {
-        pthread_mutex_unlock(&memBlockListMutex);
-    }
+    MUTEX_UNLOCK(&memBlockListMutex);
+
     return nbad;
 }
@@ -353,11 +352,8 @@
     }
     // increment the memory id safely.
-    if (safeThreads) {
-        pthread_mutex_lock(&memBlockListMutex);
-    }
+    MUTEX_LOCK(&memBlockListMutex);
     *(psMemId* ) & ptr->id = ++memid;
-    if (safeThreads) {
-        pthread_mutex_unlock(&memBlockListMutex);
-    }
+    MUTEX_UNLOCK(&memBlockListMutex);
+
     ptr->file = file;
     ptr->freeFunc = NULL;
@@ -370,7 +366,5 @@
 
     // need exclusive access of the memory block list now...
-    if (safeThreads) {
-        pthread_mutex_lock(&memBlockListMutex);
-    }
+    MUTEX_LOCK(&memBlockListMutex);
 
     // insert the new block to the front of the memBlock linked-list
@@ -381,7 +375,5 @@
     lastMemBlockAllocated = ptr;
 
-    if (safeThreads) {
-        pthread_mutex_unlock(&memBlockListMutex);
-    }
+    MUTEX_UNLOCK(&memBlockListMutex);
 
     // Did the user ask to be informed about this allocation?
@@ -417,7 +409,5 @@
     // Reallocate the memory
 
-    if (safeThreads) {
-        pthread_mutex_lock(&memBlockListMutex);
-    }
+    MUTEX_LOCK(&memBlockListMutex);
 
     bool isBlockLast = (ptr == lastMemBlockAllocated); // Is this the last block we allocated?
@@ -445,7 +435,5 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_unlock(&memBlockListMutex);
-    }
+    MUTEX_UNLOCK(&memBlockListMutex);
 
     // Did the user ask to be informed about this allocation?
@@ -492,7 +480,5 @@
     psMemBlock* topBlock = lastMemBlockAllocated;
 
-    if (safeThreads) {
-        pthread_mutex_lock(&memBlockListMutex);
-    }
+    MUTEX_LOCK(&memBlockListMutex);
 
     for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
@@ -513,7 +499,5 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_unlock(&memBlockListMutex);
-    }
+    MUTEX_UNLOCK(&memBlockListMutex);
 
     if (nleak == 0 || array == NULL) {
@@ -522,7 +506,5 @@
 
     *array = p_psAlloc(nleak * sizeof(psMemBlock), __FILE__, __LINE__);
-    if (safeThreads) {
-        pthread_mutex_lock(&memBlockListMutex);
-    }
+    MUTEX_LOCK(&memBlockListMutex);
 
     for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
@@ -538,7 +520,5 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_unlock(&memBlockListMutex);
-    }
+    MUTEX_UNLOCK(&memBlockListMutex);
 
     return nleak;
@@ -565,11 +545,7 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_lock(&ptr2->refCounterMutex);
-    }
+    MUTEX_LOCK(&ptr2->refCounterMutex);
     refCount = ptr2->refCounter;
-    if (safeThreads) {
-        pthread_mutex_unlock(&ptr2->refCounterMutex);
-    }
+    MUTEX_UNLOCK(&ptr2->refCounterMutex);
 
     return refCount;
@@ -593,11 +569,7 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_lock(&ptr->refCounterMutex);
-    }
+    MUTEX_LOCK(&ptr->refCounterMutex);
     ptr->refCounter++;
-    if (safeThreads) {
-        pthread_mutex_unlock(&ptr->refCounterMutex);
-    }
+    MUTEX_UNLOCK(&ptr->refCounterMutex);
 
     // Did the user ask to be informed about this allocation?
@@ -630,11 +602,7 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_lock(&ptr->refCounterMutex);
-    }
+    MUTEX_LOCK(&ptr->refCounterMutex);
     ptr->refCounter = count;
-    if (safeThreads) {
-        pthread_mutex_unlock(&ptr->refCounterMutex);
-    }
+    MUTEX_UNLOCK(&ptr->refCounterMutex);
 
     if (count < 1) {
@@ -666,18 +634,12 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_lock(&ptr->refCounterMutex);
-    }
+    MUTEX_LOCK(&ptr->refCounterMutex);
 
     if (ptr->refCounter > 1) {
         ptr->refCounter--;                 // multiple references, just decrement the count.
-        if (safeThreads) {
-            pthread_mutex_unlock(&ptr->refCounterMutex);
-        }
+        MUTEX_UNLOCK(&ptr->refCounterMutex);
 
     } else {
-        if (safeThreads) {
-            pthread_mutex_unlock(&ptr->refCounterMutex);
-        }
+        MUTEX_UNLOCK(&ptr->refCounterMutex);
 
         if (ptr->freeFunc != NULL) {
@@ -685,7 +647,5 @@
         }
 
-        if (safeThreads) {
-            pthread_mutex_lock(&memBlockListMutex);
-        }
+        MUTEX_LOCK(&memBlockListMutex);
 
         // cut the memBlock out of the memBlock list
@@ -700,7 +660,5 @@
         }
 
-        if (safeThreads) {
-            pthread_mutex_unlock(&memBlockListMutex);
-        }
+        MUTEX_UNLOCK(&memBlockListMutex);
 
         vptr = NULL;                       // since we freed it, make sure we return NULL.
@@ -1072,7 +1030,5 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_lock(&memBlockListMutex);
-    }
+    MUTEX_LOCK(&memBlockListMutex);
     /*
      * All memory that's currently allocated, whether persistent or not
@@ -1107,7 +1063,5 @@
     }
 
-    if (safeThreads) {
-        pthread_mutex_unlock(&memBlockListMutex);
-    }
+    MUTEX_UNLOCK(&memBlockListMutex);
 
     return *allocated + *persistent;
