stgcache.cxx (046d9d1f) | stgcache.cxx (297a844a) |
---|---|
1/************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance --- 53 unchanged lines hidden (view full) --- 62 63////////////////////////////// class StgPage ///////////////////////////// 64// This class implements buffer functionality. The cache will always return 65// a page buffer, even if a read fails. It is up to the caller to determine 66// the correctness of the I/O. 67 68StgPage::StgPage( StgCache* p, short n ) 69{ | 1/************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance --- 53 unchanged lines hidden (view full) --- 62 63////////////////////////////// class StgPage ///////////////////////////// 64// This class implements buffer functionality. The cache will always return 65// a page buffer, even if a read fails. It is up to the caller to determine 66// the correctness of the I/O. 67 68StgPage::StgPage( StgCache* p, short n ) 69{ |
70 OSL_ENSURE( n >= 512, "Unexpected page size is provided!" ); |
|
70 pCache = p; 71 nData = n; 72 bDirty = sal_False; 73 nPage = 0; 74 pData = new sal_uInt8[ nData ]; 75 pNext1 = 76 pNext2 = 77 pLast1 = --- 47 unchanged lines hidden (view full) --- 125{ 126 Clear(); 127 SetStrm( NULL, sal_False ); 128 delete (UsrStgPagePtr_Impl*)pLRUCache; 129} 130 131void StgCache::SetPhysPageSize( short n ) 132{ | 71 pCache = p; 72 nData = n; 73 bDirty = sal_False; 74 nPage = 0; 75 pData = new sal_uInt8[ nData ]; 76 pNext1 = 77 pNext2 = 78 pLast1 = --- 47 unchanged lines hidden (view full) --- 126{ 127 Clear(); 128 SetStrm( NULL, sal_False ); 129 delete (UsrStgPagePtr_Impl*)pLRUCache; 130} 131 132void StgCache::SetPhysPageSize( short n ) 133{ |
133 nPageSize = n; 134 sal_uLong nPos = pStrm->Tell(); 135 sal_uLong nFileSize = pStrm->Seek( STREAM_SEEK_TO_END ); 136 nPages = lcl_GetPageCount( nFileSize, nPageSize ); 137 pStrm->Seek( nPos ); | 134 OSL_ENSURE( n >= 512, "Unexpecte page size is provided!" ); 135 if ( n >= 512 ) 136 { 137 nPageSize = n; 138 sal_uLong nPos = pStrm->Tell(); 139 sal_uLong nFileSize = pStrm->Seek( STREAM_SEEK_TO_END ); 140 nPages = lcl_GetPageCount( nFileSize, nPageSize ); 141 pStrm->Seek( nPos ); 142 } |
138} 139 140// Create a new cache element 141// pCur points to this element 142 143StgPage* StgCache::Create( sal_Int32 nPg ) 144{ 145 StgPage* pElem = new StgPage( this, nPageSize ); --- 37 unchanged lines hidden (view full) --- 183 } 184 return pElem; 185} 186 187// Delete the given element 188 189void StgCache::Erase( StgPage* pElem ) 190{ | 143} 144 145// Create a new cache element 146// pCur points to this element 147 148StgPage* StgCache::Create( sal_Int32 nPg ) 149{ 150 StgPage* pElem = new StgPage( this, nPageSize ); --- 37 unchanged lines hidden (view full) --- 188 } 189 return pElem; 190} 191 192// Delete the given element 193 194void StgCache::Erase( StgPage* pElem ) 195{ |
191 //remove from LRU 192 pElem->pNext1->pLast1 = pElem->pLast1; 193 pElem->pLast1->pNext1 = pElem->pNext1; 194 if( pCur == pElem ) 195 pCur = ( pElem->pNext1 == pElem ) ? NULL : pElem->pNext1; 196 if( pLRUCache ) 197 ((UsrStgPagePtr_Impl*)pLRUCache)->erase( pElem->nPage ); 198 // remove from Sorted 199 pElem->pNext2->pLast2 = pElem->pLast2; 200 pElem->pLast2->pNext2 = pElem->pNext2; 201 if( pElem1 == pElem ) 202 pElem1 = ( pElem->pNext2 == pElem ) ? NULL : pElem->pNext2; 203 delete pElem; | 196 OSL_ENSURE( pElem, "The pointer should not be NULL!" ); 197 if ( pElem ) 198 { 199 OSL_ENSURE( pElem->pNext1 && pElem->pLast1, "The pointers may not be NULL!" ); 200 //remove from LRU 201 pElem->pNext1->pLast1 = pElem->pLast1; 202 pElem->pLast1->pNext1 = pElem->pNext1; 203 if( pCur == pElem ) 204 pCur = ( pElem->pNext1 == pElem ) ? NULL : pElem->pNext1; 205 if( pLRUCache ) 206 ((UsrStgPagePtr_Impl*)pLRUCache)->erase( pElem->nPage ); 207 // remove from Sorted 208 pElem->pNext2->pLast2 = pElem->pLast2; 209 pElem->pLast2->pNext2 = pElem->pNext2; 210 if( pElem1 == pElem ) 211 pElem1 = ( pElem->pNext2 == pElem ) ? NULL : pElem->pNext2; 212 delete pElem; 213 } |
204} 205 206// remove all cache elements without flushing them 207 208void StgCache::Clear() 209{ 210 StgPage* pElem = pCur; 211 if( pCur ) do --- 15 unchanged lines hidden (view full) --- 227{ 228 if( !pLRUCache ) 229 return NULL; 230 UsrStgPagePtr_Impl::iterator aIt = ((UsrStgPagePtr_Impl*)pLRUCache)->find( nPage ); 231 if( aIt != ((UsrStgPagePtr_Impl*)pLRUCache)->end() ) 232 { 233 // page found 234 StgPage* pFound = (*aIt).second; | 214} 215 216// remove all cache elements without flushing them 217 218void StgCache::Clear() 219{ 220 StgPage* pElem = pCur; 221 if( pCur ) do --- 15 unchanged lines hidden (view full) --- 237{ 238 if( !pLRUCache ) 239 return NULL; 240 UsrStgPagePtr_Impl::iterator aIt = ((UsrStgPagePtr_Impl*)pLRUCache)->find( nPage ); 241 if( aIt != ((UsrStgPagePtr_Impl*)pLRUCache)->end() ) 242 { 243 // page found 244 StgPage* pFound = (*aIt).second; |
245 OSL_ENSURE( pFound, "The pointer may not be NULL!" ); |
|
235 236 if( pFound != pCur ) 237 { | 246 247 if( pFound != pCur ) 248 { |
249 OSL_ENSURE( pFound->pNext1 && pFound->pLast1, "The pointers may not be NULL!" ); |
|
238 // remove from LRU 239 pFound->pNext1->pLast1 = pFound->pLast1; 240 pFound->pLast1->pNext1 = pFound->pNext1; 241 // insert to LRU 242 pFound->pNext1 = pCur; 243 pFound->pLast1 = pCur->pLast1; 244 pFound->pNext1->pLast1 = 245 pFound->pLast1->pNext1 = pFound; --- 30 unchanged lines hidden (view full) --- 276 StgPage* p = Find( nNew ); 277 if( !p ) 278 p = Create( nNew ); 279 if( nOld >= 0 ) 280 { 281 // old page: we must have this data! 282 StgPage* q = Get( nOld, sal_True ); 283 if( q ) | 250 // remove from LRU 251 pFound->pNext1->pLast1 = pFound->pLast1; 252 pFound->pLast1->pNext1 = pFound->pNext1; 253 // insert to LRU 254 pFound->pNext1 = pCur; 255 pFound->pLast1 = pCur->pLast1; 256 pFound->pNext1->pLast1 = 257 pFound->pLast1->pNext1 = pFound; --- 30 unchanged lines hidden (view full) --- 288 StgPage* p = Find( nNew ); 289 if( !p ) 290 p = Create( nNew ); 291 if( nOld >= 0 ) 292 { 293 // old page: we must have this data! 294 StgPage* q = Get( nOld, sal_True ); 295 if( q ) |
296 { 297 OSL_ENSURE( p->nData == q->nData, "Unexpected page size!" ); |
|
284 memcpy( p->pData, q->pData, p->nData ); | 298 memcpy( p->pData, q->pData, p->nData ); |
299 } |
|
285 } 286 p->SetDirty(); 287 return p; 288} 289 290// Flush the cache whose owner is given. NULL flushes all. 291 292sal_Bool StgCache::Commit( StgDirEntry* ) --- 158 unchanged lines hidden (view full) --- 451 SetError( pStrm->GetError() ); 452 } 453 } 454 return Good(); 455} 456 457sal_Bool StgCache::Write( sal_Int32 nPage, void* pBuf, sal_Int32 nPg ) 458{ | 300 } 301 p->SetDirty(); 302 return p; 303} 304 305// Flush the cache whose owner is given. NULL flushes all. 306 307sal_Bool StgCache::Commit( StgDirEntry* ) --- 158 unchanged lines hidden (view full) --- 466 SetError( pStrm->GetError() ); 467 } 468 } 469 return Good(); 470} 471 472sal_Bool StgCache::Write( sal_Int32 nPage, void* pBuf, sal_Int32 nPg ) 473{ |
459 if( Good() ) | 474 if( Good() ) |
460 { 461 sal_uLong nPos = Page2Pos( nPage ); | 475 { 476 sal_uLong nPos = Page2Pos( nPage ); |
462 sal_uLong nBytes = nPg * nPageSize; | 477 sal_uLong nBytes = 0; 478 if ( SAL_MAX_INT32 / nPg > nPageSize ) 479 nBytes = nPg * nPageSize; 480 |
463 // fixed address and size for the header | 481 // fixed address and size for the header |
482 // nPageSize must be >= 512, otherwise the header can not be written here, we check it on import |
|
464 if( nPage == -1 ) 465 nPos = 0L, nBytes = 512; 466 if( pStrm->Tell() != nPos ) 467 { 468 if( pStrm->Seek( nPos ) != nPos ) { 469#ifdef CHECK_DIRTY 470 ErrorBox( NULL, WB_OK, String("SO2: Seek failed") ).Execute(); 471#endif --- 71 unchanged lines hidden --- | 483 if( nPage == -1 ) 484 nPos = 0L, nBytes = 512; 485 if( pStrm->Tell() != nPos ) 486 { 487 if( pStrm->Seek( nPos ) != nPos ) { 488#ifdef CHECK_DIRTY 489 ErrorBox( NULL, WB_OK, String("SO2: Seek failed") ).Execute(); 490#endif --- 71 unchanged lines hidden --- |