00001 /************************************************************************************ 00002 TerraLib - a library for developing GIS applications. 00003 Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio. 00004 00005 This code is part of the TerraLib library. 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 You should have received a copy of the GNU Lesser General Public 00012 License along with this library. 00013 00014 The authors reassure the license terms regarding the warranties. 00015 They specifically disclaim any warranties, including, but not limited to, 00016 the implied warranties of merchantability and fitness for a particular purpose. 00017 The library provided hereunder is on an "as is" basis, and the authors have no 00018 obligation to provide maintenance, support, updates, enhancements, or modifications. 00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct, 00020 indirect, special, incidental, or consequential damages arising out of the use 00021 of this library and its documentation. 00022 *************************************************************************************/ 00023 /*! \file TeRasterMemManager.h 00024 \brief This file contains a class that deals with a raster memory management 00025 */ 00026 00027 #ifndef TERASTERMEMMANAGER_H 00028 #define TERASTERMEMMANAGER_H 00029 00030 #include "TeSharedPtr.h" 00031 00032 #include <vector> 00033 00034 /** 00035 * @brief This class deals with a raster memory management. 00036 * @author Emiliano F. Castejon <castejon@dpi.inpe.br> 00037 */ 00038 class TL_DLL TeRasterMemManager { 00039 00040 public : 00041 00042 /** 00043 * @enum Memory policy. 00044 */ 00045 enum MemoryPolicy { 00046 /** 00047 * @brief Automatic memory policy ( Try to use RAM or DISK, 00048 * if there is no avaliable RAM ). 00049 */ 00050 AutoMemPol = 1, 00051 /** 00052 * @brief RAM memory policy. 00053 */ 00054 RAMMemPol = 2, 00055 /** 00056 * @brief Disk memory policy. 00057 */ 00058 DiskMemPol = 3 00059 }; 00060 00061 /** 00062 * @brief Default Constructor. 00063 */ 00064 TeRasterMemManager(); 00065 00066 /** 00067 * @brief Default Destructor 00068 */ 00069 ~TeRasterMemManager(); 00070 00071 /** 00072 * @brief Clear all data structures. 00073 */ 00074 void clear(); 00075 00076 /** 00077 * @brief Reset the instance following new raster parameters. 00078 * @param bands The number of bands (channels). 00079 * @param tilesPerBand The tiles inside each band. 00080 * @param tilesSizesVec The tile size (bytes) for each band. 00081 * @param memoryPolicy Memory policy. 00082 * @param maxMemPercentUsage The max amount of RAM memory percentage 00083 * to use when necessary - valid range: [0,100] suggested default:40. 00084 * @param maxMemPercentUsage The the maximum temp file size (bytes) 00085 * (sugested default:2GB=2ul * 1024ul * 1024ul * 1024ul). 00086 * @return true if OK, false on errors. 00087 */ 00088 bool reset( unsigned int bands, unsigned int tilesPerBand, 00089 const std::vector< unsigned int >& tilesSizesVec, 00090 MemoryPolicy memoryPolicy, unsigned char maxMemPercentUsage, 00091 unsigned long int maxTempFileSize ); 00092 00093 /** 00094 * @brief Returnas a pointer to a internal allocated tile. 00095 * @param band Band index. 00096 * @param tile Tile index. 00097 * @return a pointer to a internal allocated tile. 00098 * @note This pointer is only valid until the next call to this function. 00099 */ 00100 void* getTilePointer( const unsigned int& band, 00101 const unsigned int& tile ); 00102 00103 protected : 00104 00105 /** 00106 * @brief Disk tile data. 00107 */ 00108 class DiskTileData 00109 { 00110 public : 00111 FILE* filePtr_; 00112 unsigned long int fileOff_; 00113 unsigned int size_; 00114 00115 DiskTileData() : filePtr_( 0 ), fileOff_( 0 ), size_( 0 ) {}; 00116 00117 ~DiskTileData() {}; 00118 }; 00119 00120 /** 00121 * @typedef unsigned char TileDataT 00122 * Tile data type. 00123 */ 00124 typedef unsigned char TileDataT; 00125 00126 /** 00127 * @typedef TileDataT* TilePtrT 00128 * Tile pointer type. 00129 */ 00130 typedef TileDataT* TilePtrT; 00131 00132 /** 00133 * @typedef std::vector< TilePtrT > TilesPtrsVecT 00134 * Tiles pointers vector type. 00135 */ 00136 typedef std::vector< TilePtrT > TilesPtrsVecT; 00137 00138 /** 00139 * @typedef std::list< DiskTileData > 00140 * Disk tile data vector type. 00141 */ 00142 typedef std::vector< DiskTileData > 00143 DiskTileDataVecT; 00144 00145 /** 00146 * @typedef std::vector< std::pair< FILE*, unsigned int > > 00147 * Openend disk files data vector type. 00148 */ 00149 typedef std::vector< std::pair< FILE*, std::string > > 00150 OpenDiskFilesVecT; 00151 00152 /** 00153 * @brief the max amount of free memory to use when necessary 00154 * (default:50). 00155 */ 00156 unsigned char maxMemPercentUsage_; 00157 00158 /** 00159 * @brief The the maximum temp file size in bytes (default:2GB). 00160 */ 00161 unsigned long int maxTempFileSize_; 00162 00163 // Temp variables used by the getTilePointer method. 00164 TilePtrT getTilePointer_tilePtr_; 00165 unsigned int getTilePointer_reqTileIdx_; 00166 unsigned int getTilePointer_swapTileIdx_; 00167 00168 /** 00169 * @brief Tiles per band (default:0). 00170 */ 00171 unsigned int tilesPerBand_; 00172 00173 /** 00174 * @brief The number of bands (default:0). 00175 */ 00176 unsigned int bandsNmb_; 00177 00178 /** 00179 * @brief A vector of tiles sizes for each band. 00180 */ 00181 std::vector< unsigned int > bandsTileSizes_; 00182 00183 /** 00184 * @brief A vector of pointers to all allocated tiles. 00185 * @note Declared as a simple vector to optimize the 00186 * tile access. 00187 */ 00188 TilesPtrsVecT allTilesPtrsVec_; 00189 00190 /** 00191 * @brief Openend disk files data vector. 00192 */ 00193 OpenDiskFilesVecT openDiskFilesVec_; 00194 00195 /** 00196 * @brief Disk tiles data vector. 00197 */ 00198 DiskTileDataVecT diskTilesDataVec_; 00199 00200 /** 00201 * @brief The indexes inside allTilesPtrsVec_ of all RAM tiles. 00202 */ 00203 std::vector< unsigned int > ramTilesIndexesVec_; 00204 00205 /** 00206 * @brief The index inside ramTilesIndexesVec_ of the next RAM 00207 * tile index that will be swapped to disk when a disk tile 00208 * is required. 00209 */ 00210 unsigned int nextSwapTileRamTilesIndexesVecIdx_; 00211 00212 /** 00213 * @brief A pointer to the auxiliar tile used when swapping 00214 * data to/from disk. 00215 */ 00216 TilePtrT swapTilePtr_; 00217 00218 /** 00219 * @brief Allocate disk files . 00220 * @param tileSize The tile size. 00221 * @param tilesNmb The tiles number. 00222 * @param openDiskFilesVec The output vector with all the 00223 * created files info. 00224 * @param diskTilesData The output tiles info. 00225 * @return true if OK, false on errors. 00226 */ 00227 bool allocateDiskFiles( unsigned int tileSize, 00228 unsigned int tilesNmb, OpenDiskFilesVecT& openDiskFilesVec, 00229 DiskTileDataVecT& diskTilesData ); 00230 00231 /** 00232 * @brief Create a new disk temp file. 00233 * @param filename The file name. 00234 * @param size The file size. 00235 * @param fileptr The file pointer. 00236 * @return true if OK. false on errors. 00237 */ 00238 bool createNewDiskFile( unsigned long int size, 00239 std::string& filename, FILE** fileptr ); 00240 00241 private : 00242 00243 /** 00244 * @brief Alternative Constructor. 00245 * @param ext External reference. 00246 */ 00247 TeRasterMemManager( const TeRasterMemManager& ) {}; 00248 00249 /** 00250 * @brief =operator implementation. 00251 * @param ext External reference. 00252 */ 00253 const TeRasterMemManager& operator=( const TeRasterMemManager& ) 00254 { return *this; }; 00255 00256 }; 00257 00258 #endif 00259
1.5.3