Index: trunk/psLib/src/sysUtils/psMemory.c
===================================================================
--- trunk/psLib/src/sysUtils/psMemory.c	(revision 876)
+++ trunk/psLib/src/sysUtils/psMemory.c	(revision 1073)
@@ -8,6 +8,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-06-04 23:46:48 $
+ *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-06-23 23:00:15 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -30,6 +30,6 @@
 static int checkMemBlock(const psMemBlock *m, const char* funcName);
 static psMemBlock *lastMemBlockAllocated = NULL;
-pthread_mutex_t   memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
-pthread_mutex_t   memIdMutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t memIdMutex = PTHREAD_MUTEX_INITIALIZER;
 
 /**
@@ -96,5 +96,5 @@
 psMemoryId p_psMemFreeID = 0;   // notify user this block is freed
 
-psMemoryId psMemAllocateCallbackSetID(psMemoryId id) // set p_psMemAllocateID to id
+psMemoryId psMemAllocateCallbackSetID(psMemoryId id)
 {
     psMemoryId old = p_psMemAllocateID;
@@ -104,5 +104,5 @@
 }
 
-psMemoryId psMemFreeCallbackSetID(psMemoryId id)  // set p_psMemFreeID to id
+psMemoryId psMemFreeCallbackSetID(psMemoryId id)
 {
     psMemoryId old = p_psMemFreeID;
@@ -184,7 +184,5 @@
 #define ALIGNED(P) ((void *)((long)(P) & ~03) == (P))
 
-static int
-checkMemBlock(const psMemBlock *m,
-              const char* funcName)
+static int checkMemBlock(const psMemBlock *m, const char* funcName)
 {
     // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked,
@@ -260,4 +258,5 @@
 
     ptr->file = file;
+    ptr->freeFcn = NULL;
     *(unsigned int*)&ptr->lineno = lineno;
     ptr->startblock = P_PS_MEMMAGIC;
@@ -346,7 +345,8 @@
     if (checkMemBlock(ptr, __func__) != 0) {
         memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
-    }
-
-    psMemDecrRefCounter(vptr);          // this handles the free, if required.
+        return;
+    }
+
+    (void)psMemDecrRefCounter(vptr);   // this handles the free, if required.
 }
 
@@ -354,8 +354,5 @@
  * Check for memory leaks. Not production quality code
  */
-int psMemCheckLeaks(
-    psMemoryId 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 psMemCheckLeaks(psMemoryId id0,psMemBlock ***arr,FILE *fd)
 {
     int nleak = 0;
@@ -365,6 +362,5 @@
     pthread_mutex_lock(&memBlockListMutex);
 
-    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
-    {
+    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) {
         if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
             nleak++;
@@ -382,6 +378,5 @@
     pthread_mutex_unlock(&memBlockListMutex);
 
-    if (nleak == 0 || arr == NULL)
-    {
+    if (nleak == 0 || arr == NULL) {
         return nleak;
     }
@@ -390,6 +385,5 @@
     pthread_mutex_lock(&memBlockListMutex);
 
-    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
-    {
+    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) {
         if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
             (*arr)[j++] = iter;
@@ -408,11 +402,11 @@
  * Reference counting APIs
  */
-psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter
+// return refCounter
+psReferenceCount psMemGetRefCounter(void *vptr)
 {
     psMemBlock *ptr;
     unsigned int refCount;
 
-    if (vptr == NULL)
-    {
+    if (vptr == NULL) {
         return 0;
     }
@@ -420,6 +414,5 @@
     ptr = ((psMemBlock *)vptr) - 1;
 
-    if (checkMemBlock(ptr, __func__) != 0)
-    {
+    if (checkMemBlock(ptr, __func__) != 0) {
         memProblemCallback(ptr, __func__, __LINE__);
     }
@@ -431,11 +424,10 @@
     return refCount;
 }
-
-void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
+// increment and return refCounter
+void *psMemIncrRefCounter(void *vptr)
 {
     psMemBlock *ptr;
 
-    if (vptr == NULL)
-    {
+    if (vptr == NULL) {
         return vptr;
     }
@@ -443,6 +435,5 @@
     ptr = ((psMemBlock *)vptr) - 1;
 
-    if (checkMemBlock(ptr, __func__))
-    {
+    if (checkMemBlock(ptr, __func__)) {
         memProblemCallback(ptr, __func__, __LINE__);
     }
@@ -455,8 +446,8 @@
 }
 
-void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
-{
-    if (vptr == NULL)
-    {
+// decrement and return refCounter
+void *psMemDecrRefCounter(void *vptr)
+{
+    if (vptr == NULL) {
         return NULL;
     }
@@ -467,12 +458,10 @@
     pthread_mutex_lock(&ptr->refCounterMutex);
 
-    if (ptr->refCounter > 1)
-    {
+    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
-    {
+    } else {
         pthread_mutex_unlock(&ptr->refCounterMutex);
 
@@ -482,4 +471,8 @@
         }
 
+        if (ptr->freeFcn != NULL) {
+            ptr->freeFcn(vptr);
+        }
+
         pthread_mutex_lock(&memBlockListMutex);
 
@@ -507,15 +500,23 @@
 }
 
-void p_psCustomFree(psFreeFcn fcn, void* ptr)
-{
-
-    if (fcn == NULL) {
+void p_psMemSetDeallocator(void* vptr, psFreeFcn freeFcn)
+{
+    if (vptr == NULL) {
         return;
-    } else {
-        if (fcn == PS_FREE) {
-            psFree(ptr);
-        } else {
-            fcn(ptr);
-        }
-    }
-}
+    }
+
+    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
+
+    ptr->freeFcn = freeFcn;
+
+}
+psFreeFcn p_psMemGetDeallocator(void* vptr)
+{
+    if (vptr == NULL) {
+        return NULL;
+    }
+
+    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
+
+    return ptr->freeFcn;
+}
