1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #include "mrilib.h"
8 #include "thd.h"
9 
10 /*---------------------------------------------------------------
11   erase the insides of a diskptr from the earth
12 -----------------------------------------------------------------*/
13 
THD_delete_diskptr(THD_diskptr * dkptr)14 void THD_delete_diskptr( THD_diskptr *dkptr )
15 {
16    if( ! ISVALID_DISKPTR(dkptr) ) return ;
17 }
18 
19 /*---------------------------------------------------------------
20   erase the insides of a datablock from the earth
21 -----------------------------------------------------------------*/
22 
THD_delete_datablock(THD_datablock * dblk)23 void THD_delete_datablock( THD_datablock *dblk )
24 {
25    int ibr ;
26 
27 ENTRY("THD_delete_datablock") ;
28 
29    if( ! ISVALID_DATABLOCK(dblk) ) EXRETURN ;
30 
31    /** free the actual brick data (method depends on how it is stored) **/
32 
33    dblk->locked = 0 ;  /* Halloween 2014 */
34 
35    if( dblk->brick != NULL ){
36       dblk->locked = 0 ;
37 
38       switch( dblk->malloc_type ){
39 
40          default:
41             STATUS("count bricks") ;
42             ibr = THD_count_databricks( dblk ) ;
43             if( ibr > 0 )
44                fprintf(stderr,
45                 "** attempt to delete non-NULL unknown type of datablock **\n");
46 
47             if( dblk->brick != NULL ) FREE_IMARR( dblk->brick ) ;
48          break ;
49 
50          case DATABLOCK_MEM_MALLOC:
51          case DATABLOCK_MEM_MMAP:
52 STATUS("call purge") ;
53             THD_purge_datablock( dblk , dblk->malloc_type ) ;
54 STATUS("destroy imarr") ;
55             DESTROY_IMARR( dblk->brick ) ;
56          break ;
57 
58          case DATABLOCK_MEM_SHARED:   /* 02 May 2003 */
59            /* ??? */
60          break ;
61       }
62    }
63 
64    /** free the other information **/
65 
66 STATUS("free brick_ stuff") ;
67 
68    myRwcFree( dblk->brick_fac ) ;
69    myRwcFree( dblk->brick_bytes ) ;
70 
71    /** 30 Nov 1997 **/
72 
73    if( dblk->brick_lab != NULL ){
74       for( ibr=0 ; ibr < dblk->nvals ; ibr++ ) myRwcFree( dblk->brick_lab[ibr] ) ;
75       myRwcFree( dblk->brick_lab ) ;
76    }
77 
78    if( dblk->brick_keywords != NULL ){
79       for( ibr=0 ; ibr < dblk->nvals ; ibr++ ) myRwcFree( dblk->brick_keywords[ibr] ) ;
80       myRwcFree( dblk->brick_keywords ) ;
81    }
82 
83    if( dblk->brick_statcode != NULL ) myRwcFree( dblk->brick_statcode ) ;
84    if( dblk->brick_stataux  != NULL ){
85       for( ibr=0 ; ibr < dblk->nvals ; ibr++ ) myRwcFree( dblk->brick_stataux[ibr] ) ;
86       myRwcFree( dblk->brick_stataux ) ;
87    }
88 
89    if( dblk->brick_fdrcurve != NULL ){ /* 23 Jan 2008 */
90      floatvec *fv ;
91      for( ibr=0 ; ibr < dblk->nvals ; ibr++ ){
92        fv = dblk->brick_fdrcurve[ibr] ; KILL_floatvec(fv) ;
93      }
94      free((void *)dblk->brick_fdrcurve) ;
95    }
96    if( dblk->brick_mdfcurve != NULL ){ /* 22 Oct 2008 */
97      floatvec *fv ;
98      for( ibr=0 ; ibr < dblk->nvals ; ibr++ ){
99        fv = dblk->brick_mdfcurve[ibr] ; KILL_floatvec(fv);
100      }
101      free((void *)dblk->brick_mdfcurve) ;
102    }
103 
104    if( DBLK_IS_MASTERED(dblk) ){       /* 11 Jan 1999 */
105       myRwcFree( dblk->master_ival ) ;
106       myRwcFree( dblk->master_bytes ) ;
107    }
108 
109    THD_delete_diskptr( dblk->diskptr ) ;
110 
111    if( dblk->nnodes > 0 && dblk->node_list ){    /* Jul 12 2006 [rickr] */
112       myRwcFree( dblk->node_list ) ;
113       dblk->nnodes = 0 ;
114    }
115 
116 STATUS("KILL_KILL") ;
117    KILL_KILL( dblk->kl ) ;
118 
119 STATUS("free attributes") ;
120    myRwcFree( dblk->atr ) ;    /* not on the kill list */
121 
122    EXRETURN ;
123 }
124 
125 /*-------------------------------------------------------------------
126    destroy a 3D dataset (possibly including files)
127 ---------------------------------------------------------------------*/
128 
THD_delete_3dim_dataset(THD_3dim_dataset * dset,RwcBoolean kill_files)129 void THD_delete_3dim_dataset( THD_3dim_dataset *dset, RwcBoolean kill_files )
130 {
131 ENTRY("THD_delete_3dim_dataset") ;
132 
133    if( ! ISVALID_3DIM_DATASET(dset) ) EXRETURN ;
134 
135    if( DSET_IS_MINC(dset)    ) kill_files = False ;  /* 29 Oct 2001 */
136    if( DSET_IS_VOLUMES(dset) ) kill_files = False ;  /* 20 Jun 2002 */
137    if( DSET_IS_ANALYZE(dset) ) kill_files = False ;  /* 27 Aug 2002 */
138    if( DSET_IS_1D(dset)      ) kill_files = False ;
139    if( DSET_IS_3D(dset)      ) kill_files = False ;
140    if( DSET_IS_CTFMRI(dset)  ) kill_files = False ;
141    if( DSET_IS_CTFSAM(dset)  ) kill_files = False ;
142    if( DSET_IS_NIFTI(dset)   ) kill_files = False ;  /* 28 Aug 2003 */
143    if( DSET_IS_NIML(dset)    ) kill_files = False ;  /* 12 Jul 2006 [rickr] */
144    if( DSET_IS_NI_SURF_DSET (dset) ) kill_files = False ;
145    if( DSET_IS_GIFTI (dset)  ) kill_files = False ;  /* 13 Feb 2008 [rickr] */
146 
147    if( kill_files ){
148       THD_diskptr *dkptr = dset->dblk->diskptr ;
149 
150 STATUS("killing files") ;
151       unlink( dkptr->header_name ) ;
152       COMPRESS_unlink(dkptr->brick_name) ;
153    }
154 
155 #ifdef ALLOW_DATASET_VLIST
156 STATUS("destroy vlist") ;
157    DESTROY_VLIST(dset->pts) ;
158 #endif
159 
160    if( ISVALID_TIMEAXIS(dset->taxis) ){
161 STATUS("destroy taxis") ;
162       myRwcFree( dset->taxis->toff_sl ) ;
163       myRwcFree( dset->taxis ) ;
164    }
165 
166    THD_delete_datablock( dset->dblk ) ;
167 
168    /* EDIT_empty_copy() is only vox_warp not in KILL_list, add it there */
169    /*                                                5 Mar 2008 [rickr] */
170    /* myRwcFree( dset->vox_warp ) ;  * 23 Sep 2008                       */
171 
172    if (dset->Label_Dtable) {  /* ZSS Feb 26 2010 */
173       destroy_Dtable(dset->Label_Dtable); dset->Label_Dtable=NULL;
174    }
175 
176 STATUS("KILL_KILL") ;
177    KILL_KILL( dset->kl ) ;
178 
179    EXRETURN ;
180 }
181