Index: trunk/psLib/src/sysUtils/psMemory.c
===================================================================
--- trunk/psLib/src/sysUtils/psMemory.c	(revision 639)
+++ trunk/psLib/src/sysUtils/psMemory.c	(revision 648)
@@ -8,6 +8,6 @@
  *  @author Robert Lupton, Princeton University
  *
- *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-11 20:11:17 $
+ *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-12 19:05:02 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -20,4 +20,5 @@
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdint.h>
 
 #include "psMemory.h"
@@ -34,5 +35,5 @@
  * Unique ID for allocated blocks
  */
-static unsigned long memid = 0;
+static psMemoryId memid = 0;
 
 /**
@@ -62,5 +63,5 @@
                                       const char *file, int lineno)
 {
-    if (ptr->refCounter <= 0) {
+    if (ptr->refCounter < 1) {
         psError(__func__,
                 "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
@@ -95,10 +96,10 @@
  * Call the callbacks when these IDs are allocated/freed
  */
-long p_psMemAllocateID = 0;  // notify user this block is allocated
-long p_psMemFreeID = 0;   // notify user this block is freed
-
-long psMemAllocateCallbackSetID(long id) // set p_psMemAllocateID to id
-{
-    long old = p_psMemAllocateID;
+psMemoryId p_psMemAllocateID = 0;  // notify user this block is allocated
+psMemoryId p_psMemFreeID = 0;   // notify user this block is freed
+
+psMemoryId psMemAllocateCallbackSetID(psMemoryId id) // set p_psMemAllocateID to id
+{
+    psMemoryId old = p_psMemAllocateID;
     p_psMemAllocateID = id;
 
@@ -106,7 +107,7 @@
 }
 
-long psMemFreeCallbackSetID(long id)  // set p_psMemFreeID to id
-{
-    long old = p_psMemFreeID;
+psMemoryId psMemFreeCallbackSetID(psMemoryId id)  // set p_psMemFreeID to id
+{
+    psMemoryId old = p_psMemFreeID;
     p_psMemFreeID = id;
 
@@ -120,14 +121,14 @@
  * isn't resignalled)
  */
-static long memAllocateCallbackDefault(const psMemBlock *ptr)
-{
-    static int incr = 0;  // "p_psMemAllocateID += incr"
+static psMemoryId memAllocateCallbackDefault(const psMemBlock *ptr)
+{
+    static psMemoryId incr = 0;  // "p_psMemAllocateID += incr"
 
     return incr;
 }
 
-static long memFreeCallbackDefault(const psMemBlock *ptr)
-{
-    static int incr = 0;  // "p_psMemFreeID += incr"
+static psMemoryId memFreeCallbackDefault(const psMemBlock *ptr)
+{
+    static psMemoryId incr = 0;  // "p_psMemFreeID += incr"
 
     return incr;
@@ -169,5 +170,5 @@
  * Return memory ID counter for next block to be allocated
  */
-int psMemGetId(void)
+psMemoryId psMemGetId(void)
 {
     return memid + 1;
@@ -199,6 +200,11 @@
 
     if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) {
-        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (%p %p)",
+        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer underflow %p %p)",
                 m->id,m->startblock,m->endblock);
+        return(1);
+    }
+    if (*(void**)((int8_t*)(m+1)+m->userMemorySize) != P_PS_MEMMAGIC) {
+        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer overflow, %p)",
+                m->id,*(void**)((int8_t*)(m+1)+m->userMemorySize));
         return(1);
     }
@@ -236,5 +242,5 @@
 void *p_psAlloc(size_t size, const char *file, int lineno)
 {
-    psMemBlock *ptr = malloc(sizeof(psMemBlock) + size);
+    psMemBlock *ptr = malloc(sizeof(psMemBlock) + size + sizeof(void*));
 
     if (ptr == NULL) {
@@ -246,5 +252,5 @@
     }
 
-    *(unsigned long*)&ptr->id = ++memid;
+    *(psMemoryId*)&ptr->id = ++memid;
     ptr->file = file;
     *(unsigned int*)&ptr->lineno = lineno;
@@ -252,4 +258,6 @@
     ptr->endblock = P_PS_MEMMAGIC;
     ptr->refCounter = 1;                // one user so far
+    ptr->userMemorySize = size;
+    *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC;
 
     // need exclusive access of the memory block list now...
@@ -287,5 +295,5 @@
         isBlockLast = (ptr == lastMemBlockAllocated);
 
-        ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);
+        ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size + sizeof(void*));
 
         if (ptr == NULL) {
@@ -293,4 +301,7 @@
                     size, file, lineno);
         }
+
+        ptr->userMemorySize = size;
+        *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC;
 
         if (isBlockLast) {
@@ -363,5 +374,5 @@
  */
 int psMemCheckLeaks(
-    int id0,                            // don't list blocks with id < id0
+    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)
@@ -416,5 +427,5 @@
  * Reference counting APIs
  */
-int psMemGetRefCounter(void *vptr) // return refCounter
+psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter
 {
     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
