home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / meschach / !Meschach / c / meminfo < prev    next >
Encoding:
Text File  |  1994-01-13  |  8.9 KB  |  424 lines

  1. rror(E_MEM,"px_get");
  2.    else if (mem_info_is_on()) {
  3.       mem_bytes(TYPE_PERM,0,size*sizeof(u_int));
  4.    }
  5.    
  6.    for ( i=0; i<size; i++ )
  7.      permute->pe[i] = i;
  8.    
  9.    return (permute);
  10. }
  11.  
  12. /* v_get -- gets a VEC of dimension 'dim'
  13.    -- Note: initialized to zero */
  14. VEC    *v_get(size)
  15. int    size;
  16. {
  17.    VEC    *vector;
  18.    
  19.    if (size < 0)
  20.      error(E_NEG,"v_get");
  21.  
  22.    if ((vector=NEW(VEC)) == (VEC *)NULL )
  23.      error(E_MEM,"v_get");
  24.    else if (mem_info_is_on()) {
  25.       mem_bytes(TYPE_VEC,0,sizeof(VEC));
  26.       mem_numvar(TYPE_VEC,1);
  27.    }
  28.    
  29.    vector->dim = vector->max_dim = size;
  30.    if ((vector->ve=NEW_A(size,Real)) == (Real *)NULL )
  31.    {
  32.       free(vector);
  33.       error(E_MEM,"v_get");
  34.    }
  35.    else if (mem_info_is_on()) {
  36.       mem_bytes(TYPE_VEC,0,size*sizeof(Real));
  37.    }
  38.    
  39.    return (vector);
  40. }
  41.  
  42. /* m_free -- returns MAT & asoociated memory back to memory heap */
  43. int    m_free(mat)
  44. MAT    *mat;
  45. {
  46. #ifdef SEGMENTED
  47.    int    i;
  48. #endif
  49.    
  50.    if ( mat==(MAT *)NULL || (int)(mat->m) < 0 ||
  51.        (int)(mat->n) < 0 )
  52.      /* don't trust it */
  53.      return (-1);
  54.    
  55. #ifndef SEGMENTED
  56.    if ( mat->base != (Real *)NULL ) {
  57.       if (mem_info_is_on()) {
  58.      mem_bytes(TYPE_MAT,mat->max_m*mat->max_n*sizeof(Real),0);
  59.       }
  60.       free((char *)(mat->base));
  61.    }
  62. #else
  63.    for ( i = 0; i < mat->max_m; i++ )
  64.      if ( mat->me[i] != (Real *)NULL ) {
  65.     if (mem_info_is_on()) {
  66.        mem_bytes(TYPE_MAT,mat->max_n*sizeof(Real),0);
  67.     }
  68.     free((char *)(mat->me[i]));
  69.      }
  70. #endif
  71.    if ( mat->me != (Real **)NULL ) {
  72.       if (mem_info_is_on()) {
  73.      mem_bytes(TYPE_MAT,mat->max_m*sizeof(Real *),0);
  74.       }
  75.       free((char *)(mat->me));
  76.    }
  77.    
  78.    if (mem_info_is_on()) {
  79.       mem_bytes(TYPE_MAT,sizeof(MAT),0);
  80.       mem_numvar(TYPE_MAT,-1);
  81.    }
  82.    free((char *)mat);
  83.    
  84.    return (0);
  85. }
  86.  
  87.  
  88.  
  89. /* px_free -- returns PERM & asoociated memory back to memory heap */
  90. int    px_free(px)
  91. PERM    *px;
  92. {
  93.    if ( px==(PERM *)NULL || (int)(px->size) < 0 )
  94.      /* don't trust it */
  95.      return (-1);
  96.    
  97.    if ( px->pe == (u_int *)NULL ) {
  98.       if (mem_info_is_on()) {
  99.      mem_bytes(TYPE_PERM,sizeof(PERM),0);
  100.      mem_numvar(TYPE_PERM,-1);
  101.       }      
  102.       free((char *)px);
  103.    }
  104.    else
  105.    {
  106.       if (mem_info_is_on()) {
  107.      mem_bytes(TYPE_PERM,sizeof(PERM)+px->max_size*sizeof(u_int),0);
  108.      mem_numvar(TYPE_PERM,-1);
  109.       }
  110.       free((char *)px->pe);
  111.       free((char *)px);
  112.    }
  113.    
  114.    return (0);
  115. }
  116.  
  117.  
  118.  
  119. /* v_free -- returns VEC & asoociated memory back to memory heap */
  120. int    v_free(vec)
  121. VEC    *vec;
  122. {
  123.    if ( vec==(VEC *)NULL || (int)(vec->dim) < 0 )
  124.      /* don't trust it */
  125.      return (-1);
  126.    
  127.    if ( vec->ve == (Real *)NULL ) {
  128.       if (mem_info_is_on()) {
  129.      mem_bytes(TYPE_VEC,sizeof(VEC),0);
  130.      mem_numvar(TYPE_VEC,-1);
  131.       }
  132.       free((char *)vec);
  133.    }
  134.    else
  135.    {
  136.       if (mem_info_is_on()) {
  137.      mem_bytes(TYPE_VEC,sizeof(VEC)+vec->max_dim*sizeof(Real),0);
  138.      mem_numvar(TYPE_VEC,-1);
  139.       }
  140.       free((char *)vec->ve);
  141.       free((char *)vec);
  142.    }
  143.    
  144.    return (0);
  145. }
  146.  
  147.  
  148.  
  149. /* m_resize -- returns the matrix A of size new_m x new_n; A is zeroed
  150.    -- if A == NULL on entry then the effect is equivalent to m_get() */
  151. MAT    *m_resize(A,new_m,new_n)
  152. MAT    *A;
  153. int    new_m, new_n;
  154. {
  155.    int    i;
  156.    int    new_max_m, new_max_n, new_size, old_m, old_n;
  157.    
  158.    if (new_m < 0 || new_n < 0)
  159.      error(E_NEG,"m_resize");
  160.  
  161.    if ( ! A )
  162.      return m_get(new_m,new_n);
  163.  
  164.    /* nothing was changed */
  165.    if (new_m == A->m && new_n == A->n)
  166.      return A;
  167.  
  168.    old_m = A->m;    old_n = A->n;
  169.    if ( new_m > A->max_m )
  170.    {    /* re-allocate A->me */
  171.       if (mem_info_is_on()) {
  172.      mem_bytes(TYPE_MAT,A->max_m*sizeof(Real *),
  173.               new_m*sizeof(Real *));
  174.       }
  175.  
  176.       A->me = RENEW(A->me,new_m,Real *);
  177.       if ( ! A->me )
  178.     error(E_MEM,"m_resize");
  179.    }
  180.    new_max_m = max(new_m,A->max_m);
  181.    new_max_n = max(new_n,A->max_n);
  182.    
  183. #ifndef SEGMENTED
  184.    new_size = new_max_m*new_max_n;
  185.    if ( new_size > A->max_size )
  186.    {    /* re-allocate A->base */
  187.       if (mem_info_is_on()) {
  188.      mem_bytes(TYPE_MAT,A->max_m*A->max_n*sizeof(Real),
  189.               new_size*sizeof(Real));
  190.       }
  191.  
  192.       A->base = RENEW(A->base,new_size,Real);
  193.       if ( ! A->base )
  194.     error(E_MEM,"m_resize");
  195.       A->max_size = new_size;
  196.    }
  197.    
  198.    /* now set up A->me[i] */
  199.    for ( i = 0; i < new_m; i++ )
  200.      A->me[i] = &(A->base[i*new_n]);
  201.    
  202.    /* now shift data in matrix */
  203.    if ( old_n > new_n )
  204.    {
  205.       for ( i = 1; i < min(old_m,new_m); i++ )
  206.     MEM_COPY((char *)&(A->base[i*old_n]),
  207.          (char *)&(A->base[i*new_n]),
  208.          sizeof(Real)*new_n);
  209.    }
  210.    else if ( old_n < new_n )
  211.    {
  212.       for ( i = (int)(min(old_m,new_m))-1; i > 0; i-- )
  213.       {   /* copy & then zero extra space */
  214.      MEM_COPY((char *)&(A->base[i*old_n]),
  215.           (char *)&(A->base[i*new_n]),
  216.           sizeof(Real)*old_n);
  217.      __zero__(&(A->base[i*new_n+old_n]),(new_n-old_n));
  218.       }
  219.       __zero__(&(A->base[old_n]),(new_n-old_n));
  220.       A->max_n = new_n;
  221.    }
  222.    /* zero out the new rows.. */
  223.    for ( i = old_m; i < new_m; i++ )
  224.      __zero__(&(A->base[i*new_n]),new_n);
  225. #else
  226.    if ( A->max_n < new_n )
  227.    {
  228.       Real    *tmp;
  229.       
  230.       for ( i = 0; i < A->max_m; i++ )
  231.       {
  232.      if (mem_info_is_on()) {
  233.         mem_bytes(TYPE_MAT,A->max_n*sizeof(Real),
  234.              new_max_n*sizeof(Real));
  235.      }    
  236.  
  237.      if ( (tmp = RENEW(A->me[i],new_max_n,Real)) == NULL )
  238.        error(E_MEM,"m_resize");
  239.      else {    
  240.         A->me[i] = tmp;
  241.      }
  242.       }
  243.       for ( i = A->max_m; i < new_max_m; i++ )
  244.       {
  245.      if ( (tmp = NEW_A(new_max_n,Real)) == NULL )
  246.        error(E_MEM,"m_resize");
  247.      else {
  248.         A->me[i] = tmp;
  249.  
  250.         if (mem_info_is_on()) {
  251.            mem_bytes(TYPE_MAT,0,new_max_n*sizeof(Real));
  252.         }        
  253.      }
  254.       }
  255.    }
  256.    else if ( A->max_m < new_m )
  257.    {
  258.       for ( i = A->max_m; i < new_m; i++ ) 
  259.     if ( (A->me[i] = NEW_A(new_max_n,Real)) == NULL )
  260.       error(E_MEM,"m_resize");
  261.     else if (mem_info_is_on()) {
  262.        mem_bytes(TYPE_MAT,0,new_max_n*sizeof(Real));
  263.     }
  264.       
  265.    }
  266.    
  267.    if ( old_n < new_n )
  268.    {
  269.       for ( i = 0; i < old_m; i++ )
  270.     __zero__(&(A->me[i][old_n]),new_n-old_n);
  271.    }
  272.    
  273.    /* zero out the new rows.. */
  274.    for ( i = old_m; i < new_m; i++ )
  275.      __zero__(A->me[i],new_n);
  276. #endif
  277.    
  278.    A->max_m = new_max_m;
  279.    A->max_n = new_max_n;
  280.    A->max_size = A->max_m*A->max_n;
  281.    A->m = new_m;    A->n = new_n;
  282.    
  283.    return A;
  284. }
  285.  
  286. /* px_resize -- returns the permutation px with size new_size
  287.    -- px is set to the identity permutation */
  288. PERM    *px_resize(px,new_size)
  289. PERM    *px;
  290. int    new_size;
  291. {
  292.    int    i;
  293.    
  294.    if (new_size < 0)
  295.      error(E_NEG,"px_resize");
  296.  
  297.    if ( ! px )
  298.      return px_get(new_size);
  299.    
  300.    /* nothing is changed */
  301.    if (new_size == px->size)
  302.      return px;
  303.  
  304.    if ( new_size > px->max_size )
  305.    {
  306.       if (mem_info_is_on()) {
  307.      mem_bytes(TYPE_PERM,px->max_size*sizeof(u_int),
  308.               new_size*sizeof(u_int));
  309.       }
  310.       px->pe = RENEW(px->pe,new_size,u_int);
  311.       if ( ! px->pe )
  312.     error(E_MEM,"px_resize");
  313.       px->max_size = new_size;
  314.    }
  315.    if ( px->size <= new_size )
  316.      /* extend permutation */
  317.      for ( i = px->size; i < new_size; i++ )
  318.        px->pe[i] = i;
  319.    else
  320.      for ( i = 0; i < new_size; i++ )
  321.        px->pe[i] = i;
  322.    
  323.    px->size = new_size;
  324.    
  325.    return px;
  326. }
  327.  
  328. /* v_resize -- returns the vector x with dim new_dim
  329.    -- x is set to the zero vector */
  330. VEC    *v_resize(x,new_dim)
  331. VEC    *x;
  332. int    new_dim;
  333. {
  334.    
  335.    if (new_dim < 0)
  336.      error(E_NEG,"v_resize");
  337.  
  338.    if ( ! x )
  339.      return v_get(new_dim);
  340.  
  341.    /* nothing is changed */
  342.    if (new_dim == x->dim)
  343.      return x;
  344.  
  345.    if ( x->max_dim == 0 )    /* assume that it's from sub_vec */
  346.      return v_get(new_dim);
  347.    
  348.    if ( new_dim > x->max_dim )
  349.    {
  350.       if (mem_info_is_on()) { 
  351.      mem_bytes(TYPE_VEC,x->max_dim*sizeof(Real),
  352.              new_dim*sizeof(Real));
  353.       }
  354.  
  355.       x->ve = RENEW(x->ve,new_dim,Real);
  356.       if ( ! x->ve )
  357.     error(E_MEM,"v_resize");
  358.       x->max_dim = new_dim;
  359.    }
  360.    
  361.    if ( new_dim > x->dim )
  362.      __zero__(&(x->ve[x->dim]),new_dim - x->dim);
  363.    x->dim = new_dim;
  364.    
  365.    return x;
  366. }
  367.  
  368.  
  369.  
  370.  
  371. /* Varying number of arguments */
  372. /* other functions of this type are in sparse.c and zmemory.c */
  373.  
  374.  
  375.  
  376. #ifdef ANSI_C
  377.  
  378.  
  379. /* To allocate memory to many arguments. 
  380.    The function should be called:
  381.    v_get_vars(dim,&x,&y,&z,...,NULL);
  382.    where 
  383.      int dim;
  384.      VEC *x, *y, *z,...;
  385.      The last argument should be NULL ! 
  386.      dim is the length of vectors x,y,z,...
  387.      returned value is equal to the number of alloegister Real    *dp1, *dp2, *out;
  388. register int    len;
  389. {
  390.     register int    i;
  391.     for ( i = 0; i < len; i++ )
  392.     out[i] = dp1[i] + dp2[i];
  393. }
  394.  
  395. /* __sub__ -- subtract arrays c.f. v_sub() */
  396. void    __sub__(dp1,dp2,out,len)
  397. register Real    *dp1, *dp2, *out;
  398. register int    len;
  399. {
  400.     register int    i;
  401.     for ( i = 0; i < len; i++ )
  402.     out[i] = dp1[i] - dp2[i];
  403. }
  404.  
  405. /* __zero__ -- zeros an array of floating point numbers */
  406. void    __zero__(dp,len)
  407. register Real    *dp;
  408. register int    len;
  409. {
  410. #ifdef CHAR0ISDBL0
  411.     /* if a floating point zero is equivalent to a string of nulls */
  412.     MEM_ZERO((char *)dp,len*sizeof(Real));
  413. #else
  414.     /* else, need to zero the array entry by entry */
  415.     int    i;
  416.     for ( i = 0; i < len; i++ )
  417.     dp[i] = 0.0;
  418. #endif
  419. }
  420.  
  421. FileDataŵmatlabÇEÿÿÿø☓?]⇧
  422. /**************************************************************************
  423. **
  424. **