Debug swcsa
[SXSI/TextCollection.git] / swcsa / utils / basics.c
index 398e975..437355e 100755 (executable)
@@ -1,3 +1,6 @@
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 // Basics
 
@@ -5,43 +8,60 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#define ALLOC_WARN_LIMIT (40*1024*1024)
 
                // Memory management
-       
-       void *Malloc (int 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;
           }
-       
-       void Free (void *p)
-       
-          { if (p) free (p); 
-          }
-       
-       void *Realloc (void *p, int 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 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);
+            }
           }
 
+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"
 
         // bits needed to represent a number between 0 and n
 
-uint bits (uint n)
+uint _bits (uint n)
 
    { uint b = 0;
      while (n)
@@ -68,7 +88,7 @@ uint bitread (uint *e, uint p, uint len)
 
        // writes e[p..p+len-1] = s, len <= W
 
-void bitwrite (register uint *e, register uint p, 
+void bitwrite (register uint *e, register uint p,
               register uint len, register uint s)
 
    { e += p/W; p %= W;
@@ -89,7 +109,7 @@ void bitwrite (register uint *e, register uint p,
    }
        // writes e[p..p+len-1] = 0
 
-void bitzero2 (register uint *e, register uint p, 
+void bitzero2 (register uint *e, register uint p,
               register uint len)
 
    { e += p/W; p %= W;
@@ -105,3 +125,6 @@ void bitzero2 (register uint *e, register uint p,
      if (len > 0)
        *e &= ~(((1<<len)-1)<<p);
    }
+#ifdef __cplusplus
+}
+#endif