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