Sophie

Sophie

distrib > Mandriva > 9.0 > i586 > by-pkgid > d67485fb8ce60f8952179bbde3b5d022 > files > 108

libgdal0-devel-1.1.7-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta name="robots" content="noindex">
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>gdalrasterblock.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body bgcolor="#ffffff">
<!-- Generated by Doxygen 1.2.3-20001105 on Thu Mar 28 09:47:30 2002 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; <a class="qindex" href="pages.html">Related Pages</a> &nbsp; </center>
<hr><h1>gdalrasterblock.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font>
00002 <font class="comment"> * Copyright (c) 1998, Frank Warmerdam</font>
00003 <font class="comment"> *</font>
00004 <font class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</font>
00005 <font class="comment"> * copy of this software and associated documentation files (the "Software"),</font>
00006 <font class="comment"> * to deal in the Software without restriction, including without limitation</font>
00007 <font class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</font>
00008 <font class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</font>
00009 <font class="comment"> * Software is furnished to do so, subject to the following conditions:</font>
00010 <font class="comment"> *</font>
00011 <font class="comment"> * The above copyright notice and this permission notice shall be included</font>
00012 <font class="comment"> * in all copies or substantial portions of the Software.</font>
00013 <font class="comment"> *</font>
00014 <font class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS</font>
00015 <font class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</font>
00016 <font class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</font>
00017 <font class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</font>
00018 <font class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</font>
00019 <font class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</font>
00020 <font class="comment"> * DEALINGS IN THE SOFTWARE.</font>
00021 <font class="comment"> ******************************************************************************</font>
00022 <font class="comment"> *</font>
00023 <font class="comment"> * gdalrasterblock.cpp</font>
00024 <font class="comment"> *</font>
00025 <font class="comment"> * The GDALRasterBlock class.</font>
00026 <font class="comment"> *</font>
00027 <font class="comment"> * </font>
00028 <font class="comment"> * $Log: gdalrasterblock_cpp-source.html,v $
00028 <font class="comment"> * Revision 1.10  2002/04/16 13:11:49  warmerda
00028 <font class="comment"> * updated
00028 <font class="comment"> *</font>
00029 <font class="comment"> * Revision 1.7  2001/09/27 16:33:41  warmerda</font>
00030 <font class="comment"> * fixed problems with blocks larger than 2GB/8</font>
00031 <font class="comment"> *</font>
00032 <font class="comment"> * Revision 1.6  2001/07/18 04:04:30  warmerda</font>
00033 <font class="comment"> * added CPL_CVSID</font>
00034 <font class="comment"> *</font>
00035 <font class="comment"> * Revision 1.5  2001/06/22 21:00:06  warmerda</font>
00036 <font class="comment"> * fixed support for caching override by environment variable</font>
00037 <font class="comment"> *</font>
00038 <font class="comment"> * Revision 1.4  2001/06/22 20:09:13  warmerda</font>
00039 <font class="comment"> * added GDAL_CACHEMAX environment variable support</font>
00040 <font class="comment"> *</font>
00041 <font class="comment"> * Revision 1.3  2000/03/31 13:42:49  warmerda</font>
00042 <font class="comment"> * added debugging code</font>
00043 <font class="comment"> *</font>
00044 <font class="comment"> * Revision 1.2  2000/03/24 00:09:05  warmerda</font>
00045 <font class="comment"> * rewrote cache management</font>
00046 <font class="comment"> *</font>
00047 <font class="comment"> * Revision 1.1  1998/12/31 18:52:58  warmerda</font>
00048 <font class="comment"> * New</font>
00049 <font class="comment"> *</font>
00050 <font class="comment"> */</font>
00051 
00052 <font class="preprocessor">#include "gdal_priv.h"</font>
00053 
00054 CPL_CVSID(<font class="stringliteral">"$Id: gdalrasterblock_cpp-source.html,v 1.10 2002/04/16 13:11:49 warmerda Exp $"</font>);
00055 
00056 <font class="keyword">static</font> <font class="keywordtype">int</font> nTileAgeTicker = 0; 
00057 <font class="keyword">static</font> <font class="keywordtype">int</font> bCacheMaxInitialized = FALSE;
00058 <font class="keyword">static</font> <font class="keywordtype">int</font> nCacheMax = 5 * 1024*1024;
00059 <font class="keyword">static</font> <font class="keywordtype">int</font> nCacheUsed = 0;
00060 
00061 <font class="keyword">static</font> <a class="code" href="class_GDALRasterBlock.html">GDALRasterBlock</a>   *poOldest = NULL;    <font class="comment">/* tail */</font>
00062 <font class="keyword">static</font> <a class="code" href="class_GDALRasterBlock.html">GDALRasterBlock</a>   *poNewest = NULL;    <font class="comment">/* head */</font>
00063 
00064 
00065 <font class="comment">/************************************************************************/</font>
00066 <font class="comment">/*                          GDALSetCacheMax()                           */</font>
00067 <font class="comment">/************************************************************************/</font>
00068 
00069 <font class="keywordtype">void</font> GDALSetCacheMax( <font class="keywordtype">int</font> nNewSize )<font class="keyword"></font>
00070 <font class="keyword"></font>
00071 <font class="keyword"></font>{
00072     nCacheMax = nNewSize;
00073     <font class="keywordflow">if</font>( nCacheUsed &gt; nCacheMax )
00074         GDALFlushCacheBlock();
00075 }
00076 
00077 <font class="comment">/************************************************************************/</font>
00078 <font class="comment">/*                          GDALGetCacheMax()                           */</font>
00079 <font class="comment">/************************************************************************/</font>
00080 
00081 <font class="keywordtype">int</font> GDALGetCacheMax()<font class="keyword"></font>
00082 <font class="keyword"></font>{
00083     <font class="keywordflow">if</font>( !bCacheMaxInitialized )
00084     {
00085         <font class="keywordflow">if</font>( getenv(<font class="stringliteral">"GDAL_CACHEMAX"</font>) != NULL )
00086         {
00087             nCacheMax = atoi(getenv(<font class="stringliteral">"GDAL_CACHEMAX"</font>));
00088             <font class="keywordflow">if</font>( nCacheMax &lt; 1000 )
00089                 nCacheMax *= 1024 * 1024;
00090         }
00091         bCacheMaxInitialized = TRUE;
00092     }
00093     
00094     <font class="keywordflow">return</font> nCacheMax;
00095 }
00096 
00097 <font class="comment">/************************************************************************/</font>
00098 <font class="comment">/*                          GDALGetCacheUsed()                          */</font>
00099 <font class="comment">/************************************************************************/</font>
00100 
00101 <font class="keywordtype">int</font> GDALGetCacheUsed()<font class="keyword"></font>
00102 <font class="keyword"></font>{
00103     <font class="keywordflow">return</font> nCacheUsed;
00104 }
00105 
00106 <font class="comment">/************************************************************************/</font>
00107 <font class="comment">/*                        GDALFlushCacheBlock()                         */</font>
00108 <font class="comment">/*                                                                      */</font>
00109 <font class="comment">/*      The workhorse of cache management!                              */</font>
00110 <font class="comment">/************************************************************************/</font>
00111 
00112 <font class="keywordtype">int</font> GDALFlushCacheBlock()<font class="keyword"></font>
00113 <font class="keyword"></font>
00114 <font class="keyword"></font>{
00115     <font class="keywordflow">if</font>( poOldest == NULL )
00116         <font class="keywordflow">return</font> FALSE;
00117     poOldest-&gt;GetBand()-&gt;FlushBlock( poOldest-&gt;GetXOff(), 
00118                                      poOldest-&gt;GetYOff() );
00119 
00120     <font class="keywordflow">return</font> TRUE;
00121 }
00122 
00123 <font class="comment">/************************************************************************/</font>
00124 <font class="comment">/*                           GDALRasterBand()                           */</font>
00125 <font class="comment">/************************************************************************/</font>
00126 
00127 GDALRasterBlock::GDALRasterBlock( <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> *poBandIn, 
00128                                   <font class="keywordtype">int</font> nXOffIn, <font class="keywordtype">int</font> nYOffIn )<font class="keyword"></font>
00129 <font class="keyword"></font>
00130 <font class="keyword"></font>{
00131     poBand = poBandIn;
00132 
00133     poBand-&gt;<a class="code" href="class_GDALRasterBand.html#a6">GetBlockSize</a>( &amp;nXSize, &amp;nYSize );
00134     eType = poBand-&gt;<a class="code" href="class_GDALRasterBand.html#a5">GetRasterDataType</a>();
00135     pData = NULL;
00136     bDirty = FALSE;
00137 
00138     poNext = poPrevious = NULL;
00139 
00140     nXOff = nXOffIn;
00141     nYOff = nYOffIn;
00142 }
00143 
00144 <font class="comment">/************************************************************************/</font>
00145 <font class="comment">/*                          ~GDALRasterBlock()                          */</font>
00146 <font class="comment">/************************************************************************/</font>
00147 
00148 GDALRasterBlock::~GDALRasterBlock()<font class="keyword"></font>
00149 <font class="keyword"></font>
00150 <font class="keyword"></font>{
00151     <font class="keywordflow">if</font>( pData != NULL )
00152     {
00153         <font class="keywordtype">int</font> nSizeInBytes;
00154 
00155         VSIFree( pData );
00156 
00157         nSizeInBytes = (nXSize * nYSize * GDALGetDataTypeSize(eType)+7)/8;
00158         nCacheUsed -= nSizeInBytes;
00159     }
00160 
00161     <font class="keywordflow">if</font>( poOldest == <font class="keyword">this</font> )
00162         poOldest = poPrevious;
00163 
00164     <font class="keywordflow">if</font>( poNewest == <font class="keyword">this</font> )
00165     {
00166         poNewest = poNext;
00167     }
00168 
00169     <font class="keywordflow">if</font>( poPrevious != NULL )
00170         poPrevious-&gt;poNext = poNext;
00171 
00172     <font class="keywordflow">if</font>( poNext != NULL )
00173         poNext-&gt;poPrevious = poPrevious;
00174 
00175 <font class="preprocessor">#ifdef ENABLE_DEBUG</font>
00176 <font class="preprocessor"></font>    Verify();
00177 <font class="preprocessor">#endif</font>
00178 <font class="preprocessor"></font>
00179     nAge = -1;
00180 }
00181 
00182 <font class="comment">/************************************************************************/</font>
00183 <font class="comment">/*                               Verify()                               */</font>
00184 <font class="comment">/************************************************************************/</font>
00185 
00186 <font class="keywordtype">void</font> GDALRasterBlock::Verify()<font class="keyword"></font>
00187 <font class="keyword"></font>
00188 <font class="keyword"></font>{
00189     CPLAssert( (poNewest == NULL &amp;&amp; poOldest == NULL)
00190                || (poNewest != NULL &amp;&amp; poOldest != NULL) );
00191 
00192     <font class="keywordflow">if</font>( poNewest != NULL )
00193     {
00194         CPLAssert( poNewest-&gt;poPrevious == NULL );
00195         CPLAssert( poOldest-&gt;poNext == NULL );
00196         
00197 
00198         <font class="keywordflow">for</font>( <a class="code" href="class_GDALRasterBlock.html">GDALRasterBlock</a> *poBlock = poNewest; 
00199              poBlock != NULL;
00200              poBlock = poBlock-&gt;poNext )
00201         {
00202             <font class="keywordflow">if</font>( poBlock-&gt;poPrevious )
00203             {
00204                 CPLAssert( poBlock-&gt;poPrevious-&gt;poNext == poBlock );
00205             }
00206 
00207             <font class="keywordflow">if</font>( poBlock-&gt;poNext )
00208             {
00209                 CPLAssert( poBlock-&gt;poNext-&gt;poPrevious == poBlock );
00210             }
00211         }
00212     }
00213 }
00214 
00215 <font class="comment">/************************************************************************/</font>
00216 <font class="comment">/*                               Write()                                */</font>
00217 <font class="comment">/************************************************************************/</font>
00218 
00219 CPLErr GDALRasterBlock::Write()<font class="keyword"></font>
00220 <font class="keyword"></font>
00221 <font class="keyword"></font>{
00222     <font class="keywordflow">if</font>( !GetDirty() )
00223         <font class="keywordflow">return</font> CE_None;
00224 
00225     <font class="keywordflow">if</font>( poBand == NULL )
00226         <font class="keywordflow">return</font> CE_Failure;
00227 
00228     MarkClean();
00229 
00230     <font class="keywordflow">return</font> poBand-&gt;<a class="code" href="class_GDALRasterBand.html#b1">IWriteBlock</a>( nXOff, nYOff, pData );
00231 }
00232 
00233 <font class="comment">/************************************************************************/</font>
00234 <font class="comment">/*                               Touch()                                */</font>
00235 <font class="comment">/************************************************************************/</font>
00236 
00237 <font class="keywordtype">void</font> GDALRasterBlock::Touch()<font class="keyword"></font>
00238 <font class="keyword"></font>
00239 <font class="keyword"></font>{
00240     nAge = nTileAgeTicker++;
00241 
00242     <font class="keywordflow">if</font>( poNewest == <font class="keyword">this</font> )
00243         <font class="keywordflow">return</font>;
00244 
00245     <font class="keywordflow">if</font>( poOldest == <font class="keyword">this</font> )
00246         poOldest = this-&gt;poPrevious;
00247     
00248     <font class="keywordflow">if</font>( poPrevious != NULL )
00249         poPrevious-&gt;poNext = poNext;
00250 
00251     <font class="keywordflow">if</font>( poNext != NULL )
00252         poNext-&gt;poPrevious = poPrevious;
00253 
00254     poPrevious = NULL;
00255     poNext = poNewest;
00256 
00257     <font class="keywordflow">if</font>( poNewest != NULL )
00258     {
00259         CPLAssert( poNewest-&gt;poPrevious == NULL );
00260         poNewest-&gt;poPrevious = <font class="keyword">this</font>;
00261     }
00262     poNewest = <font class="keyword">this</font>;
00263     
00264     <font class="keywordflow">if</font>( poOldest == NULL )
00265     {
00266         CPLAssert( poPrevious == NULL &amp;&amp; poNext == NULL );
00267         poOldest = <font class="keyword">this</font>;
00268     }
00269 <font class="preprocessor">#ifdef ENABLE_DEBUG</font>
00270 <font class="preprocessor"></font>    Verify();
00271 <font class="preprocessor">#endif</font>
00272 <font class="preprocessor"></font>}
00273 
00274 <font class="comment">/************************************************************************/</font>
00275 <font class="comment">/*                            Internalize()                             */</font>
00276 <font class="comment">/************************************************************************/</font>
00277 
00278 CPLErr GDALRasterBlock::Internalize()<font class="keyword"></font>
00279 <font class="keyword"></font>
00280 <font class="keyword"></font>{
00281     <font class="keywordtype">void</font>        *pNewData;
00282     <font class="keywordtype">int</font>         nSizeInBytes;
00283     <font class="keywordtype">int</font>         nCurCacheMax = GDALGetCacheMax();
00284 
00285     nSizeInBytes = nXSize * nYSize * (GDALGetDataTypeSize(eType) / 8);
00286 
00287     pNewData = VSIMalloc( nSizeInBytes );
00288     <font class="keywordflow">if</font>( pNewData == NULL )
00289         <font class="keywordflow">return</font>( CE_Failure );
00290 
00291     <font class="keywordflow">if</font>( pData != NULL )
00292         memcpy( pNewData, pData, nSizeInBytes );
00293     
00294     pData = pNewData;
00295 
00296 <font class="comment">/* -------------------------------------------------------------------- */</font>
00297 <font class="comment">/*      Flush old blocks if we are nearing our memory limit.            */</font>
00298 <font class="comment">/* -------------------------------------------------------------------- */</font>
00299     nCacheUsed += nSizeInBytes;
00300     <font class="keywordflow">while</font>( nCacheUsed &gt; nCurCacheMax )
00301     {
00302         <font class="keywordtype">int</font> nOldCacheUsed = nCacheUsed;
00303 
00304         GDALFlushCacheBlock();
00305 
00306         <font class="keywordflow">if</font>( nCacheUsed == nOldCacheUsed )
00307         {
00308             <font class="keyword">static</font> <font class="keywordtype">int</font> bReported = FALSE;
00309 
00310             <font class="keywordflow">if</font>( !bReported )
00311             {
00312                 bReported = TRUE;
00313             }
00314             <font class="keywordflow">break</font>;
00315         }
00316     }
00317 
00318 <font class="comment">/* -------------------------------------------------------------------- */</font>
00319 <font class="comment">/*      Add this block to the list.                                     */</font>
00320 <font class="comment">/* -------------------------------------------------------------------- */</font>
00321     Touch();
00322     <font class="keywordflow">return</font>( CE_None );
00323 }
00324 
00325 <font class="comment">/************************************************************************/</font>
00326 <font class="comment">/*                             MarkDirty()                              */</font>
00327 <font class="comment">/************************************************************************/</font>
00328 
00329 <font class="keywordtype">void</font> GDALRasterBlock::MarkDirty()<font class="keyword"></font>
00330 <font class="keyword"></font>
00331 <font class="keyword"></font>{
00332     bDirty = TRUE;
00333 }
00334 
00335 
00336 <font class="comment">/************************************************************************/</font>
00337 <font class="comment">/*                             MarkClean()                              */</font>
00338 <font class="comment">/************************************************************************/</font>
00339 
00340 <font class="keywordtype">void</font> GDALRasterBlock::MarkClean()<font class="keyword"></font>
00341 <font class="keyword"></font>
00342 <font class="keyword"></font>{
00343     bDirty = FALSE;
00344 }
00345 
00346 
</div></pre><hr><address><small>Generated at Thu Mar 28 09:47:30 2002 for GDAL by
<a href="http://www.stack.nl/~dimitri/doxygen/index.html">
<img src="doxygen.gif" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.3-20001105 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>,
 &copy;&nbsp;1997-2000</small></address>
</body>
</html>