1 #ifndef _AFNI_AMALLOC_HEADER_ 2 #define _AFNI_AMALLOC_HEADER_ 3 /************************************************************************ 4 Casting macros to fixup AFNI stuff for compiling with g++. 5 Original dealt with only malloc() casting stuff, but more 6 has been added since. 7 ************************************************************************/ 8 9 /*---------------------------------------------------------------------*/ 10 /*! Replacement for malloc(). */ 11 12 #define AFMALL(typ,siz) (typ*) calloc(1,siz) 13 14 /*---------------------------------------------------------------------*/ 15 /*! Replacement for realloc(). */ 16 17 #define AFREALL(v,typ,siz) (typ*) realloc((void*)v,sizeof(typ)*(siz)) 18 19 /*---------------------------------------------------------------------*/ 20 /*! Replacement for free(). */ 21 22 #define AFFREE(v) free((void*)v) 23 24 /*---------------------------------------------------------------------*/ 25 /*! Cast a function pointer to the right 26 prototype and call it, for 0D transformations. */ 27 28 #define AFNI_CALL_0D_function(func,nar,far) \ 29 do{ void (*fp)(int,float *) = (void (*)(int,float *))(func) ; \ 30 if( fp != NULL ) \ 31 fp( (nar) , (far) ) ; \ 32 } while(0) 33 34 /*---------------------------------------------------------------------*/ 35 /*! Cast a function pointer to the right 36 prototype and call it, for 1D transformations. */ 37 38 #define AFNI_CALL_1D_function(func,nar,d1,d2,far) \ 39 do{ void (*fp)(int,double,double,float *) = \ 40 (void (*)(int,double,double,float *))(func) ; \ 41 if( fp != NULL ) \ 42 fp(nar,(double)d1,(double)d2,far) ; \ 43 } while(0) 44 45 /*---------------------------------------------------------------------*/ 46 /*! Cast a function pointer to the right 47 prototype and call it, for 1D transformations 48 that also return a pointer to a string. */ 49 50 #define AFNI_CALL_1D_funcstr(func,nar,d1,d2,far,str) \ 51 do{ void (*fp)(int,double,double,float *,char **) = \ 52 (void (*)(int,double,double,float *,char**))(func) ; \ 53 if( fp != NULL ) \ 54 fp(nar,(double)d1,(double)d2,far,(char **)&(str)) ; \ 55 } while(0) 56 57 /*---------------------------------------------------------------------*/ 58 /*! Cast a function pointer to the right prototype 59 and call it, for 1D transformations of images. */ 60 61 #define AFNI_CALL_1D_funcmrim(func,mage) \ 62 do{ void (*fp)(MRI_IMAGE *) = (void (*)(MRI_IMAGE *))(func) ; \ 63 if( fp != NULL ) \ 64 fp(mage) ; \ 65 } while(0) 66 67 /*---------------------------------------------------------------------*/ 68 /*! Cast a function pointer to the right prototype 69 and call it, for 1D transformations of images 70 that also return a pointer to a string. */ 71 72 #define AFNI_CALL_1D_funcmrimstr(func,mage,str) \ 73 do{ void (*fp)(MRI_IMAGE *,char **) = \ 74 (void (*)(MRI_IMAGE *,char **))(func) ; \ 75 if( fp != NULL ) \ 76 fp(mage,(char **)&(str)) ; \ 77 } while(0) 78 79 /*---------------------------------------------------------------------*/ 80 /*! Cast a function pointer to the right 81 prototype and call it, for 2D transformations. */ 82 83 #define AFNI_CALL_2D_function(func,n1,n2,d1,d2,far) \ 84 do{ void (*fp)(int,int,double,double,float *) = \ 85 (void (*)(int,int,double,double,float *))(func) ; \ 86 if( fp != NULL ) \ 87 fp( (n1),(n2),(double)(d1),(double)(d2),far ) ; \ 88 } while(0) 89 90 /*---------------------------------------------------------------------*/ 91 /*! Cast a function pointer to the right 92 prototype and call it, for projections. 93 The output value is assigned to "val". */ 94 95 #define AFNI_CALL_proj_function(func,n,far,val) \ 96 do{ float (*fp)(int,float *) = (float (*)(int,float *))(func); \ 97 if( fp != NULL ) (val) = fp(n,far) ; \ 98 } while(0) 99 100 /*---------------------------------------------------------------------*/ 101 /*! Cast a function pointer to the right 102 prototype and call it, for FIM functions. */ 103 104 #define AFNI_CALL_fim_function(func,n,ts,ud,nb,vv) \ 105 do{ void (*fp)(int,float *,void *,int,void *) = \ 106 (void (*)(int,float *,void *,int,void *))(func) ; \ 107 if( fp != NULL ) \ 108 fp(n,ts,(void *)(ud),nb,(void *)(vv)) ; \ 109 } while(0) 110 111 /************************************************************************ 112 Macros to call functions with arguments of general types 113 specified in the macro call, with various numbers of arguments. 114 Functions return nothing (_VOID_) or return a value (_VALU_). 115 ************************************************************************/ 116 117 /*---------------------------------------------------------------------*/ 118 #define AFNI_CALL_VOID_1ARG(func,typ1,arg1) \ 119 do{ void (*fp)(typ1) = (void (*)(typ1))(func) ; \ 120 if( fp != NULL ) \ 121 fp((typ1)(arg1)) ; \ 122 } while(0) 123 124 /*---------------------------------------------------------------------*/ 125 #define AFNI_CALL_VALU_1ARG(func,vtyp,vval,typ1,arg1) \ 126 do{ vtyp (*fp)(typ1) = (vtyp (*)(typ1))(func) ; \ 127 if( fp != NULL ) \ 128 (vval) = fp((typ1)(arg1)) ; \ 129 } while(0) 130 131 /*---------------------------------------------------------------------*/ 132 #define AFNI_CALL_VOID_2ARG(func,typ1,arg1,typ2,arg2) \ 133 do{ void (*fp)(typ1,typ2) = (void (*)(typ1,typ2))(func) ; \ 134 if( fp != NULL ) \ 135 fp((typ1)(arg1),(typ2)(arg2)) ; \ 136 } while(0) 137 138 /*---------------------------------------------------------------------*/ 139 #define AFNI_CALL_VALU_2ARG(func,vtyp,vval,typ1,arg1,typ2,arg2) \ 140 do{ vtyp (*fp)(typ1,typ2) = (vtyp (*)(typ1,typ2))(func) ; \ 141 if( fp != NULL ) \ 142 (vval) = fp((typ1)(arg1),(typ2)(arg2)) ; \ 143 } while(0) 144 145 /*---------------------------------------------------------------------*/ 146 #define AFNI_CALL_VOID_3ARG(func,typ1,arg1,typ2,arg2,typ3,arg3) \ 147 do{ void (*fp)(typ1,typ2,typ3) = (void (*)(typ1,typ2,typ3))(func) ; \ 148 if( fp != NULL ) \ 149 fp((typ1)(arg1),(typ2)(arg2),(typ3)(arg3)) ; \ 150 } while(0) 151 152 /*-------------------------------------------------------------------------*/ 153 #define AFNI_CALL_VALU_3ARG(func,vtyp,vval,typ1,arg1,typ2,arg2,typ3,arg3) \ 154 do{ vtyp (*fp)(typ1,typ2,typ3) = (vtyp (*)(typ1,typ2,typ3))(func) ; \ 155 if( fp != NULL ) \ 156 (vval) = fp((typ1)(arg1),(typ2)(arg2),(typ3)(arg3)) ; \ 157 } while(0) 158 159 /*----------------------------------------------------------------------------*/ 160 #define AFNI_CALL_VOID_4ARG(func,typ1,arg1,typ2,arg2,typ3,arg3,typ4,arg4) \ 161 do{ void (*fp)(typ1,typ2,typ3,typ4) = (void (*)(typ1,typ2,typ3,typ4))(func); \ 162 if( fp != NULL ) \ 163 fp((typ1)(arg1),(typ2)(arg2),(typ3)(arg3),(typ4)(arg4)) ; \ 164 } while(0) 165 166 /*----------------------------------------------------------------------------------*/ 167 #define AFNI_CALL_VALU_4ARG(func,vtyp,vval,typ1,arg1,typ2,arg2,typ3,arg3,typ4,arg4) \ 168 do{ vtyp (*fp)(typ1,typ2,typ3,typ4) = (vtyp (*)(typ1,typ2,typ3,typ4))(func) ; \ 169 if( fp != NULL ) \ 170 (vval) = fp((typ1)(arg1),(typ2)(arg2),(typ3)(arg3),(typ4)(arg4)) ; \ 171 } while(0) 172 173 /*----------------------------------------------------------------------------------*/ 174 #define AFNI_CALL_VOID_7ARG(func,typ1,arg1,typ2,arg2,typ3,arg3,typ4,arg4,typ5,arg5,typ6,arg6,typ7,arg7) \ 175 do{ void (*fp)(typ1,typ2,typ3,typ4,typ5,typ6,typ7) = (void (*)(typ1,typ2,typ3,typ4,typ5,typ6,typ7))(func) ; \ 176 if( fp != NULL ) \ 177 fp((typ1)(arg1),(typ2)(arg2),(typ3)(arg3),(typ4)(arg4),(typ5)(arg5),(typ6)(arg6),(typ7)(arg7)) ; \ 178 } while(0) 179 180 /******************************************************************************/ 181 #endif /* _AFNI_AMALLOC_HEADER_ */ 182