X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=swcsa%2Futils%2Fbasics.c;h=437355ef1c8e656c48d59bd0821c8feb91792cef;hb=refs%2Fheads%2Fmaster;hp=f213ed6d4d78df843b3e9f32748f9ae3d84b4f65;hpb=102e33b134075765e6d4e0c38bc1307568ce5602;p=SXSI%2FTextCollection.git diff --git a/swcsa/utils/basics.c b/swcsa/utils/basics.c index f213ed6..437355e 100755 --- a/swcsa/utils/basics.c +++ b/swcsa/utils/basics.c @@ -1,3 +1,6 @@ +#ifdef __cplusplus +extern "C" { +#endif // Basics @@ -5,43 +8,60 @@ #include #include #include +#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 bitzero (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 bitzero (register uint *e, register uint p, if (len > 0) *e &= ~(((1<