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 static int native_order = -1 ;
11 static int no_mmap      = -1 ;
12 
THD_force_malloc_type(THD_datablock * blk,int mem_type)13 void THD_force_malloc_type( THD_datablock *blk , int mem_type )
14 {
15    int new_type ;
16 
17    no_mmap = AFNI_yesenv("AFNI_NOMMAP") ;
18 
19    if( native_order < 0 ) native_order = mri_short_order() ;
20 
21    /*-- sanity checks --*/
22 
23    if( ! ISVALID_DATABLOCK(blk) ) return ;
24 
25    if( mem_type == DATABLOCK_MEM_ANY ){  /* 14 Oct 1996 */
26 #if MMAP_THRESHOLD > 0
27       new_type = (blk->total_bytes > MMAP_THRESHOLD)
28                     ? DATABLOCK_MEM_MMAP : DATABLOCK_MEM_MALLOC ;
29 #else
30       new_type = DATABLOCK_MEM_MALLOC ;
31 #endif
32 
33    } else {
34       new_type = mem_type ;
35    }
36 
37    if( COMPRESS_filecode(blk->diskptr->brick_name) >= 0 && new_type == DATABLOCK_MEM_MMAP )
38       new_type = DATABLOCK_MEM_MALLOC ;
39 
40    /* 25 April 1998: byte order issues */
41 
42    if( blk->diskptr->byte_order <= 0 )
43       blk->diskptr->byte_order = native_order ;
44    else if( blk->diskptr->byte_order != native_order && new_type == DATABLOCK_MEM_MMAP )
45       new_type = DATABLOCK_MEM_MALLOC ;
46 
47    if( no_mmap && new_type == DATABLOCK_MEM_MMAP )
48       new_type = DATABLOCK_MEM_MALLOC ;
49 
50    if( DBLK_LOCKED(blk) )                /* 22 Mar 2001 */
51       new_type = DATABLOCK_MEM_MALLOC ;
52 
53    if( blk->malloc_type == new_type ) return ;
54    (void) THD_purge_datablock( blk , blk->malloc_type ) ;
55    blk->malloc_type = new_type ;
56 
57    DBLK_mmapfix(blk) ;  /* 28 Mar 2005 */
58    return ;
59 }
60