Debug swcsa
[SXSI/TextCollection.git] / swcsa / utils / basics.c
index c9cb275..437355e 100755 (executable)
@@ -8,37 +8,54 @@ extern "C" {
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#define ALLOC_WARN_LIMIT (40*1024*1024)
 
                // Memory management
 
-static void *Malloc (size_t n)
+void *Malloc_ (size_t n, size_t l, char * file)
 
           { void *p;
-            if (n == 0) return NULL;
+
+            if (n > ALLOC_WARN_LIMIT)
+              {
+                fprintf(stderr, "\nWarning: allocating %lu bytes, file:%s, line: %lu\n",
+                        n, file, l);
+              };
+
             p = (void*) malloc (n);
-            if (p == NULL)
-               { fprintf (stderr,"Could not allocate %i bytes\n",n);
-                 exit(1);
+            if (p == NULL && n > 0)
+              {
+                fprintf (stderr,"Could not allocate %lu bytes, file: %s, line: %lu\n",n,file,l);
+                exit(1);
                }
             return p;
           }
 
-static void Free (void *p)
+void Free_ (void *p, size_t l, char * file)
 
           { if (p) free (p);
+            else {
+              fprintf (stderr,"Double free, file: %s, line: %lu\n",file,l);
+              exit(1);
+            }
           }
 
-static void *Realloc (void *p, size_t n)
-
-          { if (p == NULL) return Malloc (n);
-            if (n == 0) { Free(p); return NULL; }
-            p = (void*) realloc (p,n);
-            if (p == NULL)
-               { fprintf (stderr,"Could not allocate %i bytes\n",n);
-                 exit(1);
-               }
-            return p;
-          }
+void *Realloc_ (void *p, size_t n, size_t l, char * file)
+{
+  if (n > ALLOC_WARN_LIMIT)
+    {
+      fprintf(stderr, "\nWarning: allocating %lu bytes, file:%s, line: %lu\n",
+             n, file, l);
+    };
+
+  p = (void*) realloc (p,n);
+  if (p == NULL)
+    {
+      fprintf (stderr,"Could not re-allocate %lu bytes, file: %s, line: %lu\n",n,file,l);
+      exit(1);
+    }
+  return p;
+}
 
 #include "basics.h"