-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxpower-matrix.c
17578 lines (15176 loc) · 504 KB
/
xpower-matrix.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright (C) 1993 David E. Stewart & 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.
**
** Modified by [email protected] on 01/11/2011
**
*/
#include <math.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include "xpower-matrix.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#pragma warning(disable:4996 4244 4018)
#endif
const int _x_version = 0x01020b00;
const char *_xversion(){
return ("Based on Meshach matrix library version 1.2b:\n"
"Meshach matrix library version 1.2b\n"
"Changes since 1.2a:\n"
"\t Fixed bug in _xmat_schur() for 2x2 blocks with real e-vals\n"
"\t Fixed bug in _xmat_schur() reading beyond end of array\n"
"\t Fixed some installation bugs\n"
"\t Fixed bugs & improved efficiency in _xspmat_ilufactor()\n"
"\t _xperm_inv() doesn't crash inverting non-permutations\n"
"\t Fixed bug in _xvec_ifft()\n");
}
static char *_xerr_mesg[] = {
"unknown error", /* 0 */
"sizes of objects don't match", /* 1 */
"index out of bounds", /* 2 */
"can't allocate memory", /* 3 */
"singular matrix", /* 4 */
"matrix not positive definite", /* 5 */
"incorrect format input", /* 6 */
"bad input file/device", /* 7 */
"null objects passed", /* 8 */
"matrix not square", /* 9 */
"object out of range", /* 10 */
"can't do operation in situ for non-square matrix", /* 11 */
"can't do operation in situ", /* 12 */
"excessive number of iterations", /* 13 */
"convergence criterion failed", /* 14 */
"bad starting value", /* 15 */
"floating exception", /* 16 */
"internal inconsistency (data structure)", /* 17 */
"unexpected end-of-file", /* 18 */
"shared vectors (cannot release them)", /* 19 */
"negative argument", /* 20 */
"cannot overwrite object", /* 21 */
"breakdown in iterative method" /* 22 */
};
#define _XMAXERR (sizeof(_xerr_mesg)/sizeof(char *))
static char *_xwarn_mesg[] = {
"unknown warning", /* 0 */
"wrong type number (use macro _XTYPE_*)", /* 1 */
"no corresponding mem_stat_mark", /* 2 */
"computed norm of a residual is less than 0", /* 3 */
"resizing a shared vector" /* 4 */
};
#define _XMAXWARN (sizeof(_xwarn_mesg)/sizeof(char *))
#define _XMAX_ERRS 100
#define _XE_SIGNAL 16
/* other local variables */
static int _xerr_flag = _XEF_EXIT, _xnum_errs = 0, _xcnt_errs = 1;
/* array of pointers to lists of errors */
typedef struct __xerr_list {
char **listp; /* pointer to a list of errors */
unsigned len; /* length of the list */
unsigned warn; /* =_XFALSE - errors, =_XTRUE - warnings */
} _xerr_list;
static _xerr_list _xerr_lists[_XERR_LIST_MAX_LEN] = {
{ _xerr_mesg, _XMAXERR, _XFALSE}, /* basic errors list */
{ _xwarn_mesg,_XMAXWARN,_XTRUE} /* basic warnings list */
};
/* number of elements in err_list */
static int _xerr_lists_end = 2;
jmp_buf _xrestart;
int _xev_err(const char *file, int err_num, int line_num, const char *fn_name, int list_num){
int num;
if (err_num < 0) err_num = 0;
if (list_num < 0 || list_num >= _xerr_lists_end || _xerr_lists[list_num].listp == (char **)NULL){
(void) fprintf(stderr,"\nNot (properly) attached list of errors: list_num = %d\n",list_num);
(void) fprintf(stderr," Call \"_xerr_list_attach\" in your program\n");
if (! isatty(_fileno(stdout))){
(void) fprintf(stderr,"\nNot (properly) attached list of errors: list_num = %d\n",list_num);
(void) fprintf(stderr,"Call \"_xerr_list_attach\" in your program\n");
}
(void) fprintf(stdout, "\nExiting program\n");
exit(0);
}
num = err_num;
if ((unsigned int)num >= _xerr_lists[list_num].len) num = 0;
if (_xcnt_errs && ++_xnum_errs >= _XMAX_ERRS){/* too many errors */
(void) fprintf(stderr,"\n\"%s\", line %d: %s in function %s()\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
if (!isatty(_fileno(stdout)))
(void) fprintf(stdout,"\n\"%s\", line %d: %s in function %s()\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
(void) fprintf(stdout, "Sorry, too many errors: %d\n",_xnum_errs);
(void) fprintf(stdout, "Exiting program\n");
exit(0);
}
if (_xerr_lists[list_num].warn)
switch (_xerr_flag){
case _XEF_SILENT: break;
default:
(void) fprintf(stderr,"\n\"%s\", line %d: %s in function %s()\n\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
if (!isatty(_fileno(stdout)))
(void) fprintf(stdout,"\n\"%s\", line %d: %s in function %s()\n\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
break;
}
else
switch (_xerr_flag){
case _XEF_SILENT:
longjmp(_xrestart,(err_num==0)? -1 : err_num);
break;
case _XEF_ABORT:
(void) fprintf(stderr,"\n\"%s\", line %d: %s in function %s()\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
if (!isatty(_fileno(stdout)))
(void) fprintf(stdout,"\n\"%s\", line %d: %s in function %s()\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
abort();
break;
case _XEF_JUMP:
(void) fprintf(stderr,"\n\"%s\", line %d: %s in function %s()\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
if (!isatty(_fileno(stdout)))
(void) fprintf(stdout,"\n\"%s\", line %d: %s in function %s()\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
longjmp(_xrestart,(err_num==0)? -1 : err_num);
break;
default:
(void) fprintf(stderr,"\n\"%s\", line %d: %s in function %s()\n\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
if (!isatty(_fileno(stdout)))
(void) fprintf(stdout,"\n\"%s\", line %d: %s in function %s()\n\n",
file,line_num,_xerr_lists[list_num].listp[num],
isascii(*fn_name) ? fn_name : "???");
break;
}
/* ensure exit if fall through */
if (!_xerr_lists[list_num].warn) exit(0);
return (0);
}
int _xset_err_flag(int flag){
return flag ^= _xerr_flag ^= flag ^= _xerr_flag;
}
int _xcount_errs(int cnt){
return cnt ^= _xcnt_errs ^= cnt ^= _xcnt_errs;
}
/*
* attach a new list of errors pointed by err_ptr or change a previous one;
* list_len is the number of elements in the list;
* list_num is the list number;
* warn == _FALSE - errors (stop the program),
* warn == _TRUE - warnings (continue the program);
* Note: lists numbered 0 and 1 are attached automatically,
* you do not need to do it
*/
int _xerr_list_attach(int list_num, int list_len, char **err_ptr, int warn){
if (list_num < 0 || list_len <= 0 || err_ptr == (char **)NULL)
return -1;
if (list_num >= _XERR_LIST_MAX_LEN){
(void) fprintf(stderr,"\n\"xmatrix.c\": increase the value of "
"_XERR_LIST_MAX_LEN in xconfig.h\n");
if (!isatty(_fileno(stdout)))
(void) fprintf(stderr,"\n\"xmatrix.c\": increase the value of "
"_XERR_LIST_MAX_LEN in xconfig.h\n");
(void) fprintf(stdout, "Exiting program\n");
exit(0);
}
if (_xerr_lists[list_num].listp != (char **)NULL && _xerr_lists[list_num].listp != err_ptr)
_xfree(_xerr_lists[list_num].listp);
_xerr_lists[list_num].listp = err_ptr;
_xerr_lists[list_num].len = list_len;
_xerr_lists[list_num].warn = warn;
_xerr_lists_end = list_num + 1;
return (list_num);
}
/* check if list_num is attached */
int _xerr_is_list_attached(int list_num){
if (list_num < 0 || list_num >= _xerr_lists_end)
return _XFALSE;
return ((_xerr_lists[list_num].listp != (char **)NULL) ? _XTRUE : _XFALSE);
}
/* release the error list numbered list_num */
int _xerr_list_free(int list_num){
if (list_num < 0 || list_num >= _xerr_lists_end) return -1;
if (_xerr_lists[list_num].listp != (char **)NULL){
_xerr_lists[list_num].listp = (char **)NULL;
_xerr_lists[list_num].len = 0;
_xerr_lists[list_num].warn = 0;
}
return (0);
}
/* names of types */
static char *_xmem_type_names[] = {
"_xmat", /* 0 - _XTYPE_MAT */
"_xband", /* 1 - _XTYPE_BAND */
"_xperm", /* 2 - _XTYPE_PERM */
"_xvec", /* 3 - _XTYPE_VEC */
"_xivec", /* 4 - _XTYPE_IVEC */
"_xiter", /* 5 - _XTYPE_ITER */
"_xsprow", /* 6 - _XTYPE_SPROW */
"_xspmat", /* 7 - _XTYPE_SPMAT */
"_xzvec", /* 8 - _XTYPE_ZVEC */
"_xzmat" /* 9 - _XTYPE_ZMAT */
};
#define _XMEM_NUM_STD_TYPES (sizeof(_xmem_type_names)/sizeof(_xmem_type_names[0]))
/* local array for keeping track of memory */
static _xmem_array _xmem_info_sum[_XMEM_NUM_STD_TYPES];
/*!
free (deallocate) a matrix.
*/
int _xmat_free(_xmat *mat){
#ifdef _XSEGMENTED
int i;
#endif
if (!mat || (int)(mat->m) < 0 || (int)(mat->n) < 0) /* don't trust it */
return (-1);
#ifndef _XSEGMENTED
if (mat->base != (_xreal *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_MAT,mat->max_m*mat->max_n*sizeof(_xreal),0);
}
_xfree(mat->base);
}
#else
for (i = 0; i < mat->max_m; i++)
if (mat->me[i] != (_xreal *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_MAT,mat->max_n*sizeof(_xreal),0);
}
_xfree(mat->me[i]);
}
#endif
if (mat->me != (_xreal **)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_MAT,mat->max_m*sizeof(_xreal *),0);
}
_xfree(mat->me);
}
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_MAT,sizeof(_xmat),0);
_xmem_numvar(_XTYPE_MAT,-1);
}
_xfree(mat);
return (0);
}
/*!
deallocate (destroy) band matrix
-- returns (-1) on error and 0 otherwise
*/
int _xband_free(_xband *A){
if (!A || A->lb < 0 || A->ub < 0) /* don't trust it */
return (-1);
if (A->mat) _xmat_free(A->mat);
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_BAND,sizeof(_xband),0);
_xmem_numvar(_XTYPE_BAND,-1);
}
_xfree(A);
return (0);
}
/*!
free (deallocate) a permutation.
*/
int _xperm_free(_xperm *px){
if (px==(_xperm *)NULL || (int)(px->size) < 0)/* don't trust it */
return (-1);
if (px->pe == (unsigned int *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_PERM,sizeof(_xperm),0);
_xmem_numvar(_XTYPE_PERM,-1);
}
_xfree(px);
}
else {
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_PERM,sizeof(_xperm)+px->max_size*sizeof(unsigned int),0);
_xmem_numvar(_XTYPE_PERM,-1);
}
_xfree(px->pe);
_xfree(px);
}
return (0);
}
/*!
free (deallocate) a vector.
*/
int _xvec_free(_xvec *vec){
if (!vec || (int)(vec->dim) < 0)/* don't trust it */
return (-1);
if (vec->ve == (_xreal *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_VEC,sizeof(_xvec),0);
_xmem_numvar(_XTYPE_VEC,-1);
}
_xfree(vec);
}
else {
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_VEC,sizeof(_xvec)+vec->max_dim*sizeof(_xreal),0);
_xmem_numvar(_XTYPE_VEC,-1);
}
_xfree(vec->ve);
_xfree(vec);
}
return (0);
}
/*!
free (deallocate) an integer vector.
*/
int _xivec_free(_xivec *iv){
if (!iv || iv->dim > _XMAXDIM) /* don't trust it */
return (-1);
if (iv->ive == (int *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_IVEC,sizeof(_xivec),0);
_xmem_numvar(_XTYPE_IVEC,-1);
}
_xfree(iv);
}
else {
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_IVEC,sizeof(_xivec)+iv->max_dim*sizeof(int),0);
_xmem_numvar(_XTYPE_IVEC,-1);
}
_xfree(iv->ive);
_xfree(iv);
}
return (0);
}
/*!
release memory
*/
int _xiter_free(_xiter *ip){
if (!ip) return -1;
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_ITER,sizeof(_xiter),0);
_xmem_numvar(_XTYPE_ITER,-1);
}
if (!ip->shared_x && ip->x) _xvec_free(ip->x);
if (!ip->shared_b && ip->b) _xvec_free(ip->b);
_xfree(ip);
return (0);
}
/*!
release a row of a matrix
*/
int _xsprow_free(_xsprow *r){
if (!r)
return -1;
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_SPROW,sizeof(_xsprow),0);
_xmem_numvar(_XTYPE_SPROW,-1);
}
if (r->elt){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_SPROW,r->maxlen*sizeof(_xrow_elt),0);
}
_xfree(r->elt);
}
_xfree(r);
return 0;
}
/*!
free up the memory for a sparse matrix
*/
int _xspmat_free(_xspmat *A){
_xsprow *r;
int i;
if (!A)
return (-1);
if (A->start_row != (int *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_SPMAT,A->max_n*sizeof(int),0);
}
_xfree(A->start_row);
}
if (A->start_idx != (int *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_SPMAT,A->max_n*sizeof(int),0);
}
_xfree(A->start_idx);
}
if (!A->row){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_SPMAT,sizeof(_xspmat),0);
_xmem_numvar(_XTYPE_SPMAT,-1);
}
_xfree(A);
return (0);
}
for (i = 0; i < A->m; i++){
r = &(A->row[i]);
if (r->elt != (_xrow_elt *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_SPMAT,A->row[i].maxlen*sizeof(_xrow_elt),0);
}
_xfree(r->elt);
}
}
if (_xmem_info_is_on()){
if (A->row)
_xmem_bytes(_XTYPE_SPMAT,A->max_m*sizeof(_xsprow),0);
_xmem_bytes(_XTYPE_SPMAT,sizeof(_xspmat),0);
_xmem_numvar(_XTYPE_SPMAT,-1);
}
_xfree(A->row);
_xfree(A);
return (0);
}
/*!
* return _xzvec & associated memory back to heap
*/
int _xzvec_free(_xzvec *vec){
if (vec == (_xzvec *)NULL || (int)(vec->dim) < 0)
return (-1);
if (vec->ve == (_xcomplex *)NULL){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_ZVEC,sizeof(_xzvec),0);
_xmem_numvar(_XTYPE_ZVEC,-1);
}
_xfree((char *)vec);
}
else {
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_ZVEC,vec->max_dim*sizeof(_xcomplex)+sizeof(_xzvec),0);
_xmem_numvar(_XTYPE_ZVEC,-1);
}
_xfree((char *)vec->ve);
_xfree((char *)vec);
}
return (0);
}
/*! return _xzmat & asoociated memory back to memory heap */
int _xzmat_free(_xzmat *mat){
#ifdef _XSEGMENTED
int i;
#endif
if (!mat || (int)(mat->m) < 0 ||
(int)(mat->n) < 0 )
/* don't trust it */
return (-1);
#ifndef _XSEGMENTED
if (mat->base != (_xcomplex *)NULL ){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_ZMAT,mat->max_m*mat->max_n*sizeof(_xcomplex),0);
}
_xfree((char *)(mat->base));
}
#else
for (i=0; i<mat->max_m; i++)
if (mat->me[i] != (_xcomplex *)NULL ){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_ZMAT,mat->max_n*sizeof(_xcomplex),0);
}
_xfree((char *)(mat->me[i]));
}
#endif
if (mat->me != (_xcomplex **)NULL ){
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_ZMAT,mat->max_m*sizeof(_xcomplex *),0);
}
_xfree((char *)(mat->me));
}
if (_xmem_info_is_on()){
_xmem_bytes(_XTYPE_ZMAT,sizeof(_xzmat),0);
_xmem_numvar(_XTYPE_ZMAT,-1);
}
_xfree((char *)mat);
return (0);
}
/* for freeing various types */
static int (*_xmem_free_funcs[_XMEM_NUM_STD_TYPES])() = {
(int (*)())_xmat_free,
(int (*)())_xband_free,
(int (*)())_xperm_free,
(int (*)())_xvec_free,
(int (*)())_xivec_free,
(int (*)())_xiter_free,
(int (*)())_xsprow_free,
(int (*)())_xspmat_free,
(int (*)())_xzvec_free,
(int (*)())_xzmat_free
};
/* it is a global variable for passing pointers to local arrays defined here */
_xmem_connect _xmem_connects[_XMEM_CONNECT_MAX_LISTS] = {
{ _xmem_type_names,
_xmem_free_funcs,
_XMEM_NUM_STD_TYPES,
_xmem_info_sum
}
};
/* local memory variables */
static int _xmem_switched_on = _XMEM_SWITCH_ON_DEF; /* on/off */
/*!
check whether memory data being accumulated or not.
*/
int _xmem_info_is_on(void){
return (_xmem_switched_on);
}
/*!
turn memory info system on/off
*/
int _xmem_info_on(int sw){
return (sw ^= _xmem_switched_on ^= sw ^= _xmem_switched_on);
}
/*!
number of bytes used by a type.
*/
int _xmem_info_bytes(int type, int list){
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS )
return (0);
if (!_xmem_switched_on || type < 0
|| (unsigned int)type >= _xmem_connects[list].ntypes
|| _xmem_connects[list].free_funcs[type] == NULL )
return (0);
return _xmem_connects[list].info_sum[type].bytes;
}
/*!
number of structures of a type.
*/
int _xmem_info_numvar(int type, int list){
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS )
return (0);
if (!_xmem_switched_on || type < 0
|| (unsigned int)type >= _xmem_connects[list].ntypes
|| _xmem_connects[list].free_funcs[type] == NULL )
return (0);
return _xmem_connects[list].info_sum[type].numvar;
}
/*!
print memory info to a stream.
*/
void _xmem_info_file(FILE *fp, int list){
unsigned int type;
int t = 0, n = 0, nt = 0, d;
_xmem_connect *mlist;
if (!_xmem_switched_on || list < 0 || list >= _XMEM_CONNECT_MAX_LISTS)
return;
if (list == 0)
(void) fprintf(fp," MEMORY INFORMATION (standard types):\n");
else
(void) fprintf(fp," MEMORY INFORMATION (list no. %d):\n",list);
mlist = &_xmem_connects[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;
(void) fprintf(fp," type %-7s %10d alloc. byte%c %6d alloc. variable%c\n",
mlist->type_names[type], d, (d!=1 ? 's' : ' '),
n, (n!=1 ? 's' : ' '));
}
(void) fprintf(fp," %-12s %10d alloc. byte%c %6d alloc. variable%c\n\n",
"total:",t, (t!=1 ? 's' : ' '), nt, (nt!=1 ? 's' : ' '));
}
/*!
notify change in memory usage.
*/
void _xmem_bytes_list(int type, int old_size, int new_size, int list){
_xmem_connect *mlist;
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS )
return;
mlist = &_xmem_connects[list];
if (type < 0 || (unsigned int)type >= mlist->ntypes || mlist->free_funcs[type] == NULL)
return;
if (old_size < 0 || new_size < 0)
_xerror(_XE_NEG,"_xmem_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){
(void) fprintf(stderr,"\nWARNING !! memory info: allocated memory is less than 0\n");
(void) fprintf(stderr,"\t TYPE %s \n\n", mlist->type_names[type]);
if (!isatty(_fileno(stdout))){
(void) fprintf(stdout,"\nWARNING !! memory info: allocated memory is less than 0\n");
(void) fprintf(stdout,"\t TYPE %s \n\n", mlist->type_names[type]);
}
}
}
}
/*!
notify change in number of structures allocated.
*/
void _xmem_numvar_list(int type, int num, int list){
_xmem_connect *mlist;
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS)
return;
mlist = &_xmem_connects[list];
if (type < 0 || (unsigned int)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){
(void) fprintf(stderr,"\nWARNING !! memory info: allocated # of variables is less than 0\n");
(void) fprintf(stderr,"\t TYPE %s \n\n", mlist->type_names[type]);
if (!isatty(_fileno(stdout))){
(void) fprintf(stdout,"\nWARNING !! memory info: allocated # of variables is less than 0\n");
(void) fprintf(stdout,"\t TYPE %s \n\n", mlist->type_names[type]);
}
}
}
}
/* local type & variables for memory stat */
typedef struct __xmem_stat {
void **var; /* for &A, where A is a pointer */
int type; /* type of A */
int mark; /* what mark is chosen */
char *fname; /* source file name where last registered */
int line; /* line # of file where last registered */
} _xmem_stat;
static int _xmem_stat_mark_many = 0; /* how many marks are used */
static int _xmem_stat_mark_curr = 0; /* current mark */
static _xmem_stat _xmem_stat_var[_XMEM_HASHSIZE];
static unsigned int _xmem_hash_idx[_XMEM_HASHSIZE];
static unsigned int _xmem_hash_idx_end = 0;
static unsigned int _xmem_hash(void **ptr){
return (unsigned int)((unsigned long)ptr % _XMEM_HASHSIZE);
}
static int _xmem_lookup(void **var){
int k, j;
k = _xmem_hash(var);
if (_xmem_stat_var[k].var == var){
return -1;
}
else if (_xmem_stat_var[k].var == NULL){
return k;
}
else { /* look for an empty place */
j = k;
while (_xmem_stat_var[j].var != var && j < _XMEM_HASHSIZE && _xmem_stat_var[j].var != NULL)
j++;
if (_xmem_stat_var[j].var == NULL) return j;
else if (_xmem_stat_var[j].var == var) return -1;
else { /* if (j == _XMEM_HASHSIZE) */
j = 0;
while (_xmem_stat_var[j].var != var && j < k && _xmem_stat_var[j].var != NULL)
j++;
if (_xmem_stat_var[j].var == NULL) return j;
else if (_xmem_stat_var[j].var == var) return -1;
else { /* if (j == k) */
(void) fprintf(stderr,"\nWARNING !!! static memory: mem_stat_var is too small\n");
(void) fprintf(stderr,"Increase MEM_HASHSIZE in file: %s (currently = %d)\n\n",
_XMEM_HASHSIZE_FILE, _XMEM_HASHSIZE);
if (!isatty(_fileno(stdout))){
(void) fprintf(stdout,"\nWARNING !!! static memory: mem_stat_var is too small\n");
(void) fprintf(stdout,"Increase MEM_HASHSIZE in file: %s (currently = %d)\n\n",
_XMEM_HASHSIZE_FILE, _XMEM_HASHSIZE);
}
_xerror(_XE_MEM,"_xmem_lookup");
}
}
}
return -1;
}
int _xmem_stat_reg_list(void **var, int type, int list, char *fname, int line){
int n;
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS) return -1;
if (_xmem_stat_mark_curr == 0) return 0; /* not registered */
if (var == NULL) return -1; /* error */
if (type < 0 || (unsigned int)type >= _xmem_connects[list].ntypes || _xmem_connects[list].free_funcs[type] == NULL){
_xwarning(_XWARN_WRONG_TYPE,"_xmem_stat_reg_list");
return -1;
}
if ((n = _xmem_lookup(var)) >= 0){
_xmem_stat_var[n].var = var;
_xmem_stat_var[n].mark = _xmem_stat_mark_curr;
_xmem_stat_var[n].type = type;
_xmem_stat_var[n].fname = fname;
_xmem_stat_var[n].line = line;
/* save n+1, not n */
_xmem_hash_idx[_xmem_hash_idx_end++] = n+1;
}
return _xmem_stat_mark_curr;
}
/*!
set mark for workspace.
*/
int _xmem_stat_mark(int mark){
if (mark < 0){
_xmem_stat_mark_curr = 0;
return -1; /* error */
}
else if (mark == 0){
_xmem_stat_mark_curr = 0;
return 0;
}
_xmem_stat_mark_curr = mark;
_xmem_stat_mark_many++;
return mark;
}
int _xmem_stat_free_list(int mark, int list){
unsigned int i,j;
int (*free_fn)();
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS || _xmem_connects[list].free_funcs == NULL)
return -1;
if (mark < 0){
_xmem_stat_mark_curr = 0;
return -1;
}
else if (mark == 0){
_xmem_stat_mark_curr = 0;
return 0;
}
if (_xmem_stat_mark_many <= 0){
_xwarning(_XWARN_NO_MARK,"_xmem_stat_free");
return -1;
}
#ifdef _XDEBUG
(void) fprintf(stdout, "mem_stat_free: Freeing variables registered for mark %d\n", mark);
#endif/* _XDEBUG */
/* deallocate the marked variables */
for (i=0; (unsigned int)i < _xmem_hash_idx_end; i++){
j = _xmem_hash_idx[i];
if (j == 0) continue;
else {
j--;
if (_xmem_stat_var[j].mark == mark){
free_fn = _xmem_connects[list].free_funcs[_xmem_stat_var[j].type];
#ifdef _XDEBUG
(void) fprintf(stdout, "# Freeing variable(s) registered in file \"%s\", line %d\n",
_xmem_stat_var[j].fname, _xmem_stat_var[j].line);
#endif/* _XDEBUG */
if (free_fn != NULL)
(*free_fn)(*_xmem_stat_var[j].var);
else
_xwarning(_XWARN_WRONG_TYPE,"_xmem_stat_free");
*(_xmem_stat_var[j].var) = NULL;
_xmem_stat_var[j].var = NULL;
_xmem_stat_var[j].mark = 0;
_xmem_stat_var[j].fname = NULL;
_xmem_stat_var[j].line = 0;
_xmem_hash_idx[i] = 0;
}
}
}
while (_xmem_hash_idx_end > 0 && _xmem_hash_idx[_xmem_hash_idx_end-1] == 0)
_xmem_hash_idx_end--;
_xmem_stat_mark_curr = 0;
_xmem_stat_mark_many--;
return (0);
}
/*!
current workspace group
*/
int _xmem_stat_show_mark(void){
return _xmem_stat_mark_curr;
}
int _xmem_stat_reg_vars(int list,int type,char *fname,int line, ...){
va_list ap;
int i=0;
void **par;
va_start(ap, line);
while (par = va_arg(ap, void **)){
_xmem_stat_reg_list(par,type,list,fname,line);
i++;
}
va_end(ap);
return (i);
}
/*!
print information on registered workspace.
*/
void _xmem_stat_dump(FILE *fp, int list){
unsigned int i,j,k=1;
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS || _xmem_connects[list].free_funcs == NULL)
return;
(void) fprintf(fp," Array mem_stat_var (list no. %d):\n",list);
for (i=0; (unsigned int)i < _xmem_hash_idx_end; i++){
j = _xmem_hash_idx[i];
if (j == 0) continue;
else {
j--;
(void) fprintf(fp," %d. var = 0x%p, type = %s, mark = %d\n",
k,_xmem_stat_var[j].var,
(unsigned int)_xmem_stat_var[j].type < _xmem_connects[list].ntypes &&
_xmem_connects[list].free_funcs[_xmem_stat_var[j].type] != NULL ?
_xmem_connects[list].type_names[(int)_xmem_stat_var[j].type] :
"???",
_xmem_stat_var[j].mark);
k++;
}
}
(void) fprintf(fp,"\n");
}
int _xmem_free_vars(int list){
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS)
return -1;
_xmem_connects[list].ntypes = 0;
_xmem_connects[list].type_names = NULL;
_xmem_connects[list].free_funcs = NULL;
_xmem_connects[list].info_sum = NULL;
return (0);
}
/*!
check whether a list of types attached or not.
*/
int _xmem_is_list_attached(int list){
if (list < 0 || list >= _XMEM_CONNECT_MAX_LISTS)
return _XFALSE;
return ((_xmem_connects[list].type_names != NULL &&
_xmem_connects[list].free_funcs != NULL &&
_xmem_connects[list].info_sum != NULL) ?
_XTRUE : _XFALSE);
}
/*!
add a new family of types
*/
int _xmem_attach_list(int list,
int ntypes,
char **type_names,
int (**free_funcs)(void *),
_xmem_array* info_sum){
if (list < 0 || list >= _XMEM_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 (_xmem_connects[list].ntypes != 0)
_xerror(_XE_OVERWRITE,"_xmem_attach_list");
_xmem_connects[list].ntypes = ntypes;
_xmem_connects[list].type_names = type_names;
_xmem_connects[list].free_funcs = free_funcs;
_xmem_connects[list].info_sum = info_sum;
return (0);
}
void _xmem_dump_list(FILE *fp, int list){
int i;