1 /*! 2 \file gk_mkmemory.h 3 \brief Templates for memory allocation routines 4 5 \date Started 3/29/07 6 \author George 7 \version\verbatim $Id: gk_mkmemory.h 10711 2011-08-31 22:23:04Z karypis $ \endverbatim 8 */ 9 10 #ifndef _GK_MKMEMORY_H_ 11 #define _GK_MKMEMORY_H_ 12 13 14 #define GK_MKALLOC(PRFX, TYPE)\ 15 /*************************************************************************/\ 16 /*! The macro for gk_?malloc()-class of routines */\ 17 /**************************************************************************/\ 18 TYPE *PRFX ## malloc(size_t n, char *msg)\ 19 {\ 20 return (TYPE *)gk_malloc(sizeof(TYPE)*n, msg);\ 21 }\ 22 \ 23 \ 24 /*************************************************************************/\ 25 /*! The macro for gk_?realloc()-class of routines */\ 26 /**************************************************************************/\ 27 TYPE *PRFX ## realloc(TYPE *ptr, size_t n, char *msg)\ 28 {\ 29 return (TYPE *)gk_realloc((void *)ptr, sizeof(TYPE)*n, msg);\ 30 }\ 31 \ 32 \ 33 /*************************************************************************/\ 34 /*! The macro for gk_?smalloc()-class of routines */\ 35 /**************************************************************************/\ 36 TYPE *PRFX ## smalloc(size_t n, TYPE ival, char *msg)\ 37 {\ 38 TYPE *ptr;\ 39 \ 40 ptr = (TYPE *)gk_malloc(sizeof(TYPE)*n, msg);\ 41 if (ptr == NULL) \ 42 return NULL; \ 43 \ 44 return PRFX ## set(n, ival, ptr); \ 45 }\ 46 \ 47 \ 48 /*************************************************************************/\ 49 /*! The macro for gk_?set()-class of routines */\ 50 /*************************************************************************/\ 51 TYPE *PRFX ## set(size_t n, TYPE val, TYPE *x)\ 52 {\ 53 size_t i;\ 54 \ 55 for (i=0; i<n; i++)\ 56 x[i] = val;\ 57 \ 58 return x;\ 59 }\ 60 \ 61 \ 62 /*************************************************************************/\ 63 /*! The macro for gk_?set()-class of routines */\ 64 /*************************************************************************/\ 65 TYPE *PRFX ## copy(size_t n, TYPE *a, TYPE *b)\ 66 {\ 67 return (TYPE *)memmove((void *)b, (void *)a, sizeof(TYPE)*n);\ 68 }\ 69 \ 70 \ 71 /*************************************************************************/\ 72 /*! The macro for gk_?AllocMatrix()-class of routines */\ 73 /**************************************************************************/\ 74 TYPE **PRFX ## AllocMatrix(size_t ndim1, size_t ndim2, TYPE value, char *errmsg)\ 75 {\ 76 gk_idx_t i, j;\ 77 TYPE **matrix;\ 78 \ 79 matrix = (TYPE **)gk_malloc(ndim1*sizeof(TYPE *), errmsg);\ 80 if (matrix == NULL) \ 81 return NULL;\ 82 \ 83 for (i=0; i<ndim1; i++) { \ 84 matrix[i] = PRFX ## smalloc(ndim2, value, errmsg);\ 85 if (matrix[i] == NULL) { \ 86 for (j=0; j<i; j++) \ 87 gk_free((void **)&matrix[j], LTERM); \ 88 return NULL; \ 89 } \ 90 }\ 91 \ 92 return matrix;\ 93 }\ 94 \ 95 \ 96 /*************************************************************************/\ 97 /*! The macro for gk_?AllocMatrix()-class of routines */\ 98 /**************************************************************************/\ 99 void PRFX ## FreeMatrix(TYPE ***r_matrix, size_t ndim1, size_t ndim2)\ 100 {\ 101 gk_idx_t i;\ 102 TYPE **matrix;\ 103 \ 104 if (*r_matrix == NULL) \ 105 return; \ 106 \ 107 matrix = *r_matrix;\ 108 \ 109 for (i=0; i<ndim1; i++) \ 110 gk_free((void **)&(matrix[i]), LTERM);\ 111 \ 112 gk_free((void **)r_matrix, LTERM);\ 113 }\ 114 \ 115 \ 116 /*************************************************************************/\ 117 /*! The macro for gk_?SetMatrix()-class of routines */\ 118 /**************************************************************************/\ 119 void PRFX ## SetMatrix(TYPE **matrix, size_t ndim1, size_t ndim2, TYPE value)\ 120 {\ 121 gk_idx_t i, j;\ 122 \ 123 for (i=0; i<ndim1; i++) {\ 124 for (j=0; j<ndim2; j++)\ 125 matrix[i][j] = value;\ 126 }\ 127 }\ 128 129 130 #define GK_MKALLOC_PROTO(PRFX, TYPE)\ 131 TYPE *PRFX ## malloc(size_t n, char *msg);\ 132 TYPE *PRFX ## realloc(TYPE *ptr, size_t n, char *msg);\ 133 TYPE *PRFX ## smalloc(size_t n, TYPE ival, char *msg);\ 134 TYPE *PRFX ## set(size_t n, TYPE val, TYPE *x);\ 135 TYPE *PRFX ## copy(size_t n, TYPE *a, TYPE *b);\ 136 TYPE **PRFX ## AllocMatrix(size_t ndim1, size_t ndim2, TYPE value, char *errmsg);\ 137 void PRFX ## FreeMatrix(TYPE ***r_matrix, size_t ndim1, size_t ndim2);\ 138 void PRFX ## SetMatrix(TYPE **matrix, size_t ndim1, size_t ndim2, TYPE value);\ 139 140 141 142 #endif 143