00001
00010 #ifndef _GK_MKMEMORY_H_
00011 #define _GK_MKMEMORY_H_
00012
00013
00014 #define GK_MKALLOC(PRFX, TYPE)\
00015 \
00016 \
00017 \
00018 TYPE *PRFX ## malloc(size_t n, char *msg)\
00019 {\
00020 return (TYPE *)gk_malloc(sizeof(TYPE)*n, msg);\
00021 }\
00022 \
00023 \
00024 \
00025 \
00026 \
00027 TYPE *PRFX ## realloc(TYPE *ptr, size_t n, char *msg)\
00028 {\
00029 return (TYPE *)gk_realloc((void *)ptr, sizeof(TYPE)*n, msg);\
00030 }\
00031 \
00032 \
00033 \
00034 \
00035 \
00036 TYPE *PRFX ## smalloc(size_t n, TYPE ival, char *msg)\
00037 {\
00038 TYPE *ptr;\
00039 \
00040 ptr = (TYPE *)gk_malloc(sizeof(TYPE)*n, msg);\
00041 if (ptr == NULL) \
00042 return NULL; \
00043 \
00044 return PRFX ## set(n, ival, ptr); \
00045 }\
00046 \
00047 \
00048 \
00049 \
00050 \
00051 TYPE *PRFX ## set(size_t n, TYPE val, TYPE *x)\
00052 {\
00053 size_t i;\
00054 \
00055 for (i=0; i<n; i++)\
00056 x[i] = val;\
00057 \
00058 return x;\
00059 }\
00060 \
00061 \
00062 \
00063 \
00064 \
00065 TYPE *PRFX ## copy(size_t n, TYPE *a, TYPE *b)\
00066 {\
00067 return (TYPE *)memmove((void *)b, (void *)a, sizeof(TYPE)*n);\
00068 }\
00069 \
00070 \
00071 \
00072 \
00073 \
00074 TYPE **PRFX ## AllocMatrix(size_t ndim1, size_t ndim2, TYPE value, char *errmsg)\
00075 {\
00076 gk_idx_t i, j;\
00077 TYPE **matrix;\
00078 \
00079 matrix = (TYPE **)gk_malloc(ndim1*sizeof(TYPE *), errmsg);\
00080 if (matrix == NULL) \
00081 return NULL;\
00082 \
00083 for (i=0; i<ndim1; i++) { \
00084 matrix[i] = PRFX ## smalloc(ndim2, value, errmsg);\
00085 if (matrix[i] == NULL) { \
00086 for (j=0; j<i; j++) \
00087 gk_free((void **)&matrix[j], LTERM); \
00088 return NULL; \
00089 } \
00090 }\
00091 \
00092 return matrix;\
00093 }\
00094 \
00095 \
00096 \
00097 \
00098 \
00099 void PRFX ## FreeMatrix(TYPE ***r_matrix, size_t ndim1, size_t ndim2)\
00100 {\
00101 gk_idx_t i;\
00102 TYPE **matrix;\
00103 \
00104 if (*r_matrix == NULL) \
00105 return; \
00106 \
00107 matrix = *r_matrix;\
00108 \
00109 for (i=0; i<ndim1; i++) \
00110 gk_free((void **)&(matrix[i]), LTERM);\
00111 \
00112 gk_free((void **)r_matrix, LTERM);\
00113 }\
00114 \
00115 \
00116 \
00117 \
00118 \
00119 void PRFX ## SetMatrix(TYPE **matrix, size_t ndim1, size_t ndim2, TYPE value)\
00120 {\
00121 gk_idx_t i, j;\
00122 \
00123 for (i=0; i<ndim1; i++) {\
00124 for (j=0; j<ndim2; j++)\
00125 matrix[i][j] = value;\
00126 }\
00127 }\
00128
00129
00130 #define GK_MKALLOC_PROTO(PRFX, TYPE)\
00131 TYPE *PRFX ## malloc(size_t n, char *msg);\
00132 TYPE *PRFX ## realloc(TYPE *ptr, size_t n, char *msg);\
00133 TYPE *PRFX ## smalloc(size_t n, TYPE ival, char *msg);\
00134 TYPE *PRFX ## set(size_t n, TYPE val, TYPE *x);\
00135 TYPE *PRFX ## copy(size_t n, TYPE *a, TYPE *b);\
00136 TYPE **PRFX ## AllocMatrix(size_t ndim1, size_t ndim2, TYPE value, char *errmsg);\
00137 void PRFX ## FreeMatrix(TYPE ***r_matrix, size_t ndim1, size_t ndim2);\
00138 void PRFX ## SetMatrix(TYPE **matrix, size_t ndim1, size_t ndim2, TYPE value);\
00139
00140
00141
00142 #endif