PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mesch/zmemory.c

https://github.com/ljsking/compiler
C | 745 lines | 500 code | 130 blank | 115 comment | 114 complexity | 1af3e558fb046903db1f03b855fbea1c MD5 | raw file
  1. /**************************************************************************
  2. **
  3. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  4. **
  5. ** Meschach Library
  6. **
  7. ** This Meschach Library is provided "as is" without any express
  8. ** or implied warranty of any kind with respect to this software.
  9. ** In particular the authors shall not be liable for any direct,
  10. ** indirect, special, incidental or consequential damages arising
  11. ** in any way from use of the software.
  12. **
  13. ** Everyone is granted permission to copy, modify and redistribute this
  14. ** Meschach Library, provided:
  15. ** 1. All copies contain this copyright notice.
  16. ** 2. All modified copies shall carry a notice stating who
  17. ** made the last modification and the date of such modification.
  18. ** 3. No charge is made for this software or works derived from it.
  19. ** This clause shall not be construed as constraining other software
  20. ** distributed on the same medium as this software, nor is a
  21. ** distribution fee considered a charge.
  22. **
  23. ***************************************************************************/
  24. /* Memory allocation and de-allocation for complex matrices and vectors */
  25. #include <stdio.h>
  26. #include "zmatrix.h"
  27. static char rcsid[] = "$Id: zmemory.c,v 1.2 1994/04/05 02:13:14 des Exp $";
  28. /* zv_zero -- zeros all entries of a complex vector
  29. -- uses __zzero__() */
  30. #ifndef ANSI_C
  31. ZVEC *zv_zero(x)
  32. ZVEC *x;
  33. #else
  34. ZVEC *zv_zero(ZVEC *x)
  35. #endif
  36. {
  37. if ( ! x )
  38. error(E_NULL,"zv_zero");
  39. __zzero__(x->ve,x->dim);
  40. return x;
  41. }
  42. /* zm_zero -- zeros all entries of a complex matrix
  43. -- uses __zzero__() */
  44. #ifndef ANSI_C
  45. ZMAT *zm_zero(A)
  46. ZMAT *A;
  47. #else
  48. ZMAT *zm_zero(ZMAT *A)
  49. #endif
  50. {
  51. int i;
  52. if ( ! A )
  53. error(E_NULL,"zm_zero");
  54. for ( i = 0; i < A->m; i++ )
  55. __zzero__(A->me[i],A->n);
  56. return A;
  57. }
  58. /* zm_get -- gets an mxn complex matrix (in ZMAT form) */
  59. #ifndef ANSI_C
  60. ZMAT *zm_get(m,n)
  61. int m,n;
  62. #else
  63. ZMAT *zm_get(int m, int n)
  64. #endif
  65. {
  66. ZMAT *matrix;
  67. unsigned int i;
  68. if (m < 0 || n < 0)
  69. error(E_NEG,"zm_get");
  70. if ((matrix=NEW(ZMAT)) == (ZMAT *)NULL )
  71. error(E_MEM,"zm_get");
  72. else if (mem_info_is_on()) {
  73. mem_bytes(TYPE_ZMAT,0,sizeof(ZMAT));
  74. mem_numvar(TYPE_ZMAT,1);
  75. }
  76. matrix->m = m; matrix->n = matrix->max_n = n;
  77. matrix->max_m = m; matrix->max_size = m*n;
  78. #ifndef SEGMENTED
  79. if ((matrix->base = NEW_A(m*n,complex)) == (complex *)NULL )
  80. {
  81. free(matrix);
  82. error(E_MEM,"zm_get");
  83. }
  84. else if (mem_info_is_on()) {
  85. mem_bytes(TYPE_ZMAT,0,m*n*sizeof(complex));
  86. }
  87. #else
  88. matrix->base = (complex *)NULL;
  89. #endif
  90. if ((matrix->me = (complex **)calloc(m,sizeof(complex *))) ==
  91. (complex **)NULL )
  92. { free(matrix->base); free(matrix);
  93. error(E_MEM,"zm_get");
  94. }
  95. else if (mem_info_is_on()) {
  96. mem_bytes(TYPE_ZMAT,0,m*sizeof(complex *));
  97. }
  98. #ifndef SEGMENTED
  99. /* set up pointers */
  100. for ( i=0; i<m; i++ )
  101. matrix->me[i] = &(matrix->base[i*n]);
  102. #else
  103. for ( i = 0; i < m; i++ )
  104. if ( (matrix->me[i]=NEW_A(n,complex)) == (complex *)NULL )
  105. error(E_MEM,"zm_get");
  106. else if (mem_info_is_on()) {
  107. mem_bytes(TYPE_ZMAT,0,n*sizeof(complex));
  108. }
  109. #endif
  110. return (matrix);
  111. }
  112. /* zv_get -- gets a ZVEC of dimension 'dim'
  113. -- Note: initialized to zero */
  114. #ifndef ANSI_C
  115. ZVEC *zv_get(size)
  116. int size;
  117. #else
  118. ZVEC *zv_get(int size)
  119. #endif
  120. {
  121. ZVEC *vector;
  122. if (size < 0)
  123. error(E_NEG,"zv_get");
  124. if ((vector=NEW(ZVEC)) == (ZVEC *)NULL )
  125. error(E_MEM,"zv_get");
  126. else if (mem_info_is_on()) {
  127. mem_bytes(TYPE_ZVEC,0,sizeof(ZVEC));
  128. mem_numvar(TYPE_ZVEC,1);
  129. }
  130. vector->dim = vector->max_dim = size;
  131. if ((vector->ve=NEW_A(size,complex)) == (complex *)NULL )
  132. {
  133. free(vector);
  134. error(E_MEM,"zv_get");
  135. }
  136. else if (mem_info_is_on()) {
  137. mem_bytes(TYPE_ZVEC,0,size*sizeof(complex));
  138. }
  139. return (vector);
  140. }
  141. /* zm_free -- returns ZMAT & asoociated memory back to memory heap */
  142. #ifndef ANSI_C
  143. int zm_free(mat)
  144. ZMAT *mat;
  145. #else
  146. int zm_free(ZMAT *mat)
  147. #endif
  148. {
  149. #ifdef SEGMENTED
  150. int i;
  151. #endif
  152. if ( mat==(ZMAT *)NULL || (int)(mat->m) < 0 ||
  153. (int)(mat->n) < 0 )
  154. /* don't trust it */
  155. return (-1);
  156. #ifndef SEGMENTED
  157. if ( mat->base != (complex *)NULL ) {
  158. if (mem_info_is_on()) {
  159. mem_bytes(TYPE_ZMAT,mat->max_m*mat->max_n*sizeof(complex),0);
  160. }
  161. free((char *)(mat->base));
  162. }
  163. #else
  164. for ( i = 0; i < mat->max_m; i++ )
  165. if ( mat->me[i] != (complex *)NULL ) {
  166. if (mem_info_is_on()) {
  167. mem_bytes(TYPE_ZMAT,mat->max_n*sizeof(complex),0);
  168. }
  169. free((char *)(mat->me[i]));
  170. }
  171. #endif
  172. if ( mat->me != (complex **)NULL ) {
  173. if (mem_info_is_on()) {
  174. mem_bytes(TYPE_ZMAT,mat->max_m*sizeof(complex *),0);
  175. }
  176. free((char *)(mat->me));
  177. }
  178. if (mem_info_is_on()) {
  179. mem_bytes(TYPE_ZMAT,sizeof(ZMAT),0);
  180. mem_numvar(TYPE_ZMAT,-1);
  181. }
  182. free((char *)mat);
  183. return (0);
  184. }
  185. /* zv_free -- returns ZVEC & asoociated memory back to memory heap */
  186. #ifndef ANSI_C
  187. int zv_free(vec)
  188. ZVEC *vec;
  189. #else
  190. int zv_free(ZVEC *vec)
  191. #endif
  192. {
  193. if ( vec==(ZVEC *)NULL || (int)(vec->dim) < 0 )
  194. /* don't trust it */
  195. return (-1);
  196. if ( vec->ve == (complex *)NULL ) {
  197. if (mem_info_is_on()) {
  198. mem_bytes(TYPE_ZVEC,sizeof(ZVEC),0);
  199. mem_numvar(TYPE_ZVEC,-1);
  200. }
  201. free((char *)vec);
  202. }
  203. else
  204. {
  205. if (mem_info_is_on()) {
  206. mem_bytes(TYPE_ZVEC,vec->max_dim*sizeof(complex)+
  207. sizeof(ZVEC),0);
  208. mem_numvar(TYPE_ZVEC,-1);
  209. }
  210. free((char *)vec->ve);
  211. free((char *)vec);
  212. }
  213. return (0);
  214. }
  215. /* zm_resize -- returns the matrix A of size new_m x new_n; A is zeroed
  216. -- if A == NULL on entry then the effect is equivalent to m_get() */
  217. #ifndef ANSI_C
  218. ZMAT *zm_resize(A,new_m,new_n)
  219. ZMAT *A;
  220. int new_m, new_n;
  221. #else
  222. ZMAT *zm_resize(ZMAT *A, int new_m, int new_n)
  223. #endif
  224. {
  225. unsigned int i, new_max_m, new_max_n, new_size, old_m, old_n;
  226. if (new_m < 0 || new_n < 0)
  227. error(E_NEG,"zm_resize");
  228. if ( ! A )
  229. return zm_get(new_m,new_n);
  230. if (new_m == A->m && new_n == A->n)
  231. return A;
  232. old_m = A->m; old_n = A->n;
  233. if ( new_m > A->max_m )
  234. { /* re-allocate A->me */
  235. if (mem_info_is_on()) {
  236. mem_bytes(TYPE_ZMAT,A->max_m*sizeof(complex *),
  237. new_m*sizeof(complex *));
  238. }
  239. A->me = RENEW(A->me,new_m,complex *);
  240. if ( ! A->me )
  241. error(E_MEM,"zm_resize");
  242. }
  243. new_max_m = max(new_m,A->max_m);
  244. new_max_n = max(new_n,A->max_n);
  245. #ifndef SEGMENTED
  246. new_size = new_max_m*new_max_n;
  247. if ( new_size > A->max_size )
  248. { /* re-allocate A->base */
  249. if (mem_info_is_on()) {
  250. mem_bytes(TYPE_ZMAT,A->max_m*A->max_n*sizeof(complex),
  251. new_size*sizeof(complex));
  252. }
  253. A->base = RENEW(A->base,new_size,complex);
  254. if ( ! A->base )
  255. error(E_MEM,"zm_resize");
  256. A->max_size = new_size;
  257. }
  258. /* now set up A->me[i] */
  259. for ( i = 0; i < new_m; i++ )
  260. A->me[i] = &(A->base[i*new_n]);
  261. /* now shift data in matrix */
  262. if ( old_n > new_n )
  263. {
  264. for ( i = 1; i < min(old_m,new_m); i++ )
  265. MEM_COPY((char *)&(A->base[i*old_n]),
  266. (char *)&(A->base[i*new_n]),
  267. sizeof(complex)*new_n);
  268. }
  269. else if ( old_n < new_n )
  270. {
  271. for ( i = min(old_m,new_m)-1; i > 0; i-- )
  272. { /* copy & then zero extra space */
  273. MEM_COPY((char *)&(A->base[i*old_n]),
  274. (char *)&(A->base[i*new_n]),
  275. sizeof(complex)*old_n);
  276. __zzero__(&(A->base[i*new_n+old_n]),(new_n-old_n));
  277. }
  278. __zzero__(&(A->base[old_n]),(new_n-old_n));
  279. A->max_n = new_n;
  280. }
  281. /* zero out the new rows.. */
  282. for ( i = old_m; i < new_m; i++ )
  283. __zzero__(&(A->base[i*new_n]),new_n);
  284. #else
  285. if ( A->max_n < new_n )
  286. {
  287. complex *tmp;
  288. for ( i = 0; i < A->max_m; i++ )
  289. {
  290. if (mem_info_is_on()) {
  291. mem_bytes(TYPE_ZMAT,A->max_n*sizeof(complex),
  292. new_max_n*sizeof(complex));
  293. }
  294. if ( (tmp = RENEW(A->me[i],new_max_n,complex)) == NULL )
  295. error(E_MEM,"zm_resize");
  296. else {
  297. A->me[i] = tmp;
  298. }
  299. }
  300. for ( i = A->max_m; i < new_max_m; i++ )
  301. {
  302. if ( (tmp = NEW_A(new_max_n,complex)) == NULL )
  303. error(E_MEM,"zm_resize");
  304. else {
  305. A->me[i] = tmp;
  306. if (mem_info_is_on()) {
  307. mem_bytes(TYPE_ZMAT,0,new_max_n*sizeof(complex));
  308. }
  309. }
  310. }
  311. }
  312. else if ( A->max_m < new_m )
  313. {
  314. for ( i = A->max_m; i < new_m; i++ )
  315. if ( (A->me[i] = NEW_A(new_max_n,complex)) == NULL )
  316. error(E_MEM,"zm_resize");
  317. else if (mem_info_is_on()) {
  318. mem_bytes(TYPE_ZMAT,0,new_max_n*sizeof(complex));
  319. }
  320. }
  321. if ( old_n < new_n )
  322. {
  323. for ( i = 0; i < old_m; i++ )
  324. __zzero__(&(A->me[i][old_n]),new_n-old_n);
  325. }
  326. /* zero out the new rows.. */
  327. for ( i = old_m; i < new_m; i++ )
  328. __zzero__(A->me[i],new_n);
  329. #endif
  330. A->max_m = new_max_m;
  331. A->max_n = new_max_n;
  332. A->max_size = A->max_m*A->max_n;
  333. A->m = new_m; A->n = new_n;
  334. return A;
  335. }
  336. /* zv_resize -- returns the (complex) vector x with dim new_dim
  337. -- x is set to the zero vector */
  338. #ifndef ANSI_C
  339. ZVEC *zv_resize(x,new_dim)
  340. ZVEC *x;
  341. int new_dim;
  342. #else
  343. ZVEC *zv_resize(ZVEC *x, int new_dim)
  344. #endif
  345. {
  346. if (new_dim < 0)
  347. error(E_NEG,"zv_resize");
  348. if ( ! x )
  349. return zv_get(new_dim);
  350. if (new_dim == x->dim)
  351. return x;
  352. if ( x->max_dim == 0 ) /* assume that it's from sub_zvec */
  353. return zv_get(new_dim);
  354. if ( new_dim > x->max_dim )
  355. {
  356. if (mem_info_is_on()) {
  357. mem_bytes(TYPE_ZVEC,x->max_dim*sizeof(complex),
  358. new_dim*sizeof(complex));
  359. }
  360. x->ve = RENEW(x->ve,new_dim,complex);
  361. if ( ! x->ve )
  362. error(E_MEM,"zv_resize");
  363. x->max_dim = new_dim;
  364. }
  365. if ( new_dim > x->dim )
  366. __zzero__(&(x->ve[x->dim]),new_dim - x->dim);
  367. x->dim = new_dim;
  368. return x;
  369. }
  370. /* varying arguments */
  371. #ifdef ANSI_C
  372. #include <stdarg.h>
  373. /* To allocate memory to many arguments.
  374. The function should be called:
  375. zv_get_vars(dim,&x,&y,&z,...,NULL);
  376. where
  377. int dim;
  378. ZVEC *x, *y, *z,...;
  379. The last argument should be NULL !
  380. dim is the length of vectors x,y,z,...
  381. returned value is equal to the number of allocated variables
  382. Other gec_... functions are similar.
  383. */
  384. int zv_get_vars(int dim,...)
  385. {
  386. va_list ap;
  387. int i=0;
  388. ZVEC **par;
  389. va_start(ap, dim);
  390. while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
  391. *par = zv_get(dim);
  392. i++;
  393. }
  394. va_end(ap);
  395. return i;
  396. }
  397. int zm_get_vars(int m,int n,...)
  398. {
  399. va_list ap;
  400. int i=0;
  401. ZMAT **par;
  402. va_start(ap, n);
  403. while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
  404. *par = zm_get(m,n);
  405. i++;
  406. }
  407. va_end(ap);
  408. return i;
  409. }
  410. /* To resize memory for many arguments.
  411. The function should be called:
  412. v_resize_vars(new_dim,&x,&y,&z,...,NULL);
  413. where
  414. int new_dim;
  415. ZVEC *x, *y, *z,...;
  416. The last argument should be NULL !
  417. rdim is the resized length of vectors x,y,z,...
  418. returned value is equal to the number of allocated variables.
  419. If one of x,y,z,.. arguments is NULL then memory is allocated to this
  420. argument.
  421. Other *_resize_list() functions are similar.
  422. */
  423. int zv_resize_vars(int new_dim,...)
  424. {
  425. va_list ap;
  426. int i=0;
  427. ZVEC **par;
  428. va_start(ap, new_dim);
  429. while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
  430. *par = zv_resize(*par,new_dim);
  431. i++;
  432. }
  433. va_end(ap);
  434. return i;
  435. }
  436. int zm_resize_vars(int m,int n,...)
  437. {
  438. va_list ap;
  439. int i=0;
  440. ZMAT **par;
  441. va_start(ap, n);
  442. while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
  443. *par = zm_resize(*par,m,n);
  444. i++;
  445. }
  446. va_end(ap);
  447. return i;
  448. }
  449. /* To deallocate memory for many arguments.
  450. The function should be called:
  451. v_free_vars(&x,&y,&z,...,NULL);
  452. where
  453. ZVEC *x, *y, *z,...;
  454. The last argument should be NULL !
  455. There must be at least one not NULL argument.
  456. returned value is equal to the number of allocated variables.
  457. Returned value of x,y,z,.. is VNULL.
  458. Other *_free_list() functions are similar.
  459. */
  460. int zv_free_vars(ZVEC **pv,...)
  461. {
  462. va_list ap;
  463. int i=1;
  464. ZVEC **par;
  465. zv_free(*pv);
  466. *pv = ZVNULL;
  467. va_start(ap, pv);
  468. while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
  469. zv_free(*par);
  470. *par = ZVNULL;
  471. i++;
  472. }
  473. va_end(ap);
  474. return i;
  475. }
  476. int zm_free_vars(ZMAT **va,...)
  477. {
  478. va_list ap;
  479. int i=1;
  480. ZMAT **par;
  481. zm_free(*va);
  482. *va = ZMNULL;
  483. va_start(ap, va);
  484. while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
  485. zm_free(*par);
  486. *par = ZMNULL;
  487. i++;
  488. }
  489. va_end(ap);
  490. return i;
  491. }
  492. #elif VARARGS
  493. #include <varargs.h>
  494. /* To allocate memory to many arguments.
  495. The function should be called:
  496. v_get_vars(dim,&x,&y,&z,...,NULL);
  497. where
  498. int dim;
  499. ZVEC *x, *y, *z,...;
  500. The last argument should be NULL !
  501. dim is the length of vectors x,y,z,...
  502. returned value is equal to the number of allocated variables
  503. Other gec_... functions are similar.
  504. */
  505. int zv_get_vars(va_alist) va_dcl
  506. {
  507. va_list ap;
  508. int dim,i=0;
  509. ZVEC **par;
  510. va_start(ap);
  511. dim = va_arg(ap,int);
  512. while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
  513. *par = zv_get(dim);
  514. i++;
  515. }
  516. va_end(ap);
  517. return i;
  518. }
  519. int zm_get_vars(va_alist) va_dcl
  520. {
  521. va_list ap;
  522. int i=0, n, m;
  523. ZMAT **par;
  524. va_start(ap);
  525. m = va_arg(ap,int);
  526. n = va_arg(ap,int);
  527. while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
  528. *par = zm_get(m,n);
  529. i++;
  530. }
  531. va_end(ap);
  532. return i;
  533. }
  534. /* To resize memory for many arguments.
  535. The function should be called:
  536. v_resize_vars(new_dim,&x,&y,&z,...,NULL);
  537. where
  538. int new_dim;
  539. ZVEC *x, *y, *z,...;
  540. The last argument should be NULL !
  541. rdim is the resized length of vectors x,y,z,...
  542. returned value is equal to the number of allocated variables.
  543. If one of x,y,z,.. arguments is NULL then memory is allocated to this
  544. argument.
  545. Other *_resize_list() functions are similar.
  546. */
  547. int zv_resize_vars(va_alist) va_dcl
  548. {
  549. va_list ap;
  550. int i=0, new_dim;
  551. ZVEC **par;
  552. va_start(ap);
  553. new_dim = va_arg(ap,int);
  554. while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
  555. *par = zv_resize(*par,new_dim);
  556. i++;
  557. }
  558. va_end(ap);
  559. return i;
  560. }
  561. int zm_resize_vars(va_alist) va_dcl
  562. {
  563. va_list ap;
  564. int i=0, m, n;
  565. ZMAT **par;
  566. va_start(ap);
  567. m = va_arg(ap,int);
  568. n = va_arg(ap,int);
  569. while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
  570. *par = zm_resize(*par,m,n);
  571. i++;
  572. }
  573. va_end(ap);
  574. return i;
  575. }
  576. /* To deallocate memory for many arguments.
  577. The function should be called:
  578. v_free_vars(&x,&y,&z,...,NULL);
  579. where
  580. ZVEC *x, *y, *z,...;
  581. The last argument should be NULL !
  582. There must be at least one not NULL argument.
  583. returned value is equal to the number of allocated variables.
  584. Returned value of x,y,z,.. is VNULL.
  585. Other *_free_list() functions are similar.
  586. */
  587. int zv_free_vars(va_alist) va_dcl
  588. {
  589. va_list ap;
  590. int i=0;
  591. ZVEC **par;
  592. va_start(ap);
  593. while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
  594. zv_free(*par);
  595. *par = ZVNULL;
  596. i++;
  597. }
  598. va_end(ap);
  599. return i;
  600. }
  601. int zm_free_vars(va_alist) va_dcl
  602. {
  603. va_list ap;
  604. int i=0;
  605. ZMAT **par;
  606. va_start(ap);
  607. while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
  608. zm_free(*par);
  609. *par = ZMNULL;
  610. i++;
  611. }
  612. va_end(ap);
  613. return i;
  614. }
  615. #endif