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