+#ifdef __cplusplus
+extern "C" {
+#endif
// Basics
#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)
// 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;
}
// writes e[p..p+len-1] = 0
-void bitzero (register uint *e, register uint p,
+void bitzero2 (register uint *e, register uint p,
register uint len)
{ e += p/W; p %= W;
if (len > 0)
*e &= ~(((1<<len)-1)<<p);
}
+#ifdef __cplusplus
+}
+#endif