LibreOffice Module sot (master) 1
stgcache.hxx
Go to the documentation of this file.
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 <o3tl/safeint.hxx>
24#include <osl/endian.h>
25#include <rtl/ref.hxx>
26#include <tools/stream.hxx>
27#include "stgelem.hxx"
29
30#include <memory>
31#include <unordered_map>
32
34class StgPage;
35class StorageBase;
36
38{
39 typedef std::unordered_map
40 <
43
44 typedef std::vector< rtl::Reference< StgPage > > LRUList;
45
46 ErrCode m_nError; // error code
47 sal_Int32 m_nPages; // size of data area in pages
48 sal_uInt16 m_nRef; // reference count
49 IndexToStgPage maDirtyPages; // hash of all dirty pages
50 int m_nReplaceIdx; // index into maLRUPages to replace next
51 LRUList maLRUPages; // list of last few non-dirty pages.
52 short m_nPageSize; // page size of the file
53 UCBStorageStream* m_pStorageStream; // holds reference to UCB storage stream
54
55 void Erase( const rtl::Reference< StgPage >& ); // delete a cache element
56 rtl::Reference< StgPage > Create( sal_Int32 ); // create a cached page
57 SvStream* m_pStrm; // physical stream
58 bool m_bMyStream; // true: delete stream in dtor
59protected:
60 bool m_bFile; // true: file stream
61 sal_Int32 Page2Pos( sal_Int32 ) const; // page address --> file position
62public:
63 StgCache();
64 ~StgCache();
65 void IncRef() { m_nRef++; }
66 sal_uInt16 DecRef() { return --m_nRef; }
67 void SetPhysPageSize( short );
68 sal_Int32 GetPhysPages() const { return m_nPages; }
69 short GetPhysPageSize() const { return m_nPageSize; }
70 SvStream* GetStrm() { return m_pStrm; }
71 void SetStrm( SvStream*, bool );
73 bool Good() const { return m_nError == ERRCODE_NONE; }
74 ErrCode const & GetError() const { return m_nError; }
75 void MoveError( StorageBase const & );
76 void SetError( ErrCode );
77 void ResetError();
78 bool Open( const OUString& rName, StreamMode );
79 void Close();
80 bool Read( sal_Int32 nPage, void* pBuf );
81 bool Write( sal_Int32 nPage, void const * pBuf );
82
83 // two routines for accessing FAT pages
84 // Assume that the data is a FAT page and get/put FAT data.
85 void SetToPage ( const rtl::Reference< StgPage >& rPage, short nOff, sal_Int32 nVal );
86 static inline sal_Int32 GetFromPage ( const rtl::Reference< StgPage >& rPage, short nOff );
87 void SetDirty ( const rtl::Reference< StgPage > &rPage );
88 bool SetSize( sal_Int32 nPages );
89 rtl::Reference< StgPage > Find( sal_Int32 ); // find a cached page
90 rtl::Reference< StgPage > Get( sal_Int32, bool ); // get a cached page
91 rtl::Reference< StgPage > Copy( sal_Int32, sal_Int32=STG_FREE ); // copy a page
92 bool Commit(); // flush all pages
93 void Clear(); // clear the cache
94};
95
97{
98 const sal_Int32 mnPage; // page index
99 std::unique_ptr<sal_uInt8[]>
100 mpData; // nSize bytes
101 short mnSize; // size of this page
102 StgPage( short nData, sal_Int32 nPage );
103 virtual ~StgPage() override;
104public:
105 StgPage(const StgPage&) = delete;
106 StgPage& operator=(const StgPage&) = delete;
107 static rtl::Reference< StgPage > Create( short nData, sal_Int32 nPage );
108
109 sal_Int32 GetPage() const { return mnPage; }
110 void* GetData() { return mpData.get(); }
111 short GetSize() const { return mnSize; }
112
113public:
114 static bool IsPageGreater( const StgPage *pA, const StgPage *pB );
115};
116
117inline sal_Int32 StgCache::GetFromPage ( const rtl::Reference< StgPage >& rPage, short nOff )
118{
119 if( nOff < 0 || ( o3tl::make_unsigned(nOff) >= rPage->GetSize() / sizeof( sal_Int32 ) ) )
120 return -1;
121 sal_Int32 n = static_cast<sal_Int32*>(rPage->GetData())[ nOff ];
122#ifdef OSL_BIGENDIAN
123 return OSL_SWAPDWORD(n);
124#else
125 return n;
126#endif
127}
128
129#endif
130
131/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
StgCache()
Definition: stgcache.cxx:84
bool Open(const OUString &rName, StreamMode)
Definition: stgcache.cxx:274
SvStream * GetStrm()
Definition: stgcache.hxx:70
short m_nPageSize
Definition: stgcache.hxx:52
static sal_Int32 GetFromPage(const rtl::Reference< StgPage > &rPage, short nOff)
Definition: stgcache.hxx:117
void ResetError()
Definition: stgcache.cxx:407
UCBStorageStream * m_pStorageStream
Definition: stgcache.hxx:53
std::unordered_map< sal_Int32, rtl::Reference< StgPage > > IndexToStgPage
Definition: stgcache.hxx:42
ErrCode const & GetError() const
Definition: stgcache.hxx:74
rtl::Reference< StgPage > Get(sal_Int32, bool)
Definition: stgcache.cxx:162
rtl::Reference< StgPage > Copy(sal_Int32, sal_Int32=STG_FREE)
Definition: stgcache.cxx:182
bool Write(sal_Int32 nPage, void const *pBuf)
Definition: stgcache.cxx:361
sal_uInt16 m_nRef
Definition: stgcache.hxx:48
short GetPhysPageSize() const
Definition: stgcache.hxx:69
std::vector< rtl::Reference< StgPage > > LRUList
Definition: stgcache.hxx:44
sal_Int32 m_nPages
Definition: stgcache.hxx:47
rtl::Reference< StgPage > Find(sal_Int32)
Definition: stgcache.cxx:148
void SetPhysPageSize(short)
Definition: stgcache.cxx:104
~StgCache()
Definition: stgcache.cxx:98
bool m_bMyStream
Definition: stgcache.hxx:58
sal_uInt16 DecRef()
Definition: stgcache.hxx:66
void Close()
Definition: stgcache.cxx:301
void MoveError(StorageBase const &)
Definition: stgcache.cxx:413
IndexToStgPage maDirtyPages
Definition: stgcache.hxx:49
rtl::Reference< StgPage > Create(sal_Int32)
Definition: stgcache.cxx:117
void SetStrm(SvStream *, bool)
Definition: stgcache.cxx:232
void Erase(const rtl::Reference< StgPage > &)
Definition: stgcache.cxx:126
bool SetSize(sal_Int32 nPages)
Definition: stgcache.cxx:390
void SetToPage(const rtl::Reference< StgPage > &rPage, short nOff, sal_Int32 nVal)
Definition: stgcache.cxx:55
bool Good() const
Definition: stgcache.hxx:73
void IncRef()
Definition: stgcache.hxx:65
void Clear()
Definition: stgcache.cxx:139
LRUList maLRUPages
Definition: stgcache.hxx:51
SvStream * m_pStrm
Definition: stgcache.hxx:57
sal_Int32 GetPhysPages() const
Definition: stgcache.hxx:68
void SetDirty(const rtl::Reference< StgPage > &rPage)
Definition: stgcache.cxx:266
sal_Int32 Page2Pos(sal_Int32) const
Definition: stgcache.cxx:424
bool m_bFile
Definition: stgcache.hxx:60
bool Commit()
Definition: stgcache.cxx:204
ErrCode m_nError
Definition: stgcache.hxx:46
void SetError(ErrCode)
Definition: stgcache.cxx:401
bool Read(sal_Int32 nPage, void *pBuf)
Definition: stgcache.cxx:312
int m_nReplaceIdx
Definition: stgcache.hxx:50
short GetSize() const
Definition: stgcache.hxx:111
const sal_Int32 mnPage
Definition: stgcache.hxx:98
static rtl::Reference< StgPage > Create(short nData, sal_Int32 nPage)
Definition: stgcache.cxx:50
sal_Int32 GetPage() const
Definition: stgcache.hxx:109
virtual ~StgPage() override
Definition: stgcache.cxx:46
void * GetData()
Definition: stgcache.hxx:110
StgPage(short nData, sal_Int32 nPage)
Definition: stgcache.cxx:35
std::unique_ptr< sal_uInt8[]> mpData
Definition: stgcache.hxx:100
static bool IsPageGreater(const StgPage *pA, const StgPage *pB)
Definition: stgcache.cxx:67
short mnSize
Definition: stgcache.hxx:101
StgPage & operator=(const StgPage &)=delete
StgPage(const StgPage &)=delete
#define ERRCODE_NONE
sal_Int64 n
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
#define STG_FREE
Definition: stgelem.hxx:139
StreamMode