home *** CD-ROM | disk | FTP | other *** search
- D *mat;
- int i,j,k;
-
- /* check if arguments are positive */
- if (l <= 0 || m <= 0 || n <= 0)
- error(E_NEG,"m3d_get");
-
- /* new structure */
- if ((mat = NEW(MAT3D)) == (MAT3D *)NULL)
- error(E_MEM,"m3d_get");
- else if (mem_info_is_on()) {
- /* record how many bytes is allocated */
- mem_bytes_list(TYPE_MAT3D,0,sizeof(MAT3D),M3D_LIST);
- /* record a new allocated variable */
- mem_numvar_list(TYPE_MAT3D,1,M3D_LIST);
- }
-
- mat->l = mat->max_l = l;
- mat->m = mat->max_m = m;
- mat->n = mat->max_n = n;
-
- /* allocate memory for 3D array */
- if ((mat->base = NEW_A(l*m*n,Real)) == (Real *)NULL)
- error(E_MEM,"m3d_get");
- else if (mem_info_is_on())
- mem_bytes_list(TYPE_MAT3D,0,l*m*n*sizeof(Real),M3D_LIST);
-
- /* allocate memory for 2D pointers */
- if ((mat->me2d = NEW_A(l*m,Real *)) == (Real **)NULL)
- error(E_MEM,"m3d_get");
- else if (mem_info_is_on())
- mem_bytes_list(TYPE_MAT3D,0,l*m*sizeof(Real *),M3D_LIST);
-
- /* allocate memory for 1D pointers */
- if ((mat->me = NEW_A(l,Real **)) == (Real ***)NULL)
- error(E_MEM,"m3d_get");
- else if (mem_info_is_on())
- mem_bytes_list(TYPE_MAT3D,0,l*sizeof(Real **),M3D_LIST);
-
- /* pointers to 2D matrices */
- for (i=0,k=0; i < l; i++)
- for (j=0; j < m; j++)
- mat->me2d[k++] = &mat->base[(i*m+j)*n];
-
- /* pointers to rows */
- for (i=0; i < l; i++)
- mat->me[i] = &mat->me2d[i*m];
-
- return mat;
- }
-
-
- /* deallocate a variable of type MAT3D */
-
- int m3d_free(mat)
- MAT3D *mat;
- {
- /* do not try to deallocate the NULL pointer */
- if (mat == (MAT3D *)NULL)
- return -1;
-
- /* first deallocate base */
- if (mat->base != (Real *)NULL) {
- if (mem_info_is_on())
- /* record how many bytes is deallocated */
- mem_bytes_list(TYPE_MAT3D,mat->max_l*mat->max_m*mat->max_n*sizeof(Real),
- 0,M3D_LIST);
- free((char *)mat->base);
- }
-
- /* deallocate array of 2D pointers */
- if (mat->me2d != (Real **)NULL) {
- if (mem_info_is_on())
- /* record how many bytes is deallocated */
- mem_bytes_list(TYPE_MAT3D,mat->max_l*mat->max_m*sizeof(Real *),
- 0,M3D_LIST);
- free((char *)mat->me2d);
- }
-
- /* deallocate array of 1D pointers */
- if (mat->me != (Real ***)NULL) {
- if (mem_info_is_on())
- /* record how many bytes is deallocated */
- mem_bytes_list(TYPE_MAT3D,mat->max_l*sizeof(Real **),0,M3D_LIST);
- free((char *)mat->me);
- }
-
- /* deallocate MAT3D structure */
- if (mem_info_is_on()) {
- mem_bytes_list(TYPE_MAT3D,sizeof(MAT3D),0,M3D_LIST);
- mem_numvar_list(TYPE_MAT3D,-1,M3D_LIST);
- }
- free((char *)mat);
-
- return 0;
-