Index: trunk/psLib/src/sysUtils/psMemory.c
===================================================================
--- trunk/psLib/src/sysUtils/psMemory.c	(revision 430)
+++ trunk/psLib/src/sysUtils/psMemory.c	(revision 432)
@@ -8,6 +8,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-04-15 21:22:09 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-04-16 02:18:57 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -20,8 +20,9 @@
 #include "psMemory.h"
 #include "psError.h"
+#include "psAbort.h"
 
 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header
 
-static int bad_memblock(const psMemBlock *m, int quiet);
+static int checkMemBlock(const psMemBlock *m, const char* funcName);
 static int numMemBlock = 0;             // size of memBlocks
 static psMemBlock **memBlocks = NULL;
@@ -58,22 +59,17 @@
                           const char *file, int lineno)
 {
-    if (bad_memblock(ptr, 0)) {
-        fprintf(stderr, "Memory corruption or an attempt to manipulate memory "
-                "not allocated with psAlloc at %s", file);
-        if (lineno > 0) {
-            fprintf(stderr, ":%d", lineno);
-        }
-        fprintf(stderr, "\n");
-    } else if (ptr->refCounter <= 0) {
-        psError(__func__, PS_ERR_BADFREE, PS_NEW_ERROR,
-                "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__,  PS_ERR_BADFREE, PS_NEW_ERROR,
-                "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
-                ptr->id, ptr->file, ptr->lineno, file, lineno);
-    }
-
-    if (lineno > 0) {
+    checkMemBlock(ptr, __func__);
+
+	if (ptr->refCounter <= 0) {
+		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);
+	}
+
+	if (lineno > 0) {
         psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno);
     }
@@ -123,19 +119,15 @@
  * isn't resignalled)
  */
-static int memAllocateCallbackDefault(const psMemBlock *ptr)
+static long memAllocateCallbackDefault(const psMemBlock *ptr)
 {
     static int incr = 0;  // "p_psMemAllocateID += incr"
 
-    assert (ptr != NULL);  // prevent compiler warnings
-
     return incr;
 }
 
-static int memFreeCallbackDefault(const psMemBlock *ptr)
+static long memFreeCallbackDefault(const psMemBlock *ptr)
 {
     static int incr = 0;  // "p_psMemFreeID += incr"
 
-    assert (ptr != NULL);  // prevent compiler warnings
-
     return incr;
 }
@@ -144,10 +136,10 @@
  * The default callbacks, and the routines to change them
  */
-static psMemCallback memAllocateCallback = memAllocateCallbackDefault;
-static psMemCallback memFreeCallback = memFreeCallbackDefault;
-
-psMemCallback psMemAllocateSetCallback(psMemCallback func)
-{
-    psMemCallback old = memAllocateCallback;
+static psMemAllocateCallback memAllocateCallback = memAllocateCallbackDefault;
+static psMemFreeCallback memFreeCallback = memFreeCallbackDefault;
+
+psMemAllocateCallback psMemAllocateSetCallback(psMemAllocateCallback func)
+{
+    psMemFreeCallback old = memAllocateCallback;
 
     if (func != NULL) {
@@ -160,7 +152,7 @@
 }
 
-psMemCallback psMemFreeSetCallback(psMemCallback func)
-{
-    psMemCallback old = memFreeCallback;
+psMemFreeCallback psMemFreeSetCallback(psMemFreeCallback func)
+{
+    psMemFreeCallback old = memFreeCallback;
 
     if (func != NULL) {
@@ -191,28 +183,19 @@
 
 static int
-bad_memblock(const psMemBlock *m, // block to check
-             int quiet)   // be quiet?
+checkMemBlock(const psMemBlock *m, // block to check
+             const char* funcName)   // be quiet?
 {
     if (m == NULL) {
-        if (!quiet) {
-            psError(__func__,  PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
-                    "psMemCheckCorruption: NULL memory block\n");
-        }
+        psError(funcName,"Memory Corruption: NULL memory block found.");
         return(1);
     }
 
     if (!ALIGNED(m)) {
-        if (!quiet) {
-            psError(__func__,  PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
-                    "psMemCheckCorruption: non-aligned memory block\n");
-        }
+        psError(funcName, "psMemCheckCorruption: non-aligned memory block");
         return(1);
     }
 
-    if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) {
-        if (!quiet) {
-            psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
-                    "psMemCheckCorruption: memory block %ld is corrupted\n", m->id);
-        }
+    if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) {
+        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted", m->id);
         return(1);
     }
@@ -231,8 +214,8 @@
     for (int i = 1; i <= memid; i++) {
         if (memBlocks[i] != NULL) {
-            if (bad_memblock(memBlocks[i], 1)) {
+            if (checkMemBlock(memBlocks[i], __func__)) {
                 nbad++;
 
-                memProblemCallback(memBlocks[i], "(psMemCheckCorruption)", 0);
+                memProblemCallback(memBlocks[i], __func__, __LINE__);
 
                 if (abort_on_error) {
@@ -263,5 +246,6 @@
     ptr->file = file;
     *(int *)&ptr->lineno = lineno;
-    *(void **)&ptr->magic0 = *(void **)&ptr->magic = P_PS_MEMMAGIC;
+    ptr->startblock = P_PS_MEMMAGIC;
+	ptr->endblock = P_PS_MEMMAGIC;
 
     ptr->refCounter = 1;  // one user so far
@@ -273,7 +257,7 @@
         p_psMemAllocateID += memAllocateCallback(ptr);
     }
-    if (memid >= nMemBlock) {
-        nMemBlock = (nMemBlock == 0) ? 100000 : 2*nMemBlock;
-        memBlocks = realloc(memBlocks, nMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
+    if (memid >= numMemBlock) {
+        numMemBlock = (numMemBlock == 0) ? 100000 : 2*numMemBlock;
+        memBlocks = realloc(memBlocks, numMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
     }
     memBlocks[ptr->id] = ptr;
@@ -311,5 +295,5 @@
     }
 
-    if (bad_memblock(ptr, 0)) {
+    if (checkMemBlock(ptr, "psFree") != 0) {
         memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
     } else {
@@ -369,5 +353,5 @@
     }
 
-    assert (*arr == NULL);  // Don't generate a memory leak!
+    p_psFree(*arr,__FILE__, __LINE__);  // Don't generate a memory leak!
 
     *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
@@ -395,7 +379,7 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (bad_memblock(ptr, 0))
-    {
-        memProblemCallback(ptr, "(psMemGetRefCounter)", -1);
+    if (checkMemBlock(ptr, __func__) != 0)
+    {
+        memProblemCallback(ptr, __func__, __LINE__);
     }
 
@@ -412,7 +396,7 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (bad_memblock(ptr, 0))
-    {
-        memProblemCallback(ptr, "(psMemIncrRefCounter)", -1);
+    if (checkMemBlock(ptr, __func__))
+    {
+        memProblemCallback(ptr, __func__, __LINE__);
     }
 
@@ -431,7 +415,7 @@
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
 
-    if (bad_memblock(ptr, 0))
-    {
-        memProblemCallback(ptr, "(psMemDecrRefCounter)", -1);
+    if (checkMemBlock(ptr, __func__))
+    {
+        memProblemCallback(ptr, __func__, __LINE__);
     }
 
