DSDP
dsdperror.c
Go to the documentation of this file.
1 #include "dsdpsys.h"
6 #include <stdarg.h>
7 #include <sys/types.h>
8 
9 
10 #define DSDP_NULL 0
11 #define DSDP_MAX_PATH_LEN 200
12 
13 static FILE *DSDPLogInfoFile = DSDP_NULL;
14 static FILE *dsdp_history=0;
15 
16 typedef void* DSDPObject;
17 
18 int DSDPFError(void *vobj, const char functionname[], int linen, const char filename[], const char message[], ...)
19 {
20  va_list Argp;
21  int urank;
22  size_t len;
23  char string[8*1024];
24 
25  DSDPFunctionBegin;
26  DSDPLogInfoFile = stdout;
27  /*
28  if (DSDPLogPrintInfo == DSDP_FALSE) DSDPFunctionReturn(0);
29  if ((DSDPLogPrintInfoNull == DSDP_FALSE) && !vobj) DSDPFunctionReturn(0);
30  */
31  urank = 0;
32 
33  va_start(Argp, message);
34  sprintf(string, "[%d] DSDP: %s(): Line %d in file %s ", urank,functionname,linen,filename);
35  len = strlen(string);
36 
37 #if defined(DSDP_HAVE_VPRINTF_CHAR)
38  vsprintf(string+len, message, (char *) Argp);
39 #else
40  vsprintf(string+len, message, Argp);
41 #endif
42  fprintf(DSDPLogInfoFile, "%s", string);
43  fflush(DSDPLogInfoFile);
44  if (dsdp_history) {
45 #if defined(DSDP_HAVE_VPRINTF_CHAR)
46  vfprintf(dsdp_history, message, (char *) Argp);
47 #else
48  vfprintf(dsdp_history, message, Argp);
49 #endif
50  }
51  va_end(Argp);
52  DSDPFunctionReturn(0);
53 }
54 
55 
56 void DSDPError(const char * functionname, int linen, const char filename[]){
57  DSDPErrorPrintf("DSDP Error in function: %s , line %d of file %s \n",functionname, linen,filename);
58  return;
59 }
60 typedef struct{
61  void *mem;
62  char fname[20];
63  size_t size;
64  int freed;
65 } DSDPMemory;
66 
67 #define DSDPMEMMAX 1
68 static DSDPMemory DSDPMemoryTable[DSDPMEMMAX];
69 
70 static long int mmmem=0;
71 #undef __FUNCT__
72 #define __FUNCT__ "DSDPMMalloc"
73 int DSDPMMalloc(const char* fname, size_t size, void** mmem){
74  void*tt=0;
75  DSDPFunctionBegin;
76  if (size>0){
77 #ifdef DSDPMATLAB
78  tt=(void*)mxMalloc(size);
79 #else
80  tt=(void*)malloc(size);
81 #endif
82  if (tt==NULL){
83  *mmem=0;
84  DSDPSETERR3(100,"Memory Error in routine '%s'. Cannot allocate %d bytes, %d MB\n",fname,size,(int)(size/1000000));
85  }
86  memset(tt,0,size);
87  *mmem=tt;
88 
89  if (mmmem<DSDPMEMMAX){
90  DSDPMemoryTable[mmmem].size=size;
91  DSDPMemoryTable[mmmem].freed=0;
92  strncpy(DSDPMemoryTable[mmmem].fname,fname,19);
93  DSDPMemoryTable[mmmem].mem=tt;
94  }
95  mmmem++;
96 
97  } else {
98  *mmem=0;
99  }
100  DSDPFunctionReturn(0);
101 }
102 
103 #undef __FUNCT__
104 #define __FUNCT__ "DSDPFFree"
105 int DSDPFFree(void** mmem){
106  int j,gotit=0;
107  DSDPFunctionBegin;
108  if (mmem && *mmem){
109  for (j=0;j<DSDPMEMMAX;j++){
110  if (*mmem==DSDPMemoryTable[j].mem){
111  DSDPMemoryTable[j].freed=1;
112  gotit=1;
113  }
114  }
115  mmmem--;
116 #ifdef DSDPMATLAB
117  mxFree(*mmem);
118 #else
119  free(*mmem);
120 #endif
121  *mmem=0;
122  if (0==1 && gotit==0){
123  printf(" DSDP MEMORY Error: Already Freed? \n");
124  }
125  }
126  DSDPFunctionReturn(0);
127 }
128 
129 void DSDPMemoryLog(){
130  int j;
131  DSDPFunctionBegin;
132  for (j=0;j<DSDPMEMMAX;j++){
133  if (DSDPMemoryTable[j].size>0 && DSDPMemoryTable[j].freed==0){
134  printf("%d, MEMORY Not FREED: %s, %d \n",j,DSDPMemoryTable[j].fname,(int)DSDPMemoryTable[j].size);
135  }
136  }
137 
138  DSDPLogInfo(0,2," MEMORY MALLOC NOT FREED: %ld\n",mmmem);
139 
140  return;
141 }
Error handling, printing, and profiling.