Index: trunk/psLib/src/sys/psMemory.c
===================================================================
--- trunk/psLib/src/sys/psMemory.c	(revision 21337)
+++ trunk/psLib/src/sys/psMemory.c	(revision 21346)
@@ -10,6 +10,6 @@
 *  @author Joshua Hoblitt, University of Hawaii
 *
-*  @version $Revision: 1.100 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2008-08-20 02:00:20 $
+*  @version $Revision: 1.101 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2009-02-06 01:05:30 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -79,17 +79,16 @@
 //      memFreeCallback -- very rarely accessed
 //      memExhaustedCallback -- very rarely accessed
-//      p_psMemAllocID -- rarely accessed
-//      p_psMemFreeID -- rarely accessed
+//      memAllocID -- rarely accessed
+//      memFreeID -- rarely accessed
 //      lastMemBlockAllocated
 //      memid -- rarely accessby
 //      "the linked list of mem blocks"
 //
-// This is a fair ammount of stuff to protect with a single mutex but most of
+// This is a fair amount of stuff to protect with a single mutex but most of
 // these items are *VERY* low contention items.  The only item that should be
 // performance issue is "the linked list of mem blocks".  If this does become a
-// problem in production use of the list should be disabled as it is really a
+// problem in production use of the list could be disabled as it is largely a
 // debugging feature.
 //
-// XXX make the mem block list a build time option
 //
 static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
@@ -158,5 +157,5 @@
 static psMemId memAllocCallbackDefault(const psMemBlock *memBlock)
 {
-    static psMemId incr = 0; // "p_psMemAllocID += incr"
+    static psMemId incr = 0; // "memAllocID += incr"
 
     return incr;
@@ -184,6 +183,5 @@
 
 // notify user when this block is allocated
-// XXX why was this not static, and why was it p_psMemAllocID?
-static psMemId p_psMemAllocID = 0;
+static psMemId memAllocID = 0;
 
 // the above callback is only called if a specific psMemId is set with this function
@@ -194,7 +192,7 @@
     MUTEX_LOCK(&memBlockListMutex);
 
-    psMemId old = p_psMemAllocID;
-
-    p_psMemAllocID = id;
+    psMemId old = memAllocID;
+
+    memAllocID = id;
 
     MUTEX_UNLOCK(&memBlockListMutex);
@@ -208,5 +206,5 @@
 static psMemId memFreeCallbackDefault(const psMemBlock *memBlock)
 {
-    static psMemId incr = 0; // "p_psMemFreeID += incr"
+    static psMemId incr = 0; // "memFreeID += incr"
 
     return incr;
@@ -235,6 +233,5 @@
 
 // notify user when this block is freed
-// XXX why was this not static?
-static psMemId p_psMemFreeID = 0;
+static psMemId memFreeID = 0;
 
 // the above callback is only called if a specific psMemId is set with this function
@@ -244,7 +241,7 @@
     MUTEX_LOCK(&memBlockListMutex);
 
-    psMemId old = p_psMemFreeID;
-
-    p_psMemFreeID = id;
+    psMemId old = memFreeID;
+
+    memFreeID = id;
 
     MUTEX_UNLOCK(&memBlockListMutex);
@@ -286,5 +283,5 @@
 psMemId memAllocCallbackCheckCorruption(const psMemBlock *memBlock)
 {
-    static psMemId incr = 10; // "p_psMemAllocID += incr"
+    static psMemId incr = 10; // "memAllocID += incr"
 
     if (psMemCheckCorruption(stderr, false) > 0) {
@@ -412,7 +409,7 @@
 
     // Did the user ask to be informed about this allocation?
-    if (memBlock->id == p_psMemAllocID) {
-        // p_psMemAllocID can only be changed while the memBlockList mutex is held
-        p_psMemAllocID += memAllocCallback(memBlock);
+    if (memBlock->id == memAllocID) {
+        // memAllocID can only be changed while the memBlockList mutex is held
+        memAllocID += memAllocCallback(memBlock);
     }
 
@@ -575,6 +572,6 @@
 
     // Did the user ask to be informed about this allocation?
-    if (memBlock->id == p_psMemAllocID) {
-        p_psMemAllocID += memAllocCallback(memBlock);
+    if (memBlock->id == memAllocID) {
+        memAllocID += memAllocCallback(memBlock);
     }
 
@@ -734,7 +731,9 @@
     HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
 
-    // XXX we probably need a MUTEX_LOCK here: otherwise, two functions can race on recCounter++ and
+    // we need a MUTEX_LOCK here: otherwise, two functions can race on recCounter++ and
     // refCounter--;
+    MUTEX_LOCK(&memBlockListMutex);
     memBlock->refCounter++;
+    MUTEX_UNLOCK(&memBlockListMutex);
 
     // Did the user ask to be informed about this allocation?
@@ -742,7 +741,7 @@
     // this lock is probably not needed: if someone changes the callback ID in a different thread,
     // do we really care about really rarely getting this wrong?
-    if (memBlock->id == p_psMemAllocID) {
+    if (memBlock->id == memAllocID) {
         MUTEX_LOCK(&memBlockListMutex);
-        p_psMemAllocID += memAllocCallback(memBlock);
+        memAllocID += memAllocCallback(memBlock);
         MUTEX_UNLOCK(&memBlockListMutex);
     }
@@ -801,16 +800,17 @@
 
     // if we have multiple references, just decrement the count and return.
-    // XXX we probably need a MUTEX_LOCK here: otherwise, two functions can race on refCounter--;
+    // we need a MUTEX_LOCK here: otherwise, two functions can race on refCounter--;
+    MUTEX_LOCK(&memBlockListMutex);
     if (memBlock->refCounter > 1) {
         memBlock->refCounter--;
 
         // Did the user ask to be informed about this deallocation?
-        MUTEX_LOCK(&memBlockListMutex);
-        if (memBlock->id == p_psMemFreeID) {
-            p_psMemFreeID += memFreeCallback(memBlock);
+        if (memBlock->id == memFreeID) {
+            memFreeID += memFreeCallback(memBlock);
         }
         MUTEX_UNLOCK(&memBlockListMutex);
         return ptr;
     }
+    MUTEX_UNLOCK(&memBlockListMutex);
 
     // we can't invoke freeFunc() while we're holding memBlockListMutex as it
@@ -824,6 +824,6 @@
 
     // Did the user ask to be informed about this deallocation?
-    if (memBlock->id == p_psMemFreeID) {
-        p_psMemFreeID += memFreeCallback(ptr);
+    if (memBlock->id == memFreeID) {
+        memFreeID += memFreeCallback(ptr);
     }
 
