Index: /branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana.h
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana.h	(revision 31588)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana.h	(revision 31589)
@@ -19,4 +19,6 @@
 # include <readline/readline.h>
 
+# define OHANA_MEMORY
+
 // XXX I was including these before, but RHL claims they are not needed
 // # include <malloc.h>
Index: /branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana_allocate.h
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana_allocate.h	(revision 31588)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana_allocate.h	(revision 31589)
@@ -5,32 +5,31 @@
 # ifdef OHANA_MEMORY
 
-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_malloc (char *file, int line, char *func, int Nelem, size_t esize);
+void *ohana_realloc (char *file, int line, char *func, void *in, int Nelem, size_t esize);
+void  ohana_free (char *file, int line, char *func, void *in);
 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(PTR,TYPE,SIZE) \
-  { PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, (SIZE), sizeof(TYPE)) }
+  { PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, __func__, (SIZE), sizeof(TYPE)) }
 # define ALLOCATE_ZERO(PTR,TYPE,SIZE)					\
-  { PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, (SIZE), sizeof(TYPE)); memset (PTR, 0, (SIZE)*sizeof(TYPE)); }
+  { PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, __func__, (SIZE), sizeof(TYPE)); memset (PTR, 0, (SIZE)*sizeof(TYPE)); }
 # define REALLOCATE(PTR,TYPE,SIZE) \
-  { PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE)); }
+  { PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, __func__, PTR, (SIZE), sizeof(TYPE)); }
 # define CHECK_REALLOCATE(PTR,TYPE,SIZE,NCURR,DELTA) {			\
   if ((NCURR) >= (SIZE)) { SIZE += DELTA;				\
-    PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE)); } }
-# define FREE(PTR) { if (PTR != NULL) { ohana_free (__FILE__, __LINE__, PTR); } }
+    PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, __func__, PTR, (SIZE), sizeof(TYPE)); } }
+# define FREE(PTR) { if (PTR != NULL) { ohana_free (__FILE__, __LINE__, __func__, PTR); } }
 # define free(PTR) { ohana_free(__FILE__, __LINE__, PTR); }
+# define real_free(PTR) { if (PTR) { free(PTR); }}
 
-# else 
-# define ohana_memregister(X) /* NOP */
+# else  /* OHANA_MEMORY */
+
 # define ohana_memcheck(X) /* NOP */
 # define ohana_memdump(X) /* NOP */
-# endif /* OHANA_MEMORY */
+# define real_free(PTR) { if (PTR) { free(PTR); }}
 
 # ifndef ALLOCATE
@@ -64,4 +63,5 @@
 # define FREE(PTR) { if (PTR != NULL) { free (PTR); } }
 # endif /* ALLOCATE */
+# endif /* OHANA_MEMORY */
 
 # endif /* OHANA_ALLOCATE */
Index: /branches/eam_branches/ipp-20110505/Ohana/src/libohana/src/ohana_allocate.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/libohana/src/ohana_allocate.c	(revision 31588)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/libohana/src/ohana_allocate.c	(revision 31589)
@@ -2,4 +2,6 @@
 # include <stdlib.h>
 # include <stdarg.h>
+# include <stdint.h>
+# include <pthread.h>
 
 /* need an internal version that does not use ohana_memory functions */
@@ -7,42 +9,19 @@
 # define TRUE 1
 # define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
-# define OHANA_ALLOCATE(X,T,S)  \
-  X = (T *) malloc ((unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-  if (X == NULL) { \
-    fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);\
-    abort(); } 
-# define OHANA_REALLOCATE(X,T,S) \
-  X = (T *) realloc(X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-  if (X == NULL) { \
-    fprintf(stderr,"failed realloc at %d in %s\n", __LINE__, __FILE__);\
-    abort(); }
-# define OHANA_CHECK_REALLOCATE(X,T,S,N,D) \
-  if ((N) >= (S)) { \
-    S += D; \
-    X = (T *) realloc(X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-    if (X == NULL) { \
-      fprintf(stderr,"failed realloc increment at %d in %s\n", __LINE__, __FILE__);\
-      abort(); } }
-# define OHANA_FREE(X) free(X)
-
-# define STATE_ALLOC    0
-# define STATE_REALLOC  1
-# define STATE_FREE     2
-# define STATE_EXTERNAL 3
 
 # define OHANA_MEMMAGIC (uint32_t) 0xdeadbeef
 
 typedef struct Memblock {
-  uint32_t startblock;
-  struct Memblock *prevBlock;
-  struct Memblock *nextBlock;
-  size_t size;
-  char *file;
-  char *func;
-  int line;
-  uint32_t endblock;
+  uint32_t startblock;	      // endpost marker
+  struct Memblock *prevBlock; // previously allocated memory
+  struct Memblock *nextBlock; // next allocated memory
+  size_t size;		      // size of memory
+  char *file;		      // file (re)allocated 
+  char *func;		      // func (re)allocated 
+  int line;		      // line (re)allocated
+  uint32_t endblock;	      // endpost marker
 } Memblock;
 
-static Memblock *lastMemBlock = NULL;
+static Memblock *lastBlock = NULL;
 
 static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
@@ -61,44 +40,52 @@
 }
 
-void *ohana_malloc (char *file, int line, int Nelem, size_t esize) {
-
-  void *ptr, *new;
-  int size;
-  size_t *marker;
+void *ohana_malloc (char *file, int line, char *func, size_t Nelem, size_t esize) {
+
+  void *ptr;		      // actual user memory allocated
+  Memblock *new;	      // new memblock created to track the user memory
+  size_t size;		      // total number of bytes requested (not items)
 
   Nelem = MAX (1, Nelem);
   size = Nelem * esize;
 
-  new = malloc (sizeof(Memblock) + size + sizeof(void *)); /* memblock + data + endpost */
-  if (new == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line);
-  ptr = new + sizeof(Memblock);
-
+  // total size is : memblock + data + endpost
+  new = (Memblock *) malloc (sizeof(Memblock) + size + sizeof(void *)); 
+  if (new == NULL) ohana_memabort ("failed to allocate memory (%s, %d, %s)\n", file, line, func);
+
+  // pointer to the start of the user memory
+  ptr = (char *)(new + 1);
+
+  // set the end-post values
   new->startblock = OHANA_MEMMAGIC;
   new->endblock = OHANA_MEMMAGIC;
-  *(uint32_t *)((char *)(new + 1) + size) = OHANA_MEMMAGIC;
-
-  // marker = (size_t *)(new + size + NMEMBYTE);
-  // *marker = 0xdeadbeef;
-
-  /* new memory, add to stack */
+  *(uint32_t *)(ptr + size) = OHANA_MEMMAGIC;
+
+  // set the memory metadata
+  new->size = size;
   new->file = file;
+  new->line = line;
   new->func = func;
-  new->line = line;
-  new->size = size;
-
-  // new memblock becomes the 'lastMemBlock':
-  // lastMemBlock = new
+
+  // new memblock becomes the 'lastBlock':
+  // lastBlock = new
   // new->prev = (new - 1)
   // (new - 1)->next = new
 
+  // for memory allocated in the order m0, m1, m2
+  // m0->next = m1, m1->next = m2, m2->next = NULL
+  // m2->prev = m1, m1->prev = m0, m0->prev = NULL
+  // lastBlock = m2
+
+  // set up the pointers
   new->nextBlock = NULL;
 
+  // protect the list during this operation
   pthread_mutex_lock(&memBlockListMutex);
 
-  if (lastMemBlock) {
-    lastMemBlock->nextBlock = new;
-  }
-  new->prevBlock = lastMemBlock;
-  lastMemBlock = new;
+  if (lastBlock) {
+    lastBlock->nextBlock = new;
+  }
+  new->prevBlock = lastBlock;
+  lastBlock = new;
 
   pthread_mutex_unlock(&memBlockListMutex);
@@ -107,16 +94,19 @@
 }
 
-void *ohana_realloc (char *file, int line, void *in, int Nelem, size_t esize) {
-
-  int i, size;
-  void *ptr, *ref, *new;
-  size_t *marker;
-
+void *ohana_realloc (char *file, int line, char *func, void *in, size_t Nelem, size_t esize) {
+
+  void *ptr;		      // actual user memory allocated
+  Memblock *old;	      // original memblock associated with user memory
+  Memblock *new;	      // new memblock associated with user memory
+  size_t size;
+
+  // just allocate if not previously allocated
   if (!in) {
-    ptr = ohana_alloc (file, line, Nelem, esize);
+    ptr = ohana_malloc (file, line, func, Nelem, esize);
     return ptr;
   }
 
-  ref = ((Memblock *)in) - 1;
+  // memblock of supplied pointer
+  old = (Memblock *) in - 1;
 
   Nelem = MAX (1, Nelem);
@@ -124,53 +114,62 @@
 
   // requested same size as current allocation
-  if (size == ref->size) {
-    return ptr;
+  if (size == old->size) {
+    return in;
   }
 
   pthread_mutex_lock(&memBlockListMutex);
 
+  Memblock *nextBlock = old->nextBlock;
+  Memblock *prevBlock = old->prevBlock;
+
+  int isLast = (old == lastBlock);
+
+  // ask for new memory
+  // total size is : memblock + data + endpost
+  new = (Memblock *) realloc (old, sizeof(Memblock) + size + sizeof(void *));
+  if (new == NULL) ohana_memabort ("failed to reallocate memory (%s, %d, %s)\n", file, line, func);
+  ptr = (char *) (new + 1);
+  
+  // give the realloc location:
+  new->size = size; 
+  new->file = file;
+  new->line = line;
+  new->func = func;
+
+  // update the endpost (the others were set originally
+  *(uint32_t *)(ptr + size) = OHANA_MEMMAGIC;
+
+  // need to reset lastBlock in case we moved
+  if (isLast) {
+    lastBlock = new;
+  }
+
+  // need to adjust the neighbors' pointers:
+  if (nextBlock) {
+    nextBlock->prevBlock = new;
+  }
+  if (prevBlock) {
+    prevBlock->nextBlock = new;
+  }
+  
+  pthread_mutex_unlock(&memBlockListMutex);
+
+  return (ptr);
+}
+
+// this is very slow.  should we speed this up by indexing on the ptr?
+void ohana_free (char *file, int line, char *func, void *in) {
+
+  Memblock *ref;
+
+  if (!in) return;
+
+  ref = (Memblock *) in - 1;
+
+  pthread_mutex_lock(&memBlockListMutex);
+  
   Memblock *nextBlock = ref->nextBlock;
   Memblock *prevBlock = ref->prevBlock;
 
-  int isLast = (ref == lastMemBlock);
-
-  /* ask for new memory */
-  new = realloc (ref, sizeof(Memblock) + size + sizeof(void *)); /* memblock + data + endpost */
-  if (new == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line);
-  ptr = new + sizeof(Memblock);
-  
-  new->size = size; 
-  *(uint32_t *)((char *)(new + 1) + size) = OHANA_MEMMAGIC;
-
-  if (isLast) {
-    lastMemBlock = new;
-  }
-
-  if (nextBlock) {
-    nextBlock->prevBlock = memBlock;
-  }
-  if (prevBlock) {
-    prevBlock->nextBlock = memBlock;
-  }
-  
-  pthread_mutex_unlock(&memBlockListMutex);
-
-  return (ptr);
-}
-
-// this is very slow.  should we speed this up by indexing on the ptr?
-void ohana_free (char *file, int line, void *in) {
-
-  int i;
-
-  if (!in) return NULL;
-
-  ref = ((Memblock *) ptr) - 1;
-
-  pthread_mutex_lock(&memBlockListMutex);
-  
-  Memblock *nextBlock = ref->nextBlock;
-  Memblock *prevBlock = ref->prevBlock;
-
   if (nextBlock) {
     nextBlock->prevBlock = prevBlock;
@@ -179,6 +178,6 @@
     prevBlock->nextBlock = nextBlock;
   }
-  if (lastMemBlock == ref) {
-    lastMemBlock = prevBlock;
+  if (lastBlock == ref) {
+    lastBlock = prevBlock;
   }
   
@@ -190,63 +189,50 @@
 }
 
-/* register externally allocated memory with ohana memory manager */
-void ohana_memregister_func (char *file, int line, void *ptr) {
-
-  // is this needed?
-  return;
-}
-
-# if (0)
 void ohana_memcheck_func (int allmemory) {
 
-  int i, j, next, prev, header;
-  size_t *marker;
-  char top, bottom;
-
-  if (Nmemlist == 0) {
+  if (!lastBlock) {
     fprintf (stderr, "no memory allocated\n");
     return;
   }
 
-  header = FALSE;
-  fprintf (stderr, "checking %d memory blocks\n", Nmemlist);
-  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 = (size_t *)(memlist[i].ptr - NMEMBYTE);
-    if (*marker == 0xdeadbeef) bottom = 'N';
-    marker = (size_t *)(memlist[i].ptr + memlist[i].size);
-    if (*marker == 0xdeadbeef) top = 'N';
+  Memblock *thisBlock = lastBlock;
+
+  size_t Ngood  = 0;
+  size_t Nbad   = 0;
+  size_t Ntotal = 0;
+  size_t Nbytes = 0;
+
+  while (thisBlock) {
+
+    int good = TRUE;
     
-    if ((top == 'N') && (bottom == 'N')) continue;
-    if (!header) {
-      fprintf (stderr, "         Nmem start end file            line\n");
-      header = TRUE;
+    if (thisBlock->startblock != OHANA_MEMMAGIC) good = FALSE;
+    if (thisBlock->endblock != OHANA_MEMMAGIC) good = FALSE;
+
+    // pointer to the start of the user memory
+    char *ptr = (char *)(thisBlock + 1) + thisBlock->size;
+    uint32_t endpost = *(uint32_t *)ptr;
+    if (endpost != OHANA_MEMMAGIC) good = FALSE;
+
+    // XXX keep checking even if memory is corrupted?
+    if (!good) {
+      if (Nbad < 1) {
+	fprintf (stderr, "memory corruption\n");
+      }
+      if (Nbad < 100) {
+	fprintf (stderr, "  file: %s, line: %d, func: %s\n", thisBlock->file, thisBlock->line, thisBlock->func);
+      }
+      Nbad ++;
+    } else {
+      Ngood ++;
     }
-    fprintf (stderr, "corrupt:  %3d   %c    %c  %-15s %3d\n", i, bottom, top, memlist[i].file, memlist[i].line);
-    prev = next = -1;
-    for (j = 0; j < Nmemlist; j++) {
-      if (memlist[j].ptr < memlist[i].ptr) {
-	if (prev == -1) prev = j;
-	if (memlist[j].ptr > memlist[prev].ptr) prev = j;
-      }
-      if (memlist[j].ptr > memlist[i].ptr) {
-	if (next == -1) next = j;
-	if (memlist[j].ptr < memlist[next].ptr) next = j;
-      }
-    }	  
-    if (prev == -1) {
-      fprintf (stderr, "no previous memory block\n");
-    } else {
-      fprintf (stderr, "prev:     %3d           %-15s %3d\n", prev, memlist[prev].file, memlist[prev].line);
-    }
-    if (next == -1) {
-      fprintf (stderr, "no next memory block\n");
-    } else {
-      fprintf (stderr, "next:     %3d           %-15s %3d\n", next, memlist[next].file, memlist[next].line);
-    }
-  }
+    Ntotal ++;
+    Nbytes += thisBlock->size;
+
+    thisBlock = thisBlock->prevBlock;
+  }
+ 
+  fprintf (stderr, "%zd memory blocks allocated (%zd bytes total), %zd good, %zd bad\n", Ntotal, Nbytes, Ngood, Nbad);
+
   return;
 }
@@ -254,40 +240,40 @@
 void ohana_memdump_func (int allmemory) {
 
-  int i, Ns[4], N;
-  char S[4];
-
-  if (Nmemlist == 0) {
+  if (!lastBlock) {
     fprintf (stderr, "no memory allocated\n");
     return;
   }
 
-  S[0] = 'A';
-  S[1] = 'R';
-  S[2] = 'F';
-  S[3] = 'X';
-  Ns[0] = Ns[1] = Ns[2] = Ns[3] = 0;
-  N = 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;
-    if (memlist[i].state == STATE_EXTERNAL) {
-      fprintf (stderr, "%3d %4d %lx : %lx - extern (extern     extern) %c %s %d\n", 
-	       Ns[N], i, (long)memlist[i].ptr, (long)memlist[i].ptr, 
-	       S[N], memlist[i].file, memlist[i].line);
+  Memblock *thisBlock = lastBlock;
+
+  size_t Ntotal = 0;
+  size_t Nbytes = 0;
+
+  fprintf (stderr, " entry | bytes | cumulative | STATUS | file | line | function\n");
+
+  while (thisBlock) {
+
+    int good = TRUE;
+    
+    if (thisBlock->startblock != OHANA_MEMMAGIC) good = FALSE;
+    if (thisBlock->endblock != OHANA_MEMMAGIC) good = FALSE;
+
+    // pointer to the start of the user memory
+    char *ptr = (char *)(thisBlock + 1) + thisBlock->size;
+    uint32_t endpost = *(uint32_t *)ptr;
+    if (endpost != OHANA_MEMMAGIC) good = FALSE;
+
+    // XXX keep checking even if memory is corrupted?
+    Ntotal ++;
+    Nbytes += thisBlock->size;
+
+    if (good) {
+      fprintf (stderr, "  %zd  %zd  %zd  GOOD  %s %d, func: %s\n", Ntotal, thisBlock->size, Nbytes, thisBlock->file, thisBlock->line, thisBlock->func);
     } else {
-      fprintf (stderr, "%3d %4d %lx : %lx - %lx (%lx %lx) %c %s %d\n", 
-	       Ns[N], i, (long)memlist[i].ptr, (long)(memlist[i].ptr - NMEMBYTE), (long)(memlist[i].ptr + memlist[i].size + NMEMBYTE), 
-	       *(long *)(memlist[i].ptr - NMEMBYTE), *(long *)(memlist[i].ptr + memlist[i].size), 
-	       S[N], memlist[i].file, memlist[i].line);
+      fprintf (stderr, "  %zd  %zd  %zd  BAD   %s %d, func: %s\n", Ntotal, thisBlock->size, Nbytes, thisBlock->file, thisBlock->line, thisBlock->func);
     }
-  }
-  fprintf (stderr, "Nused: %d (Nalloc: %d, Nrealloc: %d), Nfree: %d\n", 
-	   Ns[0]+Ns[1], Ns[0], Ns[1], Ns[2]);
-  return;
-}
-# endif
+
+    thisBlock = thisBlock->prevBlock;
+  }
+  return;
+}
