xref: /trunk/main/sot/source/sdstor/stgcache.hxx (revision 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
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _STGCACHE_HXX
25 #define _STGCACHE_HXX
26 
27 #include <osl/endian.h>
28 #ifndef _TOOLS_SOLAR_H
29 #include <tools/solar.h>
30 #endif
31 #ifndef _TOOLS_STREAM_HXX
32 #include <tools/stream.hxx>
33 #endif
34 #include <stgelem.hxx>
35 
36 class UCBStorageStream;
37 
38 class StgIo;
39 class StgPage;
40 class StgDirEntry;
41 class StorageBase;
42 
43 class StgCache {
44 	StgPage* pCur;							// top of LRU list
45 	StgPage* pElem1;						// top of ordered list
46 	sal_uLong nError;							// error code
47 	sal_Int32 nPages;							// size of data area in pages
48 	sal_uInt16 nRef;							// reference count
49 	void * pLRUCache;						// hash table of cached objects
50 	short nPageSize;						// page size of the file
51 	UCBStorageStream* pStorageStream;		// holds reference to UCB storage stream
52 
53 	void Erase( StgPage* );					// delete a cache element
54 	void InsertToLRU( StgPage* );			// insert into LRU list
55 	void InsertToOrdered( StgPage* );		// insert into ordered list
56 	StgPage* Create( sal_Int32 );				// create a cached page
57 protected:
58 	SvStream* pStrm;						// physical stream
59 	sal_Bool  bMyStream;						// sal_True: delete stream in dtor
60 	sal_Bool  bFile;							// sal_True: file stream
61 	sal_Int32 Page2Pos( sal_Int32 );				// page address --> file position
62 	sal_Int32 Pos2Page( sal_Int32 );				// file position --> page address
63 public:
64 	StgCache();
65 	~StgCache();
IncRef()66 	void  IncRef()						{ nRef++;			}
DecRef()67 	sal_uInt16 DecRef()						{ return --nRef;	}
68 	void  SetPhysPageSize( short );
GetPhysPages()69 	sal_Int32 GetPhysPages()				{ return nPages;	}
GetPhysPageSize()70 	short GetPhysPageSize()				{ return nPageSize; }
GetStrm()71 	SvStream* GetStrm()	  				{ return pStrm;  	}
72 	void  SetStrm( SvStream*, sal_Bool );
73 	void  SetStrm( UCBStorageStream* );
IsWritable()74 	sal_Bool  IsWritable()					{ return ( pStrm && pStrm->IsWritable() ); }
Good()75 	sal_Bool  Good()						{ return sal_Bool( nError == SVSTREAM_OK ); }
Bad()76 	sal_Bool  Bad()                         { return sal_Bool( nError != SVSTREAM_OK ); }
GetError()77 	sal_uLong GetError()					{ return nError; 	}
78 	void  MoveError( StorageBase& );
79 	void  SetError( sal_uLong );
80 	void  ResetError();
81 	sal_Bool  Open( const String& rName, StreamMode );
82 	void  Close();
83 	sal_Bool  Read( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
84 	sal_Bool  Write( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
85 	sal_Bool  SetSize( sal_Int32 nPages );
86 	StgPage* Find( sal_Int32 );					// find a cached page
87 	StgPage* Get( sal_Int32, sal_Bool );			// get a cached page
88 	StgPage* Copy( sal_Int32, sal_Int32=STG_FREE );	// copy a page
89 	sal_Bool Commit( StgDirEntry* = NULL );		// flush all pages
90 	void Revert( StgDirEntry* = NULL );		// revert dirty pages
91 	void Clear();							// clear the cache
92 };
93 
94 class StgPage {
95 	friend class StgCache;
96 	StgCache* pCache;						// the cache
97 	StgPage *pNext1, *pLast1;				// LRU chain
98 	StgPage *pNext2, *pLast2;				// ordered chain
99 	StgDirEntry* pOwner;					// owner
100 	sal_Int32	nPage;							// page #
101 	sal_uInt8*	pData;							// nPageSize characters
102 	short	nData;							// size of this page
103 	sal_Bool	bDirty;							// dirty flag
104 	StgPage( StgCache*, short );
105 	~StgPage();
106 public:
SetDirty()107 	void  SetDirty()					{ bDirty = sal_True; 	 		}
GetPage()108 	sal_Int32 GetPage()						{ return nPage;				}
GetData()109 	void* GetData()						{ return pData; 	 		}
GetSize()110 	short GetSize()						{ return nData; 	 		}
SetOwner(StgDirEntry * p)111 	void  SetOwner( StgDirEntry* p )	{ pOwner = p;				}
112 	// routines for accessing FAT pages
113 	// Assume that the data is a FAT page and get/put FAT data.
GetPage(short nOff)114 	sal_Int32 GetPage( short nOff )
115 	{
116 		if( ( nOff >= (short) ( nData / sizeof( sal_Int32 ) ) ) || nOff < 0 )
117 			return -1;
118 		sal_Int32 n = ((sal_Int32*) pData )[ nOff ];
119 #ifdef OSL_BIGENDIAN
120 		return SWAPLONG(n);
121 #else
122 		return n;
123 #endif
124 	}
125 	void  SetPage( short, sal_Int32 );		// put an element
126 };
127 
128 #endif
129