Index: /trunk/Ohana/src/libohana/include/ohana_allocate.h
===================================================================
--- /trunk/Ohana/src/libohana/include/ohana_allocate.h	(revision 4299)
+++ /trunk/Ohana/src/libohana/include/ohana_allocate.h	(revision 4300)
@@ -5,34 +5,27 @@
 # ifdef OHANA_MEMORY
 
-void *ohana_malloc (char *file, int line, size_t size);
-void *ohana_realloc (char *file, int line, void *in, size_t size);
+void *ohana_malloc (char *file, int line, int Nelem, size_t esize);
+void *ohana_realloc (char *file, int line, void *in, int Nelem, size_t esize);
 void  ohana_free (char *file, int line, void *in);
 void  ohana_memregister_func (char *file, int line, void *ptr);
 void  ohana_memdump_func (int mode);
+void  ohana_memcheck_func (int mode);
 
 # define ohana_memregister(X) ohana_memregister_func (__FILE__, __LINE__, (X));
+# define ohana_memcheck(X) ohana_memcheck_func (X);
 # define ohana_memdump(X) ohana_memdump_func (X);
 
-# define ALLOCATE(X,T,S)  \
-  X = (T *) ohana_malloc (__FILE__, __LINE__, (unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-  if (X == NULL) { \
-    fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);\
-    exit (10); } 
+# define ALLOCATE(X,T,S) \
+  X = (T *) ohana_malloc (__FILE__, __LINE__, (S), sizeof(T))
 # define REALLOCATE(X,T,S) \
-  X = (T *) ohana_realloc(__FILE__, __LINE__, X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-  if (X == NULL) { \
-    fprintf(stderr,"failed realloc at %d in %s\n", __LINE__, __FILE__);\
-    exit (10); }
+  X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T));
 # define CHECK_REALLOCATE(X,T,S,N,D) \
-  if ((N) >= (S)) { \
-    S += D; \
-    X = (T *) ohana_realloc(__FILE__, __LINE__, X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-    if (X == NULL) { \
-      fprintf(stderr,"failed realloc increment at %d in %s\n", __LINE__, __FILE__);\
-      exit (10); } }
+  if ((N) >= (S)) { S += D; \
+  X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T)); }
 # define free(X) ohana_free(__FILE__, __LINE__, X)
 
 # else 
 # define ohana_memregister(X) /* NOP */
+# define ohana_memcheck(X) /* NOP */
 # define ohana_memdump(X) /* NOP */
 # endif /* OHANA_MEMORY */
Index: /trunk/Ohana/src/libohana/src/ohana_allocate.c
===================================================================
--- /trunk/Ohana/src/libohana/src/ohana_allocate.c	(revision 4299)
+++ /trunk/Ohana/src/libohana/src/ohana_allocate.c	(revision 4300)
@@ -4,4 +4,6 @@
 
 /* need an internal version that does not use ohana_memory functions */
+# define FALSE 0 
+# define TRUE 1
 # define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
 # define OHANA_ALLOCATE(X,T,S)  \
@@ -24,12 +26,14 @@
 # define OHANA_FREE(X) free(X)
 
-# define STATE_ALLOC   0
-# define STATE_REALLOC 1
-# define STATE_FREE    2
+# define STATE_ALLOC    0
+# define STATE_REALLOC  1
+# define STATE_FREE     2
+# define STATE_EXTERNAL 3
 
 typedef struct {
   char *file;
   int   line;
-  void *ptr;
+  void *ptr;   // location of externally visible entry
+  int   size;
   int   state;
 } Memlist;
@@ -54,12 +58,23 @@
 }
 
-void *ohana_malloc (char *file, int line, size_t size) {
-
-  void *ptr;
+void *ohana_malloc (char *file, int line, int Nelem, size_t esize) {
+
+  void *ptr, *new;
+  int size;
+  int *marker;
 
   if (memlist == NULL) ohana_meminit ();
 
-  ptr = malloc (size);
-  if (ptr == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line);
+  Nelem = MAX (1, Nelem);
+  size = Nelem * esize;
+
+  new = malloc (size + 8); /* 8 extra to save endposts */
+  if (new == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line);
+  ptr = new + 4;
+
+  marker = (int *) new;
+  *marker = 0xdeadbeef;
+  marker = (int *)(new + size + 4);
+  *marker = 0xdeadbeef;
 
   /* new memory, add to stack */
@@ -67,4 +82,5 @@
   memlist[Nmemlist].file = file;
   memlist[Nmemlist].line = line;
+  memlist[Nmemlist].size = size;
   memlist[Nmemlist].state = STATE_ALLOC;
   Nmemlist ++;
@@ -76,28 +92,16 @@
 }
 
-/* register externally allocated memory with ohana memory manager */
-void ohana_memregister_func (char *file, int line, void *ptr) {
-
-  if (memlist == NULL) ohana_meminit ();
-
-  /* add to stack */
-  memlist[Nmemlist].ptr  = ptr;
-  memlist[Nmemlist].file = file;
-  memlist[Nmemlist].line = line;
-  memlist[Nmemlist].state = STATE_ALLOC;
-  Nmemlist ++;
-  if (Nmemlist == NMEMLIST) {
-    NMEMLIST += 1000;
-    OHANA_REALLOCATE (memlist, Memlist, NMEMLIST);
-  }
-  return;
-}
-
-void *ohana_realloc (char *file, int line, void *in, size_t size) {
-
-  int i;
-  void *ptr;
+void *ohana_realloc (char *file, int line, void *in, int Nelem, size_t esize) {
+
+  int i, size;
+  void *ptr, *ref, *new;
+  int *marker;
 
   if (memlist == NULL) ohana_memabort ("REALLOCATE before ALLOCATE");
+
+  Nelem = MAX (1, Nelem);
+  size = Nelem * esize;
+
+  ref = in - 4;
 
   /* find old entry, update ptr, file, line */
@@ -105,18 +109,32 @@
     if (memlist[i].state == STATE_FREE) continue;
     if (memlist[i].ptr == in) {
+
+      if (memlist[i].state == STATE_EXTERNAL) ohana_memabort ("ERROR: realloc of external memory");
+
       /* ask for new memory */
-      ptr = realloc (in, size);
-      if (ptr == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line);
-      /* if new memory is same as old, just return it */
-      if (in == ptr) return (ptr);
-      /* otherwise, update with new location */
+      new = realloc (ref, size + 8); /* 8 extra to save endposts */
+      if (new == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line);
+      ptr = new + 4;
+      
+      /* set the marker */
+      marker = (int *) new;
+      *marker = 0xdeadbeef;
+      marker = (int *)(ptr + size);
+      *marker = 0xdeadbeef;
+
+      /* otherwise, update memory new location */
       memlist[i].ptr = ptr;
-      memlist[i].file = file;
-      memlist[i].line = line;
+      memlist[i].size = size;
       memlist[i].state = STATE_REALLOC;
+
+      /* if new memory in new location, update references */
+      if (ptr != in) {
+	memlist[i].file = file;
+	memlist[i].line = line;
+      }
+
       return (ptr);
     }
   }
-
   ohana_memabort ("allocated memory not found for realloc (%s, %d)\n", file, line);
   return (NULL);
@@ -126,4 +144,5 @@
 
   int i;
+
   if (memlist == NULL) ohana_memabort ("FREE before ALLOCATE");
 
@@ -141,8 +160,27 @@
 }
 
-void ohana_memdump_func (int allmemory) {
-
-  int i, Ns[3], N;
-  char S[3];
+/* register externally allocated memory with ohana memory manager */
+void ohana_memregister_func (char *file, int line, void *ptr) {
+
+  if (memlist == NULL) ohana_meminit ();
+
+  /* add to stack */
+  memlist[Nmemlist].ptr  = ptr;
+  memlist[Nmemlist].file = file;
+  memlist[Nmemlist].line = line;
+  memlist[Nmemlist].state = STATE_EXTERNAL;
+  Nmemlist ++;
+  if (Nmemlist == NMEMLIST) {
+    NMEMLIST += 1000;
+    OHANA_REALLOCATE (memlist, Memlist, NMEMLIST);
+  }
+  return;
+}
+
+void ohana_memcheck_func (int allmemory) {
+
+  int i, header;
+  int *marker;
+  char top, bottom;
 
   if (Nmemlist == 0) {
@@ -151,13 +189,47 @@
   }
 
+  fprintf (stderr, "checking for memory corruption...\n");
+  header = FALSE;
+  for (i = 0; i < Nmemlist; i++) {
+    if (memlist[i].state == STATE_EXTERNAL) continue;
+    if ((allmemory == 0) && (memlist[i].state == STATE_FREE)) continue;
+
+    top = bottom = 'Y';
+    marker = (int *)(memlist[i].ptr - 4);
+    if (*marker == 0xdeadbeef) bottom = 'N';
+    marker = (int *)(memlist[i].ptr + memlist[i].size);
+    if (*marker == 0xdeadbeef) top = 'N';
+    
+    if ((top == 'N') && (bottom == 'N')) continue;
+    if (!header) {
+      fprintf (stderr, "         Nmem bot top file            line\n");
+      header = TRUE;
+    }
+    fprintf (stderr, "corrupt:  %3d  %c   %c  %-15s %3d\n", i, bottom, top, memlist[i].file, memlist[i].line);
+  }
+  return;
+}
+
+void ohana_memdump_func (int allmemory) {
+
+  int i, Ns[3], N;
+  char S[3];
+
+  if (Nmemlist == 0) {
+    fprintf (stderr, "no memory allocated\n");
+    return;
+  }
+
   S[0] = 'A';
   S[1] = 'R';
   S[2] = 'F';
-  Ns[0] = Ns[1] = Ns[2] = 0;
-
-  for (i = 0; i < Nmemlist; i++) {
-    if (memlist[i].state == STATE_ALLOC)   N = 0;
-    if (memlist[i].state == STATE_REALLOC) N = 1;
-    if (memlist[i].state == STATE_FREE)    N = 2;
+  S[3] = 'X';
+  Ns[0] = Ns[1] = Ns[2] = Ns[3] = 0;
+
+  for (i = 0; i < Nmemlist; i++) {
+    if (memlist[i].state == STATE_ALLOC)    N = 0;
+    if (memlist[i].state == STATE_REALLOC)  N = 1;
+    if (memlist[i].state == STATE_FREE)     N = 2;
+    if (memlist[i].state == STATE_EXTERNAL) N = 2;
     Ns[N] ++;
     if ((allmemory == 0) && (memlist[i].state == STATE_FREE)) continue;
