NEURON
meminfo.c
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 /**************************************************************************
4 **
5 ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
6 **
7 ** Meschach Library
8 **
9 ** This Meschach Library is provided "as is" without any express
10 ** or implied warranty of any kind with respect to this software.
11 ** In particular the authors shall not be liable for any direct,
12 ** indirect, special, incidental or consequential damages arising
13 ** in any way from use of the software.
14 **
15 ** Everyone is granted permission to copy, modify and redistribute this
16 ** Meschach Library, provided:
17 ** 1. All copies contain this copyright notice.
18 ** 2. All modified copies shall carry a notice stating who
19 ** made the last modification and the date of such modification.
20 ** 3. No charge is made for this software or works derived from it.
21 ** This clause shall not be construed as constraining other software
22 ** distributed on the same medium as this software, nor is a
23 ** distribution fee considered a charge.
24 **
25 ***************************************************************************/
26 
27 
28 /* meminfo.c revised 22/11/93 */
29 
30 /*
31  contains basic functions, types and arrays
32  to keep track of memory allocation/deallocation
33 */
34 
35 #include <stdio.h>
36 #include "matrix.h"
37 #include "meminfo.h"
38 #ifdef COMPLEX
39 #include "zmatrix.h"
40 #endif
41 #ifdef SPARSE
42 #include "sparse.h"
43 #include "iter.h"
44 #endif
45 
46 static char rcsid[] = "meminfo.c,v 1.1 1997/12/04 17:55:37 hines Exp";
47 
48 /* this array is defined further in this file */
50 
51 
52 /* names of types */
53 static char *mem_type_names[] = {
54  "MAT",
55  "BAND",
56  "PERM",
57  "VEC",
58  "IVEC"
59 #ifdef SPARSE
60  ,"ITER",
61  "SPROW",
62  "SPMAT"
63 #endif
64 #ifdef COMPLEX
65  ,"ZVEC",
66  "ZMAT"
67 #endif
68  };
69 
70 
71 #define MEM_NUM_STD_TYPES (sizeof(mem_type_names)/sizeof(mem_type_names[0]))
72 
73 
74 /* local array for keeping track of memory */
76 
77 
78 /* for freeing various types */
79 static int (*mem_free_funcs[MEM_NUM_STD_TYPES])() = {
80  m_free,
81  bd_free,
82  px_free,
83  v_free,
84  iv_free
85 #ifdef SPARSE
86  ,iter_free,
87  sprow_free,
88  sp_free
89 #endif
90 #ifdef COMPLEX
91  ,zv_free,
92  zm_free
93 #endif
94  };
95 
96 
97 
98 /* it is a global variable for passing
99  pointers to local arrays defined here */
102  mem_info_sum }
103 };
104 
105 
106 /* attach a new list of types */
107 
108 int mem_attach_list(list, ntypes, type_names, free_funcs, info_sum)
109 int list,ntypes; /* number of a list and number of types there */
110 char *type_names[]; /* list of names of types */
111 int (*free_funcs[])(); /* list of releasing functions */
112 MEM_ARRAY info_sum[]; /* local table */
113 {
114  if (list < 0 || list >= MEM_CONNECT_MAX_LISTS)
115  return -1;
116 
117  if (type_names == NULL || free_funcs == NULL
118  || info_sum == NULL || ntypes < 0)
119  return -1;
120 
121  /* if a list exists do not overwrite */
122  if ( mem_connect[list].ntypes != 0 )
123  error(E_OVERWRITE,"mem_attach_list");
124 
125  mem_connect[list].ntypes = ntypes;
126  mem_connect[list].type_names = type_names;
127  mem_connect[list].free_funcs = free_funcs;
128  mem_connect[list].info_sum = info_sum;
129  return 0;
130 }
131 
132 
133 /* release a list of types */
134 int mem_free_vars(list)
135 int list;
136 {
137  if (list < 0 || list >= MEM_CONNECT_MAX_LISTS)
138  return -1;
139 
140  mem_connect[list].ntypes = 0;
141  mem_connect[list].type_names = NULL;
142  mem_connect[list].free_funcs = NULL;
143  mem_connect[list].info_sum = NULL;
144 
145  return 0;
146 }
147 
148 
149 
150 /* check if list is attached */
151 
153 int list;
154 {
155  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
156  return FALSE;
157 
158  if ( mem_connect[list].type_names != NULL &&
159  mem_connect[list].free_funcs != NULL &&
160  mem_connect[list].info_sum != NULL)
161  return TRUE;
162  else return FALSE;
163 }
164 
165 /* to print out the contents of mem_connect[list] */
166 
167 void mem_dump_list(fp,list)
168 FILE *fp;
169 int list;
170 {
171  int i;
172  MEM_CONNECT *mlist;
173 
174  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
175  return;
176 
177  mlist = &mem_connect[list];
178  fprintf(fp," %15s[%d]:\n","CONTENTS OF mem_connect",list);
179  fprintf(fp," %-7s %-12s %-9s %s\n",
180  "name of",
181  "alloc.", "# alloc.",
182  "address"
183  );
184  fprintf(fp," %-7s %-12s %-9s %s\n",
185  " type",
186  "bytes", "variables",
187  "of *_free()"
188  );
189 
190  for (i=0; i < mlist->ntypes; i++)
191  fprintf(fp," %-7s %-12ld %-9d %p\n",
192  mlist->type_names[i], mlist->info_sum[i].bytes,
193  mlist->info_sum[i].numvar, mlist->free_funcs[i]
194  );
195 
196  fprintf(fp,"\n");
197 }
198 
199 
200 
201 /*=============================================================*/
202 
203 
204 /* local variables */
205 
206 static int mem_switched_on = MEM_SWITCH_ON_DEF; /* on/off */
207 
208 
209 /* switch on/off memory info */
210 
211 int mem_info_on(sw)
212 int sw;
213 {
214  int old = mem_switched_on;
215 
216  mem_switched_on = sw;
217  return old;
218 }
219 
220 #ifdef ANSI_C
221 int mem_info_is_on(void)
222 #else
223 int mem_info_is_on()
224 #endif
225 {
226  return mem_switched_on;
227 }
228 
229 
230 /* information about allocated memory */
231 
232 /* return the number of allocated bytes for type 'type' */
233 
235 int type,list;
236 {
237  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
238  return 0l;
239  if ( !mem_switched_on || type < 0
240  || type >= mem_connect[list].ntypes
241  || mem_connect[list].free_funcs[type] == NULL )
242  return 0l;
243 
244  return mem_connect[list].info_sum[type].bytes;
245 }
246 
247 /* return the number of allocated variables for type 'type' */
249 int type,list;
250 {
251  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
252  return 0l;
253  if ( !mem_switched_on || type < 0
254  || type >= mem_connect[list].ntypes
255  || mem_connect[list].free_funcs[type] == NULL )
256  return 0l;
257 
258  return mem_connect[list].info_sum[type].numvar;
259 }
260 
261 
262 
263 /* print out memory info to the file fp */
264 void mem_info_file(fp,list)
265 FILE *fp;
266 int list;
267 {
268  unsigned int type;
269  long t = 0l, d;
270  int n = 0, nt = 0;
271  MEM_CONNECT *mlist;
272 
273  if (!mem_switched_on) return;
274  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
275  return;
276 
277  if (list == 0)
278  fprintf(fp," MEMORY INFORMATION (standard types):\n");
279  else
280  fprintf(fp," MEMORY INFORMATION (list no. %d):\n",list);
281 
282  mlist = &mem_connect[list];
283 
284  for (type=0; type < mlist->ntypes; type++) {
285  if (mlist->type_names[type] == NULL ) continue;
286  d = mlist->info_sum[type].bytes;
287  t += d;
288  n = mlist->info_sum[type].numvar;
289  nt += n;
290  fprintf(fp," type %-7s %10ld alloc. byte%c %6d alloc. variable%c\n",
291  mlist->type_names[type], d, (d!=1 ? 's' : ' '),
292  n, (n!=1 ? 's' : ' '));
293  }
294 
295  fprintf(fp," %-12s %10ld alloc. byte%c %6d alloc. variable%c\n\n",
296  "total:",t, (t!=1 ? 's' : ' '),
297  nt, (nt!=1 ? 's' : ' '));
298 }
299 
300 
301 /* function for memory information */
302 
303 
304 /* mem_bytes_list
305 
306  Arguments:
307  type - the number of type;
308  old_size - old size of allocated memory (in bytes);
309  new_size - new size of allocated memory (in bytes);
310  list - list of types
311  */
312 
313 
314 void mem_bytes_list(type,old_size,new_size,list)
315 int type,list;
316 int old_size,new_size;
317 {
318  MEM_CONNECT *mlist;
319 
320  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
321  return;
322 
323  mlist = &mem_connect[list];
324  if ( type < 0 || type >= mlist->ntypes
325  || mlist->free_funcs[type] == NULL )
326  return;
327 
328  if ( old_size < 0 || new_size < 0 )
329  error(E_NEG,"mem_bytes_list");
330 
331  mlist->info_sum[type].bytes += new_size - old_size;
332 
333  /* check if the number of bytes is non-negative */
334  if ( old_size > 0 ) {
335 
336  if (mlist->info_sum[type].bytes < 0)
337  {
338  fprintf(stderr,
339  "\n WARNING !! memory info: allocated memory is less than 0\n");
340  fprintf(stderr,"\t TYPE %s \n\n", mlist->type_names[type]);
341 
342  if ( !isatty(fileno(stdout)) ) {
343  fprintf(stdout,
344  "\n WARNING !! memory info: allocated memory is less than 0\n");
345  fprintf(stdout,"\t TYPE %s \n\n", mlist->type_names[type]);
346  }
347  }
348  }
349 }
350 
351 
352 /* mem_numvar_list
353 
354  Arguments:
355  type - the number of type;
356  num - # of variables allocated (> 0) or deallocated ( < 0)
357  list - list of types
358  */
359 
360 
361 void mem_numvar_list(type,num,list)
362 int type,list,num;
363 {
364  MEM_CONNECT *mlist;
365 
366  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
367  return;
368 
369  mlist = &mem_connect[list];
370  if ( type < 0 || type >= mlist->ntypes
371  || mlist->free_funcs[type] == NULL )
372  return;
373 
374  mlist->info_sum[type].numvar += num;
375 
376  /* check if the number of variables is non-negative */
377  if ( num < 0 ) {
378 
379  if (mlist->info_sum[type].numvar < 0)
380  {
381  fprintf(stderr,
382  "\n WARNING !! memory info: allocated # of variables is less than 0\n");
383  fprintf(stderr,"\t TYPE %s \n\n", mlist->type_names[type]);
384  if ( !isatty(fileno(stdout)) ) {
385  fprintf(stdout,
386  "\n WARNING !! memory info: allocated # of variables is less than 0\n");
387  fprintf(stdout,"\t TYPE %s \n\n", mlist->type_names[type]);
388  }
389  }
390  }
391 }
392 
int bd_free(BAND *A)
Definition: bdfactor.c:74
short type
Definition: cabvars.h:9
static Frame * fp
Definition: code.cpp:161
double t
Definition: cvodeobj.cpp:59
#define TRUE
Definition: err.c:57
#define FALSE
Definition: err.c:56
#define error(err_num, fn_name)
Definition: err.h:73
#define E_OVERWRITE
Definition: err.h:115
#define E_NEG
Definition: err.h:114
int iter_free(ITER *ip)
Definition: iter0.c:118
int iv_free(IVEC *iv)
Definition: ivecop.c:67
int v_free(VEC *)
int px_free(PERM *)
Definition: memory.c:201
int m_free(MAT *)
#define i
Definition: md1redef.h:12
long mem_info_bytes(int type, int list)
Definition: meminfo.c:234
static MEM_ARRAY mem_info_sum[MEM_NUM_STD_TYPES]
Definition: meminfo.c:75
#define MEM_NUM_STD_TYPES
Definition: meminfo.c:71
int mem_info_numvar(int type, int list)
Definition: meminfo.c:248
static int mem_switched_on
Definition: meminfo.c:206
int mem_is_list_attached(int list)
Definition: meminfo.c:152
void mem_dump_list(FILE *fp, int list)
Definition: meminfo.c:167
void mem_info_file(FILE *fp, int list)
Definition: meminfo.c:264
void mem_numvar_list(int type, int num, int list)
Definition: meminfo.c:361
int mem_attach_list(int list, int ntypes, type_names, int(*[] free_funcs)(), info_sum)
Definition: meminfo.c:108
static int(* mem_free_funcs[MEM_NUM_STD_TYPES])()
Definition: meminfo.c:79
MEM_CONNECT mem_connect[MEM_CONNECT_MAX_LISTS]
Definition: meminfo.c:100
int mem_free_vars(int list)
Definition: meminfo.c:134
void mem_bytes_list(int type, int old_size, int new_size, int list)
Definition: meminfo.c:314
int mem_info_is_on(void)
Definition: meminfo.c:221
int mem_info_on(int sw)
Definition: meminfo.c:211
static char * mem_type_names[]
Definition: meminfo.c:53
static char rcsid[]
Definition: meminfo.c:46
#define MEM_CONNECT_MAX_LISTS
Definition: meminfo.h:152
#define MEM_SWITCH_ON_DEF
Definition: meminfo.h:44
#define fprintf
Definition: mwprefix.h:30
int const size_t const size_t n
Definition: nrngsl.h:11
int sp_free(SPMAT *A)
Definition: sparse.c:260
int sprow_free(SPROW *)
Definition: sprow.c:259
#define NULL
Definition: sptree.h:16
int numvar
Definition: meminfo.h:74
long bytes
Definition: meminfo.h:73
unsigned ntypes
Definition: meminfo.h:147
int(** free_funcs)()
Definition: meminfo.h:146
char ** type_names
Definition: meminfo.h:145
MEM_ARRAY * info_sum
Definition: meminfo.h:148
int zm_free(ZMAT *mat)
Definition: zmemory.c:149
int zv_free(ZVEC *vec)
Definition: zmemory.c:195