1 #ifdef _cplusplus
2 extern "C" {
3 #endif
4 #include "wisememman.h"
5 
6 
7 #ifdef WISE_MEMORY_WATCH
8  struct wise_memory_watcher {
9    void * position;
10    int amount;
11    int has_freed;
12    char * file;
13    int lineno;
14  };
15 
16  static struct wise_memory_watcher mempool[WISE_MEMORY_WATCH_MAX];
17  static int nextpool = 0;
18 
19 /* Function:  display_allocated_memory(tag,ofp)
20  *
21  * Descrip:    Displays to filehandle all non freed memory
22  *
23  *
24  * Arg:        tag [UNKN ] Undocumented argument [char *]
25  * Arg:        ofp [UNKN ] Undocumented argument [FILE *]
26  *
27  */
28 # line 31 "wisememman.dy"
display_allocated_memory(char * tag,FILE * ofp)29 void display_allocated_memory(char * tag,FILE * ofp)
30 {
31   int i;
32   for(i=0;i < nextpool && i<WISE_MEMORY_WATCH_MAX;i++)
33     if( mempool[i].has_freed == FALSE ) {
34       if( mempool[i].file != NULL )
35 	fprintf(ofp,"%s [%s:%d] %d of %d bytes not free\n",tag,mempool[i].file,mempool[i].lineno,mempool[i].position,mempool[i].amount);
36       else
37 	fprintf(ofp,"%s %d of %d bytes not free\n",tag,mempool[i].position,mempool[i].amount);
38       fprintf(ofp,"Bytes: %c%c\n",*((char *)mempool[i].position),*((char *)mempool[i].position +1));
39     }
40 
41 }
42 
43 
44 /* Function:  allocate_watched_memory_file(file,lineno,no_bytes)
45  *
46  * Descrip:    Only if compiled with WISE_MEMORY_WATCH
47  *
48  *             Allocates memory and watches the usage of it,
49  *             knowing the file and line number
50  *
51  *
52  * Arg:            file [UNKN ] Undocumented argument [char *]
53  * Arg:          lineno [UNKN ] Undocumented argument [int]
54  * Arg:        no_bytes [UNKN ] Undocumented argument [int]
55  *
56  * Return [UNKN ]  Undocumented return value [void *]
57  *
58  */
59 # line 52 "wisememman.dy"
allocate_watched_memory_file(char * file,int lineno,int no_bytes)60 void * allocate_watched_memory_file(char * file,int lineno,int no_bytes)
61 {
62   if( nextpool >= WISE_MEMORY_WATCH_MAX ) {
63     warn("Memory allocation over limits for watching");
64     return malloc(no_bytes);
65   }
66 
67   mempool[nextpool].position = malloc(no_bytes);
68   mempool[nextpool].amount = no_bytes;
69   mempool[nextpool].has_freed = FALSE;
70   mempool[nextpool].file = file;
71   mempool[nextpool].lineno = lineno;
72 
73 
74   nextpool++;
75 
76   return mempool[nextpool-1].position;
77 }
78 
79 
80 /* Function:  allocate_watched_memory(no_bytes)
81  *
82  * Descrip:    Only if compiled with WISE_MEMORY_WATCH
83  *
84  *             Allocates memory and watches the usage of it
85  *
86  *
87  * Arg:        no_bytes [UNKN ] Undocumented argument [int]
88  *
89  * Return [UNKN ]  Undocumented return value [void *]
90  *
91  */
92 # line 77 "wisememman.dy"
allocate_watched_memory(int no_bytes)93 void * allocate_watched_memory(int no_bytes)
94 {
95   if( nextpool >= WISE_MEMORY_WATCH_MAX ) {
96     warn("Memory allocation over limits for watching");
97     return malloc(no_bytes);
98   }
99 
100   mempool[nextpool].position = malloc(no_bytes);
101   mempool[nextpool].amount = no_bytes;
102   mempool[nextpool].has_freed = FALSE;
103   mempool[nextpool].file = NULL;
104   mempool[nextpool].lineno = -1;
105 
106   nextpool++;
107 
108   return mempool[nextpool-1].position;
109 }
110 
111 /* Function:  free_watched_memory(mem)
112  *
113  * Descrip:    Only if compiled with WISE_MEMORY_WATCH
114  *
115  *             frees memory that has been watched
116  *
117  *
118  * Arg:        mem [UNKN ] Undocumented argument [void *]
119  *
120  * Return [UNKN ]  Undocumented return value [void *]
121  *
122  */
123 # line 100 "wisememman.dy"
free_watched_memory(void * mem)124 void * free_watched_memory(void * mem)
125 {
126   int i;
127   for(i=0;i < nextpool && i<WISE_MEMORY_WATCH_MAX;i++)
128     if( mempool[i].position == mem ) {
129       break;
130     }
131 
132   if( i == nextpool || i == WISE_MEMORY_WATCH_MAX ) {
133     warn("Problem! memory position %d not watched",(int)mem);
134     return NULL;
135   }
136 
137 
138   free(mempool[i].position);
139   mempool[i].has_freed = TRUE;
140 
141   return NULL;
142 }
143 
144 #endif /* WISE_MEMORY_WATCH */
145 
146 /* Function:  ckalloc(bytes)
147  *
148  * Descrip:    Tries to alloc bytes of memory. Posts
149  *             to warn if it fails
150  *
151  *
152  * Arg:        bytes [UNKN ] Undocumented argument [size_t]
153  *
154  * Return [UNKN ]  Undocumented return value [void *]
155  *
156  */
157 # line 126 "wisememman.dy"
ckalloc(size_t bytes)158 void * ckalloc(size_t bytes)
159 {
160   register void *ret;
161   extern void *calloc (size_t nelem, size_t elsize);
162 
163 #ifdef WISE_MEMORY_WATCH
164   /* call into the watched memory pool */
165   ret = allocate_watched_memory(bytes);
166   if( ret == NULL ) {
167     warn("Out of memory (watched) on %d bytes\n",bytes);
168     return NULL;
169   } else
170     return ret;
171 
172 #endif
173 
174   if( (ret = calloc(bytes, sizeof(char))) == NULL) {
175     warn("Out of memory, on asking for %d bytes\n",bytes);
176     return NULL; /*** for the moment, could fail here ***/
177   } else
178     return ret;
179 }
180 
181 /* Function:  ckcalloc(len,bytes)
182  *
183  * Descrip:    calloc equivalent
184  *
185  *
186  * Arg:          len [UNKN ] Undocumented argument [int]
187  * Arg:        bytes [UNKN ] Undocumented argument [size_t]
188  *
189  * Return [UNKN ]  Undocumented return value [void *]
190  *
191  */
192 # line 152 "wisememman.dy"
ckcalloc(int len,size_t bytes)193 void * ckcalloc(int len,size_t bytes)
194 {
195   return ckalloc(len*bytes);
196 }
197 
198 /* Function:  ckrealloc(*ptr,bytes)
199  *
200  * Descrip:    realloc equivalent
201  *
202  *
203  * Arg:         *ptr [UNKN ] Undocumented argument [void]
204  * Arg:        bytes [UNKN ] Undocumented argument [size_t]
205  *
206  * Return [UNKN ]  Undocumented return value [void *]
207  *
208  */
209 # line 160 "wisememman.dy"
ckrealloc(void * ptr,size_t bytes)210 void * ckrealloc(void *ptr, size_t bytes)
211 {
212   register void *ret;
213   extern void *realloc (void *ptr, size_t size);
214 
215   if (ptr == NULL) {
216     warn("Bad call to ckrealloc, NULL pointer\n");
217     return NULL;
218   }
219   else if( (ret = realloc(ptr, bytes)) == NULL) {
220     warn("Out of memory, trying to realloc %d bytes\n",bytes);
221     return NULL;
222   }
223   else
224     return ret;
225 }
226 
227 /* Function:  ckfree(*ptr)
228  *
229  * Descrip:    free equivalent
230  *
231  *
232  * Arg:        *ptr [UNKN ] Undocumented argument [void]
233  *
234  * Return [UNKN ]  Undocumented return value [void *]
235  *
236  */
237 # line 180 "wisememman.dy"
ckfree(void * ptr)238 void * ckfree(void *ptr)
239 {
240 
241 #ifdef WISE_MEMORY_WATCH
242   free_watched_memory(ptr);
243   return NULL;
244 #endif
245 
246   if (ptr == NULL)
247     warn("Bad call to ckfree - NULL pointer\n");
248   else {
249     free(ptr);
250     ptr = NULL;
251   }
252   return ptr;
253 }
254 
255 
256 
257 
258 
259 # line 242 "wisememman.c"
260 
261 #ifdef _cplusplus
262 }
263 #endif
264