xref: /aoo42x/main/sot/source/sdstor/stgcache.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _STGCACHE_HXX
29 #define _STGCACHE_HXX
30 
31 #include <osl/endian.h>
32 #ifndef _TOOLS_SOLAR_H
33 #include <tools/solar.h>
34 #endif
35 #ifndef _TOOLS_STREAM_HXX
36 #include <tools/stream.hxx>
37 #endif
38 #include <stgelem.hxx>
39 
40 class UCBStorageStream;
41 
42 class StgIo;
43 class StgPage;
44 class StgDirEntry;
45 class StorageBase;
46 
47 class StgCache {
48 	StgPage* pCur;							// top of LRU list
49 	StgPage* pElem1;						// top of ordered list
50 	sal_uLong nError;							// error code
51 	sal_Int32 nPages;							// size of data area in pages
52 	sal_uInt16 nRef;							// reference count
53 	void * pLRUCache;						// hash table of cached objects
54 	short nPageSize;						// page size of the file
55 	UCBStorageStream* pStorageStream;		// holds reference to UCB storage stream
56 
57 	void Erase( StgPage* );					// delete a cache element
58 	void InsertToLRU( StgPage* );			// insert into LRU list
59 	void InsertToOrdered( StgPage* );		// insert into ordered list
60 	StgPage* Create( sal_Int32 );				// create a cached page
61 protected:
62 	SvStream* pStrm;						// physical stream
63 	sal_Bool  bMyStream;						// sal_True: delete stream in dtor
64 	sal_Bool  bFile;							// sal_True: file stream
65 	sal_Int32 Page2Pos( sal_Int32 );				// page address --> file position
66 	sal_Int32 Pos2Page( sal_Int32 );				// file position --> page address
67 public:
68 	StgCache();
69 	~StgCache();
70 	void  IncRef()						{ nRef++;			}
71 	sal_uInt16 DecRef()						{ return --nRef;	}
72 	void  SetPhysPageSize( short );
73 	sal_Int32 GetPhysPages()				{ return nPages;	}
74 	short GetPhysPageSize()				{ return nPageSize; }
75 	SvStream* GetStrm()	  				{ return pStrm;  	}
76 	void  SetStrm( SvStream*, sal_Bool );
77 	void  SetStrm( UCBStorageStream* );
78 	sal_Bool  IsWritable()					{ return pStrm->IsWritable(); }
79 	sal_Bool  Good()						{ return sal_Bool( nError == SVSTREAM_OK ); }
80 	sal_Bool  Bad()                         { return sal_Bool( nError != SVSTREAM_OK ); }
81 	sal_uLong GetError()					{ return nError; 	}
82 	void  MoveError( StorageBase& );
83 	void  SetError( sal_uLong );
84 	void  ResetError();
85 	sal_Bool  Open( const String& rName, StreamMode );
86 	void  Close();
87 	sal_Bool  Read( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
88 	sal_Bool  Write( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
89 	sal_Bool  SetSize( sal_Int32 nPages );
90 	StgPage* Find( sal_Int32 );					// find a cached page
91 	StgPage* Get( sal_Int32, sal_Bool );			// get a cached page
92 	StgPage* Copy( sal_Int32, sal_Int32=STG_FREE );	// copy a page
93 	sal_Bool Commit( StgDirEntry* = NULL );		// flush all pages
94 	void Revert( StgDirEntry* = NULL );		// revert dirty pages
95 	void Clear();							// clear the cache
96 };
97 
98 class StgPage {
99 	friend class StgCache;
100 	StgCache* pCache;						// the cache
101 	StgPage *pNext1, *pLast1;				// LRU chain
102 	StgPage *pNext2, *pLast2;				// ordered chain
103 	StgDirEntry* pOwner;					// owner
104 	sal_Int32	nPage;							// page #
105 	sal_uInt8*	pData;							// nPageSize characters
106 	short	nData;							// size of this page
107 	sal_Bool	bDirty;							// dirty flag
108 	StgPage( StgCache*, short );
109 	~StgPage();
110 public:
111 	void  SetDirty()					{ bDirty = sal_True; 	 		}
112 	sal_Int32 GetPage()						{ return nPage;				}
113 	void* GetData()						{ return pData; 	 		}
114 	short GetSize()						{ return nData; 	 		}
115 	void  SetOwner( StgDirEntry* p )	{ pOwner = p;				}
116 	// routines for accessing FAT pages
117 	// Assume that the data is a FAT page and get/put FAT data.
118 	sal_Int32 GetPage( short nOff )
119 	{
120 		if( ( nOff >= (short) ( nData / sizeof( sal_Int32 ) ) ) || nOff < 0 )
121 			return -1;
122 		sal_Int32 n = ((sal_Int32*) pData )[ nOff ];
123 #ifdef OSL_BIGENDIAN
124 		return SWAPLONG(n);
125 #else
126 		return n;
127 #endif
128 	}
129 	void  SetPage( short, sal_Int32 );		// put an element
130 };
131 
132 #endif
133