1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: stgcache.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 #include <osl/endian.h>
35 #ifndef _TOOLS_SOLAR_H
36 #include <tools/solar.h>
38 #ifndef _TOOLS_STREAM_HXX
39 #include <tools/stream.hxx>
41 #include <stgelem.hxx>
43 class UCBStorageStream
;
51 StgPage
* pCur
; // top of LRU list
52 StgPage
* pElem1
; // top of ordered list
53 ULONG nError
; // error code
54 INT32 nPages
; // size of data area in pages
55 USHORT nRef
; // reference count
56 void * pLRUCache
; // hash table of cached objects
57 short nPageSize
; // page size of the file
58 UCBStorageStream
* pStorageStream
; // holds reference to UCB storage stream
60 void Erase( StgPage
* ); // delete a cache element
61 void InsertToLRU( StgPage
* ); // insert into LRU list
62 void InsertToOrdered( StgPage
* ); // insert into ordered list
63 StgPage
* Create( INT32
); // create a cached page
65 SvStream
* pStrm
; // physical stream
66 BOOL bMyStream
; // TRUE: delete stream in dtor
67 BOOL bFile
; // TRUE: file stream
68 INT32
Page2Pos( INT32
); // page address --> file position
69 INT32
Pos2Page( INT32
); // file position --> page address
73 void IncRef() { nRef
++; }
74 USHORT
DecRef() { return --nRef
; }
75 void SetPhysPageSize( short );
76 INT32
GetPhysPages() { return nPages
; }
77 short GetPhysPageSize() { return nPageSize
; }
78 SvStream
* GetStrm() { return pStrm
; }
79 void SetStrm( SvStream
*, BOOL
);
80 void SetStrm( UCBStorageStream
* );
81 BOOL
IsWritable() { return pStrm
->IsWritable(); }
82 BOOL
Good() { return BOOL( nError
== SVSTREAM_OK
); }
83 BOOL
Bad() { return BOOL( nError
!= SVSTREAM_OK
); }
84 ULONG
GetError() { return nError
; }
85 void MoveError( StorageBase
& );
86 void SetError( ULONG
);
88 BOOL
Open( const String
& rName
, StreamMode
);
90 BOOL
Read( INT32 nPage
, void* pBuf
, INT32 nPages
);
91 BOOL
Write( INT32 nPage
, void* pBuf
, INT32 nPages
);
92 BOOL
SetSize( INT32 nPages
);
93 StgPage
* Find( INT32
); // find a cached page
94 StgPage
* Get( INT32
, BOOL
); // get a cached page
95 StgPage
* Copy( INT32
, INT32
=STG_FREE
); // copy a page
96 BOOL
Commit( StgDirEntry
* = NULL
); // flush all pages
97 void Revert( StgDirEntry
* = NULL
); // revert dirty pages
98 void Clear(); // clear the cache
102 friend class StgCache
;
103 StgCache
* pCache
; // the cache
104 StgPage
*pNext1
, *pLast1
; // LRU chain
105 StgPage
*pNext2
, *pLast2
; // ordered chain
106 StgDirEntry
* pOwner
; // owner
107 INT32 nPage
; // page #
108 BYTE
* pData
; // nPageSize characters
109 short nData
; // size of this page
110 BOOL bDirty
; // dirty flag
111 StgPage( StgCache
*, short );
114 void SetDirty() { bDirty
= TRUE
; }
115 INT32
GetPage() { return nPage
; }
116 void* GetData() { return pData
; }
117 short GetSize() { return nData
; }
118 void SetOwner( StgDirEntry
* p
) { pOwner
= p
; }
119 // routines for accessing FAT pages
120 // Assume that the data is a FAT page and get/put FAT data.
121 INT32
GetPage( short nOff
)
123 if( ( nOff
>= (short) ( nData
/ sizeof( INT32
) ) ) || nOff
< 0 )
125 INT32 n
= ((INT32
*) pData
)[ nOff
];
132 void SetPage( short, INT32
); // put an element