Index: trunk/src/sh_mem.c
===================================================================
--- trunk/src/sh_mem.c	(revision 581)
+++ trunk/src/sh_mem.c	(revision 1)
@@ -20,7 +20,4 @@
 #include "config_xor.h"
 
-#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
-#define _XOPEN_SOURCE 500
-#endif
 
 #include <stdio.h>
@@ -35,16 +32,14 @@
 #endif
 
-#define SH_REAL_SET
-
 #include "samhain.h"
 #include "sh_error.h"
 #include "sh_utils.h"
 #include "sh_mem.h"
-#include "sh_pthread.h"
-
-extern int safe_logger (int thesignal, int method, char * details);
+
+extern int safe_logger (int signal, int method, pid_t thepid);
 
 #undef  FIL__
 #define FIL__  _("sh_mem.c")
+static int eblock = 0;
 
 #ifdef MEM_DEBUG
@@ -73,41 +68,51 @@
 unsigned long Mem_Current = 0, Mem_Max = 0;
 
-#ifdef HAVE_PTHREAD
-SH_MUTEX_RECURSIVE(mutex_mem);
-#endif
-
-/* define MEM_LOG to enable this */
-/* #define MEM_LOG 1 */
+#if 0
+#define MEM_DETAILS 
+#endif
+
+#ifdef MEM_DETAILS
+int           max_upto_032 = 0;
+int           max_upto_064 = 0;
+int           max_upto_128 = 0;
+int           max_upto_256 = 0;
+int           max_upto_512 = 0;
+int           max_upto_1024 = 0;
+int           max_upto_4096 = 0;
+int           max_upto_inf = 0;
+
+int           now_upto_032 = 0;
+int           now_upto_064 = 0;
+int           now_upto_128 = 0;
+int           now_upto_256 = 0;
+int           now_upto_512 = 0;
+int           now_upto_1024 = 0;
+int           now_upto_4096 = 0;
+int           now_upto_inf = 0;
+
+int           tot_upto_032 = 0;
+int           tot_upto_064 = 0;
+int           tot_upto_128 = 0;
+int           tot_upto_256 = 0;
+int           tot_upto_512 = 0;
+int           tot_upto_1024 = 0;
+int           tot_upto_4096 = 0;
+int           tot_upto_inf = 0;
+#endif
+
 #ifdef MEM_LOG
 void sh_mem_dump ()
 {
   memlist_t   * this = memlist;
-  FILE * fd;
-  static unsigned int nr = 0;
-  char filename[256];
-
-  snprintf(filename, sizeof(filename), "sh_mem_dump.%04u.%lu", nr, (unsigned long) sh.pid);
-  
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
-  fd = fopen(filename, "w");
-  if (!fd)
-    {
-      perror(filename);
-      _exit(EXIT_FAILURE);
-    }
+
+
+  FILE * fd = fopen(MEM_LOG, "w");
 
   while (this != NULL)
     {
-      fprintf (fd, "## %20s %5d %ld\n",  this->file, this->line, this->size);
-      fprintf (fd, "%10p %8ld\n", (void *)this->address, this->size);
+      fprintf (fd, "%20s %5d %ld\n",  this->file, this->line, this->size);
       this = this->next;
     }
-  sl_fclose(FIL__, __LINE__, fd);
-
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-  ++nr;
-  /* _exit(EXIT_SUCCESS); */
+  fclose(fd);
   return;
 }
@@ -119,14 +124,10 @@
 #endif
 
-memlist_t ** sh_dummy_114_merrlist;
-
 void sh_mem_stat ()
 {
   memlist_t   * this;
-  memlist_t   * merrlist = NULL;
+
 
   SL_ENTER(_("sh_mem_stat"));
-
-  sh_dummy_114_merrlist = (memlist_t **) &merrlist;
 
   if (Alloc_Count == Free_Count) 
@@ -142,6 +143,17 @@
 		   Mem_Max, Mem_Current);
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
+#ifdef MEM_DETAILS
+  fprintf(stderr, "\n");
+  fprintf(stderr, "__SIZE_____TOTAL___MAXIMUM___\n");
+  fprintf(stderr, "    32    %6d    %6d\n", tot_upto_032, max_upto_032);
+  fprintf(stderr, "    64    %6d    %6d\n", tot_upto_064, max_upto_064);
+  fprintf(stderr, "   128    %6d    %6d\n", tot_upto_128, max_upto_128);
+  fprintf(stderr, "   256    %6d    %6d\n", tot_upto_256, max_upto_256);
+  fprintf(stderr, "   512    %6d    %6d\n", tot_upto_512, max_upto_512);
+  fprintf(stderr, "  1024    %6d    %6d\n", tot_upto_1024, max_upto_1024);
+  fprintf(stderr, "  4096    %6d    %6d\n", tot_upto_4096, max_upto_4096);
+  fprintf(stderr, "   inf    %6d    %6d\n", tot_upto_inf, max_upto_inf);
+  fprintf(stderr, "\n");
+#endif
 
   this = memlist;
@@ -149,47 +161,22 @@
   while (this != NULL) 
     {
-      memlist_t   * merr = calloc(1,sizeof(memlist_t));
-
-      memcpy(merr, this, sizeof(memlist_t));
-      merr->next = merrlist;
-      merrlist   = merr;
-
+      sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_NOTFREE,
+		       this->size, this->file, this->line);
       this = this->next;
     }
 
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-
-  while (merrlist != NULL) 
-    {
-      memlist_t   * tmp = merrlist;
-      merrlist = merrlist->next;
-      
-      sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_NOTFREE,
-		       tmp->size, tmp->file, tmp->line);
-      free(tmp);
-    }
-
   SL_RET0(_("sh_mem_stat"));
 }
 
-memlist_t ** sh_dummy_168_merrlist;
-
 void sh_mem_check ()
 {
   memlist_t * this;
-  memlist_t * merrlist = NULL;
-  memlist_t * merr;
   long        nerr = 0;
 
   SL_ENTER(_("sh_mem_check"));
-
-  sh_dummy_168_merrlist = (memlist_t **) &merrlist;
 
   sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MSTAMP,
 		   Mem_Max, Mem_Current);
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
   this = memlist;
 
@@ -198,11 +185,5 @@
       if ( this->address == NULL )
 	{
-	  merr = calloc (1,sizeof(memlist_t));
-
-	  memcpy(merr, this, sizeof(memlist_t));
-	  merr->size = 2;
-
-	  merr->next = merrlist;
-	  merrlist   = merr;
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL);
 	  ++nerr;
 	}
@@ -211,22 +192,12 @@
 	  if ( this->address[this->size]        != CHECKBYTE )
 	    {
-	      merr = calloc(1, sizeof(memlist_t));
-	      
-	      memcpy(merr, this, sizeof(memlist_t));
-	      merr->size = 1;
-	      
-	      merr->next = merrlist;
-	      merrlist   = merr;
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
+			       this->file, this->line, FIL__, __LINE__);
 	      ++nerr;
 	    }
 	  if ( this->real_address[SH_MEMMULT-1] != CHECKBYTE )
 	    {
-	      merr = calloc(1, sizeof(memlist_t));
-	      
-	      memcpy(merr, this, sizeof(memlist_t));
-	      merr->size = 0;
-	      
-	      merr->next = merrlist;
-	      merrlist   = merr;
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
+			       this->file, this->line, FIL__, __LINE__);
 	      ++nerr;
 	    }
@@ -235,25 +206,5 @@
     }
 
-
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-
-  while (merrlist != NULL) 
-    {
-      memlist_t   * tmp = merrlist;
-      merrlist = merrlist->next;
-      
-      if (tmp->size == 2)
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL,
-			   tmp->file, tmp->line, FIL__, __LINE__);
-      if (tmp->size == 1)
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
-			   tmp->file, tmp->line, FIL__, __LINE__);
-      else
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
-			   tmp->file, tmp->line, FIL__, __LINE__);
-      free(tmp);
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
-    }
+  /* if (nerr > 0) abort(); */
 
   SL_RET0(_("sh_mem_check"));
@@ -268,16 +219,21 @@
   SL_ENTER(_("sh_mem_malloc"));
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
-  the_realAddress = calloc(1,size + 2 * SH_MEMMULT);
+  the_realAddress = malloc(size + 2 * SH_MEMMULT);
   
   if ( the_realAddress  == NULL ) 
     {
-      (void) safe_logger (0, 0, NULL);
-
+      if (eblock == 0)
+	{
+	  eblock = 1;
+	  (void) safe_logger (0, 0, getpid());
+	  /*
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM,
+			   file, line);
+	  */
+	  eblock = 0;
+	}
       /* use _exit() rather than exit() - we malloc() in atexit() functions 
        */
-      _exit (EXIT_FAILURE);
+      _exit (42);
     }
   
@@ -296,68 +252,110 @@
     Max_Alloc_Count = Now_Alloc_Count;
 
+#ifdef MEM_DETAILS
+  if (size <= 32)
+    { 
+      ++now_upto_032;
+      ++tot_upto_032;
+      if (now_upto_032 > max_upto_032) max_upto_032 = now_upto_032;
+    }
+  else if  (size <= 64)
+    { 
+      ++now_upto_064;
+      ++tot_upto_064;
+      if (now_upto_064 > max_upto_064) max_upto_064 = now_upto_064;
+    }
+  else if  (size <= 128)
+    { 
+      ++now_upto_128;
+      ++tot_upto_128;
+      if (now_upto_128 > max_upto_128) max_upto_128 = now_upto_128;
+    }
+  else if  (size <= 256)
+    { 
+      ++now_upto_256;
+      ++tot_upto_256;
+      if (now_upto_256 > max_upto_256) max_upto_256 = now_upto_256;
+    }
+  else if  (size <= 512)
+    { 
+      ++now_upto_512;
+      ++tot_upto_512;
+      if (now_upto_512 > max_upto_512) max_upto_512 = now_upto_512;
+    }
+  else if  (size <= 1024)
+    { 
+      ++now_upto_1024;
+      ++tot_upto_1024;
+      if (now_upto_1024 > max_upto_1024) max_upto_1024 = now_upto_1024;
+    }
+  else if  (size <= 4096)
+    { 
+      ++now_upto_4096;
+      ++tot_upto_4096;
+      if (now_upto_4096 > max_upto_4096) max_upto_4096 = now_upto_4096;
+    }
+  else 
+    { 
+      ++now_upto_inf;
+      ++tot_upto_inf;
+      if (now_upto_inf > max_upto_inf) max_upto_inf = now_upto_inf;
+
+      fprintf(stderr, "\n___BIGSIZE___");
+      fprintf(stderr, "   %6d  -> %16s  %10d \n", size, file, line);
+      fprintf(stderr, "\n");
+
+    }
+#endif
+
   Mem_Current += size;
   Mem_Max = ( (Mem_Current > Mem_Max) ? Mem_Current : Mem_Max);
 
-  this = calloc(1,sizeof(memlist_t));
+  this = (memlist_t *) malloc (sizeof(memlist_t));
 
   if ( this == NULL) 
     {
-      (void) safe_logger(0, 0, NULL);
-
-      _exit(EXIT_FAILURE);
-    }
-  else
-    {
-      /* make list entry */
-
-      this->real_address = the_realAddress;
-      this->address      = theAddress;
-      this->size         = size;
-      this->line         = line;
-      sl_strlcpy(this->file, file, 20);
-
-      this->next = memlist;
-      memlist = this;
-    }
-
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
+      if (eblock == 0)
+	{
+	  eblock = 1;
+	  (void) safe_logger(0, 0, getpid());
+	  /*
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM,
+			   file, line);
+	  */
+	  eblock = 0;
+	}
+      SL_RETURN( the_realAddress, _("sh_mem_malloc"));
+    }
+
+  /* make list entry */
+
+  this->real_address = the_realAddress;
+  this->address      = theAddress;
+  this->size         = size;
+  this->line         = line;
+  sl_strlcpy(this->file, file, 20);
+
+  this->next = memlist;
+  memlist = this;
+
   SL_RETURN( theAddress, _("sh_mem_malloc"));
 }
 
-void ** sh_mem_dummy_a;
-memlist_t ** sh_mem_merr_3;
-
-void sh_mem_free (void * aa, char * file, int line)
-{
-  memlist_t * this;
-  memlist_t * before;
-  memlist_t * merr;
-  memlist_t * merrlist = NULL;
-  unsigned long        size   = 0;
-  void      * a;
-  volatile int         flag = 0;
+
+void sh_mem_free (void * a, char * file, int line)
+{
+  memlist_t * this   = memlist;
+  memlist_t * before = memlist;
+  unsigned long size = 0;
 
   SL_ENTER(_("sh_mem_free"));
 
-  a      = aa;
-  sh_mem_dummy_a = &a;
-  sh_mem_merr_3  = (memlist_t **) &merrlist;
-
-
   if ( a == NULL ) 
     {
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL,
-		       file, line, FIL__, __LINE__);
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
+		       file, line);
       SL_RET0(_("sh_mem_free"));
     }
     
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
-  this   = memlist;
-  before = memlist;
-  
   /* -- Find record. -- 
    */
@@ -372,6 +370,7 @@
   if (this == NULL) 
     {
-      flag = 1;
-      goto out;
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MREC,
+		       file, line);
+      SL_RET0(_("sh_mem_free"));
     } 
   else 
@@ -381,23 +380,10 @@
       if ( this->address[this->size]        != CHECKBYTE )
 	{
-	  merr = calloc(1, sizeof(memlist_t));
-
-	  memcpy(merr, this, sizeof(memlist_t));
-	  merr->size = 1;
-
-	  merr->next = merrlist;
-	  merrlist = merr;
-	}
-
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
+			   this->file, this->line, file, line);
+	}
       if ( this->real_address[SH_MEMMULT-1] != CHECKBYTE )
-	{
-	  merr = calloc(1,sizeof(memlist_t));
-
-	  memcpy(merr, this, sizeof(memlist_t));
-	  merr->size = 0;
-
-	  merr->next = merrlist;
-	  merrlist = merr;
-	}
+	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
+			 this->file, this->line, file, line);
 
       size = this->size;
@@ -412,33 +398,27 @@
   if (this)
     free(this);
-
   ++Free_Count;
   --Now_Alloc_Count;
 
+#ifdef MEM_DETAILS
+  if (size <= 32)
+    --now_upto_032;
+  else if  (size <= 64)
+    --now_upto_064;
+  else if  (size <= 128)
+    --now_upto_128;
+  else if  (size <= 256)
+    --now_upto_256;
+  else if  (size <= 512)
+    --now_upto_512;
+  else if  (size <= 1024)
+    --now_upto_1024;
+  else if  (size <= 4096)
+    --now_upto_4096;
+  else 
+    --now_upto_inf;
+#endif
+
   Mem_Current -= size;
- out:
-  ; /* label at end of compound statement */
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-
-  while (merrlist != NULL) 
-    {
-      memlist_t   * tmp = merrlist;
-      merrlist = merrlist->next;
-      
-      if (tmp->size == 1)
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
-			   tmp->file, tmp->line, file, line);
-      else
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
-			   tmp->file, tmp->line, file, line);
-      free(tmp);
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
-    }
-
-  if (flag != 0)
-    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MREC,
-		     file, line);
-
   SL_RET0(_("sh_mem_free"));
 }
@@ -457,5 +437,4 @@
     {
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL);
-      SH_ABORT;
     }
   SL_RET0(_("sh_mem_free"));
@@ -468,19 +447,24 @@
   SL_ENTER(_("sh_mem_malloc"));
 
-  theAddress = calloc(1,size);
-
-  if ( theAddress != NULL ) 
-    {
-      SL_RETURN( theAddress, _("sh_mem_malloc"));
-    }
-  else
-    {
-      (void) safe_logger(0, 0, NULL);
-
+  theAddress = malloc(size);
+
+  if ( theAddress == NULL ) 
+    {
+      if (eblock == 0)
+	{
+	  eblock = 1;
+	  (void) safe_logger(0, 0, getpid());
+	  /*
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM);
+	  */
+	  eblock = 0;
+	}
       /* use _exit() rather than exit() - we malloc() in atexit()  
        */
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
-    }
-}
-#endif
+      _exit (42);
+    }
+  /* memset (theAddress, 0, 1); *//* needs testing */
+
+  SL_RETURN( theAddress, _("sh_mem_malloc"));
+}
+#endif
