NEURON
memstat.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 /* mem_stat.c 6/09/93 */
29 
30 /* Deallocation of static arrays */
31 
32 
33 #include <stdio.h>
34 #include "matrix.h"
35 #include "meminfo.h"
36 #ifdef COMPLEX
37 #include "zmatrix.h"
38 #endif
39 #ifdef SPARSE
40 #include "sparse.h"
41 #include "iter.h"
42 #endif
43 
44 static char rcsid[] = "memstat.c,v 1.1 1997/12/04 17:55:39 hines Exp";
45 
46 /* global variable */
47 
49 
50 
51 /* local type */
52 
53 typedef struct {
54  void **var; /* for &A, where A is a pointer */
55  int type; /* type of A */
56  int mark; /* what mark is chosen */
58 
59 
60 /* local variables */
61 
62 /* how many marks are used */
63 static int mem_stat_mark_many = 0;
64 
65 /* current mark */
66 static int mem_stat_mark_curr = 0;
67 
68 
70 
71 /* array of indices (+1) to mem_stat_var */
72 static unsigned int mem_hash_idx[MEM_HASHSIZE];
73 
74 /* points to the first unused element in mem_hash_idx */
75 static unsigned int mem_hash_idx_end = 0;
76 
77 
78 
79 /* hashing function */
80 
81 static unsigned int mem_hash(ptr)
82 void **ptr;
83 {
84  unsigned long lp = (size_t)ptr;
85 
86  return (lp % MEM_HASHSIZE);
87 }
88 
89 
90 /* look for a place in mem_stat_var */
91 static int mem_lookup(var)
92 void **var;
93 {
94  int k, j;
95 
96  k = mem_hash(var);
97 
98  if (mem_stat_var[k].var == var) {
99  return -1;
100  }
101  else if (mem_stat_var[k].var == NULL) {
102  return k;
103  }
104  else { /* look for an empty place */
105  j = k;
106  while (mem_stat_var[j].var != var && j < MEM_HASHSIZE
107  && mem_stat_var[j].var != NULL)
108  j++;
109 
110  if (mem_stat_var[j].var == NULL) return j;
111  else if (mem_stat_var[j].var == var) return -1;
112  else { /* if (j == MEM_HASHSIZE) */
113  j = 0;
114  while (mem_stat_var[j].var != var && j < k
115  && mem_stat_var[j].var != NULL)
116  j++;
117  if (mem_stat_var[j].var == NULL) return j;
118  else if (mem_stat_var[j].var == var) return -1;
119  else { /* if (j == k) */
120  fprintf(stderr,
121  "\n WARNING !!! static memory: mem_stat_var is too small\n");
122  fprintf(stderr,
123  " Increase MEM_HASHSIZE in file: %s (currently = %d)\n\n",
125  if ( !isatty(fileno(stdout)) ) {
126  fprintf(stdout,
127  "\n WARNING !!! static memory: mem_stat_var is too small\n");
128  fprintf(stdout,
129  " Increase MEM_HASHSIZE in file: %s (currently = %d)\n\n",
131  }
132  error(E_MEM,"mem_lookup");
133  }
134  }
135  }
136 
137  return -1;
138 }
139 
140 
141 /* register static variables;
142  Input arguments:
143  var - variable to be registered,
144  type - type of this variable;
145  list - list of types
146 
147  returned value < 0 --> error,
148  returned value == 0 --> not registered,
149  returned value >= 0 --> registered with this mark;
150 */
151 
153 void **var;
154 int type,list;
155 {
156  int n;
157 
158  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS )
159  return -1;
160 
161  if (mem_stat_mark_curr == 0) return 0; /* not registered */
162  if (var == NULL) return -1; /* error */
163 
164  if ( type < 0 || type >= mem_connect[list].ntypes ||
165  mem_connect[list].free_funcs[type] == NULL )
166  {
167  warning(WARN_WRONG_TYPE,"mem_stat_reg_list");
168  return -1;
169  }
170 
171  if ((n = mem_lookup(var)) >= 0) {
172  mem_stat_var[n].var = var;
173  mem_stat_var[n].mark = mem_stat_mark_curr;
174  mem_stat_var[n].type = type;
175  /* save n+1, not n */
177  }
178 
179  return mem_stat_mark_curr;
180 }
181 
182 
183 /* set a mark;
184  Input argument:
185  mark - positive number denoting a mark;
186  returned:
187  mark if mark > 0,
188  0 if mark == 0,
189  -1 if mark is negative.
190 */
191 
192 int mem_stat_mark(mark)
193 int mark;
194 {
195  if (mark < 0) {
196  mem_stat_mark_curr = 0;
197  return -1; /* error */
198  }
199  else if (mark == 0) {
200  mem_stat_mark_curr = 0;
201  return 0;
202  }
203 
204  mem_stat_mark_curr = mark;
206 
207  return mark;
208 }
209 
210 
211 
212 /* deallocate static variables;
213  Input argument:
214  mark - a positive number denoting the mark;
215 
216  Returned:
217  -1 if mark < 0 (error);
218  0 if mark == 0;
219 */
220 
221 int mem_stat_free_list(mark,list)
222 int mark,list;
223 {
224  u_int i,j;
225  int (*free_fn)();
226 
227  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS
228  || mem_connect[list].free_funcs == NULL )
229  return -1;
230 
231  if (mark < 0) {
232  mem_stat_mark_curr = 0;
233  return -1;
234  }
235  else if (mark == 0) {
236  mem_stat_mark_curr = 0;
237  return 0;
238  }
239 
240  if (mem_stat_mark_many <= 0) {
241  warning(WARN_NO_MARK,"mem_stat_free");
242  return -1;
243  }
244 
245  /* deallocate the marked variables */
246  for (i=0; i < mem_hash_idx_end; i++) {
247  j = mem_hash_idx[i];
248  if (j == 0) continue;
249  else {
250  j--;
251  if (mem_stat_var[j].mark == mark) {
252  free_fn = mem_connect[list].free_funcs[mem_stat_var[j].type];
253  if ( free_fn != NULL )
254  (*free_fn)(*mem_stat_var[j].var);
255  else
256  warning(WARN_WRONG_TYPE,"mem_stat_free");
257 
258  *(mem_stat_var[j].var) = NULL;
259  mem_stat_var[j].var = NULL;
260  mem_stat_var[j].mark = 0;
261  mem_hash_idx[i] = 0;
262  }
263  }
264  }
265 
266  while (mem_hash_idx_end > 0 && mem_hash_idx[mem_hash_idx_end-1] == 0)
267  mem_hash_idx_end--;
268 
269  mem_stat_mark_curr = 0;
271  return 0;
272 }
273 
274 
275 /* only for diagnostic purposes */
276 
277 void mem_stat_dump(fp,list)
278 FILE *fp;
279 int list;
280 {
281  u_int i,j,k=1;
282 
283  if ( list < 0 || list >= MEM_CONNECT_MAX_LISTS
284  || mem_connect[list].free_funcs == NULL )
285  return;
286 
287  fprintf(fp," Array mem_stat_var (list no. %d):\n",list);
288  for (i=0; i < mem_hash_idx_end; i++) {
289  j = mem_hash_idx[i];
290  if (j == 0) continue;
291  else {
292  j--;
293  fprintf(fp," %d. var = 0x%p, type = %s, mark = %d\n",
294  k,mem_stat_var[j].var,
295  mem_stat_var[j].type < mem_connect[list].ntypes &&
296  mem_connect[list].free_funcs[mem_stat_var[j].type] != NULL ?
297  mem_connect[list].type_names[(int)mem_stat_var[j].type] :
298  "???",
299  mem_stat_var[j].mark);
300  k++;
301  }
302  }
303 
304  fprintf(fp,"\n");
305 }
306 
307 
308 /* query function about the current mark */
309 #ifdef ANSI_C
311 #else
312 int mem_stat_show_mark()
313 #endif
314 {
315  return mem_stat_mark_curr;
316 }
317 
318 
319 /* Varying number of arguments */
320 
321 
322 #ifdef ANSI_C
323 
324 /* To allocate memory to many arguments.
325  The function should be called:
326  mem_stat_vars(list,type,&v1,&v2,&v3,...,VNULL);
327  where
328  int list,type;
329  void **v1, **v2, **v3,...;
330  The last argument should be VNULL !
331  type is the type of variables v1,v2,v3,...
332  (of course they must be of the same type)
333 */
334 
335 int mem_stat_reg_vars(int list,int type,...)
336 {
337  va_list ap;
338  int i=0;
339  void **par;
340 
341  va_start(ap, type);
342  while ((par = va_arg(ap,void **))) { /* NULL ends the list*/
343  mem_stat_reg_list(par,type,list);
344  i++;
345  }
346 
347  va_end(ap);
348  return i;
349 }
350 
351 #elif VARARGS
352 /* old varargs is used */
353 
354 /* To allocate memory to many arguments.
355  The function should be called:
356  mem_stat_vars(list,type,&v1,&v2,&v3,...,VNULL);
357  where
358  int list,type;
359  void **v1, **v2, **v3,...;
360  The last argument should be VNULL !
361  type is the type of variables v1,v2,v3,...
362  (of course they must be of the same type)
363 */
364 
365 int mem_stat_reg_vars(va_alist) va_dcl
366 {
367  va_list ap;
368  int type,list,i=0;
369  void **par;
370 
371  va_start(ap);
372  list = va_arg(ap,int);
373  type = va_arg(ap,int);
374  while ((par = va_arg(ap,void **))) { /* NULL ends the list*/
375  mem_stat_reg_list(par,type,list);
376  i++;
377  }
378 
379  va_end(ap);
380  return i;
381 }
382 
383 
384 #endif
short type
Definition: cabvars.h:10
static int mem_lookup(void **var)
Definition: memstat.c:91
#define MEM_HASHSIZE
Definition: meminfo.h:38
#define MEM_CONNECT_MAX_LISTS
Definition: meminfo.h:152
static int mem_stat_mark_many
Definition: memstat.c:63
int mem_stat_mark(int mark)
Definition: memstat.c:192
MEM_CONNECT mem_connect[MEM_CONNECT_MAX_LISTS]
static philox4x32_key_t k
Definition: nrnran123.cpp:11
static MEM_STAT_STRUCT mem_stat_var[MEM_HASHSIZE]
Definition: memstat.c:69
void mem_stat_dump(FILE *fp, int list)
Definition: memstat.c:277
static Frame * fp
Definition: code.cpp:154
uint32_t u_int
Definition: machine.h:38
int const size_t const size_t n
Definition: nrngsl.h:12
static unsigned int mem_hash_idx_end
Definition: memstat.c:75
static char rcsid[]
Definition: memstat.c:44
int
Definition: nrnmusic.cpp:71
size_t j
#define WARN_WRONG_TYPE
Definition: err.h:120
fprintf(stderr, "Don't know the location of params at %p\, pp)
int mem_stat_reg_list(void **var, int type, int list)
Definition: memstat.c:152
int mem_stat_show_mark(void)
Definition: memstat.c:310
int(** free_funcs)()
Definition: meminfo.h:146
void ** var
Definition: memstat.c:54
#define WARN_NO_MARK
Definition: err.h:121
void warning(const char *s, const char *t)
Definition: hoc.cpp:1542
static unsigned int mem_hash(void **ptr)
Definition: memstat.c:81
#define i
Definition: md1redef.h:12
#define MEM_HASHSIZE_FILE
Definition: meminfo.h:39
static unsigned int mem_hash_idx[MEM_HASHSIZE]
Definition: memstat.c:72
static int mem_stat_mark_curr
Definition: memstat.c:66
#define error(err_num, fn_name)
Definition: err.h:73
int mem_stat_reg_vars(int list, int type,...)
Definition: memstat.c:335
double var(InputIterator begin, InputIterator end)
Definition: ivocvect.h:93
return NULL
Definition: cabcode.cpp:461
int mem_stat_free_list(int mark, int list)
Definition: memstat.c:221
#define E_MEM
Definition: err.h:97