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