00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef PLANCK_C_UTILS_H
00034 #define PLANCK_C_UTILS_H
00035
00036 #include <math.h>
00037 #include <stdlib.h>
00038 #include <stddef.h>
00039
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043
00044 void util_fail_ (const char *file, int line, const char *func, const char *msg);
00045 void util_warn_ (const char *file, int line, const char *func, const char *msg);
00046 void *util_malloc_ (size_t sz);
00047 void util_free_ (void *ptr);
00048
00049 #if defined (__GNUC__)
00050 #define UTIL_FUNC_NAME__ __func__
00051 #else
00052 #define UTIL_FUNC_NAME__ "unknown"
00053 #endif
00054
00055
00056
00057
00058
00059 #define UTIL_ASSERT(cond,msg) \
00060 if(!(cond)) util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
00061
00062
00063
00064 #define UTIL_WARN(cond,msg) \
00065 if(!(cond)) util_warn_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
00066
00067
00068
00069
00070 #define UTIL_FAIL(msg) \
00071 util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
00072
00073
00074
00075
00076
00077 #define ALLOC(ptr,type,num) \
00078 do { (ptr)=(type *)util_malloc_((num)*sizeof(type)); } while (0)
00079
00080
00081
00082
00083 #define RALLOC(type,num) \
00084 ((type *)util_malloc_((num)*sizeof(type)))
00085
00086
00087
00088 #define DEALLOC(ptr) \
00089 do { util_free_(ptr); (ptr)=NULL; } while(0)
00090 #define RESIZE(ptr,type,num) \
00091 do { util_free_(ptr); ALLOC(ptr,type,num); } while(0)
00092 #define GROW(ptr,type,sz_old,sz_new) \
00093 do { \
00094 if ((sz_new)>(sz_old)) \
00095 { RESIZE(ptr,type,2*(sz_new));sz_old=2*(sz_new); } \
00096 } while(0)
00097
00098
00099 #define SET_ARRAY(ptr,i1,i2,val) \
00100 do { \
00101 ptrdiff_t cnt_; \
00102 for (cnt_=(i1);cnt_<(i2);++cnt_) (ptr)[cnt_]=(val); \
00103 } while(0)
00104
00105
00106
00107 #define COPY_ARRAY(src,dest,i1,i2) \
00108 do { \
00109 ptrdiff_t cnt_; \
00110 for (cnt_=(i1);cnt_<(i2);++cnt_) (dest)[cnt_]=(src)[cnt_]; \
00111 } while(0)
00112
00113 #define ALLOC2D(ptr,type,num1,num2) \
00114 do { \
00115 size_t cnt_, num1_=(num1), num2_=(num2); \
00116 ALLOC((ptr),type *,num1_); \
00117 ALLOC((ptr)[0],type,num1_*num2_); \
00118 for (cnt_=1; cnt_<num1_; ++cnt_) \
00119 (ptr)[cnt_]=(ptr)[cnt_-1]+num2_; \
00120 } while(0)
00121 #define DEALLOC2D(ptr) \
00122 do { if(ptr) DEALLOC((ptr)[0]); DEALLOC(ptr); } while(0)
00123
00124 #define FAPPROX(a,b,eps) \
00125 (fabs((a)-(b))<((eps)*fabs(b)))
00126 #define ABSAPPROX(a,b,eps) \
00127 (fabs((a)-(b))<(eps))
00128 #define IMAX(a,b) \
00129 (((a)>(b)) ? (a) : (b))
00130 #define IMIN(a,b) \
00131 (((a)<(b)) ? (a) : (b))
00132
00133 #define SWAP(a,b,type) \
00134 do { type tmp_=(a); (a)=(b); (b)=tmp_; } while(0)
00135
00136 #define CHECK_STACK_ALIGN(align) \
00137 do { \
00138 double foo; \
00139 UTIL_WARN((((size_t)(&foo))&(align-1))==0, \
00140 "WARNING: stack not sufficiently aligned!"); \
00141 } while(0)
00142
00143 #ifdef __cplusplus
00144 }
00145 #endif
00146
00147 #endif