home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / meschach / !Meschach / c / version < prev    next >
Encoding:
Text File  |  1994-03-23  |  2.5 KB  |  96 lines

  1. D *mat;
  2.   int i,j,k;
  3.  
  4.   /* check if arguments are positive */
  5.   if (l <= 0 || m <= 0 || n <= 0)
  6.     error(E_NEG,"m3d_get");
  7.  
  8.     /* new structure */
  9.   if ((mat = NEW(MAT3D)) == (MAT3D *)NULL)
  10.     error(E_MEM,"m3d_get");
  11.   else if (mem_info_is_on()) {
  12.     /* record how many bytes is allocated */
  13.     mem_bytes_list(TYPE_MAT3D,0,sizeof(MAT3D),M3D_LIST);
  14.     /* record a new allocated variable */
  15.     mem_numvar_list(TYPE_MAT3D,1,M3D_LIST);
  16.   }
  17.  
  18.   mat->l = mat->max_l = l;
  19.   mat->m = mat->max_m = m;
  20.   mat->n = mat->max_n = n;
  21.  
  22.     /* allocate memory for 3D array */
  23.   if ((mat->base = NEW_A(l*m*n,Real)) == (Real *)NULL) 
  24.     error(E_MEM,"m3d_get");
  25.   else if (mem_info_is_on())
  26.     mem_bytes_list(TYPE_MAT3D,0,l*m*n*sizeof(Real),M3D_LIST);
  27.  
  28.     /* allocate memory for 2D pointers */
  29.   if ((mat->me2d = NEW_A(l*m,Real *)) == (Real **)NULL)
  30.     error(E_MEM,"m3d_get");
  31.   else if (mem_info_is_on())
  32.     mem_bytes_list(TYPE_MAT3D,0,l*m*sizeof(Real *),M3D_LIST);      
  33.  
  34.     /* allocate  memory for 1D pointers */
  35.   if ((mat->me = NEW_A(l,Real **)) == (Real ***)NULL)
  36.     error(E_MEM,"m3d_get");
  37.   else if (mem_info_is_on())
  38.     mem_bytes_list(TYPE_MAT3D,0,l*sizeof(Real **),M3D_LIST);
  39.  
  40.       /* pointers to 2D matrices */
  41.   for (i=0,k=0; i < l; i++)
  42.     for (j=0; j < m; j++)
  43.       mat->me2d[k++] = &mat->base[(i*m+j)*n];
  44.  
  45.        /* pointers to rows */
  46.   for (i=0; i < l; i++)
  47.     mat->me[i] = &mat->me2d[i*m];
  48.  
  49.   return mat;
  50. }
  51.  
  52.  
  53. /* deallocate a variable of type MAT3D */
  54.  
  55. int m3d_free(mat)
  56. MAT3D *mat;
  57. {
  58.        /* do not try to deallocate the NULL pointer */
  59.   if (mat == (MAT3D *)NULL)
  60.     return -1;
  61.     
  62.       /* first deallocate base */
  63.   if (mat->base != (Real *)NULL) {
  64.     if (mem_info_is_on())
  65.     /* record how many bytes is deallocated */
  66.       mem_bytes_list(TYPE_MAT3D,mat->max_l*mat->max_m*mat->max_n*sizeof(Real),
  67.              0,M3D_LIST);
  68.     free((char *)mat->base);
  69.   }
  70.  
  71.      /* deallocate array of 2D pointers */
  72.   if (mat->me2d != (Real **)NULL) {
  73.     if (mem_info_is_on())
  74.     /* record how many bytes is deallocated */
  75.       mem_bytes_list(TYPE_MAT3D,mat->max_l*mat->max_m*sizeof(Real *),
  76.              0,M3D_LIST);
  77.     free((char *)mat->me2d);
  78.   }
  79.  
  80.      /* deallocate array of 1D pointers */
  81.   if (mat->me != (Real ***)NULL) {
  82.     if (mem_info_is_on())
  83.     /* record how many bytes is deallocated */
  84.       mem_bytes_list(TYPE_MAT3D,mat->max_l*sizeof(Real **),0,M3D_LIST);
  85.     free((char *)mat->me);
  86.   }
  87.  
  88.     /* deallocate  MAT3D structure */
  89.   if (mem_info_is_on()) {
  90.     mem_bytes_list(TYPE_MAT3D,sizeof(MAT3D),0,M3D_LIST);
  91.     mem_numvar_list(TYPE_MAT3D,-1,M3D_LIST);
  92.   }
  93.   free((char *)mat);
  94.  
  95.   return 0;
  96.