1 2 #if !defined(__DSDP_MEM_H) 3 #define __DSDP_MEM_H 4 /*! \file dsdpmem.h 5 \brief Memory allocation in DSDP 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <math.h> 10 #include <string.h> 11 #include <float.h> 12 13 extern int DSDPMMalloc(const char*, size_t, void**); 14 extern int DSDPFFree(void**); 15 /* Define some macros for memory management */ 16 17 /* 18 #define DSDPFree(a) 0;printf("FREE: %s\n",__FUNCT__); free((*a)) 19 #define DSDPMalloc(a,b) 0;printf("Malloc: %s\n",__FUNCT__); if (b){*(b)=malloc((a)); } 20 */ 21 22 #ifdef DSDPMATLAB 23 24 #define DSDPCALLOC1(VAR,TYPE,MERR) { \ 25 *(VAR) = (TYPE*)mxMalloc(sizeof(TYPE)); \ 26 *MERR=0; \ 27 if ( *(VAR)==0){*(MERR)=1;} \ 28 else {memset(*(VAR),0,sizeof(TYPE));} } 29 30 #define DSDPCALLOC2(VAR,TYPE,SIZE,MERR) { \ 31 *MERR=0; \ 32 *VAR=0; \ 33 if (SIZE>0){ \ 34 *(VAR) = (TYPE*)mxMalloc((SIZE)*sizeof(TYPE)); \ 35 if (*(VAR)==0){ *(MERR)=1;} \ 36 else {memset(*(VAR),0,(SIZE)*sizeof(TYPE));} \ 37 } \ 38 } 39 40 #define DSDPFREE(VAR,MERR) {if (*(VAR)){mxFree(*(VAR));}*(VAR)=0;*(MERR)=0;} 41 #endif 42 43 /* 44 #ifndef DSDPCALLOC1 45 #define DSDPCALLOC1(VAR,TYPE,MERR) { (*MERR)=DSDPMMalloc(__FUNCT__,sizeof(TYPE),(void**)(VAR));} 46 #endif 47 48 #ifndef DSDPCALLOC2 49 #define DSDPCALLOC2(VAR,TYPE,SIZE,MERR) { (*MERR)=DSDPMMalloc(__FUNCT__,(SIZE)*sizeof(TYPE),(void**)(VAR));} 50 #endif 51 52 #ifndef DSDPFREE 53 #define DSDPFREE(a,b) {*(b)=DSDPFFree((void**)(a));} 54 #endif 55 */ 56 57 #ifndef DSDPCALLOC1 58 #define DSDPCALLOC1(VAR,TYPE,MERR) { \ 59 *(VAR) = (TYPE*)calloc(1, sizeof(TYPE)); \ 60 *MERR=0; \ 61 if ( *(VAR)==0){*(MERR)=1;} \ 62 else { memset(*(VAR),0,sizeof(TYPE)); } \ 63 } 64 #endif 65 66 #ifndef DSDPCALLOC2 67 #define DSDPCALLOC2(VAR,TYPE,SIZE,MERR) { \ 68 *MERR=0; \ 69 *VAR=0; \ 70 if (SIZE>0){ \ 71 *(VAR) = (TYPE*)calloc(SIZE, sizeof(TYPE)); \ 72 if (*(VAR)==0){ *(MERR)=1;} \ 73 else { memset(*(VAR),0,(SIZE)*sizeof(TYPE)); } \ 74 } \ 75 } 76 #endif 77 78 #ifndef DSDPFREE 79 #define DSDPFREE(VAR,MERR) {if (*(VAR)){free(*(VAR));}*(VAR)=0;*(MERR)=0;} 80 #endif 81 82 /* 83 #ifndef DSDPCALLOC1 84 #define DSDPCALLOC1(VAR,TYPE,SIZE,MERR) {*(VAR) = new TYPE;*MERR=0;} 85 #endif 86 #ifndef DSDPCALLOC2 87 #define DSDPCALLOC2(VAR,TYPE,SIZE,MERR) {*(VAR) = new TYPE[SIZE];*MERR=0;} 88 #endif 89 #ifndef DSDPFREE 90 #define DSDPFREE(a,b) {delete(*(a));*(a)=0;*(b)=0;} 91 #endif 92 */ 93 94 95 #endif 96