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