X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=basics.h;h=97cbec1589083aa06dd1b01093105f8df71e2160;hb=e38bc834442d5369a523ba47d74865e48995ace4;hp=273aa1dfd545dd2149a51034096ca29a0123fa1b;hpb=aa6692a9fd2badf8e8e686b92075f041dc03bbef;p=SXSI%2FXMLTree.git diff --git a/basics.h b/basics.h index 273aa1d..97cbec1 100644 --- a/basics.h +++ b/basics.h @@ -1,15 +1,21 @@ -#ifndef BASICS_H -#define BASICS_H +#ifndef BASICS_H_ +#define BASICS_H_ + #include #include +//#include // for memset +#include +#include +#include +#define B_ERROR(msg) do { fprintf(stderr,"%s\n", msg); exit(1); } while (0) inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t res; res = fread(ptr,size,nmemb,stream); if (res < nmemb) - throw "ufread I/O error"; + B_ERROR ("ufread I/O error" ); return; } @@ -18,7 +24,7 @@ inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) size_t res; res = fwrite(ptr,size,nmemb,stream); if (res < nmemb) - throw "ufwrite I/O error"; + B_ERROR("ufwrite I/O error"); return; } @@ -27,7 +33,24 @@ inline void *urealloc(void *ptr, size_t size) void *dest = realloc(ptr,size); //don't fail if we requested size 0 if (dest == NULL && size > 0 ) - throw std::bad_alloc(); + B_ERROR("urealoc error"); + return dest; + } +// realloc and set to 0 +inline void *urecalloc(void *ptr, size_t o_size, size_t n_size) + { + if (o_size == n_size) + return ptr; + + void *dest = realloc(ptr,n_size); + //don't fail if we requested size 0 + if (dest == NULL && n_size > 0 ) + B_ERROR("urecalloc error"); + // Initialize the new area with 0 + void * n_area_start = &(((char*) dest)[o_size]); + // memset(n_area_start,0, n_size - o_size); + for(size_t i = 0; i < n_size - o_size;i++) + ((char *) n_area_start)[i] = 0; return dest; } @@ -36,7 +59,7 @@ inline void *ucalloc(size_t nmemb, size_t size) void * dest = calloc(nmemb,size); //don't fail if we requested size 0 if (dest == NULL && nmemb > 0 && size > 0 ) - throw std::bad_alloc(); + B_ERROR("ucalloc error"); return dest; } @@ -44,7 +67,7 @@ inline void *umalloc(size_t size) { void * dest = malloc(size); if (dest == NULL && size > 0) - throw std::bad_alloc(); + B_ERROR("umaloc error"); return dest; }