/* Some general utility routines defined as macros. */ #ifndef _genutil_h #define _genutil_h #include #include #include #include #include extern void gettimeofday(); #define MAX(a,b) ((a)>(b) ? (a) : (b)) #define numbits(out,in) \ { \ unsigned _n; \ _n = (in); \ for(out=0;_n>0;_n>>=1) \ out++; \ } /* error message */ #define ERROR(string) { \ fprintf(stderr, "ERROR: %s\n", string); \ exit(-1); } /* Memory allocation with error messages */ #define MALLOC(tp,n,type) { \ if ( n && !( tp = (type *) malloc((unsigned) (n)*sizeof(type)) ) ) \ ERROR("Out of memory, ACU!"); } #define P_MALLOC(tp,n,type) { \ if ( n && !( tp = (type *) p_malloc((unsigned) (n)*sizeof(type)) ) ) \ ERROR("Out of memory, DPU!"); } #define REALLOC(tp,n,type) { \ if ( n && !( tp = (type *) realloc((plural char *) tp, \ (unsigned) (n)*sizeof(type)) ) ) \ ERROR("Out of memory, ACU!"); } #define P_REALLOC(tp,n,type) { \ if ( n && !( tp = (type *) p_realloc((plural char *) tp, \ (unsigned) (n)*sizeof(type)) ) ) \ ERROR("Out of memory, DPU!"); } #define FREE(pt) \ if (pt != NULL) free((char*) pt) #define P_FREE(pt) \ if (pt != NULL) p_free((plural char*) pt) /* Dynamic arrays */ #define DYNARR(listpt,j,list_length,data,type) { \ while ((j) >= (list_length)) { \ list_length *= 2; \ REALLOC(listpt,list_length,type); \ } \ listpt[j] = data; \ } #define P_DYNARR(listpt,j,list_length,data,type) { \ while ((j) >= (list_length)) { \ list_length *= 2; \ P_REALLOC(listpt,list_length,type); \ } \ listpt[j] = data; \ } #endif