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 INCLUDED_SOT_SOURCE_SDSTOR_STGCACHE_HXX
21 : #define INCLUDED_SOT_SOURCE_SDSTOR_STGCACHE_HXX
22 :
23 : #include <osl/endian.h>
24 : #include <rtl/ref.hxx>
25 : #include <tools/solar.h>
26 : #include <tools/stream.hxx>
27 : #include <stgelem.hxx>
28 : #include <boost/noncopyable.hpp>
29 : #include <boost/functional/hash.hpp>
30 : #include <salhelper/simplereferenceobject.hxx>
31 : #include <unordered_map>
32 :
33 : class UCBStorageStream;
34 : class StgPage;
35 : class StgDirEntry;
36 : class StorageBase;
37 :
38 : class StgCache
39 : {
40 : typedef std::unordered_map
41 : <
42 : sal_Int32, rtl::Reference< StgPage >,
43 : boost::hash< sal_Int32 >, std::equal_to< sal_Int32 >
44 : > IndexToStgPage;
45 :
46 : typedef std::vector< rtl::Reference< StgPage > > LRUList;
47 :
48 : sal_uLong nError; // error code
49 : sal_Int32 nPages; // size of data area in pages
50 : sal_uInt16 nRef; // reference count
51 : IndexToStgPage maDirtyPages; // hash of all dirty pages
52 : int nReplaceIdx; // index into maLRUPages to replace next
53 : LRUList maLRUPages; // list of last few non-dirty pages.
54 : short nPageSize; // page size of the file
55 : UCBStorageStream* pStorageStream; // holds reference to UCB storage stream
56 :
57 : void Erase( const rtl::Reference< StgPage >& ); // delete a cache element
58 : rtl::Reference< StgPage > Create( sal_Int32 ); // create a cached page
59 : protected:
60 : SvStream* pStrm; // physical stream
61 : bool bMyStream; // true: delete stream in dtor
62 : bool bFile; // true: file stream
63 : sal_Int32 Page2Pos( sal_Int32 ); // page address --> file position
64 : public:
65 : StgCache();
66 : ~StgCache();
67 10138 : void IncRef() { nRef++; }
68 10138 : sal_uInt16 DecRef() { return --nRef; }
69 : void SetPhysPageSize( short );
70 26974 : sal_Int32 GetPhysPages() { return nPages; }
71 23014 : short GetPhysPageSize() { return nPageSize; }
72 17627 : SvStream* GetStrm() { return pStrm; }
73 : void SetStrm( SvStream*, bool );
74 : void SetStrm( UCBStorageStream* );
75 : bool IsWritable() { return ( pStrm && pStrm->IsWritable() ); }
76 236010 : bool Good() { return nError == SVSTREAM_OK; }
77 : bool Bad() { return nError != SVSTREAM_OK; }
78 1570 : sal_uLong GetError() { return nError; }
79 : void MoveError( StorageBase& );
80 : void SetError( sal_uLong );
81 : void ResetError();
82 : bool Open( const OUString& rName, StreamMode );
83 : void Close();
84 : bool Read( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
85 : bool Write( sal_Int32 nPage, void* pBuf, sal_Int32 nPages );
86 :
87 : // two routines for accessing FAT pages
88 : // Assume that the data is a FAT page and get/put FAT data.
89 : void SetToPage ( const rtl::Reference< StgPage >& rPage, short nOff, sal_Int32 nVal );
90 : static inline sal_Int32 GetFromPage ( const rtl::Reference< StgPage >& rPage, short nOff );
91 : void SetDirty ( const rtl::Reference< StgPage > &rPage );
92 : bool SetSize( sal_Int32 nPages );
93 : rtl::Reference< StgPage > Find( sal_Int32 ); // find a cached page
94 : rtl::Reference< StgPage > Get( sal_Int32, bool ); // get a cached page
95 : rtl::Reference< StgPage > Copy( sal_Int32, sal_Int32=STG_FREE ); // copy a page
96 : bool Commit(); // flush all pages
97 : void Clear(); // clear the cache
98 : };
99 :
100 : class StgPage : public salhelper::SimpleReferenceObject, private boost::noncopyable
101 : {
102 : const sal_Int32 mnPage; // page index
103 : sal_uInt8* mpData; // nSize bytes
104 : short mnSize; // size of this page
105 : StgPage( short nData, sal_Int32 nPage );
106 : virtual ~StgPage();
107 : public:
108 : static rtl::Reference< StgPage > Create( short nData, sal_Int32 nPage );
109 :
110 9558119 : sal_Int32 GetPage() { return mnPage; }
111 2336283 : void* GetData() { return mpData; }
112 675884 : short GetSize() { return mnSize; }
113 :
114 : public:
115 : static bool IsPageGreater( const StgPage *pA, const StgPage *pB );
116 : };
117 :
118 550911 : inline sal_Int32 StgCache::GetFromPage ( const rtl::Reference< StgPage >& rPage, short nOff )
119 : {
120 550911 : if( ( nOff >= (short) ( rPage->GetSize() / sizeof( sal_Int32 ) ) ) || nOff < 0 )
121 1 : return -1;
122 550910 : sal_Int32 n = static_cast<sal_Int32*>(rPage->GetData())[ nOff ];
123 : #ifdef OSL_BIGENDIAN
124 : return OSL_SWAPDWORD(n);
125 : #else
126 550910 : return n;
127 : #endif
128 : }
129 :
130 : #endif
131 :
132 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|