Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #ifndef _STGCACHE_HXX
21 : : #define _STGCACHE_HXX
22 : :
23 : : #include <osl/endian.h>
24 : : #include <tools/solar.h>
25 : : #include <tools/stream.hxx>
26 : : #include <stgelem.hxx>
27 : :
28 : : class UCBStorageStream;
29 : :
30 : : class StgPage;
31 : : class StgDirEntry;
32 : : class StorageBase;
33 : :
34 : : class StgCache {
35 : : StgPage* pCur; // top of LRU list
36 : : StgPage* pElem1; // top of ordered list
37 : : sal_uLong nError; // error code
38 : : sal_Int32 nPages; // size of data area in pages
39 : : sal_uInt16 nRef; // reference count
40 : : void * pLRUCache; // hash table of cached objects
41 : : short nPageSize; // page size of the file
42 : : UCBStorageStream* pStorageStream; // holds reference to UCB storage stream
43 : :
44 : : void Erase( StgPage* ); // delete a cache element
45 : : void InsertToLRU( StgPage* ); // insert into LRU list
46 : : void InsertToOrdered( StgPage* ); // insert into ordered list
47 : : StgPage* Create( sal_Int32 ); // create a cached page
48 : : protected:
49 : : SvStream* pStrm; // physical stream
50 : : sal_Bool bMyStream; // sal_True: delete stream in dtor
51 : : sal_Bool bFile; // sal_True: file stream
52 : : sal_Int32 Page2Pos( sal_Int32 ); // page address --> file position
53 : : public:
54 : : StgCache();
55 : : ~StgCache();
56 : 3836 : void IncRef() { nRef++; }
57 : 3830 : sal_uInt16 DecRef() { return --nRef; }
58 : : void SetPhysPageSize( short );
59 : 13236 : sal_Int32 GetPhysPages() { return nPages; }
60 : 11200 : short GetPhysPageSize() { return nPageSize; }
61 : 1848 : SvStream* GetStrm() { return pStrm; }
62 : : void SetStrm( SvStream*, sal_Bool );
63 : : void SetStrm( UCBStorageStream* );
64 : : sal_Bool IsWritable() { return ( pStrm && pStrm->IsWritable() ); }
65 : 57404 : sal_Bool Good() { return sal_Bool( nError == SVSTREAM_OK ); }
66 : : sal_Bool Bad() { return sal_Bool( nError != SVSTREAM_OK ); }
67 : 734 : sal_uLong GetError() { return nError; }
68 : : void MoveError( StorageBase& );
69 : : void SetError( sal_uLong );
70 : : void ResetError();
71 : : sal_Bool Open( const String& rName, StreamMode );
72 : : void Close();
73 : : sal_Bool Read( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
74 : : sal_Bool Write( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
75 : : sal_Bool SetSize( sal_Int32 nPages );
76 : : StgPage* Find( sal_Int32 ); // find a cached page
77 : : StgPage* Get( sal_Int32, sal_Bool ); // get a cached page
78 : : StgPage* Copy( sal_Int32, sal_Int32=STG_FREE ); // copy a page
79 : : sal_Bool Commit(); // flush all pages
80 : : void Clear(); // clear the cache
81 : : };
82 : :
83 : : class StgPage {
84 : : friend class StgCache;
85 : : StgCache* pCache; // the cache
86 : : StgPage *pNext1, *pLast1; // LRU chain
87 : : StgPage *pNext2, *pLast2; // ordered chain
88 : : StgDirEntry* pOwner; // owner
89 : : sal_Int32 nPage; // page #
90 : : sal_uInt8* pData; // nPageSize characters
91 : : short nData; // size of this page
92 : : sal_Bool bDirty; // dirty flag
93 : : StgPage( StgCache*, short );
94 : : ~StgPage();
95 : : public:
96 : 16745 : void SetDirty() { bDirty = sal_True; }
97 : : sal_Int32 GetPage() { return nPage; }
98 : 2208539 : void* GetData() { return pData; }
99 : 4482 : short GetSize() { return nData; }
100 : 2208539 : void SetOwner( StgDirEntry* p ) { pOwner = p; }
101 : : // routines for accessing FAT pages
102 : : // Assume that the data is a FAT page and get/put FAT data.
103 : 370151 : sal_Int32 GetPage( short nOff )
104 : : {
105 [ + + ][ - + ]: 370151 : if( ( nOff >= (short) ( nData / sizeof( sal_Int32 ) ) ) || nOff < 0 )
106 : 3 : return -1;
107 : 370148 : sal_Int32 n = ((sal_Int32*) pData )[ nOff ];
108 : : #ifdef OSL_BIGENDIAN
109 : : return OSL_SWAPDWORD(n);
110 : : #else
111 : 370151 : return n;
112 : : #endif
113 : : }
114 : : void SetPage( short, sal_Int32 ); // put an element
115 : : };
116 : :
117 : : #endif
118 : :
119 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|